java.lang.NoSuchMethodException for onCreate

后端 未结 4 1010
自闭症患者
自闭症患者 2020-12-31 09:38

I see crashes in the Google Play crash log that is really stumping me.

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (Ac         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-31 10:37

    I'm having the same issue. The other answers did not help.

    For me, it looks like it is Proguard. That explains why it only happens in production/release builds and why I have been unable to reproduce it when debugging.

    If you're having OP's issue, try the following:

    1. Build the obfuscated .apk. I used the signed one, that I publish to the app stores...
    2. Enable "Don't Keep Activities" in your device's developer options.
    3. Install the .apk in your device and open the Activity that crashes and contains the Fragment.
    4. Leave your app (Minimize / Home button / ...) and re-open it from the recent apps menu.

    Does it crash? Then try it with the un-obfuscated debug build. If it doesn't then it's probably Proguard.

    To fix it I did the following:

    1. Create a proguard-rules.pro file in your app module's root folder.
    2. Add -keep class * extends androidx.fragment.app.Fragment{} to that file.
    3. Then, in the app's build.gradle,

    add:

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    

    See Yaroslav Mytkalyk's answer here: Fragment Instantiation crash, which helped me solve this, although it's a bit old and outdated by now (e.g. "runProguard true" is obsolete).

    At least now it doesn't crash when I do the steps above.

    PS: I did this in conjunction with adding the 0-arg constructors to my fragments, as mentioned in other answers, since that was my first fixing attempt. I believe that Proguard alone was the issue and that it isn't necessary to add said constructors, but I cannot test that hypothesis now.

提交回复
热议问题