Using Kotlin reflection sealedSubClasses property in Android release build - android

When I am trying to use the sealedSubClasses attribute of a reified class in Kotlin, it only works in my debug and not in my release build. I guess this is a problem with ProGuard, but I don't know how to fix this. I already tried keeping all classes in the module where the sealed class is, but I am having no luck with this. The sealedSubClasses property always returns an empty list.

Found two ways to fix this issue:
Add rule to your proguard-rules.pro file:
-keep class com.example.ClassName { *; }
-keep class com.example.ClassName$* { *; }
Use #Keep annotation:
#Keep
sealed class ClassName{
#Keep
object A : ClassName()
#Keep
object B : ClassName()
}
This one didn't work for me:
-keep class kotlin.Metadata { *; }
Also there is a bug https://issuetracker.google.com/issues/169264693 that could be the part of your problem.

You can try
-keep class kotlin.Metadata { *; }
to get missing attributes from your class

Add an additional rule to your proguard file:
-keepnames class com.fully.qualified.ClassName* {
*;
}
Notice the asterisk character.

Related

Found two getters or fields with conflicting case sensitivity for property: c [duplicate]

When creating a APK with proguard enabled, the following exception is thrown when using the FirebaseRecyclerAdapter from the Firebase-UI library (com.firebaseui:firebase-ui:0.3.0):
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
at com.firebase.ui.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:168)
The debug version (without proguard) works fine. Who has a working proguard config for Firebase-UI?
My current proguard config looks like this (only the Firebase related parts):
-optimizationpasses 5
-keepattributes SourceFile,LineNumberTable,Exceptions, Signature, InnerClasses,*Annotation*
-keepnames class ** { *; }
-keep class com.firebase.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
Solved this by moving the ViewHolder classes that are used by the FirebaseRecyclerAdapter to a dedicated package (e.g. com.mypackage.myapp.viewholders) and adding a rule within the proguard configuration to prevent that classes within this package become obfuscated by proguard:
-keep class com.mypackage.myapp.viewholders.** { *; }
Well, I had my ViewHolder inside relative FirebaseRecyclerAdapter as an inner class and gave me this error. Making the inner class has solved the problem.
Also https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575 states same thing with an addition.
Inner class ViewHolder must be public and static so that it could be initiated via reflection.

ProGuard with AppBundle and data-binding: IllegalStateException

I have a multi-module project (I use Android App Bundles) with data-binding, ViewModel. But when I enable Proguard the app crashes with the following error :
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MyActivity}: java.lang.IllegalStateException: DataBindingUtil.setConte… R.layout.my_activity) must not be null
Caused by: java.lang.IllegalStateException: DataBindingUtil.setConte… R.layout.my_activity) must not be null
at MyActivity.onCreate(MyActivity.kt:38)
When I remove modules and create one app module, everything works. When I disable Proguard, it also works fine.
Here is part of my proguard-rules.pro:
-dontwarn android.databinding.**
-keep class android.databinding.** { *; }
-keep class com.example.module1.databinding.** { *; }
-keep class com.example.module2.databinding.** { *; }
In build.gradle:
minifyEnabled true
useProguard true
So the error points to this line of code:
val binding: com.example.module1.databinding.MyActivityBinding =
DataBindingUtil.setContentView(this, R.layout.my_activity)
Maybe any ideas how to fix?
Only need to add this to your proguard-rules.pro, where module1 and module2 are Dynamic Feature Modules with databinding enabled.
-keep class com.example.module1.DataBinderMapperImpl { *; }
-keep class com.example.module2.DataBinderMapperImpl { *; }
I don't really know why but for me, the following combination worked:
-dontwarn androidx.databinding.**
-keep class androidx.databinding.** { *; }
-keep class * extends androidx.databinding.DataBinderMapper
I tried using just -keep class * extends androidx.databinding.DataBinderMapper but it didn't work.
Also I tried using
-dontwarn androidx.databinding.**
-keep class androidx.databinding.** { *; }
but that didn't work either.
What worked was a combination of the three.
if you enable to view merged proguard file using
-printconfiguration proguard-merged-config.txt
You will see something like this is there to keep DataBinderMapperImp
# instant apps load these via reflection so we need to keep them.
-keep public class * extends android.databinding.DataBinderMapper
I think what is missung is androidx version
I added this line it working fine now
-keep class * extends androidx.databinding.DataBinderMapper { *; }
This worked for me
-keep class * extends androidx.databinding.DataBinderMapper { *; }

Proguard error for missing inner class

I am configuring proguard for my project, but getting errors for anonymous classes like:
Warning:mypackage.editor.EditorCard$createView$1$1$2$2: can't find referenced class mypackage.editor.EditorCard$createView$1$1$2
I tried several methods like keeping everything in class, but nothing works.
-keep class mypackage.editor.EditorCard.** { *; }
EditorCard extends AnkoComponent.
How can I fix the error?
Managed to solve it by
removing empty lambda blocks in the given class
changing signing version to v1 (Jar signature)
adding rule
-keepclasseswithmembers class mypackage.editor.EditorCard { *; }
-keep class * extends org.jetbrains.anko.AnkoComponent
You can add this line to your proguard-rules.pro
-keep com.alkymia.** { *; }

App using SimpleXML with minifyEnabled does not work

I have an app that works fine when debugging, but when I make a release version, with minifyEnabled true in the build.gradle file, it seems not to work at all anymore:
D/SapphirePocket( 6520): Could not serialize telegram: <init> [interface a.a.a.b.am, interface a.a.a.a, class a.a.a.e.n]
Does SimpleXML work with minified APKs, where inspection might not completely work anymore or should this just work?
When you activate minifyEnabled, you are obfuscating the code with proguard. SimpleXML should use some classes that you are obfuscating and you must not do this. You must keep the names of the classes that SimpleXML needs.
See this post about the same problem as you have (simplexml failed to compile with proguard activated).
The first thing I recommend is put this on proguard file (extracted from the post I've linked):
-keep public class org.simpleframework.** { *; }
-keep class org.simpleframework.xml.** { *; }
-keep class org.simpleframework.xml.core.** { *; }
-keep class org.simpleframework.xml.util.** { *; }
-keepattributes ElementList, Root
-keepclassmembers class * {
#org.simpleframework.xml.* *;
}
For a more detailed response, please, show us more information (proguard file, code getting the error...).

Proguard configuration for Firebase-UI library

When creating a APK with proguard enabled, the following exception is thrown when using the FirebaseRecyclerAdapter from the Firebase-UI library (com.firebaseui:firebase-ui:0.3.0):
java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
at com.firebase.ui.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:168)
The debug version (without proguard) works fine. Who has a working proguard config for Firebase-UI?
My current proguard config looks like this (only the Firebase related parts):
-optimizationpasses 5
-keepattributes SourceFile,LineNumberTable,Exceptions, Signature, InnerClasses,*Annotation*
-keepnames class ** { *; }
-keep class com.firebase.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
Solved this by moving the ViewHolder classes that are used by the FirebaseRecyclerAdapter to a dedicated package (e.g. com.mypackage.myapp.viewholders) and adding a rule within the proguard configuration to prevent that classes within this package become obfuscated by proguard:
-keep class com.mypackage.myapp.viewholders.** { *; }
Well, I had my ViewHolder inside relative FirebaseRecyclerAdapter as an inner class and gave me this error. Making the inner class has solved the problem.
Also https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575 states same thing with an addition.
Inner class ViewHolder must be public and static so that it could be initiated via reflection.

Categories

Resources