Moshi: Moshi 1.9.2: Crash because Reflection in Codegen not handled in Proguard

Created on 9 Dec 2019  路  19Comments  路  Source: square/moshi

Using 1.9.2 and the codegen, moshi uses reflection for classes with default parameters.
This example with proguard enabled immediately crashes:

@JsonClass(generateAdapter = true)
data class Person(val name: String = "Peter", val age: Int = 42)

class MainActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Moshi.Builder().build().adapter(Person::class.java).fromJson("{}")
    }
}
     Caused by: java.lang.NoSuchMethodException: parameter type is null
        at java.lang.Class.getConstructor0(Class.java:2326)
        at java.lang.Class.getDeclaredConstructor(Class.java:2170)
        at com.example.moshitest.PersonJsonAdapter.a(:62)
        at com.example.moshitest.PersonJsonAdapter.a(:19)
        at b.a.a.u.a.a(:40)
        at b.a.a.f.a(:43)
        at com.example.moshitest.MainActivity.onCreate(:16)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)

Most helpful comment

Any plans on a 1.9.3 or 1.9.3-SNAPSHOT release now that AS 3.6.0 is stable?

All 19 comments

What's interesting is that its happening with the agp 3.6.0-beta04&05 but not with 3.5.2. Maybe it's a r8 bug?

Related to #1038?

Same issue but interesting to see it doesn't repro on the 3.5.x version of R8. Will be fixed in 1.9.3

This issue has been closed too early IMO, 1.9.3 hasn't been released yet.

Issues are fixed by PRs, not releases. This one is also a duplicate

AGP 3.6 hit RC. Is there a chance 1.9.3 is released before it becomes stable and people upgrade en masse so they are not affected by this issue?

Also, that's preventing me from trying the rest of AGP 3.6 (I actually had to revert because of this bug once I discovered it).

If you just want to try it, you can keep metadata annotations. Don't recommend shipping it without checking the APK size implications first though.

-keep class kotlin.Metadata { *; }

It should be lower case:

-keep class kotlin.Metadata { *; }

Sorry, mobile typing. Fixed!

By retaining the metadata my application size went from 6,67 mb -> 7.01mb.

I'd be happy seeing this released soon so I can let r8 remove the metadata again.

Considering it鈥檚 available on snapshots or can be built from source locally, I don鈥檛 see why your app has to eat that cost until the next release.

Using snapshots is impractical: non reproducible builds unless you go extra miles to find the snapshot with the timestamp you want.

Building locally also requires extra config for all people affected, while it could be done once for everyone.

Kotlin (the stdlin) is not using snapshots but dev versions on a dedicated repo, and I like it way better. Reproducible builds and easier discovery.

I use the same for my libraries, and that makes keeping track of updates easier as a bonus.

Keeping kotlin metadata isn't sufficient for R8 full mode. The problem only subsides if I disable full mode _and_ keep metadata.

Any plans on a 1.9.3 or 1.9.3-SNAPSHOT release now that AS 3.6.0 is stable?

I think you can try building with the r8 new version 2.0.39

buildscript {

    repositories {
        maven {
            url 'https://storage.googleapis.com/r8-releases/raw'
        }
    }

    dependencies {
        classpath 'com.android.tools:r8:2.0.39'          // Must be before the Gradle Plugin for Android.
        classpath 'com.android.tools.build:gradle:X.Y.Z' // Your current AGP version.
     }
}

https://issuetracker.google.com/u/1/issues/147972078#comment9

That worked for the R8 full-mode part of the problem. Thanks! TIL -printconfiguration

Hi, any plans to release 1.9.3 or 1.9.3-snapshot soon ?
-keep class kotlin.Metadata { *; } will be add 5mb in our case

Edited:
This fix from google worked for me:
https://issuetracker.google.com/issues/146534384
Using r8 1.6.75

AS 3.6.2 is out and fixes the problem!

https://androidstudio.googleblog.com/2020/03/android-studio-362-available.html:

Issue #146534384: NoSuchMethodException: parameter type is null moshi with R8

Was this page helpful?
0 / 5 - 0 ratings