日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )

發布時間:2025/6/17 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、創建支持 Kotlin 的 NDK 項目
  • 二、Kotlin 語言中使用 NDK 要點
    • 1、加載動態庫
    • 2、聲明 ndk 方法
    • 3、Project 下的 build.gradle 配置
    • 4、Module 下的 build.gradle 配置
  • 三、代碼示例
    • 1、Java 代碼
    • 2、C++ 代碼
    • 3、Project 下的 build.gradle
    • 4、Module 下的 build.gradle
    • 5、執行效果
  • 四、GitHub 地址





一、創建支持 Kotlin 的 NDK 項目



點擊 菜單欄 / File / New / New Project / Create New Project , 彈出以下對話框 , 選擇 Native C++ 項目 , 點擊 Next 按鈕 ;

在后續對話框中 , 使用默認的 Kotlin 語言 , 即可生成 Kotlin 中使用 NDK 的代碼 ;

默認 C++ 標準即可 ;





二、Kotlin 語言中使用 NDK 要點





1、加載動態庫



Kotlin 中在類的 companion object 伴生對象 中加載動態庫 , 類似于 Java 的靜態代碼塊 ;

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(): String

ndk 方法對應的 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 是擴展 , 選擇性配置 , 配置了之后 , 可以很方便地使用視圖綁定 ;

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、執行效果







四、GitHub 地址



https://github.com/han1202012/NDK_Kotlin

總結

以上是生活随笔為你收集整理的【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。