0. 首先配置好rust开发环境:
rust编译android命令行程序
1. 进到项目的 app/src/main 目录, 创建一个rust目录
然后进到 app/src/main/rust, 执行
cargo new --lib softsim
2.在项目根目录下的 build.gradle 里添加
id “org.mozilla.rust-android-gradle.rust-android” version “0.9.2”
例如
plugins {
id 'com.android.application' version '7.1.2' apply false
id 'com.android.library' version '7.1.2' apply false
id "org.mozilla.rust-android-gradle.rust-android" version "0.9.2"
}
3. 在 app/build.gradle 里添加
android {
...
}
cargo {
module = "./src/main/rust/softsim" // Or whatever directory contains your Cargo.toml
libname = "softsim" // Or whatever matches Cargo.toml's [package] name.
targets = ["arm", "arm64"] // See bellow for a longer list of options
profile = 'release' // default debug
pythonCommand = "python3"
}
dependencies {
.....
}
tasks.whenTaskAdded { task ->
if ((task.name == 'javaPreCompileDebug' || task.name == 'javaPreCompileRelease')) {
task.dependsOn 'cargoBuild'
}
}
4. settings.gradle
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}