FragmentContainerView with SupportMapFragment crashes when minify is enabled - android

I have the following layout:
<androidx.fragment.app.FragmentContainerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/map"
tools:context=".map.MapActivity"
android:name="com.google.android.gms.maps.SupportMapFragment" />
It works fine with my debug build, but my release build with minify enabled crashes.
09-04 15:14:06.727 15555 15555 E AndroidRuntime: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class androidx.fragment.app.FragmentContainerView
09-04 15:14:06.727 15555 15555 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class androidx.fragment.app.FragmentContainerView
09-04 15:14:06.727 15555 15555 E AndroidRuntime: Caused by: androidx.fragment.app.Fragment$c: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment: make sure class name exists
Any suggestions on how to fix it? If I replace the FragmentContainerView with the old fragment view, it works.

The problem might be related to minification. If you have minify enabled, you probably have to add something similar to this to your proguard:
-keep class com.google.android.gms.maps.** { *; }

This is caused by a bug in aapt2, and is being tracked in a bug by google here: https://issuetracker.google.com/issues/142601969
In the meantime, the easiest way to fix this is to add #Keep to any fragments that you use in this manner.
If it's a fragment from a library where you are unable to modify the code, you can add it as a rule in your proguard.cfg:
-keep class om.google.android.gms.maps.** { *; }

Related

android- navigation component Parcelable arguments not found on release variant

i have a strange Error, I'm developing an app and I'm using navigation component, i need to pass Custom Parcelable objects to some fragments using safeArgs, actually the app works fine on debug builds, unfortunately when tried to launch the app on release mode the app throws a
FATAL EXCEPTION.
Models are in a separated module, do i need to add a specific config in this module?
this is an example of the models
#Parcelize
#Serializable
data class Article(
val articleId: Int,
///
) : Parcelable
this is the stack Trace of exception:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: ..., PID: 11381
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MainActivity}: android.view.InflateException: Binary XML file line #101 in .....:layout/activity_main: Binary XML file line #101 in ...:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
at.... //
Caused by: android.view.InflateException: Binary XML file line #101 in ....:layout/activity_main: Binary XML file line #101 in ....:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: android.view.InflateException: Binary XML file line #101 in ....:layout/activity_main: Error inflating class androidx.fragment.app.FragmentContainerView
Caused by: java.lang.RuntimeException: Exception inflating ....:navigation/navigation line 168
//....
E/AndroidRuntime: at ...///
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: ......app_models.Article
at p4.o.a(NavType.java:181)
at androidx.navigation.g.e(NavInflater.java:191)
at androidx.navigation.g.g(NavInflater.java:155)
at androidx.navigation.g.b(NavInflater.java:128)
at androidx.navigation.g.b(NavInflater.java:141)
at androidx.navigation.g.c(NavInflater.java:88)
... 42 more
Caused by: java.lang.ClassNotFoundException: ....app_models.Article
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:454)
at java.lang.Class.forName(Class.java:379)
at p4.o.a(NavType.java:169)
... 47 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "....app_models.Article" on path: DexPathList[[zip file "/data/app/~~Frr3Sf0kFWBoEFseLYT-jQ==/....-BW1tOT8d2hUlYJyi1fggPw==/base.apk"],nativeLibraryDirectories=[/data/app/~~Frr3Sf0kFWBoEFseLYT-jQ==/...-BW1tOT8d2hUlYJyi1fggPw==/lib/x86, /system/lib, /system_ext/lib]]
Nb: i've replaced the package names with 3 dots
thank you
You need to add at least the following to your Proguard config:
-keepnames class * implements android.os.Parcelable
Although it can also make sense to make sure the CREATOR field is kept
-keepnames class * implements android.os.Parcelable
-keepclassmembers class * implements android.os.Parcelable {
public static final ** CREATOR;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}

Error inflating class androidx.appcompat.widget.AppCompatSpinner

We are seeing a crash Error inflating class androidx.appcompat.widget.AppCompatSpinner on the play console,but we are not able to reproduce it.
Here is my xml :
<androidx.appcompat.widget.AppCompatSpinner
android:id="#+id/spinnerState"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/xMargin_42_156"
android:layout_marginEnd="#dimen/xMargin_42_156"
android:background="#drawable/spinner_down_arrow"
android:focusableInTouchMode="true"
android:overlapAnchor="false"/>
In build.gradle
implementation 'androidx.appcompat:appcompat:1.2.0'
Logs:
Fatal Exception: java.lang.RuntimeException
Unable to start activity ComponentInfo{com.wrap/com.wrap.activities.NewIntroActivity}: android.view.InflateException: Binary XML file line #129 in com.wrap:layout/activity_new_intro: Binary XML file line #129 in com.wrap:layout/activity_new_intro: Error inflating class androidx.appcompat.widget.AppCompatSpinner

com.android.support:design with androidX (1.0.2)

I want to use TextInputLayout inside my project, but I'm using androidX library
implementation "androidx.appcompat:appcompat:1.0.2"
/*this don't work*/
implementation "com.android.support:design:28.0.0"
This is the exception which I'm facing
android.view.InflateException: Binary XML file line #30: Binary XML file line #30: Error inflating class android.support.design.widget.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #30: Error inflating class android.support.design.widget.TextInputLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.TextInputLayout" on path: DexPathList[[zip file "/data/app/com.example
Error inflating class android.support.design.widget.TextInputLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class
This error thrown when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath.
You should use
com.google.android.material.textfield.TextInputLayout
DEMO
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
Then Clean-Rebuild-Run.

Generating signed apk getting error on inflating BottomNavigationView after switching to androidX

App crashes with error error inflating class com.google.android.material.bottomnavigation.BottomNavigationView when generating signed apk. Running the app with debug apk from android studio directly to device has no problem. I have migrated my project to androidX as well.
I've read some other post saying the drawable or colors might have problem so I've confirm that there are no other drawable folders (e.g. drawable-v21, drawable-v24). Im not sure why but it only happens when I generate a signed apk.
app:itemIconTint="#drawable/drawable_selector"
app:itemTextColor="#drawable/drawable_selector"
app:itemBackground="#color/primary"
but it doesn't work
line 15 error starts here:
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_navigation"
android:elevation="8dp"
app:itemIconTint="#drawable/drawable_selector"
app:itemTextColor="#drawable/drawable_selector"
app:itemBackground="#color/primary"
android:background="?android:attr/windowBackground"/>
have already included this in my gradle:
implementation 'com.google.android.material:material:1.1.0-alpha06'
implementation 'androidx.appcompat:appcompat:1.1.0-alpha05'
Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException

What are the recommended proguard settings for android support library?

I am using android-support-library-v7 in my project and of course I would like to use progurad to minimize and obfuscate my code.
The problem is that if I use proguard I get errors similar to this:
FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.project/org.example.project.ActivityMain}: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.v7.preference.PreferenceCategory
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.v7.preference.PreferenceCategory
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.g.a(Unknown Source)
at android.support.v7.preference.h.a(Unknown Source)
at android.support.v7.preference.h.b(Unknown Source)
at org.exampple.project.ActivityMain.onCreate(Unknown Source)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
... 11 more Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
at java.lang.Class.getConstructorOrMethod(Class.java:460)
at java.lang.Class.getConstructor(Class.java:431)
... 22 more
PreferenceCategory is used in my preferences.xml file and I guess it is accessed via reflection. Proguard if probably removes this method (or class) if it is not referenced in code when shrinking. And even if not it surely obfuscates the name.
Google in itßs usual fashion provides absolutely no documentation on the subject.
So, what are the optimum settings to achieve good shrinking and good obfuscation.
There are some similar questions on Stack Overflow, but they boil down to:
-keep class android.support.v7.** { *; }
which cleary defeats the purpose of shrinking and obfuscating.
Trying to randomly guess the settings or by try and error method is extremely time consuming.
The Support Library uses the consumerProguardFiles feature to automatically include the appropriate ProGuard if you're using Gradle, meaning you don't need to manually include anything.
Looking at the ProGuard file for preferences-v7 (stored in the proguard.txt file within the AAR), it contains the following lines:
# Preference objects are inflated via reflection
-keep public class android.support.v7.preference.Preference {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keep public class * extends android.support.v7.preference.Preference {
public <init>(android.content.Context, android.util.AttributeSet);
}
Which covers the exact method that it says you are missing (as PreferenceCategory indirectly extends Preference). Check to make sure you are using the full Gradle dependency.
Check out this project on Github that has proguard rules for popular libraries.

Categories

Resources