ClassNotFoundException when using SafeArgs with Proguard and Navigation Architecture Component?

Viewed 1383

I'm using SafeArgs plugin and Navigation Architecture Component but the app is crashing.

Caused by: java.lang.ClassNotFoundException: Didn't find class "com.package.domain.models.Model"

I'm using minifyEnabled true in the Gradle.

Also, I have three modules app, data, and domain. So, in all three proguard-rules files I have added

-keepnames class com.package.domain.models.Model1
-keepnames class com.package.domain.types.ImageType
-keepnames class com.package.domain.models.Model

I'm using these three classes as argType in my Navigation graph.

But the app is still crashing. Any help would be appreciated.

4 Answers

I think the more appropriate thing to do would be adding this to add these in my proguard-rules file(s).

-keepnames class * extends android.os.Parcelable
-keepnames class * extends java.io.Serializable

As I don't have to keep annotating my models with @Keep which are Parcelable or Serializable or keeping the whole model package away from obfuscation.

Check this for more information.

Try with putting @Keep annotation top of class. Like

import androidx.annotation.Keep

@Keep
data class RepoData(val id: Long, val name: String)

You can keep the whole model package (it's usually alright to do it actually) like

-keep class com.package.domain.models.** { *; }

Always use @keep annotation whenever you want the class to stay in memory .

@Keep data class MyData(val id: long,val name: String)

Related