基於CMake Android NDK學習

基於CMake Android NDK學習

網上不少NDK開發入門都是基於Ecplise,ndk-build方式的開發教程,本文主要介紹在現有項目中用CMake腳本方式添加C/C++代碼。java

Android NDK 安裝教程參考官網

官網開發教程

NDK開發教程

一.建立源文件
  • IDE左側,切換項目爲Project試圖
  • 導航到src,右鍵點擊main目錄,選擇New > Directory
  • 在目錄中給文件夾取名(官網取名cpp,隨便取)並點擊OK
  • 右鍵點擊上一步建立目錄,而後選擇New > C/C++ Source File
  • 爲源文件輸入一個名稱,如native-lib(愛咋取咋取),而後點擊OK
二.建立CMake腳本

右鍵點擊app,選擇New > File,建立名爲CMakeLists.txt做爲文件名並點擊OK(名字不能看心情💢發揮了)
接下來咱們添加CMake如下命令,開始構建腳本(其餘相關操做配置,,根據相關需求參考官方文檔)android

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.
#定義Cmake 版本號,每一個版本新增功能,參考CMake官方文檔
cmake_minimum_required(VERSION 3.4.1)

# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.
#定義library名字(生成的so庫名:lib庫名稱.so)和定義library相對路徑
add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )
#引用庫
find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

# Links your native library against one or more other native libraries.
# 關聯相關lib
target_link_libraries( # Specifies the target library.
                       native-lib

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

複製代碼

三.使用Android GUI關聯CMake

  • 左側IDE打開Project選擇app模塊
  • 右鍵app文件夾,從菜單中選擇Link C++ Project with Gradle
  • 在彈框中,Build System選擇CMake方式,Path選擇你的CMakeLists.txt文件,點擊OK等待gradle構建完成

四.開始在java中定義native方法

  • 定義native方法
public class NativeDemo {
    //靜態native方法
    public static native String helloFromJNI();
}
//非靜態native方法
public class Hello {
    public int property;
    public int function(int foo, Date date, int[] arr) {
        System.out.println("function");
        Log.d("Yxjie","jni輸入數字:"+foo);
        return foo;
    }
    public boolean func(int a ,double b,char c){
        return true;
    }
    public native void test();
}
複製代碼
  • 開始寫native-lib.cpp文件 開始編寫以前咱們須要瞭解一下簽名格式:
java類型 相應簽名格式
boolean Z
byte B
char C
short S
int I
long L
float F
double D
void V
object L 用/分隔包完整類型,如:Ljava/lang/String
Array [ 簽名 ,如:[I
Method (參數類型...)返回值類型

注:char類型參數要在前面加個L,如L'a',java 中字符在Unicode是雙字節的,而C++中的字符是單字節,須要加寬編程

#include "jni.h"

extern "C"
JNIEXPORT jstring JNICALL Java_com_example_ndktest_NativeDemo_helloFromJNI(JNIEnv *env, jclass jclz) {
    //靜態native方法 須要兩個參數JNIEnv,以及jclass
    return env->NewStringUTF("Hello From JNI");
}

extern "C"
JNIEXPORT void Java_com_example_ndktest_Hello_test(JNIEnv *env, jobject obj) {
    //非靜態方法須要JNIEnv以及jobject(java中Hello,class對象引用)
    jclass hello_clazz = env->GetObjectClass(obj);//獲取java端 hello.class
    jfieldID fieldId_prop = env->GetFieldID(hello_clazz, "property", "I");//獲取屬性
    jint prop_int = env->GetIntField(hello_clazz, fieldId_prop);//獲取java prop默認值

    jmethodID methodId_function = env->GetMethodID(hello_clazz, "function",
                                                   "(ILjava/util/Date;[I)I");//獲取對應方法

    //批量傳入參數
    jmethodID methodId_func = env->GetMethodID(hello_clazz, "func", "(IDC)Z");
    jvalue *args = new jvalue[3];
    args[0].i = 10L;
    args[1].d = 3.14;
    args[3].c = L'j';//Java 中字符雙字節,C++字符爲單子接須要編程器寬字節
    env->CallBooleanMethod(hello_clazz, methodId_func, args);
    delete[] args;
    //直接傳值
    env->SetIntField(hello_clazz, fieldId_prop, 100L);//設置java類中props的值
    env->CallIntMethod(obj, methodId_function, 11L, NULL, NULL);//調用方法

}

複製代碼
  • 加載load lib 我是直接放在個人入口Activity裏面,直接靜態代碼塊加載lib
public class MainActivity extends AppCompatActivity {

    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView txt=findViewById(R.id.txt);
        txt.setText(NativeDemo.helloFromJNI());

        Hello hello=new Hello();
        hello.test();
    }
}
複製代碼

注:若有錯誤歡迎留言指正

相關文章
相關標籤/搜索