29

I am preparing to release an App to production. So, I generated signed apk. After generating signed apk, I was getting a problem. My apk file size is a little large and I tried ways to shrink the apk size. I already tried

app --> Refactor --> Remove Unused Resources

and it is not too reduce. So, I added shrinkResources true in my build.gradle(app)

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

After adding shrinkResources true and I got below error when I rebuild. My question is how should I turn on unused Code shrinking first? Thanks and appreciating.

enter image description here

ZarNi Myo Sett Win
  • 1,353
  • 5
  • 15
  • 37

10 Answers10

46

Resource shrinking works only in conjunction with code shrinking. After the code shrinker removes all unused code, the resource shrinker can identify which resources the app still uses. This is especially true when you add code libraries that include resources—you must remove unused library code so the library resources become unreferenced and, thus, removable by the resource shrinker

To enable resource shrinking, set the shrinkResources property to true in your build.gradle file (alongside minifyEnabled for code shrinking). For example:

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

reference

Mostafa Anter
  • 3,445
  • 24
  • 26
  • 2
    I've got `minifyEnabled true` but still getting the error – Punter Bad Mar 12 '20 at 08:16
  • @Muganwas same here, I'm also facing same problem, how you resolved it. – Vasanth May 02 '20 at 07:42
  • @Vasanth I don't think I did, I must have just given up. – Punter Bad May 02 '20 at 12:44
  • For me, it was the order of the codes, changing it to ``` release { // Caution! In production, you need to generate your own keystore file. // see https://reactnative.dev/docs/signed-apk-android. signingConfig signingConfigs.debug minifyEnabled enableProguardInReleaseBuilds minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro" }``` cleared all errors – Limbo Jun 26 '21 at 08:54
  • 1
    minifyEnabled has to be true if you are willing to apply shrinkResources true – Rohit Patil Nov 18 '21 at 05:40
9

Maybe you set by mistake minifyEnabled = false and shrinkResources = true in your buildTypes.debug, so, maybe, it is a root of a problem, not your buildTypes.release

StayCool
  • 421
  • 1
  • 9
  • 23
5
android {
    buildTypes {
        release {
            minifyEnabled true
            shrinkResources true
        }
    }
}
Ranajit Sawant
  • 181
  • 1
  • 6
4

You might want to refer to the Android Documentation to shrink your code and resources:

Shrink your code and resources

Like a comment already pointed out, resource shrinking only works when you have used the code shrinker. To enable shrinkResources in your build.gradle file you must have first set minifyEnabled to true

MasterQueue
  • 101
  • 9
2

Simple just open the build.gradle file on App level i.e. android/app/build.gradle and implement this:

 release {
        // TODO: Add your own signing config for the release build.
        // Signing with the debug keys for now, so `flutter run --release` works.
        signingConfig signingConfigs.debug
        useProguard true
        minifyEnabled false
        shrinkResources false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 

    }
HIMANSHU MISHRA
  • 149
  • 1
  • 18
1

In order to use resource shrinking, you also need to enable code shrinking as they both works in conjunction.
So to do so, set shrinkResources true along with minifyEnabled true.
You can follow the official site for the same.

priyanka.b
  • 91
  • 1
  • 2
1

make sure to add it into proper part of gradle

   signingConfigs {
    buildTypes {
        debug {
            buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"
        }
        release {
            buildConfigField "java.util.Date", "buildTime", "new java.util.Date(" + System.currentTimeMillis() + "L)"
        }
    }
}


buildTypes {
    release {
        minifyEnabled false
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}
Josef Vancura
  • 1,053
  • 10
  • 18
0

If you have added shrinkResources true make sure it comes after minifyEnabled true the order matters so https://stackoverflow.com/a/56426634/10355668 is correct thanks

Chinedu Ofor
  • 707
  • 9
  • 11
0

set minifyEnabled to true in both debug and release

Anis dev
  • 1
  • 1
0

Check these variations if this error occurs:

  1. shrinkResources = true works only with minifyEnabled = true
  2. There is minifyEnabled = true in your BuildType of apk (often minifyEnabled turned on only for release, if you will try to build apk for debug it error occurs )
  3. Try to check is there extra minifyEnabled = false that could override your minifyEnabled = true

Then try to Sync project with gradle files and rebuild project

Dmitriy
  • 346
  • 4
  • 8