Skip to content

Commit e6f2597

Browse files
committed
chore: updated documentation and code cleanup
1 parent 788dbe1 commit e6f2597

File tree

6 files changed

+78
-21
lines changed

6 files changed

+78
-21
lines changed

example/app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ androidRust {
4949
runTests = true
5050
}
5151
}
52-
minimumSupportedRustVersion = "1.62.1"
52+
minimumSupportedRustVersion = "1.91.1"
5353
}
5454

5555
dependencies {

example/app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
package="dev.matrix.rust">
3+
xmlns:tools="http://schemas.android.com/tools">
54

65
<application
76
android:allowBackup="true"

example/app/src/rust_library/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use jni::objects::JObject;
33
use jni::sys::jstring;
44

55
#[no_mangle]
6-
extern fn Java_dev_matrix_rust_MainActivity_callRustCode(env: JNIEnv, _: JObject) -> jstring {
6+
extern "C" fn Java_dev_matrix_rust_MainActivity_callRustCode(env: JNIEnv, _: JObject) -> jstring {
77
env.new_string("Hello from rust!").unwrap().into_inner()
88
}
99

src/main/kotlin/dev/matrix/agp/rust/AndroidRustExtension.kt

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,26 @@ annotation class AndroidRustDslMarker
88
@AndroidRustDslMarker
99
@Suppress("unused")
1010
open class AndroidRustExtension : AndroidRustConfiguration() {
11+
/**
12+
* Specify minimum supported rust version.
13+
*
14+
* Plugin will automatically use `rustup update` command to
15+
* update rust version in case installed versions is not high enough.
16+
*/
1117
var minimumSupportedRustVersion = ""
12-
var modules = mutableMapOf<String, AndroidRustModule>()
1318

1419
/**
15-
* Disable IDE ABI injection optimization.
16-
* When true, all requested ABIs will be built regardless of IDE deployment target.
17-
* When false (default), only the IDE target ABI will be built to speed up development builds.
20+
* Configuration map of all rust libraries to build.
1821
*
19-
* Set to true if you experience "library not found" errors when running from Android Studio.
20-
* See: https://github.com/MatrixDev/GradleAndroidRustPlugin/issues/3
22+
* Keys of this map are rust crates names.
2123
*/
22-
var disableAbiOptimization = false
24+
var modules = mutableMapOf<String, AndroidRustModule>()
2325

26+
/**
27+
* Configure rust module/library to build.
28+
*
29+
* @param name Rust crate name.
30+
*/
2431
fun module(name: String, configure: AndroidRustModule.() -> Unit) {
2532
modules.getOrPut(name, ::AndroidRustModule).configure()
2633
}
@@ -29,17 +36,33 @@ open class AndroidRustExtension : AndroidRustConfiguration() {
2936
@AndroidRustDslMarker
3037
@Suppress("unused")
3138
class AndroidRustModule : AndroidRustConfiguration() {
39+
/**
40+
* Path to the rust project folder.
41+
*
42+
* This is the folder containing `Cargo.toml` file.
43+
*/
3244
lateinit var path: File
3345

46+
/**
47+
* All supported build type configurations.
48+
*
49+
* Keys of this map should correspond to this project's build variants.
50+
*/
3451
var buildTypes = hashMapOf(
3552
"debug" to AndroidRustBuildType().also {
3653
it.profile = "dev"
3754
},
3855
"release" to AndroidRustBuildType().also {
3956
it.profile = "release"
57+
it.disableAbiOptimization = true
4058
},
4159
)
4260

61+
/**
62+
* Configure rust build options.
63+
*
64+
* @param name this project's build variant.
65+
*/
4366
fun buildType(name: String, configure: AndroidRustBuildType.() -> Unit) {
4467
buildTypes.getOrPut(name, ::AndroidRustBuildType).configure()
4568
}
@@ -52,7 +75,32 @@ class AndroidRustBuildType : AndroidRustConfiguration()
5275
@AndroidRustDslMarker
5376
@Suppress("unused")
5477
open class AndroidRustConfiguration {
78+
/**
79+
* Rust profile (dev, release, etc.).
80+
*
81+
* See: https://doc.rust-lang.org/cargo/reference/profiles.html
82+
*/
5583
var profile = ""
84+
85+
/**
86+
* List of ABIs to build.
87+
*/
5688
var targets = listOf<String>()
89+
90+
/**
91+
* Run tests after the build.
92+
*
93+
* This will run `cargo test` command and check its result.
94+
*/
5795
var runTests: Boolean? = null
96+
97+
/**
98+
* Disable IDE ABI injection optimization.
99+
* - When `true`, all requested ABIs will be built regardless of IDE deployment target.
100+
* - When `false` (default), only the IDE target ABI will be built to speed up development builds.
101+
*
102+
* Set to `true` if you experience "library not found" errors when running from Android Studio.
103+
* See: https://github.com/MatrixDev/GradleAndroidRustPlugin/issues/3
104+
*/
105+
var disableAbiOptimization: Boolean? = null
58106
}

src/main/kotlin/dev/matrix/agp/rust/AndroidRustPlugin.kt

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import javax.inject.Inject
2020
//
2121
@Suppress("unused")
2222
abstract class AndroidRustPlugin @Inject constructor(
23-
private val execOperations: ExecOperations
23+
private val execOperations: ExecOperations,
2424
) : Plugin<Project> {
2525
override fun apply(project: Project) {
2626
val rustBinaries = RustBinaries(project)
@@ -65,7 +65,7 @@ abstract class AndroidRustPlugin @Inject constructor(
6565
else -> null
6666
}
6767

68-
val rustAbiSet = resolveAbiList(project, rustConfiguration.targets, extension)
68+
val rustAbiSet = resolveAbiList(project, rustConfiguration)
6969
allRustAbiSet.addAll(rustAbiSet)
7070

7171
for (rustAbi in rustAbiSet) {
@@ -90,7 +90,12 @@ abstract class AndroidRustPlugin @Inject constructor(
9090
}
9191

9292
val minimumSupportedRustVersion = SemanticVersion(extension.minimumSupportedRustVersion)
93-
installRustComponentsIfNeeded(project, execOperations, minimumSupportedRustVersion, allRustAbiSet, rustBinaries)
93+
installRustComponentsIfNeeded(
94+
execOperations,
95+
minimumSupportedRustVersion,
96+
allRustAbiSet,
97+
rustBinaries
98+
)
9499
}
95100

96101
androidComponents.onVariants { variant ->
@@ -106,11 +111,11 @@ abstract class AndroidRustPlugin @Inject constructor(
106111
}
107112
}
108113

109-
private fun resolveAbiList(project: Project, requested: Collection<String>, extension: AndroidRustExtension): Collection<Abi> {
110-
val requestedAbi = Abi.fromRustNames(requested)
114+
private fun resolveAbiList(project: Project, config: AndroidRustConfiguration): Collection<Abi> {
115+
val requestedAbi = Abi.fromRustNames(config.targets)
111116

112117
// If optimization is disabled, build all requested ABIs
113-
if (extension.disableAbiOptimization) {
118+
if (config.disableAbiOptimization == true) {
114119
return requestedAbi
115120
}
116121

@@ -122,19 +127,23 @@ abstract class AndroidRustPlugin @Inject constructor(
122127

123128
val intersectionAbi = requestedAbi.intersect(injectedAbi)
124129
check(intersectionAbi.isNotEmpty()) {
125-
"ABIs requested by IDE ($injectedAbi) are not supported by the build config ($requested)"
130+
"ABIs requested by IDE ($injectedAbi) are not supported by the build config (${config.targets})"
126131
}
127132

128133
// Return all intersecting ABIs, not just one
129134
// The original logic only returned a single ABI which caused deployment issues
130135
return intersectionAbi.toList()
131136
}
132137

138+
/**
139+
* Merge configurations with the first one having the highest priority
140+
*/
133141
private fun mergeRustConfigurations(vararg configurations: AndroidRustConfiguration?): AndroidRustConfiguration {
134142
val defaultConfiguration = AndroidRustConfiguration().also {
135143
it.profile = "release"
136144
it.targets = Abi.values().mapTo(ArrayList(), Abi::rustName)
137145
it.runTests = null
146+
it.disableAbiOptimization = null
138147
}
139148

140149
return configurations.asSequence()
@@ -150,6 +159,9 @@ abstract class AndroidRustPlugin @Inject constructor(
150159
if (result.runTests == null) {
151160
result.runTests = base.runTests
152161
}
162+
if (result.disableAbiOptimization == null) {
163+
result.disableAbiOptimization = base.disableAbiOptimization
164+
}
153165
result
154166
}
155167
}

src/main/kotlin/dev/matrix/agp/rust/RustInstaller.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import dev.matrix.agp.rust.utils.Os
66
import dev.matrix.agp.rust.utils.RustBinaries
77
import dev.matrix.agp.rust.utils.SemanticVersion
88
import dev.matrix.agp.rust.utils.log
9-
import org.gradle.api.Project
109
import org.gradle.process.ExecOperations
1110
import java.io.ByteArrayOutputStream
1211

1312
internal fun installRustComponentsIfNeeded(
14-
project: Project,
1513
execOperations: ExecOperations,
1614
minimalVersion: SemanticVersion?,
1715
abiSet: Collection<Abi>,
@@ -54,7 +52,7 @@ private fun installRustUp(execOperations: ExecOperations, rustBinaries: RustBina
5452
if (result.exitValue == 0) {
5553
return
5654
}
57-
} catch (ignored: Exception) {
55+
} catch (_: Exception) {
5856
}
5957

6058
log("installing rustup")

0 commit comments

Comments
 (0)