Implementation of dynamic library developed by golang

Time:2020-12-4

The dynamic libraries we usually use are. So files developed by C / C + +.

Let’s take a look at a JNI development process.

1、 Developing JNI

There are two ways. Now a faster way is Android studio. When you create a project and select a module, it will give you a JNI template. Just use that one directly.

But I prefer the traditional method.

Simply put, the traditional way is that you use commands to turn java files into C + + header files

A simple demonstration, first write a Java class


public class TestJni {

  static {
    System.loadLibrary("KylimTest");
  }

  public static native String getMsg();

}

A native decorated method is defined. After the code calls this method, JNI will automatically call the corresponding method in the dynamic library.

Use the command to generate the header file for this class, and enter the command under the folder path

Javah - JNI package name. Class name

You can see that by default, a. H header file is generated, which is automatically named as the package name_ Class name. H


/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_kylim_nativetest_TestJni */

#ifndef _Included_com_kylim_nativetest_TestJni
#define _Included_com_kylim_nativetest_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:   com_kylim_nativetest_TestJni
 * Method:  getMsg
 * Signature: (I)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_kylim_nativetest_TestJni_getMsg
 (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

The main core is this sentence


JNIEXPORT jstring JNICALL Java_com_kylim_nativetest_TestJni_getMsg
 (JNIEnv *, jclass);

I don’t know anything else. It’s all C-related. If you’re tired of using commands to generate problems, you can create an. H file by yourself, and then write the method name according to this specification

The header file is just for definition. We need to write the original file ourselves, so we need to create a file with the end of. CPP

#include "com_kylim_nativetest_TestJni.h"

JNIEXPORT jstring JNICALL Java_com_kylim_nativetest_TestJni_getMsg
    (JNIEnv *env, jclass cls){

  Jstring result = env - > newstringutf ("result is");
  return result;
}

There is a standard for method naming. You can see the demo to know how to standardize it. There is no need to explain it. In this way, the code at both ends is finished. However, the project cannot be run only in this way.

We also need some configuration, because in Android studio, gradle helps us compile c + + code, so we need to write these configurations. If you do not use as development, you use other tools to develop and directly generate. So files and then throw them into as, you can ignore this step.

Look at my JNI directory first

To create a Android.mk

#Fixed writing
LOCAL_PATH:=$(call my-dir)
#Fixed writing
include $(CLEAR_VARS)
#Generate so name
LOCAL_MODULE := KylimTest
LOCAL_SRC_FILES := testone.cpp
#Fixed writing
include $(BUILD_SHARED_LIBRARY)

The specific configuration can be found by yourself. This is not mainly about JNI, so I won’t talk about it in such detail.

We need another one Application.mk

#Select different ABI, multiple use spaces as separators, all are all
# APP_ABI := armeabi armeabi-v7a
APP_ABI := armeabi-v7a

#Specifies the runtime to use
APP_STL := c++_static

Of course, this is not enough. It is said that gradle is responsible for compiling, so we must write some configuration in gradle

android {
   defaultConfig {
        ndkBuild {
        //Designation Application.mk  The path of
        arguments "NDK_APPLICATION_MK:=src/main/jni/Application.mk"
        //Specifies which platform so files are generated
        abiFilters "armeabi-v7a"
        //Cflags and cppflags are used to set environment variables, which generally do not need to be moved
        cFlags "-DTEST_C_FLAG1", "-DTEST_C_FLAG2"
        cppFlags "-DTEST_CPP_FLAG2", "-DTEST_CPP_FLAG2"
      }
   }

  sourceSets { main { jni.srcDirs = ['src/main/jni'] } }

  externalNativeBuild {
    ndkBuild {
      path file('src/main/jni/Android.mk')
    }
  }

}

In this way, you can simply run a JNI demo. Generally speaking, Java writes a class, defines some native methods and loads, and C + + writes specific method implementations.

2、 Developing dynamic library with go

The above-mentioned native method is developed with C / C + +, so what if you don’t know C + +, the learning of C + + is not so easy. Take the types on both sides for example, beginners will encounter the problem of type conversion at the beginning, and will often dissuade some people first. But go is different. If you have Java foundation, you can learn to go quickly.

So what is a dynamic library developed with go? Is it also so file? Is it the same as C + +, it generates so file after some operation after compilation.

I saw that some articles on the Internet did write about how to generate so, but they said too little and felt unreliable until I saw that the official had written it.

As can be seen in the official library, there is a mobile library

https://github.com/golang/mobile

Read on and it will guide you to the wiki

https://github.com/golang/go/wiki/Mobile

We can see that it will print out an AAR file. Then AAR is a good way for us to access. But I want to explore what is in this AAR, so we need to print out an AAR and decompress it to see what is in it

First of all, before downloading this library, you must configure the go environment locally

Then follow the process here to download


go get -d golang.org/x/mobile/example/basic

But the input of this command requires scientific Internet access to download it. Generally speaking, it is very troublesome.

So we can directly clone the mobile library, which is the link above https://github.com/golang/mobile

Download it directly. In addition, you also need tools. These are all in go. Links https://github.com/golang/tools

Download these two and copy them to your go on the following path

Go / SRC create a folder golang.org/x Throw these two folders in

Then enter the command


gomobile init

You can enter the command to see if the installation configuration is successful


gomobile bind -help

If the configuration is successful, it will give you some tips

I don’t think the document is clear, but it tells you what command to use to generate AAR


gomobile bind -o app/hello.aar -target=android golang.org/x/mobile/example/bind/hello

You create a go file in gopath. I developed it with GoLand. The directory of the project is set to gopath. After writing, you can run it directly in the file

Gomobile bind - O input file name. AAR - target = Android

This will generate an AAR in the folder

And then we’ll see what’s in AAR, unzip

First, you can see the so file that generated these ABIS, and then look at the manifest

There is a limited minimum version, so if your version is lower than it, you need to pay attention to it

Then you can see that there are no files in res.

Finally, we decompile the class file

Because this is not a demo, it is a project written by the company directly, so some places need code, but it does not affect it.

From this, we can see that go helps us generate a java file, which defines the native methods named in go.

In fact, it can be seen from here that go basically uses the method of JNI written on the top of us. It’s just that he encapsulated it for you

But I don’t know if his native code is converted to C + +, because I won’t decompile so files.

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Api: tiktok: user video list

Tiktok tiktok, tiktok, tiktok, Api, Api, jitter, voice, bullet screen comments, jitter, jitter, and jitter. Tiktok tiktok tiktok data, jitter data acquisition, live broadcast of shaking sound Titodata: professional short video data acquisition and processing platform. For more information, please contact:TiToData Massive data collection Collect 500 million pieces of data for customers every day Tiktok […]