涉及到一些算法或者底层驱动的时候,往往需要使用jni来开发。现在官方推荐使用CMake工具来开发jni。
使用CMake开发Jni其实挺简单的,要求不高的话只需要简单配置一下就可以使用了。
使用CMake进行Jni开发需要使用CMake插件、LLDB插件、NDK插件,这些都可以通过Android Studio很快地安装。
打开SDK Manager,找到Android SDK->SDK Tool选项,安装CMake、LLDB、NDK插件。
勾选Include C++ support选项,勾选后Android Studio可以更好地支持及帮助我们检查jni代码,之后一路next即可。
native-lib.cpp文件
该文件是之前勾选支持c++后Android Studio主动为我们生成的一个示例调用文件,里面包含了jni代码。
#include <jni.h>#include <string>extern "C" JNIEXPORT jstringJNICALLJava_mhwang_com_jnidemo_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str());}
MainActivity.java文件
public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup. 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 = (TextView) findViewById(R.id.sample_text); tv.setText(stringFromJNI()); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ public native String stringFromJNI();}
这里的代码主要注意2点,一是
static { System.loadLibrary("native-lib"); }
这里主要加载native-lib库,这里加载库的名称需要跟CMakeLists.txt里面配置的一样。
二是public native String stringFromJNI(),
方法,主要提供java本地jni接口,java层代码就是调用这个接口来通过jni调用C++函数的。
CMakeLists.txt文件
这个文件的作用是通过配置将C++代码编译到共享对象库中,具体功能项直接通过注释来说明吧。
# 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构造本地库所需要的最低版本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.# 创建并命名库,可以将库的类型设置为静态# 或共享,并提供该库源代码的相对路径。# 你可以定义多个library库,并使用CMake来构建。# Gradle会自动打包库集成到apk中。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). # 库所在位置的相对路径 src/main/cpp/native-lib.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.# NDK提供了一套实用的原生API和库,可以使用find_library搜索NDK中存在的库,只需要加入所需要使用库的名称即可,如下面的日志库log-lib。# 更多的库可参考https://developer.android.com/ndk/guides/stable_apisfind_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.# 指定CMake连接到目标库中,可以链接多个库,如上面的native-lib库以及NDK中自带的log-lib库target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. # ${log-lib} )
截至目前为止,我们什么也没有添加,但是这已经是一个完整的jni开发的Android 应用了,我们来运行看一下效果。
更多详细的说明可以参考官方文档
联系客服