Image for post
Image for post

Build your Android app Faster and Smaller than ever

Jirawatee
Jirawatee
Dec 6, 2018 · 11 min read
Image for post
Image for post
Image for post
Image for post
Developer Build Clinic by Android Studio team
Image for post
Image for post
https://lineman.line.me/

Tips for building app Smaller

Image for post
Image for post
Current app size is 9MB
Image for post
Image for post
APK Analyzer

Tip 1: Remove unused resources

Image for post
Image for post

Tip 2: Add just Dependencies Needed

$ ./gradlew app:dependencies
Image for post
Image for post

Tip 3: Build Multiple APKs for Screen Densities

android {
splits {
density {
enable true
// Specify a list of screen densities which Gradle won't create multiple APKs for
exclude 'ldpi', 'mdpi'
// Specify a list of compatible screen size for the manifest
compatibleScreens 'small', 'normal', 'large', 'xlarge'
}
}
}

Tip 4: Build Multiple APKs for ABIs

android {
splits {
abi {
enable true
reset() // Specify a list of ABIs that Gradle should create APKs for
include 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a'
// If you don’t want to generate a universal APK that includes all ABIs.
universalApk false
}
}
}

Tip 5: Build an APK with Specific ABIs

android {
defaultConfig {
...
ndk {
abiFilters 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a'
// armeabi, mips and mips64 has removed since NDK r17
}
}
}

Tip 6: Remove Unused Alternative Resources

android {
defaultConfig {
resConfigs 'en', 'th'
...
}
}

Tip 7: Shrink Unused Code and Resources

android {
buildTypes {
release {
minifyEnabled true

shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

Tip 8: Use Shape Drawable

Image for post
Image for post

Tip 9: Use WebP

Image for post
Image for post
Image for post
Image for post

Tip 10: Use VectorDrawable

Image for post
Image for post

Cumulative Improvement

Image for post
Image for post

One more thing

Image for post
Image for post

Techniques for faster app builds

Image for post
Image for post

Technique 1: Use latest Android plugin

buildscript {
repositories {
jcenter()
google()
}
dependencies {
- classpath 'com.android.tools.build:gradle:3.0.0'
+ classpath 'com.android.tools.build:gradle:3.2.1'
}
...
}

Technique 2: Avoid Legacy Multidex

productFlavors {
development {
minSdkVersion 21
...
}
}

Technique 3: Disable Multi APK

buildTypes {
...
debug {
splits.abi.enable = false
splits.density.enable = false

}
}

Technique 4: Include Minimal Resources

productFlavors {
dev {
resConfigs('en', 'xhdpi')
...
}
}

Technique 5: Disable PNG Crunching

buildTypes {
...
debug {
aaptOptions.cruncherEnabled = false
...
}
}

Technique 6: Use Instant Run

Image for post
Image for post

Technique 7: Disable Updating Build ID

buildTypes {
debug {
ext.alwaysUpdateBuildId = false
...
}
}

Technique 8: Don’t Use Dynamic Versions

android {
dependencies {
implementation 'com.android.support:appcompat-v7:+'
...
}
}

Technique 9: Config gradle.properties

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=1024m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8org.gradle.daemon=trueorg.gradle.parallel=trueorg.gradle.configureondemand=trueorg.gradle.caching=trueandroid.enableBuildScriptClasspathCheck=false

Technique 10: Use R8 new code shrinker

Image for post
Image for post

Cumulative Improvement

Image for post
Image for post

Summary

LINE Developers Thailand

Closing the distance.

Jirawatee

Written by

Jirawatee

Technology Evangelist at LINE Thailand / Google Developer Expert - 🔥Firebase

LINE Developers Thailand

Closing the distance. Our mission is to bring people, information and services closer together

Jirawatee

Written by

Jirawatee

Technology Evangelist at LINE Thailand / Google Developer Expert - 🔥Firebase

LINE Developers Thailand

Closing the distance. Our mission is to bring people, information and services closer together

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store