Skip to content

Commit 1df5cde

Browse files
refactor!: use tauri.conf.json > identifier as package name and bundle id, close #9851 (#9858)
* fix(android): use identifier as Android package name * update android_binding and android_fn interface * chore: rename TAURI_ANDROID_PACKAGE_PREFIX to TAURI_ANDROID_PACKAGE_NAME * revert back to split prefix and app_name * rename `domain` to `identifier` * add change log * simplify reverse config identifier * Update .changes/mobile-use-identifier-as-id.md * Update core/tauri-build/src/lib.rs * lint * cargo-mobile2 0.12 * fmt --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
1 parent 8a1ae2d commit 1df5cde

13 files changed

Lines changed: 101 additions & 77 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"tauri": patch:breaking
3+
"tauri-build": patch:breaking
4+
"tauri-cli": patch:breaking
5+
"@tauri-apps/cli": patch:breaking
6+
"tauri-macros": patch:breaking
7+
---
8+
9+
Use `tauri.conf.json > identifier` to set the `PackageName` in Android and `BundleId` in iOS.

core/tauri-build/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,13 +459,15 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
459459
let last = s.clone().count() - 1;
460460
let mut android_package_prefix = String::new();
461461
for (i, w) in s.enumerate() {
462-
if i == 0 || i != last {
462+
if i == last {
463+
println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_NAME_APP_NAME={}", w);
464+
} else {
463465
android_package_prefix.push_str(w);
464466
android_package_prefix.push('_');
465467
}
466468
}
467469
android_package_prefix.pop();
468-
println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_PREFIX={android_package_prefix}");
470+
println!("cargo:rustc-env=TAURI_ANDROID_PACKAGE_NAME_PREFIX={android_package_prefix}");
469471

470472
if let Some(project_dir) = var_os("TAURI_ANDROID_PROJECT_PATH").map(PathBuf::from) {
471473
mobile::generate_gradle_files(project_dir)?;

core/tauri-macros/src/mobile.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,14 @@ pub fn entry_point(_attributes: TokenStream, item: TokenStream) -> TokenStream {
3737
let function_name = &function.sig.ident;
3838

3939
let mut error = None;
40-
let domain = get_env_var("TAURI_ANDROID_PACKAGE_PREFIX", |r| r, &mut error, &function);
40+
let domain = get_env_var(
41+
"TAURI_ANDROID_PACKAGE_NAME_PREFIX",
42+
|r| r,
43+
&mut error,
44+
&function,
45+
);
4146
let app_name = get_env_var(
42-
"CARGO_PKG_NAME",
47+
"TAURI_ANDROID_PACKAGE_NAME_APP_NAME",
4348
|r| r.replace('-', "_"),
4449
&mut error,
4550
&function,

core/tauri/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,17 @@ pub type WryHandle = tauri_runtime_wry::WryHandle<EventLoopMessage>;
129129
#[doc(hidden)]
130130
#[macro_export]
131131
macro_rules! android_binding {
132-
($domain:ident, $package:ident, $main: ident, $wry: path) => {
132+
($domain:ident, $app_name:ident, $main:ident, $wry:path) => {
133133
use $wry::{
134134
android_setup,
135135
prelude::{JClass, JNIEnv, JString},
136136
};
137137

138-
::tauri::wry::android_binding!($domain, $package, $wry);
138+
::tauri::wry::android_binding!($domain, $app_name, $wry);
139139

140140
::tauri::tao::android_binding!(
141141
$domain,
142-
$package,
142+
$app_name,
143143
WryActivity,
144144
android_setup,
145145
$main,

examples/api/src-tauri/Cargo.lock

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

tooling/cli/Cargo.lock

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

tooling/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ name = "cargo-tauri"
3939
path = "src/main.rs"
4040

4141
[dependencies]
42-
cargo-mobile2 = { version = "0.11", default-features = false }
42+
cargo-mobile2 = { version = "0.12", default-features = false }
4343
jsonrpsee = { version = "0.22", features = [ "server" ] }
4444
jsonrpsee-core = "0.22"
4545
jsonrpsee-client-transport = { version = "0.22", features = [ "ws" ] }

tooling/cli/src/mobile/android/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,13 @@ pub fn get_config(
133133
..Default::default()
134134
};
135135

136-
set_var(
137-
"WRY_ANDROID_PACKAGE",
138-
format!("{}.{}", app.reverse_domain(), app.name_snake()),
139-
);
136+
set_var("WRY_ANDROID_PACKAGE", app.reverse_identifier());
140137
set_var("WRY_ANDROID_LIBRARY", app.lib_name());
141138
set_var("TAURI_ANDROID_PROJECT_PATH", config.project_dir());
142139

143140
let src_main_dir = config.project_dir().join("app/src/main").join(format!(
144-
"java/{}/{}",
145-
app.reverse_domain().replace('.', "/"),
146-
app.name_snake()
141+
"java/{}",
142+
app.reverse_identifier().replace('.', "/"),
147143
));
148144
if config.project_dir().exists() {
149145
if src_main_dir.exists() {

tooling/cli/src/mobile/android/project.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ pub fn gen(
109109
);
110110
map.insert("windows", cfg!(windows));
111111

112-
let domain = config.app().reverse_domain().replace('.', "/");
113-
let package_path = format!("java/{}/{}", domain, config.app().name_snake());
112+
let identifier = config.app().reverse_identifier().replace('.', "/");
113+
let package_path = format!("java/{}", identifier);
114114

115115
map.insert("package-path", &package_path);
116116

tooling/cli/src/mobile/mod.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,24 +288,21 @@ fn read_options(identifier: &str) -> CliOptions {
288288
}
289289

290290
pub fn get_app(config: &TauriConfig, interface: &AppInterface) -> App {
291-
let mut s = config.identifier.rsplit('.');
292-
let app_name = s.next().unwrap_or("app").to_string();
293-
let mut domain = String::new();
294-
for w in s {
295-
domain.push_str(w);
296-
domain.push('.');
297-
}
298-
if domain.is_empty() {
299-
domain.clone_from(&config.identifier);
300-
if domain.is_empty() {
301-
log::error!("Bundle identifier set in `tauri.conf.json > identifier` cannot be empty");
302-
exit(1);
303-
}
304-
} else {
305-
domain.pop();
291+
let identifier = config
292+
.identifier
293+
.rsplit('.')
294+
.collect::<Vec<&str>>()
295+
.join(".");
296+
297+
if identifier.is_empty() {
298+
log::error!("Bundle identifier set in `tauri.conf.json > identifier` cannot be empty");
299+
exit(1);
306300
}
307301

308-
let app_name = interface.app_settings().app_name().unwrap_or(app_name);
302+
let app_name = interface
303+
.app_settings()
304+
.app_name()
305+
.unwrap_or_else(|| "app".into());
309306
let lib_name = interface
310307
.app_settings()
311308
.lib_name()
@@ -315,7 +312,7 @@ pub fn get_app(config: &TauriConfig, interface: &AppInterface) -> App {
315312
name: app_name,
316313
lib_name: Some(lib_name),
317314
stylized_name: config.product_name.clone(),
318-
domain,
315+
identifier,
319316
asset_dir: None,
320317
template_pack: None,
321318
};

0 commit comments

Comments
 (0)