Skip to content

Commit da381e0

Browse files
authored
feat(core): resources on mobile apps (#10696)
* feat(core): resources on mobile apps * resources dir on android
1 parent 086271b commit da381e0

File tree

14 files changed

+111
-23
lines changed

14 files changed

+111
-23
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:feat
3+
"@tauri-apps/cli": patch:feat
4+
---
5+
6+
Inject configured resources on mobile apps.

.changes/resource-dir-android.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": patch:bug
3+
---
4+
5+
Implemented `resource_dir` on Android, which returns a URI that needs to be resolved using [AssetManager::open](https://developer.android.com/reference/android/content/res/AssetManager#open(java.lang.String,%20int)). This will be handled by the file system plugin.

.changes/resource-dir-ios.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri-utils": patch:bug
3+
---
4+
5+
Fix `resource_dir` on iOS.

core/tauri-utils/src/platform.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ use crate::{Env, PackageInfo};
1515

1616
mod starting_binary;
1717

18+
#[cfg(target_os = "android")]
19+
pub const ANDROID_ASSET_PROTOCOL_URI_PREFIX: &str = "asset://localhost/";
20+
1821
/// Platform target.
1922
#[derive(PartialEq, Eq, Copy, Debug, Clone, Serialize, Deserialize)]
2023
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
@@ -256,7 +259,13 @@ fn is_cargo_output_directory(path: &Path) -> bool {
256259
/// `${exe_dir}/../lib/${exe_name}`.
257260
///
258261
/// On MacOS, it's `${exe_dir}../Resources` (inside .app).
262+
///
263+
/// On iOS, it's `${exe_dir}/assets`.
264+
///
265+
/// Android uses a special URI prefix that is resolved by the Tauri file system plugin `asset://localhost/`
259266
pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<PathBuf> {
267+
#[cfg(target_os = "android")]
268+
return Ok(PathBuf::from(ANDROID_ASSET_PROTOCOL_URI_PREFIX));
260269
let exe = current_exe()?;
261270
resource_dir_from(exe, package_info, env)
262271
}
@@ -320,6 +329,11 @@ fn resource_dir_from<P: AsRef<Path>>(
320329
.map_err(Into::into);
321330
}
322331

332+
#[cfg(target_os = "ios")]
333+
{
334+
res = exe_dir.join("assets").canonicalize().map_err(Into::into);
335+
}
336+
323337
res
324338
}
325339

core/tauri/mobile/android/src/main/java/app/tauri/PathPlugin.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import app.tauri.plugin.Plugin
1212
import app.tauri.plugin.Invoke
1313
import app.tauri.plugin.JSObject
1414

15+
const val TAURI_ASSETS_DIRECTORY_URI = "asset://localhost/"
16+
1517
@TauriPlugin
1618
class PathPlugin(private val activity: Activity): Plugin(activity) {
1719
private fun resolvePath(invoke: Invoke, path: String?) {
@@ -67,8 +69,7 @@ class PathPlugin(private val activity: Activity): Plugin(activity) {
6769

6870
@Command
6971
fun getResourcesDir(invoke: Invoke) {
70-
// TODO
71-
resolvePath(invoke, activity.cacheDir.absolutePath)
72+
resolvePath(invoke, TAURI_ASSETS_DIRECTORY_URI)
7273
}
7374

7475
@Command

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/src/helpers/fs.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2+
// SPDX-License-Identifier: Apache-2.0
3+
// SPDX-License-Identifier: MIT
4+
5+
use anyhow::Result;
6+
use std::path::Path;
7+
8+
pub fn copy_file(from: impl AsRef<Path>, to: impl AsRef<Path>) -> Result<()> {
9+
let from = from.as_ref();
10+
let to = to.as_ref();
11+
if !from.exists() {
12+
return Err(anyhow::anyhow!("{:?} does not exist", from));
13+
}
14+
if !from.is_file() {
15+
return Err(anyhow::anyhow!("{:?} is not a file", from));
16+
}
17+
let dest_dir = to.parent().expect("No data in parent");
18+
std::fs::create_dir_all(dest_dir)?;
19+
std::fs::copy(from, to)?;
20+
Ok(())
21+
}

tooling/cli/src/helpers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub mod cargo_manifest;
88
pub mod config;
99
pub mod flock;
1010
pub mod framework;
11+
pub mod fs;
1112
pub mod npm;
1213
pub mod plugins;
1314
pub mod prompts;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// SPDX-License-Identifier: MIT
44

55
use super::{
6-
configure_cargo, delete_codegen_vars, ensure_init, env, get_app, get_config, inject_assets,
6+
configure_cargo, delete_codegen_vars, ensure_init, env, get_app, get_config, inject_resources,
77
log_finished, open_and_wait, MobileTarget, OptionsHandle,
88
};
99
use crate::{
@@ -209,7 +209,7 @@ fn run_build(
209209
cli_options,
210210
)?;
211211

212-
inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
212+
inject_resources(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
213213

214214
let apk_outputs = if options.apk {
215215
apk::build(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use super::{
66
configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, get_app, get_config,
7-
inject_assets, open_and_wait, MobileTarget,
7+
inject_resources, open_and_wait, MobileTarget,
88
};
99
use crate::{
1010
dev::Options as DevOptions,
@@ -244,7 +244,7 @@ fn run_dev(
244244
cli_options,
245245
)?;
246246

247-
inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
247+
inject_resources(config, tauri_config.lock().unwrap().as_ref().unwrap())?;
248248

249249
if open {
250250
open_and_wait(config, &env)

0 commit comments

Comments
 (0)