【Android NDK 开发】Kotlin 语言中使用 NDK ( 创建支持 Kotlin 的 NDK 项目 | Kotlin 语言中使用 NDK 要点 | 代码示例 )
2023-06-13 09:17:48 时间
文章目录
一、创建支持 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_kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// Example of a call to a native method
findViewById<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(): String
companion 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 29
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "kim.hsl.ndk_kotlin"
minSdkVersion 18
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles 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_8
targetCompatibility 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 地址
相关文章
- android scaleanimation动画,【Android动画九章】-RotateAnimation(旋转动画)和ScaleAnimation(尺寸动画)…[通俗易懂]
- android onresume方法,非静态方法’onResume’Android Studio
- android系统开机画面_Android开机画面
- 代码加密 android,Android 开发怎样做代码加密或混淆「建议收藏」
- android 的hook技术,Android Native Hook技术(一)
- android 混淆不起作用,Android代码混淆的写法总结
- iphone4装android,iPhone4可安装Android实现双系统启动.pdf
- Android resource linking failed_android:authorities
- Android保存图片到相册(适配android 10以下及以上)
- android调用相册并显示图片_Android获取相册列表
- android跳转到相册需要权限,Android打开相册获取图片路径[通俗易懂]
- Android修改字体_android设置字体样式
- android线程间通信的几种方法_Android进程间和线程间通信方式
- 【Android 事件分发】事件分发源码分析 ( ViewGroup 事件传递机制 四 | View 事件传递机制 )
- 【Android 逆向】函数拦截原理 ( 通过修改 GOT 全局偏移表拦截函数 | 通过在实际被调用的函数中添加跳转代码实现函数拦截 )
- 【Android Gradle 插件】Android Module 模块 build.gradle 构建脚本 Groovy 语法分析 ① ( Gradle 二进制插件引入 | Gradle依赖配置 )
- 【错误记录】Android Studio 编译报错 ( To use data binding annotations in Kotlin, apply the ‘kotlin-kapt‘ plu )
- 【错误记录】Android Studio 编译报错 ( Could not resolve com.android.tools.build:gradle:7.4.2. )
- Android约束布局ConstraintLayout详解手机开发
- Unable to resolve target ‘android-i’详解手机开发
- android dataBinding详解手机开发
- android 集成百度地图详解手机开发
- 宣判iPad死刑的不是iPhone,更不是Android
- Android使用Intent传递数据的实现思路与代码
- Android系统自带样式(android:theme)
- Android不同Activity间数据的传递Bundle对象的应用