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

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)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 19 (6 by maintainers)

Most upvoted comments

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

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

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.

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 { *; }