Skip to content

Commit

Permalink
feat(android): versionName and versionCode support
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotaku committed May 23, 2024
1 parent d950ac1 commit 1ec4cb5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion core/tauri-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
21 changes: 20 additions & 1 deletion core/tauri-build/src/mobile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,22 @@
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();
let mut app_build_gradle = "// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
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();
Expand Down Expand Up @@ -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(())
}
4 changes: 4 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
}

impl Default for AndroidConfig {
fn default() -> Self {
Self {
min_sdk_version: default_min_sdk_version(),
version_code: None,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion tooling/cli/templates/mobile/android/app/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/src/main/jniLibs/**/*.so
/src/main/assets/tauri.conf.json
/tauri.build.gradle.kts
/proguard-tauri.pro
/proguard-tauri.pro
/tauri.properties
13 changes: 11 additions & 2 deletions tooling/cli/templates/mobile/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Properties

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
Expand All @@ -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}}"
Expand All @@ -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") {
Expand Down

0 comments on commit 1ec4cb5

Please sign in to comment.