R8 now have automatic detection of Class.forName and auto generate keep rules.

This makes application build with Moshi to keep unwanted Kotlin Metadata that have a huge impact on APK size.

After discussion with R8 team, there's currently no solution to avoid this other than using the trick in this PR.

Fixes https://github.com/square/moshi/issues/1115
This commit is contained in:
Tolriq
2020-04-20 14:45:38 +02:00
parent dd8611a1d9
commit 8c8c5977a7
2 changed files with 11 additions and 1 deletions

View File

@@ -54,7 +54,7 @@ public final class Util {
Class<? extends Annotation> metadata = null;
try {
//noinspection unchecked
metadata = (Class<? extends Annotation>) Class.forName("kotlin.Metadata");
metadata = (Class<? extends Annotation>) Class.forName(getKotlinMetadataClassName());
} catch (ClassNotFoundException ignored) {
}
METADATA = metadata;
@@ -69,6 +69,11 @@ public final class Util {
DEFAULT_CONSTRUCTOR_MARKER = defaultConstructorMarker;
}
// Extracted as a method with a keep rule to prevent R8 from keeping Kotlin Metada
private static String getKotlinMetadataClassName() {
return "kotlin.Metadata";
}
private Util() {
}

View File

@@ -14,3 +14,8 @@
<fields>;
**[] values();
}
# Keep helper method to avoid R8 optimisation that would keep all Kotlin Metadata when unwanted
-keepclassmembers class com.squareup.moshi.internal.Util {
private static java.lang.String getKotlinMetadataClassName();
}