Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Studio 1.3 新版 NDK 支援上手:告別 Android.mk 的 C++ 開發流程

編輯:關於 Android 開發

背景:Google 終於把 NDK 收進官方工具鏈

2015 年 5 月底的 Google I/O 大會上,Google 宣布 Android Studio 1.3 將整合 NDK 支援,背後其實是與 JetBrains CLion 合作、並透過新版 Android Gradle 外掛所達成的成果。這項功能在同年 7 月正式推出,雖然當時仍在快速演進階段,但方向已經相當明確:NDK 開發不必再手寫 Android.mk。

要使用這套工具,需要 Android Studio 1.3 RC1 以上版本,以及 com.android.tools.build:gradle-experimental 外掛。

一、調整 Gradle 版本

experimental 外掛要求 Gradle 2.6 或更高版本。可以透過圖形介面修改:

或直接編輯 gradle/wrapper/gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-all.zip

接著把原本的 Android 外掛換成 experimental 版本,修改根目錄的 build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle-experimental:0.3.0-alpha5' } }

二、新舊 DSL 對照

最顯著的差異是模組的 build.gradle 需要改用新的 model { } 語法。

舊的寫法:

apply plugin: 'com.android.application' android { compileSdkVersion rootProject.ext.compileSdkVersion buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { applicationId "com.ph0b.example" minSdkVersion 15 targetSdkVersion 23 versionCode 4 versionName "1.0.1" ndk { moduleName "mymodule" ldLibs "log" stl "gnustl_static" cFlags "-std=c++11 -fexceptions" } } signingConfigs { release { storeFile file(STORE_FILE) storePassword STORE_PASSWORD keyAlias KEY_ALIAS keyPassword KEY_PASSWORD } } buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt' signingConfig signingConfigs.release } debug { jniDebuggable true } } } dependencies { compile 'com.android.support:support-v4:23.0.1' compile fileTree(dir: 'libs', include: ['*.jar']) }

新的寫法:

apply plugin: 'com.android.model.application' model { android { compileSdkVersion = rootProject.ext.compileSdkVersion buildToolsVersion = rootProject.ext.buildToolsVersion defaultConfig.with { applicationId = "com.ph0b.example" minSdkVersion.apiLevel = 15 targetSdkVersion.apiLevel = 23 versionCode = 4 versionName = "1.0.1" } } android.ndk { moduleName = "mymodule" ldLibs += ['log'] cppFlags += "-std=c++11" cppFlags += "-fexceptions" stl = 'gnustl_shared' } android.signingConfigs { create("release") { keyAlias = KEY_ALIAS keyPassword = STORE_PASSWORD storeFile = file(STORE_FILE) storePassword = KEY_PASSWORD } } android.buildTypes { release { minifyEnabled = true shrinkResources = true proguardFiles += file('proguard-rules.txt') // signingConfig = signingConfigs.release //not activated yet because of https://code.google.com/p/android/issues/detail?id=182249 } } } dependencies { compile 'com.android.support:support-v4:23.0.1' compile fileTree(dir: 'libs', include: ['*.jar']) }

轉換時要留意幾個規則:所有 android 相關宣告都要搬進 model { } 裡;賦值要明確寫上 =;陣列無法直接覆寫,必須用 += 追加;而 xxxSdkVersion 這類屬性則要改寫成 xxxSdkVersion.apiLevel。

好消息是,NDK 的設定可以直接寫在 DSL 中,不必再維護 Android.mk。

三、啟用 C++ 支援

第一步:在 build.gradle 中宣告 NDK 模組,C++ 支援才會被啟用。

model { //... android.ndk { moduleName = "mymodule" } }

第二步:完成後,你就能在 Java 檔案中建立帶 native 關鍵字的方法,接著按下 Alt + Enter,CLion 的外掛會直接幫你產生對應的 C++ 實作骨架:

就這麼簡單。產生的 cpp 檔會被放進 jni 資料夾中。Google 也提供了官方範例專案可供參考:https://github.com/googlesamples/android-ndk

以下是 NDK 區塊可用的完整設定項目:

android.ndk { moduleName = "mymodule" ldLibs += ['log'] ldFlags += "" toolchain = "clang" toolchainVersion = "3.9" abiFilters += "x86" CFlags += "" cppFlags += "" debuggable = false renderscriptNdkMode = false stl = "system" platformVersion = 15 }

四、除錯原生程式碼

Android Studio 1.3 以上支援 LLDB 與 GDB 兩種後端。只要建立並使用新的「Android Native」執行/除錯設定即可:

以 debug 模式啟動時,debugable 標記會自動設為 true,之後就能像除錯 Java 程式碼一樣中斷點、逐步執行原生程式。

本文轉譯自:https://ph0b.com/new-android-studio-ndk-support/

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved