Skip to content

Commit

Permalink
feat(android): inject tauri.conf.json asset, add plugin config load A…
Browse files Browse the repository at this point in the history
…PI (#6837)
  • Loading branch information
lucasfernog committed May 3, 2023
1 parent bb2a8cc commit 73c803a
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changes/android-load-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Added static function `loadConfig` on the Android `PluginManager` class.
6 changes: 6 additions & 0 deletions .changes/inject-config.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---

Inject Tauri configuration in the Android assets.
7 changes: 7 additions & 0 deletions core/tauri/mobile/android/src/main/java/app/tauri/FsUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package app.tauri

import android.content.ContentUris
import android.content.Context
import android.content.res.AssetManager
import android.database.Cursor
import android.net.Uri
import android.os.Environment
Expand All @@ -18,6 +19,12 @@ import kotlin.math.min

internal class FsUtils {
companion object {
fun readAsset(assetManager: AssetManager, fileName: String): String {
assetManager.open(fileName).bufferedReader().use {
return it.readText()
}
}

fun getFileUrlForUri(context: Context, uri: Uri): String? {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

package app.tauri.plugin

import android.content.Context
import android.content.Intent
import android.webkit.WebView
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import app.tauri.FsUtils
import app.tauri.JniMethod
import app.tauri.Logger

Expand Down Expand Up @@ -123,5 +125,14 @@ class PluginManager(val activity: AppCompatActivity) {
}
}

companion object {
fun loadConfig(context: Context, plugin: String): JSObject {
val tauriConfigJson = FsUtils.readAsset(context.assets, "tauri.conf.json")
val tauriConfig = JSObject(tauriConfigJson)
val plugins = tauriConfig.getJSObject("plugins", JSObject())
return plugins.getJSObject(plugin, JSObject())
}
}

private external fun handlePluginResponse(id: Int, success: String?, error: String?)
}
1 change: 0 additions & 1 deletion core/tauri/mobile/proguard-tauri.pro
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.


-keep class $PACKAGE.TauriActivity {
public app.tauri.plugin.PluginManager getPluginManager();
}
20 changes: 19 additions & 1 deletion tooling/cli/src/mobile/android.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
// SPDX-License-Identifier: MIT

use clap::{Parser, Subcommand};
use std::{env::set_var, fs::create_dir, process::exit, thread::sleep, time::Duration};
use std::{
env::set_var,
fs::{create_dir, create_dir_all, write},
process::exit,
thread::sleep,
time::Duration,
};
use sublime_fuzzy::best_match;
use tauri_mobile::{
android::{
Expand Down Expand Up @@ -296,3 +302,15 @@ fn open_and_wait(config: &AndroidConfig, env: &Env) -> ! {
sleep(Duration::from_secs(24 * 60 * 60));
}
}

fn inject_assets(config: &AndroidConfig, tauri_config: &TauriConfig) -> Result<()> {
let asset_dir = config.project_dir().join("app/src/main/assets");
create_dir_all(&asset_dir)?;

write(
asset_dir.join("tauri.conf.json"),
serde_json::to_string(&tauri_config)?,
)?;

Ok(())
}
10 changes: 7 additions & 3 deletions tooling/cli/src/mobile/android/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, delete_codegen_vars, ensure_init, env, log_finished, open_and_wait, with_config,
MobileTarget,
configure_cargo, delete_codegen_vars, ensure_init, env, inject_assets, log_finished,
open_and_wait, with_config, MobileTarget,
};
use crate::{
build::Options as BuildOptions,
Expand Down Expand Up @@ -152,14 +152,16 @@ fn run_build(
let out_dir = bin_path.parent().unwrap();
let _lock = flock::open_rw(out_dir.join("lock").with_extension("android"), "Android")?;

let tauri_config = get_config(options.config.as_deref())?;

let cli_options = CliOptions {
features: build_options.features.clone(),
args: build_options.args.clone(),
noise_level,
vars: Default::default(),
};
let _handle = write_options(
&get_config(options.config.as_deref())?
&tauri_config
.lock()
.unwrap()
.as_ref()
Expand All @@ -175,6 +177,8 @@ fn run_build(
.get_or_insert(Vec::new())
.push("custom-protocol".into());

inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;

let apk_outputs = if options.apk {
apk::build(
config,
Expand Down
10 changes: 7 additions & 3 deletions tooling/cli/src/mobile/android/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// SPDX-License-Identifier: MIT

use super::{
configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, open_and_wait,
setup_dev_config, with_config, MobileTarget,
configure_cargo, delete_codegen_vars, device_prompt, ensure_init, env, inject_assets,
open_and_wait, setup_dev_config, with_config, MobileTarget,
};
use crate::{
dev::Options as DevOptions,
Expand Down Expand Up @@ -186,8 +186,10 @@ fn run_dev(
noise_level,
vars: Default::default(),
};

let tauri_config = get_config(options.config.as_deref())?;
let _handle = write_options(
&get_config(options.config.as_deref())?
&tauri_config
.lock()
.unwrap()
.as_ref()
Expand All @@ -198,6 +200,8 @@ fn run_dev(
cli_options,
)?;

inject_assets(config, tauri_config.lock().unwrap().as_ref().unwrap())?;

if open {
open_and_wait(config, &env)
} else if let Some(device) = &device {
Expand Down
5 changes: 3 additions & 2 deletions tooling/cli/src/mobile/android/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::{ensure_init, env, with_config, MobileTarget};
use crate::Result;
use super::{ensure_init, env, inject_assets, with_config, MobileTarget};
use crate::{helpers::config::get as get_config, Result};
use tauri_mobile::os;

pub fn command() -> Result<()> {
with_config(
Some(Default::default()),
|_root_conf, config, _metadata, _cli_options| {
ensure_init(config.project_dir(), MobileTarget::Android)?;
inject_assets(config, get_config(None)?.lock().unwrap().as_ref().unwrap())?;
let env = env()?;
os::open_file_with("Android Studio", config.project_dir(), &env.base).map_err(Into::into)
},
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/templates/mobile/android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/src/main/{{package-path}}/generated
/src/main/jniLibs/**/*.so
/src/main/assets/tauri.conf.json
/tauri.build.gradle.kts
/proguard-tauri.pro

0 comments on commit 73c803a

Please sign in to comment.