Apk Size Reduction - take appcompat / lottie as example

Download size in KB

ApkSize

dex

resources.arsc

res

empty app

64.3

0.35

0.53

62.3

import AppCompat

435.7

167.8

49.7

222.8

dummy_layout

228.3

0.65

48.9

198.6

aapt - remove res

113.4

0.65

48.9

63

import lottie

200.8

84.1

49.1

63.2

resguard

196.5

84.1

40.9

63.2

resguard[mod]

189.7

84.1

37.3

63.2

remove string

173.2

84.1

18.4

63.2

Remove AppCompat Styles

149.2

73.5

5.7

62.4

Remove XML entity

144.9

72.5

2.2

62.4

Empty App

proguardFiles

  1. proguard-android-optimize.txt
  2. appt-rules.txt

Screenshot from 2017-03-24 11:10:21.png

aapt_rules.txt

Screenshot from 2017-03-24 11:10:32.png

mapping.txt

import AppComapt-v7

mapping.txt

dummy_layout

aapt_rules.txt

Screenshot from 2017-03-24 11:10:32.png

mapping.txt

aapt - remove res

import lottie

resguard

resguard [mod] - clean entity

http://www.jianshu.com/p/60ce4bf20f72

http://blog.csdn.net/luoshengyang/article/details/8744683

com.tencent.mm.androlib.ApkDecoder#decode

Remove Strings

Remove AppCompat - Style/Layout/Drawable reffed by values.xml

Reference

gradle tasks

Gradle Tasks

Comments

:app:generateReleaseResValues

:app:mergeReleaseResources

:app:processReleaseManifest

doFirst

replace files (layout/ …)

modify values.xml to remove styles

:app:processReleaseResources

intermediates: (most of intermediates files are created at this point)

output:

aapt_rules.txt

resources-release.ap_

doLast

remove PNGs from resources-release.ap_

:app:transformClassesWithDexForRelease

:app:transformClassesWithShrinkResForRelease

:app:packageRelease

:app:assembleRelease

output: app-release.apk

:app:resguardRelease

output: AndResGuard_app-release/app-release_signed_aligned.apk

rename res/*

remove entity R.drawable.XXX

Findings

styles -- ref --> layout -- affect -->  proguard (aapt_rules.txt)

                   --> drawable -- affect -->  shrink resource

AppCompat

values.xml

Screenshot from 2017-03-23 11:50:07.png

Screenshot from 2017-03-23 11:48:19.png

NOT WORKING (in this case)

https://developer.android.com/studio/build/shrink-code.html#keep-resources

res/raw/keep.xml

<?xml version="1.0" encoding="utf-8"?>

<resources xmlns:tools="http://schemas.android.com/tools"

    tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*"

    tools:discard="@layout/unused2" />

Remove files directly will cause runtime exceptions

aapt commands

Starting process 'command '/home/wychi/Android/Sdk/build-tools/25.0.2/aapt''. Working directory: /home/wychi/repository/AppCompatMin/app Command: /home/wychi/Android/Sdk/build-tools/25.0.2/aapt package -f --no-crunch -I /home/wychi/Android/Sdk/platforms/android-25/android.jar -M /home/wychi/repository/AppCompatMin/app/build/intermediates/manifests/full/release/AndroidManifest.xml -S /home/wychi/repository/AppCompatMin/app/build/intermediates/res/merged/release -m -J /home/wychi/repository/AppCompatMin/app/build/generated/source/r/release -F /home/wychi/repository/AppCompatMin/app/build/intermediates/res/resources-release.ap_ -G /home/wychi/repository/AppCompatMin/app/build/intermediates/proguard-rules/release/aapt_rules.txt --custom-package cms.appcompatmin -0 apk --output-text-symbols /home/wychi/repository/AppCompatMin/app/build/intermediates/symbols/release --no-version-vectors

Successfully started process 'command '/home/wychi/Android/Sdk/build-tools/25.0.2/aapt''

ref

Android gradle plugin

http://google.github.io/android-gradle-dsl/javadoc/

https://github.com/google/android-gradle-dsl

https://android.googlesource.com/platform/tools/build/+/master/gradle/src/main/groovy/com/android/build/gradle/tasks/

git clone https://android.googlesource.com/platform/tools/build

https://android.googlesource.com/platform/dalvik/+/a9ac3a9d1f8de71bcdc39d1f4827c04a952a0c29/dx/src/com/android/dx/command/dexer/Main.java?autodive=0

Proguard

https://www.guardsquare.com/en/proguard/manual/usage#keepoptionmodifiers

If you specify a class, without class members, ProGuard only preserves the class and its parameterless constructor as entry points. It may still remove, optimize, or obfuscate its other class members.

If you specify a method, ProGuard only preserves the method as an entry point. Its code may still be optimized and adapted.

/home/wychi/repository/cmsecurity/build/intermediates/proguard-files/proguard-android-optimize.txt-2.3.0

# Keep setters in Views so that animations can still work.

-keepclassmembers public class !android.support.v7.** extends android.view.View {

   void set*(***);

   *** get*();

}