Skip to content

Commit

Permalink
feat(android): enable minify on release, add proguard rules (#6257)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Feb 13, 2023
1 parent 7258a64 commit bef4ef5
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 30 deletions.
6 changes: 6 additions & 0 deletions .changes/enable-minify.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions .changes/inject-proguard.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Inject `proguard-tauri.pro` file in the Android project.
9 changes: 9 additions & 0 deletions core/tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions core/tauri/mobile/android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
targetSdk = 33

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
consumerProguardFiles("proguard-rules.pro")
}

buildTypes {
Expand Down Expand Up @@ -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")
}
}
Empty file.
26 changes: 6 additions & 20 deletions core/tauri/mobile/android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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>(...);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package app.tauri

@Retention(AnnotationRetention.RUNTIME)
internal annotation class JniMethod() { }
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -14,6 +16,7 @@ class PluginManager {
}
}

@JniMethod
fun load(webView: WebView?, name: String, plugin: Plugin) {
val handle = PluginHandle(plugin)
plugins[name] = handle
Expand All @@ -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(
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package app.tauri.plugin

@Retention(AnnotationRetention.RUNTIME)
annotation class TauriPlugin() { }
29 changes: 29 additions & 0 deletions core/tauri/mobile/proguard-tauri.pro
Original file line number Diff line number Diff line change
@@ -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>(...);
}
14 changes: 8 additions & 6 deletions examples/api/src-tauri/Cargo.lock

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

Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
7 changes: 5 additions & 2 deletions tooling/cli/templates/mobile/android/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit bef4ef5

Please sign in to comment.