Android Studio JNI 多坑總結
1.什麼是 JNI
JNI——(Java Native Interface),他是java平臺的特性,不是安卓系統提供的。他定義了一些JNI函式,來讓開發者可以通過呼叫這些函式來實現java程式碼呼叫C/C++程式碼。
2.如何使用 JNI
我們先將寫好的C/C++程式碼編譯成對應平臺的動態庫(windows是.dll檔案,linux是.so檔案)。
下面我們來舉個栗子:使用AndroidStudio來實現JNI
要實現JNI先下載NDK,那麼NDK又是什麼呢?(面試寶典來了,趕緊掏出小本本)
-
NDK是一系列工具的集合
-
NDK提供了一份穩定、功能有限的API標頭檔案宣告
-
NDK的釋出,使“Java+C”的開發方式終於轉正,成為官方支援的開發方式
-
NDK將使Android平臺支援C開發的開端
-
好,那接下來我們來下載NDK,有倆種方式:
-
Google官方下載NDK:點選下載
-
通過SDKManager來下載NDK:
3.樣例
下來我們new一個新工程:這個工程只包含一個MainActivity
我們來檢查一下NDK下載好了沒有,怎麼檢查呢?如下:
-
檢查SDK Location裡面的NDK路徑:
-
檢查local.properties檔案裡面有沒有NDK路徑:
下來我們要編寫JNI介面啦,如下:
JNI介面需要用native關鍵字修飾,我們會看到方法名報紅,沒關係,我們繼續
我們先build一下工程,檢查myJNIUtils.java編譯後有沒有生成class檔案,在這個位置下:
AndroidJNITest/app/build/intermediates/classes/debug/com/kissdream/androidjnitest/myJNIUtils.class
使用 javah 生成.h標頭檔案,具體如下:
-
開啟Terminal,輸入命令進入到debug目錄下,命令如下:
cd/Users/apple/Desktop/AndroidJNITest/app/build/intermediates/classes/debug
-
然後使用javah+包名+檔案路徑來生成標頭檔案,命令如下:
javah com.kissdream.androidjnitest.myJNIUtils
-
檢查標頭檔案有沒有生成:
我們發現這個路徑下多了個.h檔案AndroidJNITest/app/build/intermediates/classes/debug/com/kissdream
哈哈,沒錯這個就是我們要生成的標頭檔案
生成了.h檔案還不行,只是聲明瞭方法,我們還需要去實現它,那麼如何去實現他呢,如下:
-我們在main下新建一個jni資料夾,如圖:
.h檔案內容如下:
/* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class com_kissdream_androidjnitest_myJNIUtils */ #ifndef _Included_com_kissdream_androidjnitest_myJNIUtils #define _Included_com_kissdream_androidjnitest_myJNIUtils #ifdef __cplusplus extern "C" { #endif /* * Class: com_kissdream_androidjnitest_myJNIUtils * Method: getName * Signature: ()Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_kissdream_androidjnitest_myJNIUtils_getName (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
-
把生成的.h檔案拷貝到jni資料夾下
-
在jni資料夾下,新建一個.c(c語言)或者.cpp(c++)的檔案,來實現.h檔案裡宣告的方法:
把.h檔案裡面宣告的方法拷貝到新建的c++檔案裡面,然後在檔案裡面引入.h檔案:
引入.h檔案#include "com_kissdream_androidjnitest_myJNIUtils.h"
#include "com_kissdream_androidjnitest_myJNIUtils.h" JNIEXPORT jstring JNICALL Java_com_kissdream_androidjnitest_myJNIUtils_getName (JNIEnv * env, jobject obj){ //如果是用C語言格式就用這種方式 // return (*env)->NewStringUTF(env,"Kiss dream"); //如果是用C語言格式就用這種方式 return env->NewStringUTF((char *)"Kiss dream"); }
到這裡我們的方法就實現完畢了
方法我們實現了,但是我們如何呼叫呢,不要著急,Follow me:
-
首先引入動態庫:
public class myJNIUtils { static { //名字注意,需要跟你的build.gradle ndk節點下面的名字一樣 System.loadLibrary("NameProvider"); } //JNI介面需要用native關鍵字修飾 public native String getName(); }
NameProvider
就是你要生成d的.so檔案的檔名
最重要的一步來了,生成so檔案:
這個小編也不會,於是就去百度了下,得到結果:
-
在根目錄gradle.properties下面加上:
android.useDeprecatedNdk=true
意思就是允許使用低版本的NDK -
在module下面的build.gradle下面加上ndk節點如下圖:
ndk { moduleName "NameProvider" }
NameProvider
注意這個名字要跟你引入動態庫的名字一樣
需要這倆步就可以執行生成so檔案了
然兒,並沒有想象的那麼順利,報錯了,我頓時心中飛過一萬隻草泥瑪,上log:
Error:Execution failed for task ':app:compileDebugNdk'. > Error: Flag android.useDeprecatedNdk is no longer supported and will be removed in the next version of Android Studio. Please switch to a supported build system. Consider using CMake or ndk-build integration. For more information, go to: https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile To get started, you can use the sample ndk-build script the Android plugin generated for you at: /Users/apple/Desktop/AndroidJNITest/app/build/intermediates/ndk/debug/Android.mk Alternatively, you can use the experimental plugin: https://developer.android.com/r/tools/experimental-plugin.html To continue using the deprecated NDK compile for another 60 days, set android.deprecatedNdkCompileLease=1512283120054 in gradle.properties
百思不得其姐啊,百度的答案大家都是這樣做啊,為什麼人家可以我的就不行呢,我的程式碼和他的一模一樣啊
為什麼人家可以我的就不行呢,我的程式碼和他的一模一樣啊這句話作為程式設計師的我們很熟悉!難到我要放棄嗎?no no no,作為程式設計師的我怎麼能輕言放棄呢!每個人都有這樣的經歷,藍瘦過、香菇過,到最後我們都找到我們的錯誤
來我們仔細看下Log,大概意思就是說:
-
android.useDeprecatedNdk不再支援了
-
讓使用CMake or ndk-build
-
然後還有連結
注意:
Android Studio 用於構建原生庫的預設工具是 CMake。由於很多現有專案都使用構建工具包編譯其原生程式碼,Android Studio 還支援 ndk-build。如果您想要將現有的 ndk-build 庫匯入到您的 Android Studio 專案中,請參閱介紹如何配置 Gradle 以關聯到您的原生庫的部分。不過,如果您在建立新的原生庫,則應使用 CMake。
-
考慮使用CMake或ndk構建整合。要了解更多資訊,請訪問:
https://d.android.com/r/studio-ui/add-native-code.html#ndkCompile
首先,您可以使用Android的ndk構建指令碼示例外掛為您生成:
/Users/apple/Desktop/AndroidJNITest/app/build/intermediates/ndk/debug/Android.mk
或者,你可以使用實驗外掛:
https://developer.android.com/r/tools/experimental-plugin.html
繼續使用已棄用的NDK編譯60天,設定
在gradle.properties
android.deprecatedNdkCompileLease = 1512283120054(這個測試不起作用)
經過各種查資料,發現原來在gradle3.0以上以前這種方法不在支援
學習過程就不詳細描述了,直接上結果:
-
先通過SDKManager下載:CMake和LLDB
-
在build.gradle的
defaultConfig
節點下加入:
// 使用Cmake工具 externalNativeBuild { cmake { cppFlags "" //生成多個版本的so檔案 abiFilters 'arm64-v8a','armeabi-v7a','x86','x86_64' } }
-
在build.gradle的
android
節點下加入:
// 配置CMakeLists.txt路徑 externalNativeBuild { cmake { path "CMakeLists.txt" // 設定所要編寫的c原始碼位置,以及編譯後so檔案的名字 } }
-
新增CMakeLists.txt檔案到
build.gradle
檔案同級目錄下,具體內容如下:
# 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. #CMakeLists.txt 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檔名稱. NameProvider # Sets the library as a shared library. SHARED # 設定這個so檔案為共享. # Provides a relative path to your source file(s). # 設定這個so檔案為共享. src/main/jni/getName.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. # 制定目標庫. NameProvider # Links the target library to the log library # included in the NDK. ${log-lib} )