AndroidStudio3.*+ jni 開發配置cmake模式

本文簡介:
  最近看了一些文章,看到大部分關於Android jni的配置都還是ndkBuild模式的;有的則是說怎麼引用已經編譯好的os文件。本文主要以一個小dome爲列,記錄Android jni cmake模式下的配置。
一、環境配置

  Android 開發的環境配置網上已經很多了,這裏就不重複。我們做jni開發的,需要用到snk,所以我們第一步,在Android studio 上引入ndk

這裏我們主要需要勾選NDK、CMake。爲啥要勾選CMake因爲我們自己常常用傳統方法生成,配置太過於麻煩,所以我們這裏用CMake,直接生成一個標準的例子,進行修改。這樣就會減少很多問題的產生,節省我們的時間。

二、項目創建

(一)首先創建新項目,選擇創建NativityC++項目

(二)填寫項目信息。

選擇對c++標準,這裏選擇的默認。

(三)創建成功後項目架構是如下圖

三、與Android項目有些差別及說明

(一)app下的build.gradle

這裏多了些關於編譯ndk的入口配置數據

externalNativeBuild {
    cmake {
        cppFlags ""
    }
}

這裏的cppFlags 值爲引用的其他類庫比如:

cppFlags "-fexceptions"

編譯snk異常捕獲的

externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
        version "3.10.2"
    }
}

path :指定編譯的目標配置文件地址
version:編譯版本的
 

(二)結構上:
main下面多了cpp的包,裏面有多了一個CMakeLists.txt和native-lib.cpp

這兩個文件,CMakeLists.txt是關於cmake的一些基礎配置和編譯ndk的文件關聯配置

native-lib.cpp則是C++將要編譯的代碼文件

1、首先看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( # 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.

target_link_libraries( # Specifies the target library.
        native-lib

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

 

add_library 下面配置的os類庫對應類。native-lib爲類庫名,是生成os文件的名稱,也是我們java加載java的名稱。
native-lib.cpp 則是對應java方法生成的對C/C++的目錄地址。該文件地址與CMakeLists.txt文件位置相對
2、native-lib.cpp 查看其內容:
 

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

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

這是一個C/C++的代碼方法,起作用就是調用stringFromJNI該方法,返回"Hello form C++"的字符串

(三)java中MainActivity

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @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(stringFromJNI());
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}

代碼中多了 static { System.loadLibrary("native-lib"); } 靜態代碼塊,作用將native-lib庫加載到系統庫中,這裏native-lib對應的就是CMakeLists.txt配置文件中設置的庫名。

public native String stringFromJNI();代碼聲明瞭native映射C/C++的方法。在native-lib.cpp中實現
(四)、os庫編譯
選AndroidStudio的build-Rebuild Project,進行編譯。

編譯成功後可以從項目app/build/intermediates/cmake/debug/obj/下面查看到對應不同處理器的C庫了


(五)項目運行效果


這裏已經基本講述完整個項目的關鍵地方,不過這些都是項目自己生成的,不自己動手不容易變成自己的知識,接下來我們嘗試去修改,從而掌握理解


四、修改項目
1、創建一個JNIHelper類來聲明管理接口,將MainActivity中關於調用C庫的代碼移動到該類,

public class JNIHelper {
    // Used to load the 'native-lib' library on application startup.
    //加載到系統 自定義的JNILibrary 庫
    static {
        System.loadLibrary("JNILibrary");
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     * 聲明JNILibrary庫中獲取String的方法
     */
    public static native String stringFromJNI();
}

2、MainActivity代碼內容是:

public class MainActivity extends AppCompatActivity {


    @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(JNIHelper.stringFromJNI());
    }

}

這樣移動後項目會報錯,會讓你重新編譯,暫時不去理會他,當修改完全後從新編譯就不會出問題
3、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.

aux_source_directory(./ DIR_SRCS)
add_library( # Sets the name of the library.
        JNILibrary

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        ${DIR_SRCS})

# 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.
        JNILibrary

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

這裏將add_library 方法和target_link_libraries方法中的 native-lib 修改爲了自定義JNILibrary庫名,這裏庫名任意改動但是要與JNIHelper中的引用庫名對應一致。

此外這裏我將指定的C/C++庫文件做了調整。在上面聲明瞭一個字典變量 aux_source_directory(./ DIR_SRCS)
其中./是對於CMakeLists.txt 路徑的C/C++源碼文件的相對路徑。DIR_SRCS 則是引用庫名文件對應的C/C++源文件名
比如這裏的${DIR_SRCS} = JNILibrary.cpp

修改完CMakeLists.txt後我們去修改C/C++源文件
在對應的cpp目錄下創建一個NILibrary.cpp的文件(注意創建的文件名與自己定義庫名保持一致,不然無法編譯通過)

創建成功後可以看到cpp目錄下

然後往JNILibrary.cpp 實現對應JNIHelper類聲明的方法,
 

//
// Created by MZJ on 2020-04-22.
//
#include <jni.h>
#include <string>


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

這裏要注意的是實現的方法和你的JNIHelper有關聯,可以看到方法名 Java_com_toy_key_cjni_JNIHelper_stringFromJNI
及規則爲Java+包名+方法名 以_隔開。
當然自己寫比較麻煩你也可以讓Android Studio自動幫你生成。

創建完JNILibrary.cpp 不去寫C/C++代碼,然後進行build project。這是我們可以看到java的JNIHelper中未實現的方法名爲紅色

選中方法按 option+回車鍵提示,選擇創建jni對應方法

創建成功後是這樣

方法的實現需要自己進行代碼完善,添加返回想要返回的字符。
注意:這裏通過提示生成的可能會生成是.c文件這是默認生成C語言的文件,這個時候實現的第一個方法只能自己手工打對應方法了。後面的再有其他方法纔會映射到這個JNILibrary.cpp文件自動生成。
然後再在方法裏實現對應的邏輯。我上面的則是直接按照dome的返回字符寫的


好這裏算是修改完成了,運行後如上面的運行的結果一樣則說明修改成功了。我再舉一反三。修改一個回調方法出來。
 

五、修改一個回調方法
1、在java裏創建一個NativeListener接口類裏面有個修改Text的方法

public interface NativeListener {
    void changeText(String msg);
}

2、在JNIHelper裏聲明一個綁定NativeListener的方法
 

package com.toy.key.cjni;

public class JNIHelper {
    // Used to load the 'native-lib' library on application startup.
    //加載到系統 自定義的JNILibrary 庫
    static {
        System.loadLibrary("JNILibrary");
    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     * 聲明JNILibrary庫中獲取String的方法
     */
    public static native String stringFromJNI();
    /**
     * 註冊NativeListener監聽
     */
    public static native void registerListener(NativeListener listener);
}

按option+回車鍵提示,並創建 jni 方法,然後在JNILibrary.cpp 會生成對應方法

//
// Created by MZJ on 2020-04-22.
//
#include <jni.h>
#include <string>


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


extern "C"
JNIEXPORT void JNICALL
Java_com_toy_key_cjni_JNIHelper_registerListener(JNIEnv *env, jclass clazz, jobject listener) {
        // TODO: implement registerListener()
}

3、編輯JNILibrary.cpp的邏輯

//
// Created by MZJ on 2020-04-22.
//
#include <jni.h>
#include <string>

//聲明變量
jobject _listener;//
JNIEnv* _env;
jmethodID  _changeTextMethodID;
jclass _class;
extern "C"
JNIEXPORT jstring JNICALL
Java_com_toy_key_cjni_JNIHelper_stringFromJNI(JNIEnv *env, jobject thiz) {
        //當調用stringFromJNI時調用registerListener中NativeListener的changeText回調函數
        _env->CallVoidMethod(_listener, _changeTextMethodID,  _env->NewStringUTF("你猜我說啥了"));
        //創建變量std並賦值爲Hello from C++
        std::string hello = "Hello from C++";
        //返回數據
        return env->NewStringUTF(hello.c_str());
}


extern "C"
JNIEXPORT void JNICALL
Java_com_toy_key_cjni_JNIHelper_registerListener(JNIEnv *env, jclass clazz, jobject listener) {
        // TODO: implement registerListener()
        //將對應的類和方法存放臨時變量
        _listener = env->NewGlobalRef(listener);//獲取對象類
        _env = env;
        _class = _env->GetObjectClass(_listener);//獲取實體類
        _changeTextMethodID = _env->GetMethodID(_class, "changeText", "(Ljava/lang/String;)V");//獲取方法
}

4、編輯MainActivity
 

//實現接口NativeListener
public class MainActivity extends AppCompatActivity implements NativeListener {



    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = findViewById(R.id.sample_text);
        //綁定監聽
        registerListener(this);
    }

    public void onViewClick(View view) {
        //調用c庫的stringFromJNI方法獲取返回值並從Toast中顯示
        String str = stringFromJNI();
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void changeText(String msg) {//實現接口NativeListener 的chaneText方法。修改文字
        tv.setText(msg);
    }

}


5、activity_main.xml
 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="Hello World!"
        android:onClick="onViewClick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

六、運行結果

點擊後的效果

注意:若是之前編譯運行過的,可能會出現庫引用錯誤的衝突,所以在新編譯運行時記得清理一下


先刪除這框起來的兩個文件,然後在clean 項目,再運行就可以。
七、C/C++內容延展

在寫邏輯是有個方獲取,其中涉及到了參數類型
_changeTextMethodID = _env->GetMethodID(_class, "changeText", "(Ljava/lang/String;)V");//獲取方法
這裏的慘(Ljava/lang/String;)表示String參數,V表示的void方法

對應表NDK中的規則如下:

字符簽名 jni中類型 java中類型
V void void
Z jboolean boolean
I jint int
J jlong long
D jdouble double
F jfloat float
B jbyte byte
C jchar char
S jshort short

 

對於數組而言,需要以"["開始,組合以上規則即可,具體對應關係表如下:

字符簽名 jni中類型 java中類型
[Z jbooleanArray boolean[]
[I jintArray int[]
[J jlongArray long[]
[D jdoubleArray double[]
[F jfloatArray float[]
[B jbyteArray byte[]
[C jcharArray char[]
[S jshortArray short[]

其中要注意的對象類型數據後面要跟“;”,比如上面的“String;”
其寫法爲 包名+對象如 String 就是 java/long/String;
暫時就寫這麼多吧,作爲一個簡單記錄,以後有機會再深入寫。
 

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