From a4b5331748df175dec64c70ff48b35affd578bd4 Mon Sep 17 00:00:00 2001 From: Mariotaku Date: Thu, 23 May 2024 10:58:49 +0900 Subject: [PATCH 1/5] feat(android): versionName and versionCode support --- core/tauri-build/src/lib.rs | 2 +- core/tauri-build/src/mobile.rs | 21 ++++++++++++++++++- core/tauri-utils/src/config.rs | 4 ++++ .../templates/mobile/android/app/.gitignore | 3 ++- .../mobile/android/app/build.gradle.kts | 13 ++++++++++-- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/core/tauri-build/src/lib.rs b/core/tauri-build/src/lib.rs index 2c90b5b64ee0..5b6dd9a1184e 100644 --- a/core/tauri-build/src/lib.rs +++ b/core/tauri-build/src/lib.rs @@ -468,7 +468,7 @@ pub fn try_build(attributes: Attributes) -> Result<()> { println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_PREFIX={android_package_prefix}"); if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) { - mobile::generate_gradle_files(project_dir)?; + mobile::generate_gradle_files(project_dir, &config)?; } cfg_alias("dev", dev()); diff --git a/core/tauri-build/src/mobile.rs b/core/tauri-build/src/mobile.rs index c12ac1e62029..0ed289e8cb89 100644 --- a/core/tauri-build/src/mobile.rs +++ b/core/tauri-build/src/mobile.rs @@ -5,10 +5,13 @@ use std::{fs::write, path::PathBuf}; use anyhow::{Context, Result}; +use semver::Version; +use tauri_utils::config::Config; -pub fn generate_gradle_files(project_dir: PathBuf) -> Result<()> { +pub fn generate_gradle_files(project_dir: PathBuf, config: &Config) -> Result<()> { let gradle_settings_path = project_dir.join("tauri.settings.gradle"); let app_build_gradle_path = project_dir.join("app").join("tauri.build.gradle.kts"); + let app_tauri_properties_path = project_dir.join("app").join("tauri.properties"); let mut gradle_settings = "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n".to_string(); @@ -16,6 +19,8 @@ pub fn generate_gradle_files(project_dir: PathBuf) -> Result<()> { val implementation by configurations dependencies {" .to_string(); + let mut app_tauri_properties = + "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n".to_string(); for (env, value) in std::env::vars_os() { let env = env.to_string_lossy(); @@ -48,13 +53,27 @@ dependencies {" app_build_gradle.push_str("\n}"); + if let Some(version) = config.version.as_ref() { + app_tauri_properties.push_str(&format!("tauri.android.versionName={}\n", version)); + if let Some(version_code) = config.bundle.android.version_code.as_ref() { + app_tauri_properties.push_str(&format!("tauri.android.versionCode={}\n", version_code)); + } else if let Ok(version) = Version::parse(version) { + let version_code = version.major * 1000000 + version.minor * 1000 + version.patch; + app_tauri_properties.push_str(&format!("tauri.android.versionCode={}\n", version_code)); + } + } + write(&gradle_settings_path, gradle_settings).context("failed to write tauri.settings.gradle")?; write(&app_build_gradle_path, app_build_gradle) .context("failed to write tauri.build.gradle.kts")?; + write(&app_tauri_properties_path, app_tauri_properties) + .context("failed to write tauri.properties")?; + println!("cargo:rerun-if-changed={}", gradle_settings_path.display()); println!("cargo:rerun-if-changed={}", app_build_gradle_path.display()); + println!("cargo:rerun-if-changed={}", app_tauri_properties_path.display()); Ok(()) } diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 73acca8fc353..f9962aabab94 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -1770,12 +1770,16 @@ pub struct AndroidConfig { /// The Android system will prevent the user from installing the application if the system's API level is lower than the value specified. #[serde(alias = "min-sdk-version", default = "default_min_sdk_version")] pub min_sdk_version: u32, + + #[serde(alias = "version-code")] + pub version_code: Option, } impl Default for AndroidConfig { fn default() -> Self { Self { min_sdk_version: default_min_sdk_version(), + version_code: None, } } } diff --git a/tooling/cli/templates/mobile/android/app/.gitignore b/tooling/cli/templates/mobile/android/app/.gitignore index 532fcd9e0745..439b71f0e392 100644 --- a/tooling/cli/templates/mobile/android/app/.gitignore +++ b/tooling/cli/templates/mobile/android/app/.gitignore @@ -2,4 +2,5 @@ /src/main/jniLibs/**/*.so /src/main/assets/tauri.conf.json /tauri.build.gradle.kts -/proguard-tauri.pro \ No newline at end of file +/proguard-tauri.pro +/tauri.properties \ No newline at end of file diff --git a/tooling/cli/templates/mobile/android/app/build.gradle.kts b/tooling/cli/templates/mobile/android/app/build.gradle.kts index 06870f32d1f1..e6befa182cde 100644 --- a/tooling/cli/templates/mobile/android/app/build.gradle.kts +++ b/tooling/cli/templates/mobile/android/app/build.gradle.kts @@ -1,3 +1,5 @@ +import java.util.Properties + plugins { id("com.android.application") id("org.jetbrains.kotlin.android") @@ -6,6 +8,13 @@ plugins { id("{{this}}"){{/each}} } +val tauriProperties = Properties().apply { + val propFile = file("tauri.properties") + if (propFile.exists()) { + propFile.inputStream().use { load(it) } + } +} + android { compileSdk = 33 namespace = "{{reverse-domain app.domain}}.{{snake-case app.name}}" @@ -14,8 +23,8 @@ android { applicationId = "{{reverse-domain app.domain}}.{{snake-case app.name}}" minSdk = {{android.min-sdk-version}} targetSdk = 33 - versionCode = 1 - versionName = "1.0" + versionCode = tauriProperties.getProperty("tauri.android.versionCode", "1").toInt() + versionName = tauriProperties.getProperty("tauri.android.versionName", "1.0") } buildTypes { getByName("debug") { From 4569bc97df6192887d5f80489ae4fd29aa64e166 Mon Sep 17 00:00:00 2001 From: Mariotaku Date: Fri, 24 May 2024 14:39:36 +0900 Subject: [PATCH 2/5] formatted code and updated schema --- core/tauri-build/src/mobile.rs | 15 ++++++++++++--- core/tauri-config-schema/schema.json | 10 ++++++++++ core/tauri-utils/src/config.rs | 3 +++ tooling/cli/schema.json | 10 ++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/core/tauri-build/src/mobile.rs b/core/tauri-build/src/mobile.rs index 0ed289e8cb89..f791a65e49a2 100644 --- a/core/tauri-build/src/mobile.rs +++ b/core/tauri-build/src/mobile.rs @@ -21,6 +21,7 @@ dependencies {" .to_string(); let mut app_tauri_properties = "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n".to_string(); + let mut has_app_tauri_properties = false; for (env, value) in std::env::vars_os() { let env = env.to_string_lossy(); @@ -55,6 +56,7 @@ dependencies {" if let Some(version) = config.version.as_ref() { app_tauri_properties.push_str(&format!("tauri.android.versionName={}\n", version)); + has_app_tauri_properties = true; if let Some(version_code) = config.bundle.android.version_code.as_ref() { app_tauri_properties.push_str(&format!("tauri.android.versionCode={}\n", version_code)); } else if let Ok(version) = Version::parse(version) { @@ -68,12 +70,19 @@ dependencies {" write(&app_build_gradle_path, app_build_gradle) .context("failed to write tauri.build.gradle.kts")?; - write(&app_tauri_properties_path, app_tauri_properties) - .context("failed to write tauri.properties")?; + if has_app_tauri_properties { + write(&app_tauri_properties_path, app_tauri_properties) + .context("failed to write tauri.properties")?; + } println!("cargo:rerun-if-changed={}", gradle_settings_path.display()); println!("cargo:rerun-if-changed={}", app_build_gradle_path.display()); - println!("cargo:rerun-if-changed={}", app_tauri_properties_path.display()); + if has_app_tauri_properties { + println!( + "cargo:rerun-if-changed={}", + app_tauri_properties_path.display() + ); + } Ok(()) } diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 964c616d8bf7..a6c392e103e5 100644 --- a/core/tauri-config-schema/schema.json +++ b/core/tauri-config-schema/schema.json @@ -2840,6 +2840,16 @@ "type": "integer", "format": "uint32", "minimum": 0.0 + }, + "versionCode": { + "description": "The version code of the application. It was limited to 2,100,000,000 as per Google Play Store requirements.", + "type": [ + "integer", + "null" + ], + "format": "uint32", + "maximum": 2100000000.0, + "minimum": 1.0 } }, "additionalProperties": false diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index f9962aabab94..946614df0cde 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -1771,7 +1771,10 @@ pub struct AndroidConfig { #[serde(alias = "min-sdk-version", default = "default_min_sdk_version")] pub min_sdk_version: u32, + /// The version code of the application. + /// It was limited to 2,100,000,000 as per Google Play Store requirements. #[serde(alias = "version-code")] + #[validate(range(min = 1, max = 2_100_000_000))] pub version_code: Option, } diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 964c616d8bf7..a6c392e103e5 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -2840,6 +2840,16 @@ "type": "integer", "format": "uint32", "minimum": 0.0 + }, + "versionCode": { + "description": "The version code of the application. It was limited to 2,100,000,000 as per Google Play Store requirements.", + "type": [ + "integer", + "null" + ], + "format": "uint32", + "maximum": 2100000000.0, + "minimum": 1.0 } }, "additionalProperties": false From 931e0732aab9e462248e27ad06acd3636db38c48 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 24 May 2024 10:20:00 -0300 Subject: [PATCH 3/5] fix config, cleanup generation --- core/tauri-build/src/mobile.rs | 25 ++++++++++++++----------- core/tauri-utils/src/config.rs | 2 +- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/core/tauri-build/src/mobile.rs b/core/tauri-build/src/mobile.rs index f791a65e49a2..eec8288f2044 100644 --- a/core/tauri-build/src/mobile.rs +++ b/core/tauri-build/src/mobile.rs @@ -19,9 +19,7 @@ pub fn generate_gradle_files(project_dir: PathBuf, config: &Config) -> Result<() val implementation by configurations dependencies {" .to_string(); - let mut app_tauri_properties = - "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n".to_string(); - let mut has_app_tauri_properties = false; + let mut app_tauri_properties = Vec::new(); for (env, value) in std::env::vars_os() { let env = env.to_string_lossy(); @@ -55,13 +53,12 @@ dependencies {" app_build_gradle.push_str("\n}"); if let Some(version) = config.version.as_ref() { - app_tauri_properties.push_str(&format!("tauri.android.versionName={}\n", version)); - has_app_tauri_properties = true; + app_tauri_properties.push(format!("tauri.android.versionName={}", version)); if let Some(version_code) = config.bundle.android.version_code.as_ref() { - app_tauri_properties.push_str(&format!("tauri.android.versionCode={}\n", version_code)); + app_tauri_properties.push(format!("tauri.android.versionCode={}", version_code)); } else if let Ok(version) = Version::parse(version) { let version_code = version.major * 1000000 + version.minor * 1000 + version.patch; - app_tauri_properties.push_str(&format!("tauri.android.versionCode={}\n", version_code)); + app_tauri_properties.push(format!("tauri.android.versionCode={}", version_code)); } } @@ -70,14 +67,20 @@ dependencies {" write(&app_build_gradle_path, app_build_gradle) .context("failed to write tauri.build.gradle.kts")?; - if has_app_tauri_properties { - write(&app_tauri_properties_path, app_tauri_properties) - .context("failed to write tauri.properties")?; + if !app_tauri_properties.is_empty() { + write( + &app_tauri_properties_path, + format!( + "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n{}", + app_tauri_properties.join("\n") + ), + ) + .context("failed to write tauri.properties")?; } println!("cargo:rerun-if-changed={}", gradle_settings_path.display()); println!("cargo:rerun-if-changed={}", app_build_gradle_path.display()); - if has_app_tauri_properties { + if !app_tauri_properties.is_empty() { println!( "cargo:rerun-if-changed={}", app_tauri_properties_path.display() diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 946614df0cde..b81849e38dc0 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -1774,7 +1774,7 @@ pub struct AndroidConfig { /// The version code of the application. /// It was limited to 2,100,000,000 as per Google Play Store requirements. #[serde(alias = "version-code")] - #[validate(range(min = 1, max = 2_100_000_000))] + #[cfg_attr(feature = "schema", validate(range(min = 1, max = 2_100_000_000)))] pub version_code: Option, } From 204297d4d6296a4e97e9cb4f8828c891dbc098ab Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 24 May 2024 10:39:04 -0300 Subject: [PATCH 4/5] update docs --- core/tauri-config-schema/schema.json | 2 +- core/tauri-utils/src/config.rs | 5 ++++- tooling/cli/schema.json | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index 98cccd571fa5..c7b53ec4388a 100644 --- a/core/tauri-config-schema/schema.json +++ b/core/tauri-config-schema/schema.json @@ -2837,7 +2837,7 @@ "minimum": 0.0 }, "versionCode": { - "description": "The version code of the application. It was limited to 2,100,000,000 as per Google Play Store requirements.", + "description": "The version code of the application. It is limited to 2,100,000,000 as per Google Play Store requirements.\n\nBy default we use your configured version and perform the following math: versionCode = version.major * 1000000 + version.minor * 1000 + version.patch", "type": [ "integer", "null" diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 768b20958a9f..338ee4e8830e 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -1767,7 +1767,10 @@ pub struct AndroidConfig { pub min_sdk_version: u32, /// The version code of the application. - /// It was limited to 2,100,000,000 as per Google Play Store requirements. + /// It is limited to 2,100,000,000 as per Google Play Store requirements. + /// + /// By default we use your configured version and perform the following math: + /// versionCode = version.major * 1000000 + version.minor * 1000 + version.patch #[serde(alias = "version-code")] #[cfg_attr(feature = "schema", validate(range(min = 1, max = 2_100_000_000)))] pub version_code: Option, diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index 98cccd571fa5..c7b53ec4388a 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -2837,7 +2837,7 @@ "minimum": 0.0 }, "versionCode": { - "description": "The version code of the application. It was limited to 2,100,000,000 as per Google Play Store requirements.", + "description": "The version code of the application. It is limited to 2,100,000,000 as per Google Play Store requirements.\n\nBy default we use your configured version and perform the following math: versionCode = version.major * 1000000 + version.minor * 1000 + version.patch", "type": [ "integer", "null" From b6192f4221ad48c1b5fea6cdbaa8c7316a8f963c Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Fri, 24 May 2024 10:42:14 -0300 Subject: [PATCH 5/5] mention default version for android --- core/tauri-config-schema/schema.json | 2 +- core/tauri-utils/src/config.rs | 2 ++ tooling/cli/schema.json | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/tauri-config-schema/schema.json b/core/tauri-config-schema/schema.json index c7b53ec4388a..f4071d6c7c7b 100644 --- a/core/tauri-config-schema/schema.json +++ b/core/tauri-config-schema/schema.json @@ -20,7 +20,7 @@ "pattern": "^[^/\\:*?\"<>|]+$" }, "version": { - "description": "App version. It is a semver version number or a path to a `package.json` file containing the `version` field. If removed the version number from `Cargo.toml` is used.", + "description": "App version. It is a semver version number or a path to a `package.json` file containing the `version` field. If removed the version number from `Cargo.toml` is used.\n\nBy default version 1.0 is used on Android.", "type": [ "string", "null" diff --git a/core/tauri-utils/src/config.rs b/core/tauri-utils/src/config.rs index 338ee4e8830e..8b5d93556d6b 100644 --- a/core/tauri-utils/src/config.rs +++ b/core/tauri-utils/src/config.rs @@ -2040,6 +2040,8 @@ pub struct Config { #[cfg_attr(feature = "schema", validate(regex(pattern = "^[^/\\:*?\"<>|]+$")))] pub product_name: Option, /// App version. It is a semver version number or a path to a `package.json` file containing the `version` field. If removed the version number from `Cargo.toml` is used. + /// + /// By default version 1.0 is used on Android. #[serde(deserialize_with = "version_deserializer", default)] pub version: Option, /// The application identifier in reverse domain name notation (e.g. `com.tauri.example`). diff --git a/tooling/cli/schema.json b/tooling/cli/schema.json index c7b53ec4388a..f4071d6c7c7b 100644 --- a/tooling/cli/schema.json +++ b/tooling/cli/schema.json @@ -20,7 +20,7 @@ "pattern": "^[^/\\:*?\"<>|]+$" }, "version": { - "description": "App version. It is a semver version number or a path to a `package.json` file containing the `version` field. If removed the version number from `Cargo.toml` is used.", + "description": "App version. It is a semver version number or a path to a `package.json` file containing the `version` field. If removed the version number from `Cargo.toml` is used.\n\nBy default version 1.0 is used on Android.", "type": [ "string", "null"