JNI attached so third-party libraries

JNI attached so third-party libraries (androidstudio)

Recently doing a project, so libraries need to hook other departments to provide, android pure white, the Internet to find a bunch of information, and take a lot of detours finally articulated the initial success, the entire process to share with you

Basic tools

androidstudio, google official development tools, a lot of iterative version, and indeed there are many drawbacks, but many developers do with it easily.

The basic foundation

Since the library is mounted so c ++ compiler, c ++ basic needs, basic knowledge JNI should also know some, but not the children's shoes can go to check other information, do not need to learn how deep, jni memory mechanism, learned how to type conversion on the same subject.

The first step in the establishment of the project

Establish a Native C ++ project in androidstudio where, yes, androidstudio own example.

The second step to prepare CMkelist

androidstudio directory structure here is not to say, open the file in a directory called main-cpp CMakelist of, androidstduio will be responsible for all operational processes are concentrated in this txt file inside, how much work is not complicated, just follow a fixed format fill, androidstudio will automatically execute a command to generate static dynamic link library file so.

add_library( # Sets the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             native-lib.cpp )

This is your own library so generated, there is nothing you can modify the local, native-lib.cpp your own compile cpp file, call the third-party libraries are involved in here, which is why android-based programming requires a c ++ the reason.

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

This has nothing to say, without modification, here is the positioning lib location.
Next we need to add their own

target_include_directories(native-lib
        PRIVATE
        ${CMAKE_SOURCE_DIR}/include
        )

This is to add the header file, and inform the head of the project file.

add_library( # Sets the name of the library.
        abc
        # Sets the library as a shared library.
        SHARED
        # Provides a relative path to your source file(s).
        IMPORTED)
set_target_properties(abc
        PROPERTIES IMPORTED_LOCATION
        ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libabc.so)

addlibrary add third-party libraries, and inform the type of third-party libraries, SHARED is a static library, STATIC is a static library.
set_target_properties is to inform third-party library location, $ {ANDROID_ABI} is automatically obtain the phone's architecture, such as arm64-v8a.
How do you want to import multiple third-party libraries, it is in accordance with add_library (), set_target_properties () continue to add to.

target_link_libraries( # Specifies the target library.
                       native-lib  abc abc1 abc2
                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

The last target_link_libraries (), all third-party libraries to connect to our own native-lib library.

The third step is to compile native-lib.cpp file

Here we will be a third party header file import in, you can call the method of a third party.

int sum(int i,int j)
{
return i+j;
}

For example such a third-party library function, we want it to be packaged function java

extern "C" JNIEXPORT jint JNICALL
Java_com_example_test_math_sum(JNIEnv* env,jobject /* this */,jint i ,jint j)
{
    jint sum_ = i+jj
    return sum_;
}

JNI specification is defined as a function, the function name is the name of the class package name + + configuration function name, parameters must be passed jni type, parameters must also be returned while jni type, due to the int and directly jint conversion, the function type conversion process will be omitted.

package com.example.test;
public class math{
    static {
        System.loadLibrary("native-lib");
    }
    public int java_math(int i,int j)
    {
        return load(i,j);
    }
    public native long math(int i ,int j);
}

While in our java classes and also have a corresponding function corresponding to, java math of such a simple implementation of a C ++ () function to a good package.

Published an original article · won praise 2 · views 49

Guess you like

Origin blog.csdn.net/qq_28666281/article/details/105244948