美文网首页
Android so库的使用

Android so库的使用

作者: 神棄丶Aria | 来源:发表于2018-08-04 11:47 被阅读0次

当前AS版本:3.1.3

0、创建前

在使用前应先下载 CMake、LLDB、NDK工具包。在SDK Tools里下载即可。

1、项目创建

项目创建同普通项目,只是在一开始记得勾选 Include C++ support

1533352024795.jpg

以下是创建后得到的项目目录。


image.png

比普通的项目会多出cpp包,app目录下还有一个CMakeLists.txt

2、编写jni调用类及生成so库

一般新建的项目中它会提供一个example给你参考,在cpp目录下会有个native-lib.cpp,可以参照它来实现。
下面举个例子
(1)编写jni调用类:

package com.example.aria.jniapp;

public class JniTest {

    public native void set(String str);

    public native String get();

}

(2)创建对应的cpp:
在main/cpp文件下创建test.cpp文件,名字随意,cpp的具体命名规则以及编写规则可以参考native_lib.cpp

#include <jni.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

JNIEXPORT jstring JNICALL Java_com_example_aria_jniapp_JniTest_get(JNIEnv *env, jobject thiz) {
    printf("invoke get in c++\n");
    return env->NewStringUTF("Hello from JNI int libjni-test.so!");
}

JNIEXPORT void JNICALL Java_com_example_aria_jniapp_JniTest_set(JNIEnv *env, jobject thiz, jstring string) {
    printf("invoke set from c++\n");
    char* str = (char*)env->GetStringUTFChars(string, NULL);
    printf("%s\n", str);
    env->ReleaseStringUTFChars(string, str);
}

#ifdef __cplusplus
}
#endif

这里要注意的是方法的命名规则。

(3)修改CMakeLists.txt
这里主要告诉AS哪些文件需要编译

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.其实它备注里都写的听清楚了,这里是最后要生成的so库的名字
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).这里留下你要打入so库的cpp文件
             src/main/cpp/native-lib.cpp
              src/main/cpp/test.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

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 )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

根据官方的提示修改CMakeLists文件,一般只要修改find_library函数就行了。
修改后编译一下,就可以得到编译后的so库文件了。文件的路径在build/intermediates/cmake/debug/obj/下,有各个版本下对应的so库文件,按需选择。

3、jni调用

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.加载jni库
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        JniTest jniTest = new JniTest();
        tv.setText(new JniTest().get());
    }

}

4、总结

简单整理了当前版本jni编译的流程,做个记录方便以后看。

参考文章:https://www.jianshu.com/p/50af2b92fa94

相关文章

网友评论

      本文标题:Android so库的使用

      本文链接:https://www.haomeiwen.com/subject/fsopvftx.html