You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The goal of this change is to support explicitly building particular Cargo
profiles per Android build type. This change makes it possible to build
both release and debug tasks in a single gradle invocation without editing
the use of the `cargo` extension.
There are some backwards incompatible changes present:
`profile` is deprecated and is replaced with the *required* map `buildTypeToProfile`.
This map controls which `cargoBuild${buildType}` tasks are created and
what Cargo profile is used for each. Once
rust-lang/cargo#6988 is resolved and stabilized,
we should switch the implementation to use `cargo build --profile=$x`
explicitly rather than `--release`.
Finally, run the`cargoBuild` task to cross compile:
56
+
Finally, run a`cargoBuild${buildType}` task (see `buildTypeToProfile`) to cross compile:
57
57
```sh
58
-
./gradlew cargoBuild
58
+
./gradlew cargoBuildDebug
59
59
```
60
60
Or add it as a dependency to one of your other build tasks, to build your rust code when you normally build your project:
61
61
```gradle
62
62
tasks.whenTaskAdded { task ->
63
-
if ((task.name == 'javaPreCompileDebug' || task.name == 'javaPreCompileRelease')) {
64
-
task.dependsOn 'cargoBuild'
63
+
if (task.name == 'javaPreCompileDebug') {
64
+
task.dependsOn 'cargoBuildDebug'
65
+
} else if (task.name == 'javaPreCompileRelease') {
66
+
task.dependsOn 'cargoBuildRelease'
65
67
}
66
68
}
67
69
```
@@ -99,11 +101,6 @@ which can be specified using the `apiLevel` option. This option defaults to the
99
101
level. As of API level 21, 64-bit builds are possible; and conversely, the `arm64` and `x86_64`
100
102
targets require `apiLevel >= 21`.
101
103
102
-
### Cargo release profile
103
-
104
-
The `profile` option selects between the `--debug` and `--release` profiles in `cargo`. *Defaults
105
-
to `debug`!*
106
-
107
104
### Extension reference
108
105
109
106
### module
@@ -190,18 +187,24 @@ cargo {
190
187
}
191
188
```
192
189
193
-
### profile
194
-
195
-
The Cargo [release profile](https://doc.rust-lang.org/book/second-edition/ch14-01-release-profiles.html#customizing-builds-with-release-profiles) to build.
190
+
### buildTypeToProfile
196
191
197
-
Defaults to `"debug"`.
192
+
This mandatory option specifies the Cargo [release profile](https://doc.rust-lang.org/book/second-edition/ch14-01-release-profiles.html#customizing-builds-with-release-profiles) to build per [Android build type](https://developer.android.com/studio/build/build-variants#build-types).
193
+
Each entry in the map causes a new `cargoBuild${buildType}` task to be created.
198
194
199
195
```groovy
200
196
cargo {
201
-
profile = 'release'
197
+
buildTypeToProfile = [
198
+
"debug": "debug",
199
+
"alpha": "debug",
200
+
"beta": "release",
201
+
"release": "release",
202
+
]
202
203
}
203
204
```
204
205
206
+
The above example creates these targets: `cargoBuildDebug`, `cargoBuildAlpha`, `cargoBuildBeta`, `cargoBuildRelease`.
207
+
205
208
### features
206
209
207
210
Set the Cargo [features](https://doc.rust-lang.org/cargo/reference/manifest.html#the-features-section).
val buildTask = tasks.maybeCreate("cargoBuild${buildType.capitalize()}",
265
+
DefaultTask::class.java).apply {
266
+
group =RUST_TASK_GROUP
267
+
description ="Build library (all targets) for android build type ${buildType}"
268
+
}
264
269
265
-
cargoExtension.targets!!.forEach { target ->
266
-
val theToolchain = toolchains
267
-
.filter {
268
-
if (usePrebuilt) {
269
-
it.type !=ToolchainType.ANDROID_GENERATED
270
-
} else {
271
-
it.type !=ToolchainType.ANDROID_PREBUILT
270
+
cargoExtension.targets!!.forEach { target ->
271
+
val theToolchain = toolchains
272
+
.filter {
273
+
if (usePrebuilt) {
274
+
it.type !=ToolchainType.ANDROID_GENERATED
275
+
} else {
276
+
it.type !=ToolchainType.ANDROID_PREBUILT
277
+
}
272
278
}
273
-
}
274
-
.find { it.platform == target }
275
-
if (theToolchain ==null) {
276
-
throwGradleException("Target ${target} is not recognized (recognized targets: ${toolchains.map { it.platform }.sorted()}). Check `local.properties` and `build.gradle`.")
277
-
}
279
+
.find { it.platform == target }
280
+
if (theToolchain ==null) {
281
+
throwGradleException("Target ${target} is not recognized (recognized targets: ${toolchains.map { it.platform }.sorted()}). Check `local.properties` and `build.gradle`.")
282
+
}
278
283
279
-
val targetBuildTask = tasks.maybeCreate("cargoBuild${target.capitalize()}",
280
-
CargoBuildTask::class.java).apply {
281
-
group =RUST_TASK_GROUP
282
-
description ="Build library ($target)"
283
-
toolchain = theToolchain
284
-
}
284
+
val targetBuildTask = tasks.maybeCreate("cargoBuild${target.capitalize()}${buildType.capitalize()}",
285
+
CargoBuildTask::class.java).apply {
286
+
group =RUST_TASK_GROUP
287
+
description ="Build library ($target) for android build type ${buildType}"
0 commit comments