From 4a5f2ec1aee0d1af026bf74b1cfad049fc537872 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Thu, 25 Aug 2022 00:19:47 -0300 Subject: [PATCH] feat(android): enable dev HMR in both HTTP and HTTPS dev servers (#5033) --- core/tauri/src/manager.rs | 5 ++++- examples/api/src-tauri/Cargo.lock | 2 +- examples/api/vite.config.js | 1 + tooling/cli/src/mobile/android.rs | 8 +++++++ tooling/cli/src/mobile/android/build.rs | 11 +++++++++- tooling/cli/src/mobile/android/dev.rs | 22 ++++++++++++++++++- .../mobile/android/app/build.gradle.kts | 2 ++ .../android/app/src/main/AndroidManifest.xml | 3 ++- 8 files changed, 49 insertions(+), 5 deletions(-) diff --git a/core/tauri/src/manager.rs b/core/tauri/src/manager.rs index 1f670d235..35083fb82 100644 --- a/core/tauri/src/manager.rs +++ b/core/tauri/src/manager.rs @@ -847,7 +847,10 @@ impl WindowManager { let mut response = { let mut url = url.clone(); url.set_path(&path); - match attohttpc::get(url.as_str()).send() { + match attohttpc::get(url.as_str()) + .danger_accept_invalid_certs(true) + .send() + { Ok(r) => { for (name, value) in r.headers() { builder = builder.header(name, value); diff --git a/examples/api/src-tauri/Cargo.lock b/examples/api/src-tauri/Cargo.lock index 86333c28c..ce1d02a8c 100644 --- a/examples/api/src-tauri/Cargo.lock +++ b/examples/api/src-tauri/Cargo.lock @@ -4222,7 +4222,7 @@ dependencies = [ [[package]] name = "wry" version = "0.20.2" -source = "git+https://github.com/tauri-apps/wry?branch=dev#b4789034dc4d10ab83f6acce6b4152d79f702940" +source = "git+https://github.com/tauri-apps/wry?branch=dev#b1e8560c3f13f2674528f6ca440ba476ddbef7c2" dependencies = [ "block", "cocoa", diff --git a/examples/api/vite.config.js b/examples/api/vite.config.js index c5d66fbbc..6ccdda26a 100644 --- a/examples/api/vite.config.js +++ b/examples/api/vite.config.js @@ -21,6 +21,7 @@ export default defineConfig(async ({ command, mode }) => { port: 5173, strictPort: true, hmr: { + protocol: 'ws', host, port: 5183 }, diff --git a/tooling/cli/src/mobile/android.rs b/tooling/cli/src/mobile/android.rs index 440938ba5..4e5809321 100644 --- a/tooling/cli/src/mobile/android.rs +++ b/tooling/cli/src/mobile/android.rs @@ -113,6 +113,14 @@ fn env() -> Result { cargo_mobile::android::env::Env::from_env(env).map_err(Error::AndroidEnvInitFailed) } +fn delete_codegen_vars() { + for (k, _) in std::env::vars() { + if k.starts_with("WRY_") && (k.ends_with("CLASS_EXTENSION") || k.ends_with("CLASS_INIT")) { + std::env::remove_var(k); + } + } +} + fn device_prompt<'a>(env: &'_ Env) -> Result, PromptError> { let device_list = adb::device_list(env).map_err(|cause| PromptError::detection_failed("Android", cause))?; diff --git a/tooling/cli/src/mobile/android/build.rs b/tooling/cli/src/mobile/android/build.rs index 5766f5f68..fdc8be9ef 100644 --- a/tooling/cli/src/mobile/android/build.rs +++ b/tooling/cli/src/mobile/android/build.rs @@ -1,4 +1,7 @@ -use super::{ensure_init, env, init_dot_cargo, log_finished, with_config, Error, MobileTarget}; +use super::{ + delete_codegen_vars, ensure_init, env, init_dot_cargo, log_finished, with_config, Error, + MobileTarget, +}; use crate::{ helpers::{config::get as get_tauri_config, flock}, interface::{AppSettings, Interface, Options as InterfaceOptions}, @@ -13,6 +16,8 @@ use cargo_mobile::{ target::TargetTrait, }; +use std::env::set_var; + #[derive(Debug, Clone, Parser)] #[clap(about = "Android build")] pub struct Options { @@ -60,7 +65,11 @@ impl From for crate::build::Options { } pub fn command(options: Options) -> Result<()> { + delete_codegen_vars(); with_config(|root_conf, config, _metadata| { + set_var("WRY_RUSTWEBVIEWCLIENT_CLASS_EXTENSION", ""); + set_var("WRY_RUSTWEBVIEW_CLASS_INIT", ""); + ensure_init(config.project_dir(), MobileTarget::Android) .map_err(|e| Error::ProjectNotInitialized(e.to_string()))?; diff --git a/tooling/cli/src/mobile/android/dev.rs b/tooling/cli/src/mobile/android/dev.rs index 073d1af7c..3c2e17aac 100644 --- a/tooling/cli/src/mobile/android/dev.rs +++ b/tooling/cli/src/mobile/android/dev.rs @@ -1,4 +1,7 @@ -use super::{device_prompt, ensure_init, env, init_dot_cargo, with_config, Error, MobileTarget}; +use super::{ + delete_codegen_vars, device_prompt, ensure_init, env, init_dot_cargo, with_config, Error, + MobileTarget, +}; use crate::{ helpers::{config::get as get_tauri_config, flock}, interface::{AppSettings, Interface, MobileOptions, Options as InterfaceOptions}, @@ -14,6 +17,17 @@ use cargo_mobile::{ os, }; +use std::env::set_var; + +const WEBVIEW_CLIENT_CLASS_EXTENSION: &str = " + @android.annotation.SuppressLint(\"WebViewClientOnReceivedSslError\") + override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler, error: android.net.http.SslError) { + handler.proceed() + } +"; +const WEBVIEW_CLASS_INIT: &str = + "this.settings.mixedContentMode = android.webkit.WebSettings.MIXED_CONTENT_ALWAYS_ALLOW"; + #[derive(Debug, Clone, Parser)] #[clap(about = "Android dev")] pub struct Options { @@ -50,7 +64,13 @@ impl From for crate::dev::Options { } pub fn command(options: Options) -> Result<()> { + delete_codegen_vars(); with_config(|root_conf, config, metadata| { + set_var( + "WRY_RUSTWEBVIEWCLIENT_CLASS_EXTENSION", + WEBVIEW_CLIENT_CLASS_EXTENSION, + ); + set_var("WRY_RUSTWEBVIEW_CLASS_INIT", WEBVIEW_CLASS_INIT); ensure_init(config.project_dir(), MobileTarget::Android) .map_err(|e| Error::ProjectNotInitialized(e.to_string()))?; run_dev(options, root_conf, config, metadata).map_err(|e| Error::DevFailed(format!("{:#}", e))) diff --git a/tooling/cli/templates/mobile/android/app/build.gradle.kts b/tooling/cli/templates/mobile/android/app/build.gradle.kts index 9031fd8ed..7ebfc0480 100644 --- a/tooling/cli/templates/mobile/android/app/build.gradle.kts +++ b/tooling/cli/templates/mobile/android/app/build.gradle.kts @@ -9,6 +9,7 @@ plugins { android { compileSdk = 33 defaultConfig { + manifestPlaceholders["usesCleartextTraffic"] = "false" applicationId = "{{reverse-domain app.domain}}.{{snake-case app.name}}" minSdk = {{android.min-sdk-version}} targetSdk = 33 @@ -23,6 +24,7 @@ android { } buildTypes { getByName("debug") { + manifestPlaceholders["usesCleartextTraffic"] = "true" isDebuggable = true isJniDebuggable = true isMinifyEnabled = false diff --git a/tooling/cli/templates/mobile/android/app/src/main/AndroidManifest.xml b/tooling/cli/templates/mobile/android/app/src/main/AndroidManifest.xml index 657c7e775..de5a85690 100644 --- a/tooling/cli/templates/mobile/android/app/src/main/AndroidManifest.xml +++ b/tooling/cli/templates/mobile/android/app/src/main/AndroidManifest.xml @@ -5,7 +5,8 @@ + android:theme="@style/Theme.{{snake-case app.name}}" + android:usesCleartextTraffic="${usesCleartextTraffic}">