Android NDK JNI 开发之旅01 环境搭建入门篇

450 阅读4分钟

作者简介

微信公众号(高质量文章推送):走向全栈工程师

作者:陈博易

声明:本文是个人原创,未经允许请勿转载

商业合作请在微信公众号回复:联系方式


build-->make project结果展示

项目运行结果展示

前言


  1. 为什么要学习JNI呢,我的回答是:因为我好学!哈哈哈
  2. 因为c/c++比Java效率高,所以应用运行起来速度比较快,特别是一些游戏中的算法。
  3. 为了保密,都知道apk都可以被反编译,就算有代码混淆,也只是难看懂,并不是完全看不懂,但用jni编译成.so就不同了,可以使破解的难度更加大。
  4. 一个平台(C++代码)迁移到Android平台,底层逻辑是相同的,这样就可以通过移植,利用JNI调用底层C++代码,避免相同逻辑的代码重复去写,不过这个过程一定要注意底层对象的释放问题。

环境以及工具


  1. Android项目:AndroidStudio3.0
  2. NDK
  3. CMake3.6.4
  4. LLDB3.0

整体步骤


  1. AndroidStudio 3.0 NDK环境搭建
  2. NDK入门案例目录介绍
  3. NDK入门案例代码介绍

核心步骤解读


1. NDK环境搭建

解释说明一下为什么需要在sdk tools中下载 ndk cmake LLDB呢?

  1. NDK:让我们可以在 Android 上面使用 C 和 C++ 代码的工具集。
  2. cmake:是外部构建工具。如果你已经知道如何使用ndk-build的话,可以不使用它。
  3. LLDB: 可以在Android Studio上调试本地代码

2.NDK入门案例目录介绍
  1. cpp 文件夹存放你所有 native 代码的地方,例如:c c++语言
  2. CMakeLists.txt 存放 CMake 或 ndk-build 构建脚本的地方
  3. externalNativeBuild是构建工具自动生成的文件

3.NDK入门案例代码介绍

MainActivity中的代码介绍

  1. 加载类库
  2. 声明native方法
  3. 调用方法
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中代码介绍:

image.png

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.1
cmake_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"
        }
    }
}

总结


  1. 请大家多关注关注我。
  2. 声明的public native String stringFromJNI()方法和Java_com_example_administrator_ndk_MainActivity_stringFromJNI中的stringFromJNI要一致。

个人相关教程

各种大佬推荐的编程视频资源分享 Android 微信 支付宝支付,2行代码实现支付 Android前端 Java后端 集成支付宝支付 postman使用 Android java后端 接口调试工具 Android抓包 Charles http接口调试 消息推送 Android java后端集成小米推送 如何导入简单的java项目-IntelliJ IDEA

请关注我(高质量文章推送)

源码地址———关注微信公众号,回复:ndk环境搭建

Android NDK JNI 开发之旅 开源项目

长按二维码“识别”关注或者扫一扫