177 lines
5.1 KiB
Markdown
177 lines
5.1 KiB
Markdown
# Kotlin调用Jni的写法
|
||
|
||
## 一、安装CMake和NDK
|
||
|
||
在android studio的设置中安装Cmake,安装Cmake的时候注意cmake的版本。如果忘记CMake的版本,那么可以通过Android SDK Locatio目录中查看CMake的版本。如下图所示的CMake和NDK。
|
||
|
||
![image-20230221221558825](E:\技术武器库\技术开发笔记\Android开发笔记\Image\image-20230221221558825.png)
|
||
|
||
## 二、创建工程
|
||
|
||
选择一个带 Native C++ 支持的工程,如下图所示:
|
||
|
||
![image-20230221223742943](E:\技术武器库\技术开发笔记\Android开发笔记\Image\image-20230221223742943.png)
|
||
|
||
## 三、配置 ndk 目录
|
||
|
||
在文件 local.properties 中加入 ndk 的配置
|
||
|
||
## 四、目录结构
|
||
|
||
以下文件都是自动生成的
|
||
|
||
![image-20230221223834839](E:\技术武器库\技术开发笔记\Android开发笔记\Image\image-20230221223834839.png)
|
||
|
||
## 五、配置模块 build.gradle
|
||
|
||
```shell
|
||
apply plugin: 'com.android.application'
|
||
apply plugin: 'kotlin-android'
|
||
apply plugin: 'kotlin-android-extensions'
|
||
|
||
android {
|
||
compileSdkVersion 29
|
||
buildToolsVersion "29.0.2"
|
||
|
||
defaultConfig {
|
||
applicationId "com.lujianfei.androidjnicall"
|
||
minSdkVersion 26
|
||
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"
|
||
}
|
||
}
|
||
}
|
||
|
||
dependencies {
|
||
implementation fileTree(dir: 'libs', include: ['*.jar'])
|
||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
|
||
|
||
|
||
implementation 'androidx.appcompat:appcompat:1.1.0'
|
||
implementation 'androidx.core:core-ktx:1.2.0'
|
||
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
|
||
testImplementation 'junit:junit:4.12'
|
||
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
|
||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||
}
|
||
```
|
||
|
||
## 六、配置CMakeLists.txt和native-lib.cpp
|
||
|
||
```
|
||
# For more information about using CMake with Android Studio, read the
|
||
# documentation: https://d.android.com/studio/projects/add-native-code.html
|
||
|
||
# Sets the minimum version of CMake required to build the native library.
|
||
|
||
cmake_minimum_required(VERSION 3.4.1)
|
||
|
||
# Creates and names a library, sets it as either STATIC
|
||
# or SHARED, and provides the relative paths to its source code.
|
||
# You can define multiple libraries, and CMake builds them for you.
|
||
# Gradle automatically packages shared libraries with your APK.
|
||
|
||
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).
|
||
native-lib.cpp )
|
||
|
||
# Searches for a specified prebuilt library and stores the path as a
|
||
# variable. Because CMake includes system libraries in the search path by
|
||
# default, you only need to specify the name of the public NDK library
|
||
# you want to add. CMake verifies that the library exists before
|
||
# completing its build.
|
||
|
||
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 )
|
||
|
||
# Specifies libraries CMake should link to your target library. You
|
||
# can link multiple libraries, such as libraries you define in this
|
||
# build script, prebuilt third-party libraries, or system libraries.
|
||
|
||
target_link_libraries( # Specifies the target library.
|
||
native-lib
|
||
|
||
# Links the target library to the log library
|
||
# included in the NDK.
|
||
${log-lib} )
|
||
```
|
||
|
||
```C++
|
||
#include <jni.h>
|
||
#include <string>
|
||
|
||
extern "C" JNIEXPORT jstring JNICALL
|
||
Java_com_lujianfei_androidjnicall_MainActivity_stringFromJNI(
|
||
JNIEnv* env,
|
||
jobject /* this */) {
|
||
std::string hello = "Hello from C++";
|
||
return env->NewStringUTF(hello.c_str());
|
||
}
|
||
```
|
||
|
||
## 七、Activity调用
|
||
|
||
```kotlin
|
||
package com.lujianfei.androidjnicall
|
||
|
||
import androidx.appcompat.app.AppCompatActivity
|
||
import android.os.Bundle
|
||
import kotlinx.android.synthetic.main.activity_main.*
|
||
|
||
class MainActivity : AppCompatActivity() {
|
||
|
||
override fun onCreate(savedInstanceState: Bundle?) {
|
||
super.onCreate(savedInstanceState)
|
||
setContentView(R.layout.activity_main)
|
||
|
||
// Example of a call to a native method
|
||
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")
|
||
}
|
||
}
|
||
}
|
||
```
|
||
|