feat(android): enable minify on release, add proguard rules (#6257)

This commit is contained in:
Lucas Fernandes Nogueira
2023-02-13 05:22:03 -08:00
committed by GitHub
parent 7258a64730
commit bef4ef51bc
16 changed files with 87 additions and 30 deletions

View File

@@ -0,0 +1,6 @@
---
"cli.rs": patch
"cli.js": patch
---
Change the Android template to enable minification on release and pull ProGuard rules from proguard-tauri.pro.

View File

@@ -0,0 +1,5 @@
---
"tauri": patch
---
Inject `proguard-tauri.pro` file in the Android project.

View File

@@ -154,6 +154,15 @@ fn main() {
&[],
)
.expect("failed to copy tauri-api Android project");
let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
"$PACKAGE",
&var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
);
std::fs::write(
project_dir.join("app").join("proguard-tauri.pro"),
tauri_proguard,
)
.expect("failed to write proguard-tauri.pro");
}
let lib_path =
PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");

View File

@@ -12,7 +12,7 @@ android {
targetSdk = 33
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
consumerProguardFiles("proguard-rules.pro")
}
buildTypes {
@@ -41,4 +41,4 @@ dependencies {
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
}
}

View File

@@ -1,21 +1,7 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
-keep class app.tauri.** {
@app.tauri.JniMethod public <methods>;
}
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
-keep class app.tauri.JSArray,app.tauri.JSObject {
public <init>(...);
}

View File

@@ -0,0 +1,4 @@
package app.tauri
@Retention(AnnotationRetention.RUNTIME)
internal annotation class JniMethod() { }

View File

@@ -1,11 +1,13 @@
package app.tauri.plugin
import android.webkit.WebView
import app.tauri.JniMethod
import app.tauri.Logger
class PluginManager {
private val plugins: HashMap<String, PluginHandle> = HashMap()
@JniMethod
fun onWebViewCreated(webView: WebView) {
for ((_, plugin) in plugins) {
if (!plugin.loaded) {
@@ -14,6 +16,7 @@ class PluginManager {
}
}
@JniMethod
fun load(webView: WebView?, name: String, plugin: Plugin) {
val handle = PluginHandle(plugin)
plugins[name] = handle
@@ -22,6 +25,7 @@ class PluginManager {
}
}
@JniMethod
fun postIpcMessage(webView: WebView, pluginId: String, methodName: String, data: JSObject, callback: Long, error: Long) {
val invoke = Invoke({ successResult, errorResult ->
val (fn, result) = if (errorResult == null) Pair(callback, successResult) else Pair(
@@ -34,6 +38,7 @@ class PluginManager {
dispatchPluginMessage(invoke, pluginId, methodName)
}
@JniMethod
fun runPluginMethod(id: Int, pluginId: String, methodName: String, data: JSObject) {
val invoke = Invoke({ successResult, errorResult ->
handlePluginResponse(id, successResult?.toString(), errorResult?.toString())

View File

@@ -0,0 +1,4 @@
package app.tauri.plugin
@Retention(AnnotationRetention.RUNTIME)
annotation class TauriPlugin() { }

View File

@@ -0,0 +1,29 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
-keep class $PACKAGE.TauriActivity {
getAppClass(...);
getVersion();
}
-keep class $PACKAGE.RustWebView {
public <init>(...);
loadUrlMainThread(...);
}
-keep class $PACKAGE.Ipc {
public <init>(...);
@android.webkit.JavascriptInterface public <methods>;
}
-keep class $PACKAGE.RustWebChromeClient,$PACKAGE.RustWebViewClient {
public <init>(...);
}
-keep class $PACKAGE.MainActivity {
public getPluginManager();
}
-keep @app.tauri.plugin.TauriPlugin public class * {
@app.tauri.plugin.PluginMethod public <methods>;
public <init>(...);
}

View File

@@ -2932,8 +2932,9 @@ dependencies = [
[[package]]
name = "tao"
version = "0.17.0"
source = "git+https://github.com/tauri-apps/tao?branch=dev#8971d731b02ec61a1665351c9bae11f5e4058dc4"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d16138f5d521fcde40580e1a34df784b063dd9ac05c7cbe344bf01f02a23be"
dependencies = [
"bitflags",
"cairo-rs",
@@ -2980,8 +2981,9 @@ dependencies = [
[[package]]
name = "tao-macros"
version = "0.1.0"
source = "git+https://github.com/tauri-apps/tao?branch=dev#8971d731b02ec61a1665351c9bae11f5e4058dc4"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b27a4bcc5eb524658234589bdffc7e7bfb996dbae6ce9393bfd39cb4159b445"
dependencies = [
"proc-macro2",
"quote",
@@ -4108,8 +4110,8 @@ dependencies = [
[[package]]
name = "wry"
version = "0.26.0"
source = "git+https://github.com/tauri-apps/wry?branch=dev#9613a08312645bbad80de6b91c5ba33656cf6a5e"
version = "0.27.0"
source = "git+https://github.com/tauri-apps/wry?branch=dev#fc113d6e85ca5cd899c1f9c8c28a1e3c13612698"
dependencies = [
"base64 0.13.1",
"block",

View File

@@ -5,7 +5,9 @@ import app.tauri.plugin.JSObject
import app.tauri.plugin.Plugin
import app.tauri.plugin.Invoke
import app.tauri.plugin.PluginMethod
import app.tauri.plugin.TauriPlugin
@TauriPlugin
class ExamplePlugin(private val activity: Activity): Plugin() {
private val implementation = Example()

View File

@@ -36,8 +36,11 @@ android {
}
}
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
isMinifyEnabled = true
val proguards = fileTree(".") {
include("*.pro")
}
proguardFiles(*proguards.toList().toTypedArray())
}
}
flavorDimensions.add("abi")

View File

@@ -5,7 +5,9 @@ import app.tauri.plugin.JSObject
import app.tauri.plugin.Plugin
import app.tauri.plugin.Invoke
import app.tauri.plugin.PluginMethod
import app.tauri.plugin.TauriPlugin
@TauriPlugin
class ExamplePlugin(private val activity: Activity): Plugin() {
private val implementation = Example()