作者简介
微信公众号(高质量文章推送):走向全栈工程师
作者:陈博易
声明:本文是个人原创,未经允许请勿转载
商业合作请在微信公众号回复:联系方式
前言
- 为什么要学习JNI呢,我的回答是:因为我好学!哈哈哈
- 因为c/c++比Java效率高,所以应用运行起来速度比较快,特别是一些游戏中的算法。
- 为了保密,都知道apk都可以被反编译,就算有代码混淆,也只是难看懂,并不是完全看不懂,但用jni编译成.so就不同了,可以使破解的难度更加大。
- 一个平台(C++代码)迁移到Android平台,底层逻辑是相同的,这样就可以通过移植,利用JNI调用底层C++代码,避免相同逻辑的代码重复去写,不过这个过程一定要注意底层对象的释放问题。
环境以及工具
- Android项目:AndroidStudio3.0
- NDK
- CMake3.6.4
- LLDB3.0
整体步骤
- AndroidStudio 3.0 NDK环境搭建
- NDK入门案例目录介绍
- NDK入门案例代码介绍
核心步骤解读
1. NDK环境搭建
解释说明一下为什么需要在sdk tools中下载 ndk cmake LLDB呢?
- NDK:让我们可以在 Android 上面使用 C 和 C++ 代码的工具集。
- cmake:是外部构建工具。如果你已经知道如何使用ndk-build的话,可以不使用它。
- LLDB: 可以在Android Studio上调试本地代码
2.NDK入门案例目录介绍
- cpp 文件夹存放你所有 native 代码的地方,例如:c c++语言
- CMakeLists.txt 存放 CMake 或 ndk-build 构建脚本的地方
- externalNativeBuild是构建工具自动生成的文件
3.NDK入门案例代码介绍
MainActivity中的代码介绍
- 加载类库
- 声明native方法
- 调用方法
public class MainActivity extends AppCompatActivity { // Used to load the 'native-lib' library on application startup. //在应用开启的时候就加载native-lib static { System.loadLibrary("native-lib"); } /** * A native method that is implemented by the 'native-lib' native library, * which is packaged with this application. */ //本地方法在java类中的声明,具体实现在'native-lib' native library public native String stringFromJNI(); @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); //调用jni中的方法 tv.setText(stringFromJNI()); }}复制代码
native-lib.cpp中代码介绍:
app/CMakeLists.txt构建脚本翻译:
# For more information about using CMake with Android Studio, read the# documentation: https://d.android.com/studio/projects/add-native-code.html#CmakeLists.txt翻译:对于更多Android Studio使用CMake的文档信息,请#阅读documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.#指定CMake编译器的最低版本3.4.1cmake_minimum_required(VERSION 3.4.1)#设置生成的so动态库最后输出的路径#它将会把生成的so库按照你在 build.gradle 指定的 abi分别放置在 jniLibs下 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})# 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.#CmakeLists.txt翻译:创建一个类库的name,设置这个类库为STATIC#或者SHARED类型,并且设置c或者c++的源代码的的相对路径。#你可以定义多个类库,同事CMake会为你构建。#Gradle可以自动将shared类库打包到你的APK中。# CMake根据指定的源文件生成库文件add_library( # Sets the name of the library #设置类库的名字 native-lib # Sets the library as a shared library. #生成的库的类型[SHARED|STATIC|MODULE] #SHARED库会被动态链接,在运行时被加载 #STATIC库是在链接其它目标的时候使用 SHARED # Provides a relative path to your source file(s). #指定路径下的源文件代码,可以为这个类库指定多个.cpp文件 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.#CmakeLists.txt翻译:搜索指定构建库并将变量作为存储路径。#因为Cmake构建工具默认包含了系统类库,你仅仅需要指定你想要添加的公共NDK类库的name.#CMake构建工具会在完成构建之前校验指定的类库name是否存在# 将NDK log类库的位置存储到变量 log-lib中#可以在构建脚本的其他地方使用这个变量 ${log-lib}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.#CmakeLists.txt翻译:指定类库 CMake构建工具会连接到你的目标类库中。#你可以连接到多个类库中,例如:在这个CmakeLists.txt的构建脚本中定义的类库,#预构建的第三方类库或者系统类库。#为生成的目标类库指定需要的库文件target_link_libraries( # Specifies the target library. #生成的目标库文件 native-lib # Links the target library to the log library # included in the NDK. #需要在目标库文件中使用的库,表示可以在native-lib中使用log-lib库的内容 ${log-lib} )复制代码
app/build.gradle构建文件代码介绍
android { compileSdkVersion 26 defaultConfig { applicationId "com.example.administrator.ndk" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" //Gradle 文件配置对CMake的配置 externalNativeBuild { cmake { cppFlags "" } } //Gradle 构建并打包某个特定abi体系架构下的.so库 ndk { // Specifies the ABI configurations of your native // libraries Gradle should build and package with your APK. abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a' } } buildTypes { release { } } //Gradle 文件配置对CMake的配置 externalNativeBuild { cmake { path "CMakeLists.txt" } }}复制代码
总结
- 请大家多关注关注我。
- 声明的public native String stringFromJNI()方法和Java_com_example_administrator_ndk_MainActivity_stringFromJNI中的stringFromJNI要一致。
个人相关教程
请关注我(高质量文章推送)
源码地址———关注微信公众号,回复:ndk环境搭建