Skip to content

Commit

Permalink
R8 now have automatic detection of Class.forName and auto generate ke…
Browse files Browse the repository at this point in the history
…ep 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 square#1115
  • Loading branch information
Tolriq committed Jun 4, 2020
1 parent dd8611a commit 916ab24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
13 changes: 13 additions & 0 deletions moshi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<name>Moshi</name>

<dependencies>
<dependency>
<groupId>androidx.annotation</groupId>
<artifactId>annotation</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
Expand All @@ -34,6 +39,14 @@
</dependency>
</dependencies>

<repositories>
<repository>
<id>google</id>
<name>Google</name>
<url>https://maven.google.com/</url>
</repository>
</repositories>

<build>
<plugins>
<plugin>
Expand Down
8 changes: 7 additions & 1 deletion moshi/src/main/java/com/squareup/moshi/internal/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.moshi.internal;

import androidx.annotation.Keep;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonClass;
import com.squareup.moshi.JsonDataException;
Expand Down Expand Up @@ -54,7 +55,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(getKotlinMetadataClassNameForReflection());
} catch (ClassNotFoundException ignored) {
}
METADATA = metadata;
Expand All @@ -69,6 +70,11 @@ public final class Util {
DEFAULT_CONSTRUCTOR_MARKER = defaultConstructorMarker;
}

@Keep /* Kept to avoid optimizing out reflection. */
private static String getKotlinMetadataClassNameForReflection() {
return "kotlin.Metadata";
}

private Util() {
}

Expand Down

0 comments on commit 916ab24

Please sign in to comment.