際際滷

際際滷Share a Scribd company logo
Kirill Rozov
Android Developer
Optimize APK size
GDG Minsk
medium.com/googleplaydev/shrinking-apks-growing-installs-5d3fcba23ce2
Average downloaded APK size
64 MB
What for?
DownloadAPKsize
0
5
10
15
20
25
Size from Google Play for Xiaomi Mi A1 Android 8.0
Why its problem?
 Support low-end devices

 Low space on device memory

 How long time it will take to download app
I need it right now!!!

 No Wi-Fi

 Cellular data costs
Effect
 Lower install conversion rate

 Google Play Featuring problems

 Google Play Security Checks problems

 Problems with Instant App (4Mb limits)
Optimize APK size
APK Structure
APK Structure
Size related to app
 APK size

 Initial download size

 On-device install size

 Update download size
Size related to app
 APK size

 Initial download size

 On-device install size

 Update download size
Graphic Optimizations
Image Optimizers
PNGQuant ImageMagick PNGGauntlet PNGOut
PNGCrush OptiPNG CryoPNG PNG Compressor
Yahoo Smush.it PNGOptimizer PunyPNG TinyPNG
PNGWolf Advpng De鍖Opt De鍖u鍖
Hu鍖mix TruePNG PNGng-s9 zop鍖iPNG
ImageOptim
Optimization results
9.0 MB
Optimization results
6.5 MB
Optimization results
228 Images

Save: 28%

Average: 36%

Max saving: 98.6%
6.5 MB
Android PNG Cruncher
android {
buildTypes {
release {
// Disable PNG optimisation
crunchPngs false
}
}
}
SVGO
github.com/svg/svgo
Use WebP
PNG vs WebP
PNG, 958 KB WebP 100%, 286 KB-70%
PNG vs WebP
PNG, 958 KB WebP 75%, 29 KB-97%
Vector graphics
Vector graphics
xxxhdpi
3.6 KB
xxhdpi
xhdpi
hdpi
mdpi
2.5 KB 1.6 KB 1.1 KB 0.7 KB
PNG, 100x100dp, 8 bit
Vector graphics
Size, bytes
Optimized PNG,
5 鍖les
SVG VectorDrawable Android Shape
RAW 10,023 359 416 232
In APK 10,023 - 371 241
Glide + SVG
github.com/kirich1409/SvgGlidePlugins
POTrace
PNG SVG
potrace.sourceforge.net
Multiple APKs
Multiple APKs
android {
 splits {
  // Configures multiple APKs based on screen density
  density {
   enable true
   // Specifies a list of screen densities Gradle
// should not create multiple APKs for
   exclude "ldpi", "xxhdpi", "xxxhdpi"
   // Specifies a list of compatible screen size
// settings for the manifest
   compatibleScreens 'small', 'normal', 'large', 'xlarge'
  }
 }
}
Reuse graphics
Optimize APK size
Optimize APK size
Image tint
<ImageView
android:src=/slideshow/optimize-apk-size-91989353/91989353/"@drawable/ic_favorite_black_48dp"
android:tint=@color/blue"
/>
Drawable.setTint(int)
Drawable.setTint(ColorStateList)
Optimize images
 Use image optimizer

 Use WebP

 Use vector graphics

 Use multiple APKs for di鍖erent screen

 Reuse similar images

 Skip images for low densities
Resources
Optimizations
Fonts
Optimize APK size
Downloadable fonts
Unnecessary
resources
Resource shrinker
android {
  buildTypes {
    release {
      minifyEnabled true
      shrinkResources true
    }
  }
}
Resource con鍖gurations
android {
  defaultConfig {
    resConfigs ru, en, es, fr, ge
  }
}
Resource con鍖gurations
android {
  defaultConfig {
resConfig auto// Only for localization languages
  }
}
Resource con鍖gurations
android {
  defaultConfig {
    resConfigs ru, en, es, fr, ge
    resConfigs mdpi, hdpi, xhdpi, xxhdpi
    resConfigs normal, large, xlarge
  }
}
Resource Optimizations
 Downloadable fonts

 Remove unused resources

 Reuse resources

 Use recommended media formats
AAC for audio, H264 AVC for Video and etc.

 Update strings over-the-air
localise.so
Code Optimizations
D8 dex compiler
DX compilation timeTime(s)
0
5
10
15
20
DX D8
* Tested with benchmark project here
.dex 鍖le sizeFilesize,MB
0
10
20
30
40
50
DX D8
* Tested with benchmark project here
Code optimizations tools
Code optimizations tools
 Java bytecode

 ProGuard

 DexGuard

 R8

 dex

 Facebook ReDex
Input JARs
Optimised code
Optimize
Shrunk code
Shrink
Obfuscated code
Obfuscate
Output JARs
Preverify
ProGuard Result
Plaid App Results
No optimization Optimization
Optimization
+
obfuscation
.dex size 2.6 Mb 1.2 Mb 0.9 Mb
Classes number 4827 2747 2715
Method references 42664 22319 18922
medium.com/google-developers/practical-proguard-rules-examples-5640a3907dc9
External libraries usage
External libraries usage
 Remove unused Android Support Library parts

 Try to dont add libraries for 1 class/method usage

 Re鍖ection based libraries cant be properly optimised and
shrinked

 Dont add debug libraries to a release builds

 Facebook Stetho

 LeakCanary

 etc.
Native libraries
Remove unused ABIs
android {
 defaultConfig {
  ndk {
   abiFilters armeabi-v7a, arm64-v8a, x86
}
 }
}
Multiple APKs for ABIs
android {
 splits {
  // Configures multiple APKs based on ABI
  abi {
   enable true
   // Resets the list of ABIs
// that Gradle should create APKs for to none
   reset()
   // Specifies a list of ABIs
// that Gradle should create APKs for
   include armeabi, armeabi-v7a, arm64-v8a
  }
 }
}
Code Optimisations
 Use D8 compiler instead of DX

 Code optimizations & shrinker tools

 Be careful about using external libraries

 Remove support of unused ABIs

 Use multiple APKs for ABIs
Other Optimizations
APK Structure
Packaging options
android {
packagingOptions {
exclude "/okhttp3/**"
exclude "/com/google/api/**"
exclude "/kotlin/**"
exclude "/com/applovin/**.java"
exclude "/META-INF/*.version"
exclude "/META-INF/**.pro"
exclude "/META-INF/services/**"
exclude "**/*.kotlin_module"
exclude "**/*.properties"
exclude "**/*.xml"
}
}
Google Play
Features
 Special compression format

 File-by-File updates

 Multiple APKs
Google Play Multiple APKs
 OpenGL texture compression formats

 Screen sizes and densities

 Device feature sets

 Platform versions

 CPU architectures

 Special APK for Android Go Edition
Google Play App Signing
More about App Signing youtu.be/5tdGAP927dk
Optimize APK size
github.com/kirich1409/apk-size-optimization
APK size optimization guide
Optimize APK size
Thanks
kiryl.rozau@apalon.com
krlrozov
Kirill Rozov
Android Developer

More Related Content

Optimize APK size