該文章首發於微信公衆號「字節流動」
創建帶有 native 方法的類,build 項目。
生成與類名相關的 .h 文件。
進入 app -> build -> intermediates -> classes -> debug 目錄下
執行: javah com.haohao.hellojni.MyJNI
(先配置好 JDK 的環境變量),生成 com_haohao_hellojni_MyJNI.h 文件
創建 cpp 文件。
在 main 文件夾下,新建 jni 目錄,剪切 .h 文件到 jni 目錄下,創建 hello.cpp 文件
hello.cpp
配置 build.gradle 文件。
修改 app/build.gradle 文件, muduleName 爲引入的 .so name , 直接運行項目,安裝 apk ,運行就 OK 了
生成的 .so 文件位置。
PS: 未指定 CPU 框架時,AS 會生成支持所有 CPU 框架的 .so 文件。
創建 Android.mk 和 Application.mk 文件。
新建一個項目,在 app 目錄下(任目錄下都可以)新建 jni 文件,添加 Android.mk 和 Application.mk 文件,以及 com_haohao_hellojni_MyJNI.h 文件(運用上一小節的方法生成)。
Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # 要生成的.so庫名稱。java代碼System.loadLibrary("hello");加載的就是它 LOCAL_MODULE := hello # C++文件 LOCAL_SRC_FILES := hello.cpp include $(BUILD_SHARED_LIBRARY)
# 不寫 APP_ABI 會生成全部支持的平臺,目前支持:armeabi arm64-v8a armeabi-v7a # APP_ABI := armeabi arm64-v8a armeabi-v7a mips mips64 x86 x86_64 APP_ABI := armeabi arm64-v8a armeabi-v7a
生成 .so 文件。
在 jni 目錄下(配置好NDK環境變量)直接執行 ndk-build , 生成 .so 文件。
配置項目工程。
在 main 目錄下新建 jniLibs 目錄,並拷貝 armeabi arm64-v8a armeabi-v7a 文件夾,運行 proj 。
從 Android Studio 2.2 開始,就默認使用 CMake 工具構建 NDK 項目,請確保你的 AS 版本大於 2.2 。
創建項目時,勾選 Include C++ support
選擇默認的 Toolchain Default
AS 自動生成 CMakeLists.txt 文件(CMake 構建腳本)
CMakeLists.txt
# 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. # 設置模塊名爲 native-lib,SHARED 可分享的,以及配置源文件的路徑 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. # 找到 log 本地模塊 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. # 關聯 native-lib 模塊和 log 模塊 target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} )
在配置 app/build.gradle ,針對特殊平臺 abiFilters 。配置完成之後,同步,運行。
新建一個工程,創建 native 類,快捷鍵 Alt + Enter ,自動創建 jni 目錄和相應的 .cpp 文件。
native-lib.cpp
#include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_haohao_ndk_1cpp_MyJNI_stringFromJNI(JNIEnv *env, jobject instance) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); }
在工程根目錄下創建 CMakeLists.txt 文件。
# 指定CMake的最小版本 cmake_minimum_required(VERSION 3.4.1) 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 )
選擇 app
modulde ,右擊選擇Link C++ Project with Gradle
。
選擇腳本文件的路徑。
app/build.gradle 會自動同步。同步完成後,運行項目。
微信公衆號
個人微信