From ef1d15a267b24e953f1c1bbb9510d23d04d74bf2 Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Tue, 23 Dec 2025 22:04:06 +0100 Subject: [PATCH 1/2] add possibility to set gradle plugin und kotlin plugin versions from the manifest #Conflicts: # .vscode/launch.json --- xbuild/Cargo.toml | 2 +- xbuild/src/config.rs | 16 ++++++++++++++++ xbuild/src/gradle/build.gradle | 10 ---------- xbuild/src/gradle/mod.rs | 20 ++++++++++++++++++-- 4 files changed, 35 insertions(+), 13 deletions(-) delete mode 100644 xbuild/src/gradle/build.gradle diff --git a/xbuild/Cargo.toml b/xbuild/Cargo.toml index 816b4cce..017991b7 100644 --- a/xbuild/Cargo.toml +++ b/xbuild/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xbuild" -version = "0.2.0" +version = "0.3.0" edition = "2021" description = "Builds rust mobile/desktop projects." repository = "https://github.com/rust-mobile/xbuild" diff --git a/xbuild/src/config.rs b/xbuild/src/config.rs index d5d204f8..6e5fb7cf 100644 --- a/xbuild/src/config.rs +++ b/xbuild/src/config.rs @@ -430,6 +430,22 @@ pub struct AndroidConfig { pub manifest: AndroidManifest, #[serde(default)] pub dependencies: Vec, + /// The gradle plugin version which shall be set in the main build.gradle file + /// Make sure it is compatible with the installed gradle version! + /// Depending on the plugin version a minimum gradle version must be available + /// Depending on the api level a minimum gradle plugin version is required + /// https://developer.android.com/build/releases/gradle-plugin?#kts + /// + /// Recommendation: + /// 1) Select desired api level + /// 2) Determine minium gradle plugin version and use at least that version (See url above) + /// 3) Determin minium gradle version (See url above) + #[serde(default)] + pub gradle_plugin_version: Option, + /// The kotlin plugin version which shall be set in the main build.gradle file + /// Determine version from the gradle plugin version using https://developer.android.com/build/kotlin-support + #[serde(default)] + pub kotlin_plugin_version: Option, /// Defaults to [`false`], but uses [`true`] when the user builds a format that requires /// `gradle` (i.e. [`Format::Aab`]). #[serde(default)] diff --git a/xbuild/src/gradle/build.gradle b/xbuild/src/gradle/build.gradle deleted file mode 100644 index c75b5ce2..00000000 --- a/xbuild/src/gradle/build.gradle +++ /dev/null @@ -1,10 +0,0 @@ -// Top-level build file where you can add configuration options common to all sub-projects/modules. -plugins { - id 'com.android.application' version '7.3.0' apply false - id 'com.android.library' version '7.3.0' apply false - id 'org.jetbrains.kotlin.android' version '1.7.20' apply false -} - -task clean(type: Delete) { - delete rootProject.buildDir -} diff --git a/xbuild/src/gradle/mod.rs b/xbuild/src/gradle/mod.rs index 35955337..3172666b 100644 --- a/xbuild/src/gradle/mod.rs +++ b/xbuild/src/gradle/mod.rs @@ -4,7 +4,6 @@ use apk::Target; use std::path::{Path, PathBuf}; use std::process::Command; -static BUILD_GRADLE: &[u8] = include_bytes!("./build.gradle"); static GRADLE_PROPERTIES: &[u8] = include_bytes!("./gradle.properties"); static SETTINGS_GRADLE: &[u8] = include_bytes!("./settings.gradle"); static IC_LAUNCHER: &[u8] = include_bytes!("./ic_launcher.xml"); @@ -41,8 +40,25 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R let jnilibs = main.join("jniLibs"); let res = main.join("res"); + let gradle_plugin_version = env.config().android().gradle_plugin_version.as_ref().map(|s| s.as_str()).unwrap_or("7.3.0"); + let kotlin_plugin_version = env.config().android().kotlin_plugin_version.as_ref().map(|s| s.as_str()).unwrap_or("1.7.20"); + + let build_gradle = format!(r#" + // Top-level build file where you can add configuration options common to all sub-projects/modules. + plugins {{ + id 'com.android.application' version '{gradle_plugin_version}' apply false + id 'com.android.library' version '{gradle_plugin_version}' apply false + id 'org.jetbrains.kotlin.android' version '{kotlin_plugin_version}' apply false + }} + + task clean(type: Delete) {{ + delete rootProject.buildDir + }} + "# + ); + std::fs::create_dir_all(&kotlin)?; - std::fs::write(gradle.join("build.gradle"), BUILD_GRADLE)?; + std::fs::write(gradle.join("build.gradle"), build_gradle)?; std::fs::write(gradle.join("gradle.properties"), GRADLE_PROPERTIES)?; std::fs::write(gradle.join("settings.gradle"), SETTINGS_GRADLE)?; From 231f803df1724393e9e98eb1196bd32f4feb06aa Mon Sep 17 00:00:00 2001 From: Martin Marmsoler Date: Tue, 23 Dec 2025 22:03:21 +0100 Subject: [PATCH 2/2] add possibility to specify also the java version Reason: so the user is more flexible what to use --- xbuild/src/config.rs | 13 +++++++++++-- xbuild/src/gradle/mod.rs | 20 ++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/xbuild/src/config.rs b/xbuild/src/config.rs index 6e5fb7cf..81a70870 100644 --- a/xbuild/src/config.rs +++ b/xbuild/src/config.rs @@ -421,6 +421,10 @@ pub struct AndroidDebugConfig { pub reverse: HashMap, } +fn default_java_version() -> usize { + 8 +} + #[derive(Clone, Debug, Default, Deserialize)] #[serde(deny_unknown_fields)] pub struct AndroidConfig { @@ -430,6 +434,10 @@ pub struct AndroidConfig { pub manifest: AndroidManifest, #[serde(default)] pub dependencies: Vec, + /// Java version used for compilation + /// For Java 1.8 and below specify `8` and below + #[serde(default = "default_java_version")] + pub java_version: usize, /// The gradle plugin version which shall be set in the main build.gradle file /// Make sure it is compatible with the installed gradle version! /// Depending on the plugin version a minimum gradle version must be available @@ -442,10 +450,11 @@ pub struct AndroidConfig { /// 3) Determin minium gradle version (See url above) #[serde(default)] pub gradle_plugin_version: Option, - /// The kotlin plugin version which shall be set in the main build.gradle file + /// The kotlin version which shall be set in the main build.gradle file /// Determine version from the gradle plugin version using https://developer.android.com/build/kotlin-support + /// Important: This version of Kotlin must be installed on the system! #[serde(default)] - pub kotlin_plugin_version: Option, + pub kotlin_version: Option, /// Defaults to [`false`], but uses [`true`] when the user builds a format that requires /// `gradle` (i.e. [`Format::Aab`]). #[serde(default)] diff --git a/xbuild/src/gradle/mod.rs b/xbuild/src/gradle/mod.rs index 3172666b..d622ecd2 100644 --- a/xbuild/src/gradle/mod.rs +++ b/xbuild/src/gradle/mod.rs @@ -39,16 +39,17 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R let kotlin = main.join("kotlin"); let jnilibs = main.join("jniLibs"); let res = main.join("res"); + let config = env.config().android(); - let gradle_plugin_version = env.config().android().gradle_plugin_version.as_ref().map(|s| s.as_str()).unwrap_or("7.3.0"); - let kotlin_plugin_version = env.config().android().kotlin_plugin_version.as_ref().map(|s| s.as_str()).unwrap_or("1.7.20"); + let gradle_plugin_version = config.gradle_plugin_version.as_ref().map(|s| s.as_str()).unwrap_or("7.3.0"); + let kotlin_version = config.kotlin_version.as_ref().map(|s| s.as_str()).unwrap_or("1.7.20"); let build_gradle = format!(r#" // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins {{ id 'com.android.application' version '{gradle_plugin_version}' apply false id 'com.android.library' version '{gradle_plugin_version}' apply false - id 'org.jetbrains.kotlin.android' version '{kotlin_plugin_version}' apply false + id 'org.jetbrains.kotlin.android' version '{kotlin_version}' apply false }} task clean(type: Delete) {{ @@ -62,7 +63,6 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R std::fs::write(gradle.join("gradle.properties"), GRADLE_PROPERTIES)?; std::fs::write(gradle.join("settings.gradle"), SETTINGS_GRADLE)?; - let config = env.config().android(); let mut manifest = config.manifest.clone(); let package = manifest.package.take().unwrap_or_default(); @@ -88,6 +88,9 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R r#"assetPacks = [":baseAssets"]"# }; + let java_version = config.java_version; + let gradle_java_version_rep = if java_version <= 8 { format!("1_{java_version}") } else {format!("{java_version}")}; + let app_build_gradle = format!( r#" plugins {{ @@ -104,6 +107,15 @@ pub fn build(env: &BuildEnv, libraries: Vec<(Target, PathBuf)>, out: &Path) -> R versionCode {version_code} versionName '{version_name}' }} + + compileOptions {{ + sourceCompatibility JavaVersion.VERSION_{gradle_java_version_rep} + targetCompatibility JavaVersion.VERSION_{gradle_java_version_rep} + }} + + kotlin {{ + jvmToolchain({java_version}) + }} {asset_packs} }} dependencies {{