簡單的JNI調用

準備工作:NKD環境搭建

  1. 使用Android studio 打開Project structure,在設置頁面找到SDK location,可以設置NDK的路徑,如果目前還沒有NDK開發包,studio會提示你下載,下載完成後設置路徑即可
    NDK路徑設置
  2. 檢查local.properties文件裏面有沒有NDK路徑:這裏寫圖片描述

開始創建JNI接口

  1. 新建一個類myJNI,然後添加JNI接口定義,如下所示,
 public static native String sayHello();

JNI接口需要用native關鍵字修飾,我們會看到方法名報紅,沒關係,我們繼續創建JNI方法,圖片中的方法名未報紅是因爲我們已經編譯生成了.so文件。
2. build一下工程,檢查myJNI.java編譯後有沒有生成class文件,在這個位置下:
app\build\intermediates\classes\debug\com\study\view\studytest\myJNI.class這裏寫圖片描述
3. 使用javah生成.h頭文件,具體如下:
打開Terminal,輸入命令進入到debug目錄下,命令如下:
cd app/build/intermediates/classes/debug
然後使用javah+包名+文件路徑來生成頭文件,命令如下:
javah com.study.view.studytest.myJNI
然後我們會發現在app\build\intermediates\classes\debug\com\study\view\studytes目錄下會有一個.h的文件生成這裏寫圖片描述
4. 有個.h頭文件後,我們去實現.h文件裏的方法,我們在main下新建一個jni文件夾,如圖:新建jni文件夾
把生成的.h文件拷貝到jni文件夾下,在jni文件夾下,新建一個.c(c語言)或者.cpp(c++)的文件,來實現.h文件裏聲明的方法:把.h文件裏面聲明的方法拷貝到新建的c++文件裏面,然後在文件裏面引入.h文件:
引入.h文件#include “com_study_view_studytest_myJNI.h”,完成的.c文件如圖這裏寫圖片描述
5. 在需要調用JNI接口的地方先加載動態庫

 static {
        System.loadLibrary("jni_test");
    }

生成so文件

  1. gradle3.0以前生成方式:
    在根目錄gradle.properties下面加上: android.useDeprecatedNdk=true意思就是允許使用低版本的NDK ;
    在module下面的build.gradle下面加上ndk節點如下圖:
ndk { 
moduleName “jni_test”//定義自己Ndklibrary的名字 
ldLibs “log//添加log庫 
// 設置支持的SO庫架構 
abiFilters ‘armeabi’ 
} 

build項目後發現報錯:

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

android.useDeprecatedNdk 不再支持了
讓使用CMake or ndk-build
2. gradle3.0 生成so的方式:
先通過SDKManager下載:CMake和LLDB下載CMake
在build.gradle的defaultConfig節點下加入:

  // 使用Cmake工具
        externalNativeBuild {
            cmake {
                cppFlags ""
                //生成多個版本的so文件
                abiFilters 'arm64-v8a','armeabi-v7a','x86'
            }
        }

在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文件名稱.
       jni_test

       # Sets the library as a shared library.
       SHARED
       # 設置這個so文件爲共享.

       # Provides a relative path to your source file(s).
       # 設置這個so文件爲共享.
       src/main/jni/com_study_view_studytest_myJNI.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.
            # 制定目標庫.
            jni_test

            # Links the target library to the log library
            # included in the NDK.
            ${log-lib} )

cmakelist
再重新build一下工程,即可生成so文件,生成的so文件位置:
app\build\intermediates\cmake\debug\obj
例如:app\build\intermediates\cmake\debug\obj\x86\libjni_test.so

最後調用JNI方法

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myJNI jni = new myJNI();
        Log.d("jni", "onCreate: " + jni.sayHello());
    }
}

運行結果:這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章