R8 removes GeneralFunctions initialization in release build uploaded to Google Play Console (works in Android Studio & Bundle tool)


I'm maintaining a legacy Android taxi application (Java) that was originally built from an old third-party template. The app crashes immediately on launch, only when the signed release AAB is uploaded to Google Play Console.

The Problem

In LauncherActivity.onCreate(), I initialize a utility class like this:

Java

generalFunc = new GeneralFunctions(this);

However, in the release build published through Google Play, generalFunc is null when code in ParentActivity.onCreate() tries to use it, causing the following crash:

Java

java.lang.NullPointerException: Attempt to invoke virtual method 
'java.lang.String com.general.files.GeneralFunctions.retrieveValue(java.lang.String)' 
on a null object reference

at com.activity.ParentActivity.onCreate(SourceFile:16)
at com.cabo.passengers.LauncherActivity.onCreate(LauncherActivity.java:68)

Important observations:

  • The app works perfectly in Android Studio debug builds.

  • The app also works when I generate a signed release APK/AAB locally using Android Studio's Build → Generate Signed Bundle/APK.

  • The crash only happens with the AAB that was uploaded to Google Play Console.

This strongly suggests that R8 is removing or optimizing away the generalFunc initialization during the Play Console build process.

What I have already tried:

  • Added multiple -keep rules for GeneralFunctions and the field in LauncherActivity

  • Set android.enableR8.fullMode=false in gradle.properties

  • Used -dontshrink, -dontoptimize, and -dontobfuscate

  • Wrapped the initialization in if (generalFunc == null) checks and added immediate usage after creation

  • Tried reflection-based initialization as fallback

  • Cleaned and rebuilt multiple times

None of these solutions have resolved the issue in the Google Play Console build.

Relevant code:

LauncherActivity.java:

Java

public class LauncherActivity extends ParentActivity {

    private GeneralFunctions generalFunc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        generalFunc = new GeneralFunctions(this);   // ← This line gets removed by R8 in Play build
        super.onCreate(savedInstanceState);
        ...
    }
}

build.gradle.kts (Module :app):

Kotlin

release {
    isMinifyEnabled = true
    isShrinkResources = true
    proguardFiles(
        getDefaultProguardFile("proguard-android-optimize.txt"),
        "proguard-rules.pro"
    )
}

Question:

Why does R8 remove the GeneralFunctions initialization only in the build processed by Google Play Console, while local signed builds work fine?

Is there any difference in how R8 runs during local builds versus when Google processes the AAB? What additional keep rules or configurations should I use to prevent this?

Any help would be greatly appreciated!

0
Jul 3 at 1:02 PM
User AvatarSam
#java#android#kotlin

No answer found for this question yet.