feat(android): inject tauri.conf.json asset, add plugin config load API (#6837)

This commit is contained in:
Lucas Fernandes Nogueira 2023-05-03 04:16:05 -07:00 committed by GitHub
parent bb2a8ccf13
commit 73c803a561
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 66 additions and 10 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Added static function `loadConfig` on the Android `PluginManager` class.

View File

@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---
Inject Tauri configuration in the Android assets.

View File

@ -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
@ -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)) {

View File

@ -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
@ -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?)
}

View File

@ -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();
}

View File

@ -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::{
@ -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(())
}

View File

@ -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,
@ -152,6 +152,8 @@ 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(),
@ -159,7 +161,7 @@ fn run_build(
vars: Default::default(),
};
let _handle = write_options(
&get_config(options.config.as_deref())?
&tauri_config
.lock()
.unwrap()
.as_ref()
@ -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,

View File

@ -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,
@ -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()
@ -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 {

View File

@ -2,8 +2,8 @@
// 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<()> {
@ -11,6 +11,7 @@ pub fn command() -> Result<()> {
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)
},

View File

@ -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