【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )
文章目錄
- 一、創(chuàng)建支持 Kotlin 的 NDK 項(xiàng)目
- 二、Kotlin 語言中使用 NDK 要點(diǎn)
- 1、加載動態(tài)庫
- 2、聲明 ndk 方法
- 3、Project 下的 build.gradle 配置
- 4、Module 下的 build.gradle 配置
- 三、代碼示例
- 1、Java 代碼
- 2、C++ 代碼
- 3、Project 下的 build.gradle
- 4、Module 下的 build.gradle
- 5、執(zhí)行效果
- 四、GitHub 地址
一、創(chuàng)建支持 Kotlin 的 NDK 項(xiàng)目
點(diǎn)擊 菜單欄 / File / New / New Project / Create New Project , 彈出以下對話框 , 選擇 Native C++ 項(xiàng)目 , 點(diǎn)擊 Next 按鈕 ;
在后續(xù)對話框中 , 使用默認(rèn)的 Kotlin 語言 , 即可生成 Kotlin 中使用 NDK 的代碼 ;
默認(rèn) C++ 標(biāo)準(zhǔn)即可 ;
二、Kotlin 語言中使用 NDK 要點(diǎn)
1、加載動態(tài)庫
Kotlin 中在類的 companion object 伴生對象 中加載動態(tài)庫 , 類似于 Java 的靜態(tài)代碼塊 ;
companion object {// Used to load the 'native-lib' library on application startup.init {System.loadLibrary("native-lib")}}2、聲明 ndk 方法
Java 中使用 native 聲明 ndk 方法 , 在 Kotlin 中 , 使用 external 聲明 ndk 方法 ;
/*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*/external fun stringFromJNI(): Stringndk 方法對應(yīng)的 C++ 方法 ;
#include <jni.h> #include <string>extern "C" JNIEXPORT jstring JNICALL Java_kim_hsl_ndk_1kotlin_MainActivity_stringFromJNI(JNIEnv* env,jobject /* this */) {std::string hello = "Hello from C++";return env->NewStringUTF(hello.c_str()); }3、Project 下的 build.gradle 配置
需要配置 Kotlin 版本號 , 和 Kotlin 插件版本號 ;
buildscript {ext.kotlin_version = "1.4.10"dependencies {classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"} }4、Module 下的 build.gradle 配置
在 Module 下的 build.gradle 中 ,
kotlin-android 是必須配置的 ,
kotlin-android-extensions 是擴(kuò)展 , 選擇性配置 , 配置了之后 , 可以很方便地使用視圖綁定 ;
kotlin-kapt 也是選擇性配置 , 配置使用注解 ;
plugins {id 'com.android.application'id 'kotlin-android'id 'kotlin-android-extensions'id 'kotlin-kapt' }三、代碼示例
1、Java 代碼
package kim.hsl.ndk_kotlinimport androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.widget.TextViewclass MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// Example of a call to a native methodfindViewById<TextView>(R.id.sample_text).text = stringFromJNI()}/*** A native method that is implemented by the 'native-lib' native library,* which is packaged with this application.*/external fun stringFromJNI(): Stringcompanion object {// Used to load the 'native-lib' library on application startup.init {System.loadLibrary("native-lib")}} }
2、C++ 代碼
#include <jni.h> #include <string>extern "C" JNIEXPORT jstring JNICALL Java_kim_hsl_ndk_1kotlin_MainActivity_stringFromJNI(JNIEnv* env,jobject /* this */) {std::string hello = "Hello from C++";return env->NewStringUTF(hello.c_str()); }
3、Project 下的 build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript {ext.kotlin_version = "1.4.10"repositories {google()jcenter()}dependencies {classpath "com.android.tools.build:gradle:4.1.0"classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files} }allprojects {repositories {google()jcenter()} }task clean(type: Delete) {delete rootProject.buildDir }
4、Module 下的 build.gradle
plugins {id 'com.android.application'id 'kotlin-android'id 'kotlin-android-extensions'id 'kotlin-kapt' }android {compileSdkVersion 29buildToolsVersion "30.0.2"defaultConfig {applicationId "kim.hsl.ndk_kotlin"minSdkVersion 18targetSdkVersion 29versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"externalNativeBuild {cmake {cppFlags ""}}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}externalNativeBuild {cmake {path "src/main/cpp/CMakeLists.txt"version "3.10.2"}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}kotlinOptions {jvmTarget = '1.8'} }dependencies {implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"implementation 'androidx.core:core-ktx:1.3.2'implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'com.google.android.material:material:1.2.1'implementation 'androidx.constraintlayout:constraintlayout:2.0.4'testImplementation 'junit:junit:4.+'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' }
5、執(zhí)行效果
四、GitHub 地址
https://github.com/han1202012/NDK_Kotlin
總結(jié)
以上是生活随笔為你收集整理的【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【计算理论】计算复杂性 ( NP 完全问
- 下一篇: 【计算理论】计算复杂性 ( coNP 问