android studio搭建cmake的ndk環境

下載所需資源

  1. android studio中下載Ndk、LLDB、CMake

創建可開發的jni項目環境

  1. 新建jni文件路徑

項目右鍵: new -> folder -> Jni Folder

文件存放路徑:“src/main/cpp/”

  1. 創建cmake配置文件

CMakeLists.txt

  1. 配置信息詳見地址

https://d.android.com/studio/projects/add-native-code.html

  1. CMakeLists.txt配置
# 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.
# 創建和命名庫,將其設置爲靜態或共享,並提供到其源代碼的相對路徑。
# 您可以定義多個庫,並由CMake爲您構建它們。Gradle自動將共享庫打包到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.
# 搜索指定的預構建庫並將路徑存儲爲變量。
# 因爲CMake默認情況下在搜索路徑中包含系統庫,所以您只需要指定要添加的公共NDK庫的名稱。
find_library( # Sets the name of the path variable.
			  # 設置path變量的名稱。
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              # 指定要CMake定位的NDK庫的名稱。
              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.
# 指定庫CMake應該鏈接到目標庫。
# 您可以鏈接多個庫,例如在此構建腳本中定義的庫、預構建的第三方庫或系統庫。
target_link_libraries( # Specifies the target library.
					   # 指定目標庫。
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       # 將目標庫鏈接到包含在NDK中的日誌庫。
                       ${log-lib} )
  1. build.gradle中的配置
android {
	defaultConfig {
        //使用Cmake工具
        externalNativeBuild {
            cmake {
            	//生成模式
                cppFlags ""
                //設置支持的SO庫架構。arm64-v8a, armeabi-v7a, x86, x86_64
                abiFilters 'arm64-v8a', 'x86', 'armeabi-v7a' , 'x86_64'
            }
        }
    }
	// 配置CMakeLists.txt路徑,放在哪裏不規定,只要能夠通過路徑找到
	externalNativeBuild {
        cmake {
            path "src/main/jni/CMakeLists.txt"
        }
    }
}
  1. 創建原生方法java類
public class TestJni {
    //首先要制定加載的庫
    static {
        System.loadLibrary("native-lib");
    }

    //原生方法
    public native String getHelloJni();
}
  1. 創建實現java方法的c++代碼
#include <jni.h>
#include <string>

extern "C" {//如果使用了.cpp文件,需要生命{}中的方法以c的方式編譯

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

JNIEXPORT jint JNICALL
Java_com_lh_jni_JniTest_add(JNIEnv *env, jobject instance, jint a, jint b) {
    return a + b;
}

}
  1. 運行,測試
JniTest jniTest = new JniTest();
String content = jniTest.getHelloJni();
int addNum = jniTest.add(10,15);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章