Skip to content

Commit

Permalink
resources dir on android
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 20, 2024
1 parent 877c15c commit 6e20a96
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .changes/inject-mobile-resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"@tauri-apps/cli": patch:feat
---

Inject configured resources on mobil apps.
Inject configured resources on mobile apps.
5 changes: 5 additions & 0 deletions .changes/resource-dir-android.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri-utils": patch:bug
---

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.
9 changes: 9 additions & 0 deletions core/tauri-utils/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ use crate::{Env, PackageInfo};

mod starting_binary;

#[cfg(target_os = "android")]
pub const ANDROID_ASSET_PROTOCOL_URI_PREFIX: &str = "asset://localhost/";

/// Platform target.
#[derive(PartialEq, Eq, Copy, Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
Expand Down Expand Up @@ -256,7 +259,13 @@ fn is_cargo_output_directory(path: &Path) -> bool {
/// `${exe_dir}/../lib/${exe_name}`.
///
/// On MacOS, it's `${exe_dir}../Resources` (inside .app).
///
/// On iOS, it's `${exe_dir}/assets`.
///
/// Android uses a special URI prefix that is resolved by the Tauri file system plugin `asset://localhost/`
pub fn resource_dir(package_info: &PackageInfo, env: &Env) -> crate::Result<PathBuf> {
#[cfg(target_os = "android")]
return Ok(PathBuf::from(ANDROID_ASSET_PROTOCOL_URI_PREFIX));
let exe = current_exe()?;
resource_dir_from(exe, package_info, env)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import app.tauri.plugin.Plugin
import app.tauri.plugin.Invoke
import app.tauri.plugin.JSObject

const val TAURI_ASSETS_DIRECTORY_URI = "asset://localhost/"

@TauriPlugin
class PathPlugin(private val activity: Activity): Plugin(activity) {
private fun resolvePath(invoke: Invoke, path: String?) {
Expand Down Expand Up @@ -67,8 +69,7 @@ class PathPlugin(private val activity: Activity): Plugin(activity) {

@Command
fun getResourcesDir(invoke: Invoke) {
// TODO
resolvePath(invoke, activity.cacheDir.absolutePath)
resolvePath(invoke, TAURI_ASSETS_DIRECTORY_URI)
}

@Command
Expand Down
16 changes: 8 additions & 8 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6e20a96

Please sign in to comment.