How to properly use Resource Shrinking to reduce APK size? - android

My main goal is to remove unused assets and libraries from the apk so that I can reduce it's size. I found Resource Shrinking while I was browsing through this SO post.
So I changed my gradle file to this:
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId 'com.galleri5.android'
multiDexEnabled true
minSdkVersion 16
targetSdkVersion 23
versionCode 12
versionName "0.10"
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
And to check whether this was working, I generated the apk and the size of the apk was same as before, no shrinking. So I tried to see if/which resources are being removed by running this command:
./gradlew clean assembleDebug --info | grep "Skipped unused resource"
And this is the output that I am getting:
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
I don't see and resources that are getting removed. Is there something I have done wrong or am I totally misunderstanding this tool? Any help would be highly appreciated. Thanks.
Edit I am just checking the size of the apk(apk-debug.apk) generated after running the app with above configuration. Does shrinking happen when I generate signed apk or would it be visible in the normal apk too?

Resource shrinking will only occur in your release builds, as that's what you've specified in your build.gradle. You could add the following closure to buildTypes if you want to minify in your debug builds, generally this isn't done.
buildTypes {
debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

Related

Why is release build throwing error, but not debug when buildTypes are identical? (Android gradle build types)

I'm trying to figure out why I can't get a release build to install correctly using Android Studio. This is my buildTypes block:
buildTypes {
debug {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
release {
shrinkResources false
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
and these are my product flavors:
flavorDimensions "version"
productFlavors {
free {
applicationId "com.example.app.free"
dimension "version"
signingConfig signingConfigs.config
}
paid {
applicationId "com.example.app.paid"
dimension "version"
signingConfig signingConfigs.config
}
}
When I install the debug paid or free versions, all is fine and dandy. Nothing wrong at all. When I try to install the release versions (paid or free) I'm getting:
'Execution failed for task ':app:transformDexArchiveWithDexMergerForFreeRelease'. com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ... Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes. Program type already present: com.google.android.youtube.player.YouTubeApiServiceUtil'
My questions is this: Why is the duplicate class exception happening only during release, when I have nothing different defined between the release and debug buildTypes?
EDIT I've solved the issue by removing the YouTubePlayer Library dependency in gradle (b/c apparently my implementation of the google YT service was causing an internal library to be created, so depending on the imported one was redundant?). This still leaves my question valid. Why did the debug work, but not the release when nothing was declared differently?
I had the same issue when linking with another module when I added another (indirect) dependency on its assemble (it was Protobuf module which needs to generate Java files from .proto files before the app module can generate its JSON model). It seems that assembleRelease optimises the code in a way that it may generate slightly different class file depending on where it is called from and the Dex merger then cannot decide which of the class files to use. assembleDebug generates always the same (unoptimised) code which it can merge.
(For anyone having the same problem with Protobuf, the solution for that is to depend on :protobuf:GenerateProto instead of :protobuf:assemble.)

Android Espresso multidex fail

We use multidex in our app for a long time but recently with latest update it fails on android API <19
e.g. emulator with api 16
It is standard java.lang.NoClassDefFoundError.
If I define multidexKeepProguard for missing class e.g.
java.lang.NoClassDefFoundError. rx.plugins.RxJavaHooks exception
-keep class rx.plugins.**{*;}
then it will just fail in a different place with the same reason NoClassDefFound
Here is the runner, app and manifest setup:
https://gist.github.com/originx/1890599b57b0ee3e14a85a4732301cd9
Logcat:
https://gist.github.com/originx/887f80d405334f1903b3024eb5cd1024
Build enviroment setup:
Android Studio 2.2.2
Build #AI-145.3360264, built on October 18, 2016
JRE: 1.8.0_112-release-b05 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Compile options
compile 'com.android.support:multidex:1.0.1'
build tools info:
classpath 'com.android.tools.build:gradle:2.2.2'
compileSdkVersion 25
buildToolsVersion '25'
defaultConfig {
applicationId "app.packagename.com"
minSdkVersion 16
targetSdkVersion 25
testInstrumentationRunner "de.payback.app.CustomAndroidJUnitRunner"
multiDexEnabled true
}
dexOptions {
jumboMode true
preDexLibraries false
javaMaxHeapSize "4g"
maxProcessCount = 8
}
debug {
applicationIdSuffix '.debug'
versionNameSuffix '-debug'
signingConfig signingConfigs.debug
minifyEnabled false
shrinkResources debugShrinkResourcesEnabled
proguardFiles getDefaultProguardFile('proguard-android.txt'), '../proguardRules/proguard-rules.pro', '../proguardRules/proguard-debug-rules.pro'
// multiDexKeepProguard file('../proguardRules/multidex-proguard.pro')
testProguardFiles getDefaultProguardFile('proguard-android.txt'), '../proguardRules/proguard-rules.pro', '../proguardRules/proguard-debug-test-rules.pro'
testCoverageEnabled false
}
release {
minifyEnabled true
shrinkResources true
testProguardFiles getDefaultProguardFile('proguard-android.txt'), '../proguardRules/proguard-rules.pro'
proguardFiles getDefaultProguardFile('proguard-android.txt'), '../proguardRules/proguard-rules.pro'
// multiDexKeepProguard file('../proguardRules/multidex-proguard.pro')
}
I tried everything from extending MultiDexApplication, to custom MultiDex.install(context) to using MultiDexRunner
same results always
if using multidexkeepproguard file for classes which are usually not found then they are in main dex file but of course something else is missing which indicates that multidex was not properly installed and initialized
Google bug report:
https://code.google.com/p/android/issues/detail?id=228449
repo to reproduce the issue can be found here:
https://github.com/originx/multidex/tree/master
To run please disable instant run
To reproduce multidex issue please run following command
./gradlew clean connectedPayGermanyCompatDebugAndroidTest
run on any device or API 16 emulator Tests on GTI8190 4.1.2 failed Instrumentation run failed due to java.lang.NoClassDefFoundError
Any suggestions how to work around this until I get more info from the Google team?
Explanation by Google dev:
The issue is that the rx.plugins.RxJavaHooks class referenced from the
CustomJunitRunner.onCreate() method is in the secondary dex file of
the main app, and you are accessing it before the class loaders get
fully patched.
When the main application and test code share a dependency, we will
remove it from the test's dependencies (as we expect it to be
available in the main application). However, with legacy multidex,
this is causing problems.
Currently, there are 2 workarounds:
Option 1 Ensure the rx.plugins.RxJavaHooks is in the main dex by
creating a file multidexKeepProguard.pro and adding "-keep class
rx.plugins.**"
Option 2 Remove references to RxJavaHooks from onCreate(), and move
them to onStart() (not sure if this accomplishes when you want
though):
#Override
public void onStart() {
super.onStart();
//hook up schedulers to rxjava so espresso idling resouces can fetch it properly
RxJavaHooks.setOnComputationScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR));
RxJavaHooks.setOnIOScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR));
RxJavaHooks.setOnNewThreadScheduler(current -> Schedulers.from(AsyncTask.THREAD_POOL_EXECUTOR));
}
Solution
Workaround
So current workaround would be either use
multidexKeepProguard.pro file and in your debug config point to that file:
debug {
applicationIdSuffix '.debug'
multiDexKeepProguard file('../proguardRules/multidex-proguard.pro')
}
Your multidex proguard file should contain classes which are not being found in the main dex file, in my case it was RxJavaPlugin, so my multidexproguard file contains:
-keep class rx.** { *; }

android release apk bigger than debug apk

I'm using proguard to reduce my apk size. The debug apk reduce from 90mb to 55mb, but the signed apk is 71mb. Here is my build.gradle code:
apply plugin: 'com.android.application'
android {
signingConfigs {
XXXX {
keyAlias 'xxxx'
keyPassword 'xxxx'
storeFile file('/Users/xxxx.jks')
storePassword 'xxxxxx'
}
}
compileSdkVersion 23
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.xxxx"
minSdkVersion 14
targetSdkVersion 22
versionCode 61
versionName "4.1.8.1"
multiDexEnabled true
signingConfig signingConfigs.XXXX
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.XXXX
}
debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.XXXX
}
}
productFlavors {
}
dexOptions {
javaMaxHeapSize "4g"
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
}
and
repositories {
mavenLocal()
maven {
name "jcenter"
url "http://jcenter.bintray.com/"
}
}
dependencies {
...
}
Further explaining sosite's answer, it seems that this happens only if comparing a debug apk built via Run or Debug meant for a specific device (even without Instant Run enabled) instead of a debugapk built via Build > Build APK (for any supported device).
Any variant (even debug itself) built via Build APK will include all the resources for that variant. Also, the Run/Debug apk includes pre-dexed classes specific for that single device, while Build APK ones includes only some general pre-dexed classes that the compiler determines safe for all supported devices - the full dexing only occurs in the device itself, when the apk is installed.
I've zipdiff-ed an apk generated via Debug with another via Build APK for the same variant of the same project and published the simplified output for demonstration (also available as html).
When you build your app locally for specific type of phone then Android Studio attach only necessary resource files. When you build release version then you have attached all types of drawables so you app file size can increase drastically.
I suggest you to use jpg in place of png in as many places as you can and compress them of course - often I use tinyPNG website or just Photoshop ;)

Android Gradle 2.2 Not Allowing ShrinkResources for Signed APK

As part of the Android Studio 2.2 roll out I updated my Gradle Build tools to v2.2. After doing that my signed APK build process fails because I have shrinkResources = true.
Once I switch back to Gradle v2.1.3 OR set shrinkResources = false everything works fine. Here's my app gradle build file:
android {
signingConfigs {
}
compileSdkVersion 24
buildToolsVersion '24.0.0'
defaultConfig {
applicationId "com.sample.testapp"
minSdkVersion 21
targetSdkVersion 24
versionCode 4
versionName "0.0.4"
}
buildTypes {
release {
minifyEnabled false
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
proguardFile 'C:/Users/code/testapp/app/proguard-rules.pro'
}
}
productFlavors {
}
}
With Gradle set to v2.2 here's the build error I get when generating a signed APK
Does anyone know why this is occurring and if there's a work around? I've Googled around a bit and have seen some older Android Bug reports about alpha and beta Gradle builds having this issue, but the reports I found were >6 months old (and for previous Gradle versions).
P.S. I know that minifyEnabled = false currently, i have yet to investigate the correct set of proguard rules for my included libraries to prevent the Signed Build from erroring out due to missing libs.
To use shrinkResources you have to use minifyEnabled
As per Android documentation:
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).
use
minifyEnabled false
shrinkResources false
or
minifyEnabled true
shrinkResources true
may be a bug on android gradle plugin
wait google fix bugs

Avoid shrinking/removing wearable micro app from apk file

I activated the resouce shrinking in my build.gradle but now my embeded wearable app is stripped out. How can I avoid that my micro app is removed, because it is unused?
Skipped unused resource res/raw/android_wear_micro_apk.apk: 382310 bytes
Since I want to shrink the other not used resouces I'm using this DSL:
buildTypes {
release {
shrinkResources true
// ...
}
}
I would guess that I need to use proguard but I have no idea how to achieve that. I checked of cause the documentation, but I didn't get it how protect a single member variable.
Are you referencing the R.raw.apkpath? Looking at the Packaging Wearable Apps training mentions rawPathResId in the res/xml/wearable_app_desc.xml
On a side note enabling proGuard is simple with Gradle
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
This is Bug 78620 and was fixed in the gradle build tools 0.14.1.

Categories

Resources