Question

Some of my users are experiencing crashes, and this is the error that shows in the crash report on the Google Play Developer Console:

Unable to start activity ComponentInfo{com.havens1515.autorespond/com.havens1515.autorespond.SettingsMenuNew}: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.havens1515.autorespond.NotificationOptions: make sure class name exists, is public, and has an empty constructor that is public

The users say that this occurs when opening any settings menu inside of SettingsMenuNew wich is mentioned in the error above, but I don't experience the crash on my phone. SettingsMenuNew is a PreferenceActivity and all of the submenus are PreferenceFragment

Each PreferenceFragment has an empty constructor, and I don't know what else the issue could be. I also saw in someone else's question that it needs the newInstance method, but I don't think I really need that if I'm not putting any other arguments into the fragment.

here is some the code showing those methods:

public class NotificationOptions extends PreferenceFragment
{
    public NotificationOptions()
    {

    }

    public static NotificationOptions newInstance(int title, String message)
    {
        NotificationOptions f = new NotificationOptions();
        return f;
    }
    ...
}
Was it helpful?

Solution

This probably happens due to proguard removing your Fragments.

To reproduce, build the obfuscated APK, enable "Don't Keep Activities" in developer options, open the Activity wich contains the Fragment that crashes. Minimize on home button and restore app from recents.

To merge proguard configurations with default one and yours in ADT you should specify in project.properties

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

If you're using Gradle build system

buildTypes {
    debug {
        runProguard false
    }

    release {
        runProguard true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
    }
}

And proguard-project.txt should contain at least these rules

-keep public class * extends android.preference.PreferenceFragment

And if you're using support Fragments

-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.FragmentActivity

Don't forget that there are already some rules contained in ${sdk.dir}/tools/proguard/proguard-android.txt so add only missing based on your needs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top