Skip to content

Commit e2e585a

Browse files
fix(android): update template to use gradle 9 (#15828)
* Android: Gradle upgrades * typos * bump agp to latest * Remove all jvmTarget * clean up unused * migrate to getByName * remove `id("org.jetbrains.kotlin.android")` from plugin template * Try disable `builtInKotlin` * revert all gradle 9 migrations * back to JVM_1_8 * remove `org.gradle.toolchains.foojay-resolver-convention` as I still don't understand what it does * `android.useAndroidx` is true by default * fix white spaces before first packaging * Only `consumerProguardFiles` is used for Android libaries * pickup the right proguard rules * install cli instead of moving the built one * bump spin and install locked * try latest java and ndk * try to work around missing `consumer-rules.pro` * Add change files * more specific about both gradle and plugin versions * remove unused linux deps installation step * Revert "try to work around missing `consumer-rules.pro`" This reverts commit f1afdd9. * Disable `android.proguard.failOnMissingFiles` instead * update change file that we don't do the work around in tauri-build anymore * update gradle wrapper and scripts * use file helper reference: https://issuetracker.google.com/issues/463283604#comment10 --------- Co-authored-by: Megh Parikh <meghprkh@gmail.com>
1 parent 2f11853 commit e2e585a

21 files changed

Lines changed: 287 additions & 224 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
tauri: minor:deps
3+
tauri-cli: minor:deps
4+
"tauri-apps/cli": minor:deps
5+
---
6+
7+
On Android, fix missing `consumer-rules.pro` file in the template.
8+
9+
**IMPORTANT**: For plugin authors, update your `build.gradle.kts` file to remove the
10+
11+
```kotlin
12+
buildTypes {
13+
release {
14+
isMinifyEnabled = false
15+
proguardFiles(
16+
getDefaultProguardFile("proguard-android-optimize.txt"),
17+
"proguard-rules.pro"
18+
)
19+
}
20+
}
21+
```
22+
23+
section and rename your `proguard-rules.pro` to `consumer-rules.pro` to match the `consumerProguardFiles("consumer-rules.pro")` in the template.

‎.changes/android-gradle-9.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
tauri: minor:deps
3+
tauri-build: minor:deps
4+
tauri-cli: minor:deps
5+
"tauri-apps/cli": minor:deps
6+
---
7+
8+
On Android, updated the template to use Gradle v9.6.1 (`com.android.tools.build:gradle` v9.3.1) and Kotlin v2.2. Use `tauri android init` to apply the change.

‎.github/workflows/test-android.yml‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,24 @@ jobs:
3636
- name: install Rust 1.90
3737
uses: dtolnay/rust-toolchain@1.90
3838

39-
- name: install Linux dependencies
40-
if: matrix.platform == 'ubuntu-latest'
41-
run: |
42-
sudo apt-get update
43-
sudo apt-get install -y libgtk-3-dev webkit2gtk-4.1
44-
4539
- run: npm i -g --force corepack
4640
- name: setup node
4741
uses: actions/setup-node@v7
4842
with:
4943
node-version: lts/*
5044
cache: 'pnpm'
5145

52-
- uses: actions/setup-java@v3
46+
- uses: actions/setup-java@v5
5347
with:
5448
distribution: temurin
55-
java-version: 17
56-
cache: 'gradle'
49+
java-version: 25
50+
cache: gradle
5751

5852
- name: Setup NDK
5953
uses: nttld/setup-ndk@v1
6054
id: setup-ndk
6155
with:
62-
ndk-version: r25b
56+
ndk-version: r29
6357
local-cache: true
6458

6559
# TODO check after https://github.com/nttld/setup-ndk/issues/518 is fixed
@@ -76,16 +70,8 @@ jobs:
7670
7771
- uses: Swatinem/rust-cache@v2
7872

79-
- name: build CLI
80-
run: cargo build --manifest-path ./crates/tauri-cli/Cargo.toml
81-
82-
- name: move CLI to cargo bin dir
83-
if: matrix.platform != 'windows-latest'
84-
run: mv ./target/debug/cargo-tauri $HOME/.cargo/bin
85-
86-
- name: move CLI to cargo bin dir
87-
if: matrix.platform == 'windows-latest'
88-
run: mv ./target/debug/cargo-tauri.exe $HOME/.cargo/bin
73+
- name: build and install CLI
74+
run: cargo install tauri-cli --path ./crates/tauri-cli/ --locked --debug
8975

9076
- run: pnpm i --frozen-lockfile
9177

‎Cargo.lock‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/tauri-build/src/mobile.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn generate_gradle_files(project_dir: PathBuf) -> Result<()> {
151151
let mut gradle_settings =
152152
"// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n".to_string();
153153
let mut app_build_gradle = "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
154-
val implementation by configurations
154+
val implementation = configurations.getByName(\"implementation\")
155155
dependencies {
156156
implementation(\"androidx.lifecycle:lifecycle-process:2.10.0\")"
157157
.to_string();

‎crates/tauri-cli/templates/mobile/android/app/build.gradle.kts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
21
import java.util.Properties
2+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
33

44
plugins {
55
id("com.android.application")
@@ -37,20 +37,27 @@ android {
3737
isJniDebuggable = true
3838
isMinifyEnabled = false
3939
packaging {
40-
{{~#each abi-list}}
40+
{{#each abi-list}}
4141
jniLibs.keepDebugSymbols.add("*/{{this}}/*.so")
4242
{{/each}}
4343
}
4444
}
4545
getByName("release") {
46-
isMinifyEnabled = true
46+
optimization {
47+
enable = true
48+
}
4749
proguardFiles(
48-
*fileTree(".") { include("**/*.pro") }
49-
.plus(getDefaultProguardFile("proguard-android-optimize.txt"))
50-
.toList().toTypedArray()
50+
*fileTree(".") {
51+
include("**/*.pro")
52+
exclude("build/**")
53+
}.files.toTypedArray()
5154
)
5255
}
5356
}
57+
compileOptions {
58+
sourceCompatibility = JavaVersion.VERSION_1_8
59+
targetCompatibility = JavaVersion.VERSION_1_8
60+
}
5461
buildFeatures {
5562
buildConfig = true
5663
}
@@ -81,4 +88,4 @@ dependencies {
8188
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.0")
8289
}
8390

84-
apply(from = "tauri.build.gradle.kts")
91+
apply(from = file("tauri.build.gradle.kts"))

‎crates/tauri-cli/templates/mobile/android/build.gradle.kts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ buildscript {
44
mavenCentral()
55
}
66
dependencies {
7-
classpath("com.android.tools.build:gradle:8.11.0")
8-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.25")
7+
classpath("com.android.tools.build:gradle:9.3.1")
8+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.10")
99
{{~#each android-project-dependencies}}
1010
classpath("{{this}}"){{/each}}
1111
}

‎crates/tauri-cli/templates/mobile/android/buildSrc/build.gradle.kts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ repositories {
1818

1919
dependencies {
2020
compileOnly(gradleApi())
21-
implementation("com.android.tools.build:gradle:8.11.0")
21+
implementation("com.android.tools.build:gradle:9.3.1")
2222
}
2323

‎crates/tauri-cli/templates/mobile/android/buildSrc/src/main/kotlin/BuildTask.kt‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import org.gradle.api.GradleException
55
import org.gradle.api.logging.LogLevel
66
import org.gradle.api.tasks.Input
77
import org.gradle.api.tasks.TaskAction
8+
import javax.inject.Inject
9+
import org.gradle.process.ExecOperations
10+
11+
abstract class BuildTask : DefaultTask() {
12+
@get:Inject
13+
abstract val execOperations: ExecOperations
814

9-
open class BuildTask : DefaultTask() {
1015
@Input
1116
var rootDirRel: String? = null
1217
@Input
18+
var projectDir: String? = null
19+
@Input
1320
var target: String? = null
1421
@Input
1522
var release: Boolean? = null
@@ -27,7 +34,7 @@ open class BuildTask : DefaultTask() {
2734
"$executable.cmd",
2835
"$executable.bat",
2936
)
30-
37+
3138
var lastException: Exception = e
3239
for (fallback in fallbacks) {
3340
try {
@@ -50,13 +57,13 @@ open class BuildTask : DefaultTask() {
5057
val release = release ?: throw GradleException("release cannot be null")
5158
val args = listOf({{quote-and-join tauri-binary-args}});
5259

53-
project.exec {
54-
workingDir(File(project.projectDir, rootDirRel))
60+
execOperations.exec {
61+
workingDir(File(projectDir, rootDirRel))
5562
executable(executable)
5663
args(args)
57-
if (project.logger.isEnabled(LogLevel.DEBUG)) {
64+
if (logger.isEnabled(LogLevel.DEBUG)) {
5865
args("-vv")
59-
} else if (project.logger.isEnabled(LogLevel.INFO)) {
66+
} else if (logger.isEnabled(LogLevel.INFO)) {
6067
args("-v")
6168
}
6269
if (release) {

‎crates/tauri-cli/templates/mobile/android/buildSrc/src/main/kotlin/RustPlugin.kt‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,14 @@ open class RustPlugin : Plugin<Project> {
6363
val targetName = targetPair.value
6464
val targetArch = archList[targetPair.index]
6565
val targetArchCapitalized = targetArch.replaceFirstChar { it.uppercase() }
66-
val targetBuildTask = project.tasks.maybeCreate(
66+
val targetBuildTask = project.tasks.register(
6767
"rustBuild$targetArchCapitalized$profileCapitalized",
6868
BuildTask::class.java
69-
).apply {
69+
) {
7070
group = TASK_GROUP
7171
description = "Build dynamic library in $profile mode for $targetArch"
7272
rootDirRel = config.rootDirRel
73+
projectDir = project.projectDir.path
7374
target = targetName
7475
release = profile == "release"
7576
}

0 commit comments

Comments
 (0)