JNI&NDK開發(fā)最佳實踐(二):CMake實現(xiàn)調(diào)用已有C/C++文件中的本地方法

目標(biāo)

用CMake方法實現(xiàn)在java中調(diào)用本地C/C++文件中的方法,并生成相應(yīng)so庫導(dǎo)出。

實現(xiàn)步驟梳理

  1. 在需要調(diào)用本地方法的java文件中加載so庫,并聲明本地函數(shù)。
  2. 新建與java同級的cpp文件夾,在其中新建c/c++文件,實現(xiàn)本地方法。
  3. 新建CMakeList.txt文件,在其中作相應(yīng)so庫名稱、C文件路徑等相關(guān)配置。
  4. 在build.gradle中配置CMakeList.txt文件路徑、ABI類別等。
  5. 編譯運行,在app/build/intermediates/cmake/debug/obj路徑下即可找到生成的so庫。

具體實現(xiàn)

1.在需要調(diào)用本地方法的java文件中加載so庫,并聲明本地函數(shù)。

package com.example.taoying.testndkapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");//加載so庫,待加載的so庫的名稱為libnative-lib.so。
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Example of a call to a native method
        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNIwithp("11"));
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();//本地方法用native修飾


    public native String stringFromJNIwithp(String a);
}

注意兩點:

  • System.loadLibrary("native-lib");加載so庫,so庫的名稱為libnative-lib.so。
  • 本地函數(shù)用native修飾。

2.新建與java同級的cpp文件夾,在其中新建c/c++文件,實現(xiàn)本地方法。

本地新建cpp文件夾和相應(yīng)文件.png

native-lib.cpp

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_taoying_testndkapp_MainActivity_stringFromJNI(
        JNIEnv* env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}


extern "C" JNIEXPORT jstring JNICALL Java_com_example_taoying_testndkapp_MainActivity_stringFromJNIwithp
        (JNIEnv * env, jobject /* this */, jstring jstring1){
    std::string hello = "stringFromJNIwithp";
    return env->NewStringUTF(hello.c_str());
}
  • 本地函數(shù)名的格式需要遵循如下規(guī)則:Java_包名類名方法名,否則該方法將無法被找到。
  • JNIEnv* 表示一個指向JNI環(huán)境的指針,可以通過他來訪問JNI提供的接口方法。
  • jobject表示Java對象中的this(如果該方法是非靜態(tài)方法)或者Class對象(如果該方法是static方法)。
  • JNIEXPORT和JNICALL是JNI中定義的宏,可以在jni.h這個頭文件中找到。JNIEXPORT確保函數(shù)在符號表中可見, JNICALL確保函數(shù)使用正確的調(diào)用約定。

3.新建CMakeList.txt文件,在其中作相應(yīng)so庫名稱、C文件路徑等相關(guān)配置。

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_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表示生成靜態(tài)鏈接庫libnative-lib.so
//native-lib 即為生成庫的名稱
//SHARED 表示生成動態(tài)庫libnative-lib.so, STATIC表示生成靜態(tài)庫libnative-lib.a。
//native-lib.cpp 表示c/c++源文件的路徑。
//${CMAKE_SOURCE_DIR}:表示CMakeLists.txt的當(dāng)前文件夾路徑。
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).
             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.

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.
//需要加載的so庫,需要加載多個時用空格" "連接
target_link_libraries( # Specifies the target library.
                       native-lib

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

這里有更多關(guān)于CMakeList.txt的編寫規(guī)則的介紹。

4.在build.gradle中配置CMakeList.txt文件路徑、ABI類別等。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.taoying.testndkapp"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                //代表編譯C++代碼時用的編譯選項
                //如cppFlags "-frtti -fexceptions" 配置支持RTTI和C++異常處理(這個非必須 也可以不配)
                cppFlags ""
            }
        }
        ndk {
            //配置ABI類別,注意這里配置的Cpu類型在編譯后會生成相應(yīng)的so動態(tài)庫,不配置默認(rèn)生成所有
            //生成的so庫在C:\Users\taoying\Desktop\TestNdkApp\app\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so路徑下
            abiFilters "arm64-v8a"
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        // 指定CMake編譯配置文件路徑
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }

    //可選配置
    /*sourceSets.main {
        //默認(rèn)so庫放在src/main/jniLibs目錄下,此時可以制定新的存放so庫的目錄
        jniLibs.srcDirs = ['libs']
    }*/
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

5. 編譯運行,在app/build/intermediates/cmake/debug/obj路徑下即可找到生成的so庫。

so庫路徑.png

更多JNI&NDK系列文章,參見:
JNI&NDK開發(fā)最佳實踐(一):開篇
JNI&NDK開發(fā)最佳實踐(二):CMake實現(xiàn)調(diào)用已有C/C++文件中的本地方法
JNI&NDK開發(fā)最佳實踐(三):CMake實現(xiàn)調(diào)用已有so庫中的本地方法
JNI&NDK開發(fā)最佳實踐(四):JNI數(shù)據(jù)類型及與Java數(shù)據(jù)類型的映射關(guān)系
JNI&NDK開發(fā)最佳實踐(五):本地方法的靜態(tài)注冊與動態(tài)注冊
JNI&NDK開發(fā)最佳實踐(六):JNI實現(xiàn)本地方法時的數(shù)據(jù)類型轉(zhuǎn)換
JNI&NDK開發(fā)最佳實踐(七):JNI之本地方法與java互調(diào)
JNI&NDK開發(fā)最佳實踐(八):JNI局部引用、全局引用和弱全局引用
JNI&NDK開發(fā)最佳實踐(九):調(diào)試篇

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容