Skip to content

Commit bef4ef5

Browse files
authored
feat(android): enable minify on release, add proguard rules (#6257)
1 parent 7258a64 commit bef4ef5

File tree

16 files changed

+87
-30
lines changed

16 files changed

+87
-30
lines changed

.changes/enable-minify.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"cli.rs": patch
3+
"cli.js": patch
4+
---
5+
6+
Change the Android template to enable minification on release and pull ProGuard rules from proguard-tauri.pro.

.changes/inject-proguard.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Inject `proguard-tauri.pro` file in the Android project.

core/tauri/build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,15 @@ fn main() {
154154
&[],
155155
)
156156
.expect("failed to copy tauri-api Android project");
157+
let tauri_proguard = include_str!("./mobile/proguard-tauri.pro").replace(
158+
"$PACKAGE",
159+
&var("WRY_ANDROID_PACKAGE").expect("missing `WRY_ANDROID_PACKAGE` environment variable"),
160+
);
161+
std::fs::write(
162+
project_dir.join("app").join("proguard-tauri.pro"),
163+
tauri_proguard,
164+
)
165+
.expect("failed to write proguard-tauri.pro");
157166
}
158167
let lib_path =
159168
PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").unwrap()).join("mobile/android");

core/tauri/mobile/android/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ android {
1212
targetSdk = 33
1313

1414
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
15-
consumerProguardFiles("consumer-rules.pro")
15+
consumerProguardFiles("proguard-rules.pro")
1616
}
1717

1818
buildTypes {
@@ -41,4 +41,4 @@ dependencies {
4141
testImplementation("junit:junit:4.13.2")
4242
androidTestImplementation("androidx.test.ext:junit:1.1.5")
4343
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
44-
}
44+
}

core/tauri/mobile/android/consumer-rules.pro

Whitespace-only changes.
Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
1-
# Add project specific ProGuard rules here.
2-
# You can control the set of applied configuration files using the
3-
# proguardFiles setting in build.gradle.
4-
#
5-
# For more details, see
6-
# http://developer.android.com/guide/developing/tools/proguard.html
1+
-keep class app.tauri.** {
2+
@app.tauri.JniMethod public <methods>;
3+
}
74

8-
# If your project uses WebView with JS, uncomment the following
9-
# and specify the fully qualified class name to the JavaScript interface
10-
# class:
11-
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12-
# public *;
13-
#}
14-
15-
# Uncomment this to preserve the line number information for
16-
# debugging stack traces.
17-
#-keepattributes SourceFile,LineNumberTable
18-
19-
# If you keep the line number information, uncomment this to
20-
# hide the original source file name.
21-
#-renamesourcefileattribute SourceFile
5+
-keep class app.tauri.JSArray,app.tauri.JSObject {
6+
public <init>(...);
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package app.tauri
2+
3+
@Retention(AnnotationRetention.RUNTIME)
4+
internal annotation class JniMethod() { }

core/tauri/mobile/android/src/main/java/app/tauri/plugin/PluginManager.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package app.tauri.plugin
22

33
import android.webkit.WebView
4+
import app.tauri.JniMethod
45
import app.tauri.Logger
56

67
class PluginManager {
78
private val plugins: HashMap<String, PluginHandle> = HashMap()
89

10+
@JniMethod
911
fun onWebViewCreated(webView: WebView) {
1012
for ((_, plugin) in plugins) {
1113
if (!plugin.loaded) {
@@ -14,6 +16,7 @@ class PluginManager {
1416
}
1517
}
1618

19+
@JniMethod
1720
fun load(webView: WebView?, name: String, plugin: Plugin) {
1821
val handle = PluginHandle(plugin)
1922
plugins[name] = handle
@@ -22,6 +25,7 @@ class PluginManager {
2225
}
2326
}
2427

28+
@JniMethod
2529
fun postIpcMessage(webView: WebView, pluginId: String, methodName: String, data: JSObject, callback: Long, error: Long) {
2630
val invoke = Invoke({ successResult, errorResult ->
2731
val (fn, result) = if (errorResult == null) Pair(callback, successResult) else Pair(
@@ -34,6 +38,7 @@ class PluginManager {
3438
dispatchPluginMessage(invoke, pluginId, methodName)
3539
}
3640

41+
@JniMethod
3742
fun runPluginMethod(id: Int, pluginId: String, methodName: String, data: JSObject) {
3843
val invoke = Invoke({ successResult, errorResult ->
3944
handlePluginResponse(id, successResult?.toString(), errorResult?.toString())
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package app.tauri.plugin
2+
3+
@Retention(AnnotationRetention.RUNTIME)
4+
annotation class TauriPlugin() { }
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
3+
-keep class $PACKAGE.TauriActivity {
4+
getAppClass(...);
5+
getVersion();
6+
}
7+
8+
-keep class $PACKAGE.RustWebView {
9+
public <init>(...);
10+
loadUrlMainThread(...);
11+
}
12+
13+
-keep class $PACKAGE.Ipc {
14+
public <init>(...);
15+
@android.webkit.JavascriptInterface public <methods>;
16+
}
17+
18+
-keep class $PACKAGE.RustWebChromeClient,$PACKAGE.RustWebViewClient {
19+
public <init>(...);
20+
}
21+
22+
-keep class $PACKAGE.MainActivity {
23+
public getPluginManager();
24+
}
25+
26+
-keep @app.tauri.plugin.TauriPlugin public class * {
27+
@app.tauri.plugin.PluginMethod public <methods>;
28+
public <init>(...);
29+
}

0 commit comments

Comments
 (0)