ProGuard with AppBundle and data-binding: IllegalStateException - android

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

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.

Using Kotlin reflection sealedSubClasses property in Android release build

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.

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

Proguard doesnt obfuscate Class name, only methods are obfuscated

I try using Proguard in android studio, but seems like Proguard is not obfuscating the class name, for example, my app structure, and the config:
and config
but when i try trigger the exception in the app:
the exception is listed in ADB console:
only the methods are obfuscated, the MainActivity.class is not
This is an expected behaviour because the class is an activity!
All classes that are mentioned in AndroidManifest.xml have to keep their names (activities, services, providers, receivers, application, instrumentation). Otherwise the system won't be able to find them.
Gradle build automatically generates some rules for your ProGuard configuration to achieve this. It scans AndroidManifest.xml and adds rules for each class found there.
If you want to see all the rules that are used, add this line to your ProGuard rules:
-printconfiguration "build/outputs/mapping/configuration.txt"
It will create configuration.txt file containing all the rules.
There should be something like this:
# view AndroidManifest.xml #generated:50
-keep class com.github.browep.proguard.MainActivity {
<init>(...);
}
I was facing the same problems,
After updating my Android plugin for Gradle, Proguard stop obfuscating my utility and other class files.
After few searching, I found that Android studio gradle now uses newer version of Proguard.
And according to this stack-overflow answer, which stated that:
proguard automatically add rules specific for android/google package.
Therefore, After few rule changes in my app, Proguard obfuscated the class names again.
Old proguard-rules.pro:
#support-v4
##link https://stackoverflow.com/questions/18978706/obfuscate-android-support-v7-widget-gridlayout-issue
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
#support-v7
-dontwarn android.support.v7.**
-keep class android.support.v7.** { *; }
#https://stackoverflow.com/a/34895791/4754141
-keep class !android.support.v7.view.menu.**
-keep interface android.support.v7.* { *; }
#support design
##link https://stackoverflow.com/a/31028536
-dontwarn android.support.design.**
-keep class android.support.design.** { *; }
-keep interface android.support.design.** { *; }
-keep public class android.support.design.R$* { *; }
#error : Note: the configuration refers to the unknown class 'com.google.vending.licensing.ILicensingService'
#solution : #link https://stackoverflow.com/a/14463528
-dontnote com.google.vending.licensing.ILicensingService
-dontnote **ILicensingService
#updating to Gradle 2.14.1 caused error : https://stackoverflow.com/q/17141832/4754141
-keepattributes EnclosingMethod
#render script
##link https://stackoverflow.com/questions/22161832/renderscript-support-library-crashes-on-x86-devices
-keepclasseswithmembernames class * { native <methods>; }
-keep class android.support.v8.renderscript.** { *; }
New proguard-rules.pro:
#https://stackoverflow.com/a/41901653/4754141
#https://stackoverflow.com/a/23840049/4754141
-keep class android.support.** { *; }
-keep interface android.support.** { *; }

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