sudokit
sudokit

Reputation: 3

Android minifyEnabled option

Recently I have noticed the difference in android application behaviour as minifyEnabled option value changes. Assuming that by default minifyEnabled option is set to false.

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId ""
        minSdkVersion 24
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
        }
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

The configuration above is the default configuration of my android project. At the time as the application is compiled everything is working fine. However when the value of minifyEnabled is set to true, the application throws different exceptions and is not working properly. The one of the errors is:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at b.c.a.a.d.i.a(:94)
        at b.c.a.a.d.b.a(Unknown Source:10)
        at b.c.a.a.d.j.onPostExecute(:36)
        at android.os.AsyncTask.finish(AsyncTask.java:696)
        at android.os.AsyncTask.access$600(AsyncTask.java:180)
        at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:713)
        at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

What is more, I cannot notice the source of the exception. (Unknown Source:10)

When minifyEnabled is false, in similar situation there is no exception. Could anyone explain why changing the value causes the application to not work properly.

It does not matter whether the option is changed within the debug or release version

Upvotes: 0

Views: 1972

Answers (1)

Pawel
Pawel

Reputation: 17268

minifyEnabled activates ProGuard or R8 (depending on Gradle plugin version) which attempts to trim unused code and obfuscate (rename) classes and methods. That's why you're seeing at b.c.a.a.d.i.a(:94) in your error stack.

In most cases this does not affect how your app works but there are some instances (usually when using reflection) where it causes errors. In those cases you have to manually add exceptions to the proguard-rules.pro files.

Read in detail on official page: Shrink, obfuscate, and optimize your app

Upvotes: 1

Related Questions