Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reference kotlin.Metadata #1115

Closed
mopsalarm opened this issue Mar 27, 2020 · 9 comments · Fixed by #1152
Closed

Do not reference kotlin.Metadata #1115

mopsalarm opened this issue Mar 27, 2020 · 9 comments · Fixed by #1152

Comments

@mopsalarm
Copy link

After updating from moshi 1.8.0 to 1.9.2 kotlin.Metadata annotations are suddenly kept by r8, which increases apk size on android unnecessarily. My guess is the class lookup in https://github.com/square/moshi/blob/master/moshi/src/main/java/com/squareup/moshi/internal/Util.java#L57

This is really not required if you are only using generated adapters for kotlin classes and don't want to use the ClassJsonAdapter at all.

@rharter
Copy link
Collaborator

rharter commented Mar 27, 2020

Hmm, the only keep rule for kotlin.Metadata that we add is in the kotlin-reflect artifact. Codegen also keeps the synthetic constructors marked with that, but I wouldn't expect that would keep the annotation. Can you ensure you don't have the reflect artifact?

@mopsalarm
Copy link
Author

Yes I am not using the reflect artifact. The line of code I highlighted in the issue is not present in the 1.8.0 release, so my guess is that r8 is smart enough to realize that the kotlin.Metadata class is used via r reflection and keeps it because of that.

@JakeWharton
Copy link
Collaborator

Ask it? -whyareyoukeeping class kotlin.Metadata

@mopsalarm
Copy link
Author

mopsalarm commented Mar 28, 2020

Seems like my guess was correct:

moshi 1.9.2

|- is reflected from:
|  void com.squareup.moshi.internal.Util.<clinit>()
|- is reachable from:
|  com.squareup.moshi.internal.Util
|- is referenced from:
|  com.squareup.moshi.JsonAdapter com.squareup.moshi.Moshi.adapter(java.lang.reflect.Type,java.util.Set,java.lang.String)
|- is invoked from:
|  void com.pr0gramm.app.api.pr0gramm.Api_TagDetails_TagInfoJsonAdapter.<init>(com.squareup.moshi.Moshi)

moshi 1.8.0

Nothing is keeping kotlin.Metadata

@Tolriq
Copy link
Contributor

Tolriq commented Mar 30, 2020

Faced the same after finding a workaround for my generics issue.
Adding

-assumevalues public class com.squareup.moshi.internal.Util {
  private static final * METADATA return null;
}

To R8 config does workaround this.

@Tolriq
Copy link
Contributor

Tolriq commented Mar 31, 2020

FYI: Asked R8 team https://issuetracker.google.com/issues/152811099

Seems only a workaround is available for now.

Tolriq added a commit to Tolriq/moshi that referenced this issue Jun 4, 2020
…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
Tolriq added a commit to Tolriq/moshi that referenced this issue Jun 4, 2020
…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
Tolriq added a commit to Tolriq/moshi that referenced this issue Jun 4, 2020
…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
Tolriq added a commit to Tolriq/moshi that referenced this issue Jun 5, 2020
…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
@Thomas-Vos
Copy link
Contributor

Hello @Tolriq, I tried your workaround by adding assumevalues but it does not work for me with Moshi 1.9.2/1.9.3. Do you have any suggestions?

-assumevalues public class com.squareup.moshi.internal.Util {
  private static final * METADATA return null;
}
-whyareyoukeeping class kotlin.Metadata
|- is reflected from:
|  void com.squareup.moshi.internal.Util.<clinit>()
|- is reachable from:
|  com.squareup.moshi.internal.Util
|- is referenced from:
|  java.lang.Object com.squareup.moshi.ClassJsonAdapter.fromJson(com.squareup.moshi.JsonReader)
|- is overriding method:
|  java.lang.Object com.squareup.moshi.JsonAdapter.fromJson(com.squareup.moshi.JsonReader)
|- is invoked from:
...

@mopsalarm
Copy link
Author

There is a pull request with the workaround to not reference it still open.

@Tolriq
Copy link
Contributor

Tolriq commented Jun 14, 2020

Assume value no more works with last R8.

The proper fix is yet to be merged :(

I hope my 2 PR are one day merged and a new release is made.

murillorodney62 added a commit to murillorodney62/kotlin-codegen-android that referenced this issue Oct 12, 2022
…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/moshi#1115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants