Mac下AndroidStudio3.x的NDK開發環境搭建

Sorrower發表於2019-02-02

目錄

  • 前言
  • CMake
  • 用CMake向已有AS專案新增C/C++程式碼
  • ndk-build
  • 最後

前言

mac上安裝軟體真的很簡單, 一路下一步就可以安裝好android studio. 這裡有一篇舊文-Mac下安裝配置Android Studio 2.x和3.x並配置使用adb可供參考.
而寫這篇的目的, 主要是我發現之前的ndk開發方式已經過時了, 需要更新一下新的流程.


CMake

CMake的方式是官方預設的ndk構建方式, 先從預設栗子開始看吧.

  • 新建一個專案, 勾選C++ support:

圖片描述

  • 你會發現初始的Activity就只能是基礎或者空的型別了, 其他的都沒了.

圖片描述

  • 這裡預設C++標準即可:
  • C++ Standard: 選擇哪一種C++標準, 預設選擇Toolchain Default選項, 其會使用預設的CMake配置
  • Exceptions Support: 是否啟用對C++異常處理的支援, 如果選中, AS會將-fexceptions標誌新增到模組級build.grade檔案的cppFlags中
  • Runtime Type Information Support: 是否支援RTTI, 如果選中, AS會將-frtti標誌新增到模組級build.gradle檔案的cppFlags中

圖片描述

  • 來看看專案都多了什麼, 先切換到Android標籤下, 多了cpp目錄(ps: 注意, 這裡就算切換到Project標籤, 依舊是cpp哈), 一些標頭檔案, 和native-lib.cpp, 不用說, 這個cpp裡面肯定是jni程式碼了, 我貼出來:

圖片描述

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

extern "C" JNIEXPORT jstring JNICALL
Java_com_so_testcmake_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
  • 然後切換到Project標籤, 這個CMakeLists.txt就特別惹眼了, 我把裡面大段註釋都去掉, 然後貼出程式碼. .externalNativeBuild資料夾: 用於存放cmake編譯好的檔案, 包括支援的各種硬體等資訊. 其實看到前面的.也知道是系統管理的了.

圖片描述

cmake_minimum_required(VERSION 3.4.1)

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).
        src/main/cpp/native-lib.cpp)

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)

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這一段

  • 第一個引數生成函式庫的名稱, 即libnative-lib.so或libnative-lib.a(lib和.so/.a預設預設)
  • 第二個引數生成庫型別: 動態庫為SHARED, 靜態庫為STATIC
  • 第三個引數依賴的c/cpp檔案(相對路徑)
  • 最後回到Activity類來看看, 操作還是一樣的, 載入庫, 宣告native函式.
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 = (TextView) 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();
}
  • 再來到build.gradle檔案, 發現多出來了兩個標籤段, 也就是說, 如果我們自己要建CMake環境, 是要加這兩段的.

圖片描述

用CMake向已有AS專案新增C/C++程式碼

  • 新建一個空專案, 不含C++ support, 剛才的專案不要關, 之後會大段複製黏貼:

圖片描述

  • 新建JNI目錄, 發現在Android標籤下是cpp, 到了Project標籤下又是jni, 我一直很想知道谷歌是怎麼實現這一點的.

圖片描述
圖片描述

  • 建立一個Java類, 將之前專案的程式碼複製過來, 如下:
public class MyJNI {
    // Used to load the `native-lib` library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    /**
     * A native method that is implemented by the `native-lib` native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}
  • 然後在jni目錄下建立cpp檔案, 複製之前專案的程式碼, 注意包名的變動:
#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_so_addcmake_MyJNI_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}
  • 然後將之前專案的CMakeLists.txt複製到這個專案的app目錄下, 修改相對路徑, 即將cpp變成jni, 然後檔名也可以更改, 但是注意對應.
  • 接下來在build.gradle中加入程式碼, 之後同步:
ndk {
    abiFilters `x86`, `x86_64`, `armeabi-v7a`, `arm64-v8a`
}
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}

當然, 你可以手動操作進行關聯, 右擊app目錄, 點選Link C++ Project with Gradle, 選擇之前的CMakeLists.txt檔案.

圖片描述
圖片描述

  • 最後回到Activity, 設定元件顯示從cpp函式返回的字串, 編譯執行:
TextView tvTest = (TextView) findViewById(R.id.tv_test);
tvTest.setText(new MyJNI().stringFromJNI());
  • 最後來自效果圖:

圖片描述


ndk-build

  • 這是個有些過時的方式, 但是依舊是可以用的, 同樣, 新建空專案. 然後和之前一樣, 建一個cpp/jni目錄.
  • 複用之前的JNI類, 也就是載入了C++庫和宣告瞭本地函式的Java類.
  • 建立Android.mk, Application.mk, helloNDK.cpp檔案, 程式碼依次貼出:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE := HelloNDK
LOCAL_SRC_FILES := helloNDK.cpp

include $(BUILD_SHARED_LIBRARY)
APP_MODULES := HelloNDK
APP_ABI := all
//
// Created by 楊驍 on 2019/2/2.
//

#include <jni.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * 函式名規則: Java_包名_類名_方法名
 * @param env  表示一個指向JNI環境的指標, 可以通過它來方位JNI提供的介面方法
 * @param thiz 表示Java物件中的this
 * @return
 */
jstring Java_com_so_addndk_HelloNDK_get(JNIEnv *env, jobject thiz) {
    printf("invoke get in c++
");
    return env->NewStringUTF("Hello from JNI in helloJni.so !");
}

void Java_com_so_addndk_HelloNDK_set(JNIEnv *env, jobject thiz, jstring string) {
    printf("invoke set from C++
");
    char* str = (char*)env->GetStringUTFChars(string,NULL);
    printf("%s
", str);
    env->ReleaseStringUTFChars(string, str);
}

#ifdef __cplusplus
}
#endif
  • 然後開啟終端, 進入到jni目錄, 使用ndk-build指令生成.so檔案, 接著把生成的.so檔案拷貝到app目錄下的libs目錄:

圖片描述
圖片描述

  • 最後在Activity中呼叫就大功告成了:

效果圖


最後

要說操作上這兩種的複雜度感覺差不多, 但是我依舊推薦CMake方案, 至少這種是短時間不會過時的方案.


相關文章