SlideShare uma empresa Scribd logo
1 de 53
Decrease build time and
application size by 60%
Keval Patel
Android Developer @multidots
1
Agenda
- What is Gradle? How does it work?
- Why decrease application build time?
- How to decrease application build time?
- Introduction to APK analyzer
- Why is APK size so important?
- How to trim your APK?
2
What is Gradle?
- Very powerful build system - Written in Groovy & Java
- Open source
- Flexible dependency management
- Task based
3
What Gradle does?
- Building .dx files from java source.
- Merge all the resources and assets.
- Generates .apk from the source
code.
4
How to decrease application
build time?
PART 1:
5
Why you should care about your build time?
Let’s say,
- You are generating 15 clean builds every day.
- Each build takes 3 minutes to generate.
- Total time: 15 * 3 = 45 mins.
- If you work for 8 hours a day, that’s almost 10% of your work time.
- That’s about 4 hours a week !!!
6
Less build
time
More time
for
productive
stuff
Better app
with less
bugs
Happy
users
Less Build Time = More Productivity
Why you should care about your build time?
7
Benchmarking with I/O’16 Application
- Open source (https://github.com/google/iosched)
- 25+ dependencies and third party libraries
- 41538 method references
8
- Two modules:
- android: Contains android application classes and resources.
- server: Contains codebase for backend server.
- Minimum SDK - 16
- Target SDK - 22/23
- 9 different languages
Benchmarking with I/O’16 Application
9
Gradle build life cycle
1. Initialization 2. Configuration 3. Execution
- Initialize resources
- Start daemon thread
- Figure out what to build
- Evaluate your
build.gradle script
- Configure all the plugins
- Evaluate the task graph.
- Runs all the
previously
evaluated task
- Build the
application. 10
--profile will tell gradle to measure the time taken to execute each task and
dump that data into HTML file.
--profile your gradle build time
$ ./gradlew assembleDebug
You can find that report under /[your project dir]/build/reports/profile
directory.
Sample
11
--profile
Initilization
Configuration
Execution
12
How to decrease
Configuration
time?
13
- --dry-run tells gradle to evaluate the project but don’t run any task.
- The last phase of the life cycle (execution phase) won’t get executed.
- Tells us the time required by Gradle to configure the project and to start
the build.
Measure time required to configure
14
Measure time required to configure
$ ./gradlew assembleDebug --dry-run --profile
15
— configure-on-demand
$ ./gradlew assembleDebug --dry-run --configure-on-demand --profile
16
How to decrease
Initialization
time?
17
Gradle Daemon
- Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries
to be load at initialization time. That's why it's slow to start.
More info : https://docs.gradle.org/current/userguide/gradle_daemon.html
- Gradle Daemon is a long-lived process that will run in the background.
- Avoid the cost of JVM startup for every build,
- Able to cache information about project structure, files, tasks, and more in
memory.
18
Commands for Gradle Daemon
- To enable daemon for gradle build use --daemon.
- To get a list of running Gradle Daemons and their statuses use the --status
command.
- To stop daemon use --stop command.
19More info : https://docs.gradle.org/current/userguide/gradle_daemon.html
Gradle Daemon
$ ./gradlew assembleDebug --dry-run --configure-on-demand --daemon --profile
Initialization time decreased by
4.1 seconds than before after
running the build for the second
time.
Speed improvements will apply
for subsequent builds.
20
How to decrease
Execution
time?
21
Dex in the process
- “Dex in process” is a process that allows to run multiple dex processes to
run within the single VM that is also shared with the gradle.
- Requirement:
- Build tool version 23.0.2 or above.
- Android Studio 2.0 or above.
22
Dex in the process
- Multiple dx processes runs on
different VM instances.
- These increases build time as each
dx has to pass data between
different VM instances.
- Requires less per VM heap size as
single VM is hosting only single dx
process.
https://www.youtube.com/watch?v=-SY5nkNVUn0
23
Dex in the process
Starting from AS 2.0,
- All these dx processes run in the
single VM and that VM is also
shared with the gradle.
- Require higher hip size to run as
much as possible dx processes in
a single instance.
https://www.youtube.com/watch?v=-SY5nkNVUn0
24
Dex in the process
Add below line in your /gradle.properties file to re-configure the heap size.
org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
- Select the best suitable heap size for your system based on the amount of
RAM available.
- Generally, after 4GB, any further increase in heap size won’t affect the
build time.
25
Enable parallel builds
- If you have multiple modules in you project, then by enabling this, gradle
can run build operations for independent modules parallelly.
- Gradle will only build modules parallelly if two modules are not
interdependent.
Google I/O project doesn’t have multiple modules, building parallel won’t make much difference.
26
Enable parallel builds
Add below line in your /gradle.properties file to enable parallel builds.
org.gradle.parallel=true
27
Other small things to remember…
- Avoid heavy computations, like increasing the version code from git
commits count or downloading some files from URL while building the
project.
- Always use latest gradle version. (Current is 3.4.1)
- Don’t use dynamic dependency.
compile 'com.android.support:appcompat-v7:23.0.+' compile 'com.android.support:appcompat-v7:23.0.1'
28
TL;DR
#Enable daemon
org.gradle.daemon=true
# Try and find out the best heap size for your project build.
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
# Modularise your project and enable parallel build
org.gradle.parallel=true
# Enable configure on demand.
org.gradle.configureondemand=true
- Add below line in your /gradle.properties.
- Modularize your project.
29
How to decrease application
size?
PART 2:
30
Why APK size is so important?
- Because you want to get your next billion users.
- Mobile devices have limited amount of battery, limited storage, limited
processing power, limited RAM, limited internet connectivity…
- Currently, more than 11000 android devices are out in the market and
most of them are having low-end configurations.
- Two key things that are precious to every smartphone user,
1)Storage.
31
Storage
- Most low-mid range devices have 8GB to 16GB
internal storage.
- The less storage your application uses, the
more free space user gets to store their videos
and images.
- You don’t want your user to uninstall your
application because of “Storage out of space”
notification. 32
Network connectivity
- Most of the android users are from developing
countries (like India, Brazil or countries in Africa)
where they have very limited 2G/3G connectivity.
- If your application is large in size, it will take a longer
time to download the application.
- Most of the users have limited amount of data.
- Every byte user uses to download the application, will
affect your user’s wallet.
33
Breakdown your APK using
APK Analyser
34
APK Analyser
Provides immediate insight into the composition of your APK.
Features:
- View file size and the download size of the APK
- View AndroidManifest.xml
- View resources
- View dex files and method counts for each dex files.
- Compare two APK files. 35
36
Breakdown of APK
- APK file mainly contains 3 major components which utilize the maximum
amount of size in whole apk package:
1.classes.dex - dex file which contains bytecode of your java classes.
2. res - Contains images, icons and raw files, menu files, and layouts.
3. resources.arsc - Holds all the value resources. This resource contains
strings, dimensions, styles, integers, view ids etc.
37
Reducing the size of
classes.dex file
38
Proguard
- A Java class file shrinker, optimizer, obfuscator, and preverifier.
- It detects and removes unused classes, fields, methods, and attributes. It
optimizes bytecode and removes unused instructions.
- It renames the remaining classes, fields, and methods using short
meaningless names to prevent reverse engineering of your apk file.
39
How to apply proguard?
- You have two default proguard files bundled in your SDK that you can
apply.
- proguard-android.txt - contains light-weight proguard rules.
- proguard-android-optimize.txt - contains very aggressive obfuscation and shrinking rules.
release {
//Enable the proguard
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), "proguard-rules.pro"
}
40
Let’s see the results…
Before:
After:
Size decreased from 3.1 MB to 1.98MB. (~50% decrease) 41
Reducing the size of
res folder
and
resources.arsc
42
Enable “shrinkResources”
- Many unused images and strings (e.g. resources for sign in with google button
images) those are not used in your entire application.
- Most of these unused images/resources are from third party libraries (e.g.
Google Play Services).
43
Enable “shrinkResources”
- Remove all the resources (images/strings), those are not used anywhere in
the project.
- To enable this in your build, add below lines:
release{
//...
//...
shrinkResources true
//...
} 44
Set “resConfigs”
- Libraries like Google Play Services contains resources from different
locales.
- Your application does not support all locales.
- Provide the list of locales your application supports and “resConfigs” will
remove all the resources for not supported locales.
defaultConfig {
//...
//strip other than english resources
resConfigs "en"
}
45
Convert your png images to webp
- webp is image format like png but having less size than png image with
the same quality.
- webp is natively supported by Android since the beginning. So you can
load webp images in ImageView same as other raster images.
- Android Studio 2.3 provides a built-in tool that can convert your png
images to webp.
46
How to convert png images to webp?
- Right-click on the image and select
“Convert to webp”.
- Select quality of the output image.
- Select whether to convert
transparent images to webp or not.
- Hit “OK”. That’s it!!
47
Let’s see the results…
Before:
48
Let’s see the results…
After:
res folder size decreased from 710KB to 597KB. (Decrease by 16%) 49
Final comparison
* This are the results from the current version of the Anti-Theft apk. So, numbers are different than previous slides.
50
TL;DR
Enable proguard in your project by adding following lines to your release
build type.
Enable “shrinkResources”.
Strip down all the unused locale resources by adding required resources
name in “resConfigs”.
Convert all the images to the webp or vector drawable.
51
How to decrease your Gradle build time by 65%?
http://bit.ly/2jvi9XB
How you can decrease application size by 60% (In only 5 minutes)?
http://bit.ly/2mWQQIc
52
References:
Thank you
@kevalpatel2106
53
medium.com/@kevalpatel2106

Mais conteúdo relacionado

Mais procurados

MobileConf 2013 - Aerogear Android
MobileConf 2013 - Aerogear AndroidMobileConf 2013 - Aerogear Android
MobileConf 2013 - Aerogear Android
Daniel Passos
 
Lund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & Weave
Lund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & WeaveLund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & Weave
Lund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & Weave
Constantin Musca
 

Mais procurados (20)

Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - Bradford ...
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - Bradford ...Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - Bradford ...
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - Bradford ...
 
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - dotNET Stoke
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - dotNET StokeBuilding a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - dotNET Stoke
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - dotNET Stoke
 
PyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_AyanPyCon_India_2017_MicroPython_Ayan
PyCon_India_2017_MicroPython_Ayan
 
Hack the Real World with ANDROID THINGS
Hack the Real World with ANDROID THINGSHack the Real World with ANDROID THINGS
Hack the Real World with ANDROID THINGS
 
Go native benchmark test su dispositivi x86: java, ndk, ipp e tbb
Go native  benchmark test su dispositivi x86: java, ndk, ipp e tbbGo native  benchmark test su dispositivi x86: java, ndk, ipp e tbb
Go native benchmark test su dispositivi x86: java, ndk, ipp e tbb
 
Teknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
Teknologi Baru Android di Google I/O 2021 - Andrew KurniadiTeknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
Teknologi Baru Android di Google I/O 2021 - Andrew Kurniadi
 
ASP.NET 5 on the Raspberry PI 2
ASP.NET 5 on the Raspberry PI 2ASP.NET 5 on the Raspberry PI 2
ASP.NET 5 on the Raspberry PI 2
 
Myths of Angular 2: What Angular Really Is
Myths of Angular 2: What Angular Really IsMyths of Angular 2: What Angular Really Is
Myths of Angular 2: What Angular Really Is
 
Azure Percept Home Automation - .NET Docs Show - 20-09-21
Azure Percept Home Automation  - .NET Docs Show - 20-09-21Azure Percept Home Automation  - .NET Docs Show - 20-09-21
Azure Percept Home Automation - .NET Docs Show - 20-09-21
 
Android Things Robocar with TensorFlow for object recognition
Android Things Robocar with TensorFlow for object recognitionAndroid Things Robocar with TensorFlow for object recognition
Android Things Robocar with TensorFlow for object recognition
 
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - CSharp Co...
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - CSharp Co...Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - CSharp Co...
Building a Raspberry Pi Robot Arm with .NET 5, Blazor and SignalR - CSharp Co...
 
Accessing Hardware on Android
Accessing Hardware on AndroidAccessing Hardware on Android
Accessing Hardware on Android
 
Android Open Accessory Protocol - Turn Your Linux machine as ADK
Android Open Accessory Protocol - Turn Your Linux machine as ADKAndroid Open Accessory Protocol - Turn Your Linux machine as ADK
Android Open Accessory Protocol - Turn Your Linux machine as ADK
 
20180124 android taipei_arinterface
20180124 android taipei_arinterface20180124 android taipei_arinterface
20180124 android taipei_arinterface
 
The abcs of gpu
The abcs of gpuThe abcs of gpu
The abcs of gpu
 
MobileConf 2013 - Aerogear Android
MobileConf 2013 - Aerogear AndroidMobileConf 2013 - Aerogear Android
MobileConf 2013 - Aerogear Android
 
ITCamp 2018 - Mete Atamel Ian Talarico - Google Home meets .NET containers on...
ITCamp 2018 - Mete Atamel Ian Talarico - Google Home meets .NET containers on...ITCamp 2018 - Mete Atamel Ian Talarico - Google Home meets .NET containers on...
ITCamp 2018 - Mete Atamel Ian Talarico - Google Home meets .NET containers on...
 
Lund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & Weave
Lund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & WeaveLund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & Weave
Lund Linux Conference 2016, Lund, Sweden - Introduction to Brillo OS & Weave
 
Getting started with the NDK
Getting started with the NDKGetting started with the NDK
Getting started with the NDK
 
Android Things Getting Started
Android Things Getting StartedAndroid Things Getting Started
Android Things Getting Started
 

Semelhante a Decrease build time and application size

Semelhante a Decrease build time and application size (20)

Using Docker For Development
Using Docker For DevelopmentUsing Docker For Development
Using Docker For Development
 
DCEU 18: Developing with Docker Containers
DCEU 18: Developing with Docker ContainersDCEU 18: Developing with Docker Containers
DCEU 18: Developing with Docker Containers
 
Introduction to Containers: From Docker to Kubernetes and everything in-between
Introduction to Containers:  From Docker to Kubernetes and everything in-betweenIntroduction to Containers:  From Docker to Kubernetes and everything in-between
Introduction to Containers: From Docker to Kubernetes and everything in-between
 
ICONUK 2015 - Gradle Up!
ICONUK 2015 - Gradle Up!ICONUK 2015 - Gradle Up!
ICONUK 2015 - Gradle Up!
 
Commit to excellence - Java in containers
Commit to excellence - Java in containersCommit to excellence - Java in containers
Commit to excellence - Java in containers
 
Michal Havryluk: How To Speed Up Android Gradle Builds
Michal Havryluk: How To Speed Up Android Gradle BuildsMichal Havryluk: How To Speed Up Android Gradle Builds
Michal Havryluk: How To Speed Up Android Gradle Builds
 
All Things Containers - Docker, Kubernetes, Helm, Istio, GitOps and more
All Things Containers - Docker, Kubernetes, Helm, Istio, GitOps and moreAll Things Containers - Docker, Kubernetes, Helm, Istio, GitOps and more
All Things Containers - Docker, Kubernetes, Helm, Istio, GitOps and more
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
OSDC 2018 | Three years running containers with Kubernetes in Production by T...
OSDC 2018 | Three years running containers with Kubernetes in Production by T...OSDC 2018 | Three years running containers with Kubernetes in Production by T...
OSDC 2018 | Three years running containers with Kubernetes in Production by T...
 
PERFORMANCE COMPARISON ON JAVA TECHNOLOGIES - A PRACTICAL APPROACH
PERFORMANCE COMPARISON ON JAVA TECHNOLOGIES - A PRACTICAL APPROACHPERFORMANCE COMPARISON ON JAVA TECHNOLOGIES - A PRACTICAL APPROACH
PERFORMANCE COMPARISON ON JAVA TECHNOLOGIES - A PRACTICAL APPROACH
 
Performance comparison on java technologies a practical approach
Performance comparison on java technologies   a practical approachPerformance comparison on java technologies   a practical approach
Performance comparison on java technologies a practical approach
 
What's coming in Airflow 2.0? - NYC Apache Airflow Meetup
What's coming in Airflow 2.0? - NYC Apache Airflow MeetupWhat's coming in Airflow 2.0? - NYC Apache Airflow Meetup
What's coming in Airflow 2.0? - NYC Apache Airflow Meetup
 
Windows Debugging Tools - JavaOne 2013
Windows Debugging Tools - JavaOne 2013Windows Debugging Tools - JavaOne 2013
Windows Debugging Tools - JavaOne 2013
 
Building a Modern Enterprise SOA at LinkedIn
Building a Modern Enterprise SOA at LinkedInBuilding a Modern Enterprise SOA at LinkedIn
Building a Modern Enterprise SOA at LinkedIn
 
Improving build solutions dependency management with webpack
Improving build solutions  dependency management with webpackImproving build solutions  dependency management with webpack
Improving build solutions dependency management with webpack
 
Android gradle-build-system-overview
Android gradle-build-system-overviewAndroid gradle-build-system-overview
Android gradle-build-system-overview
 
Scaling PHP apps
Scaling PHP appsScaling PHP apps
Scaling PHP apps
 
Android presentation - Gradle ++
Android presentation - Gradle ++Android presentation - Gradle ++
Android presentation - Gradle ++
 
Introduction to Docker and Monitoring with InfluxData
Introduction to Docker and Monitoring with InfluxDataIntroduction to Docker and Monitoring with InfluxData
Introduction to Docker and Monitoring with InfluxData
 
Creating Developer-Friendly Docker Containers with Chaperone
Creating Developer-Friendly Docker Containers with ChaperoneCreating Developer-Friendly Docker Containers with Chaperone
Creating Developer-Friendly Docker Containers with Chaperone
 

Mais de Keval Patel (10)

Contributor Qualities
Contributor QualitiesContributor Qualities
Contributor Qualities
 
Views and environment of management
Views and environment of management Views and environment of management
Views and environment of management
 
White revolution
White revolutionWhite revolution
White revolution
 
Control of dc drives
Control of dc drivesControl of dc drives
Control of dc drives
 
Wave guide tees
Wave guide teesWave guide tees
Wave guide tees
 
Demand forecasting
Demand forecastingDemand forecasting
Demand forecasting
 
Various types of data recorders
Various types of data recordersVarious types of data recorders
Various types of data recorders
 
Transition from 1G to 4G
Transition from 1G to 4GTransition from 1G to 4G
Transition from 1G to 4G
 
Log periodic antenna
Log periodic antennaLog periodic antenna
Log periodic antenna
 
Dsp application on mobile communication
Dsp application on mobile communicationDsp application on mobile communication
Dsp application on mobile communication
 

Último

Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Cara Menggugurkan Kandungan 087776558899
 

Último (7)

Mobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s ToolsMobile Application Development-Android and It’s Tools
Mobile Application Development-Android and It’s Tools
 
Mobile App Penetration Testing Bsides312
Mobile App Penetration Testing Bsides312Mobile App Penetration Testing Bsides312
Mobile App Penetration Testing Bsides312
 
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pureBromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
Bromazolam CAS 71368-80-4 high quality opiates, Safe transportation, 99% pure
 
Mobile Application Development-Components and Layouts
Mobile Application Development-Components and LayoutsMobile Application Development-Components and Layouts
Mobile Application Development-Components and Layouts
 
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
Satara Call girl escort *74796//13122* Call me punam call girls 24*7hour avai...
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Android Application Components with Implementation & Examples
Android Application Components with Implementation & ExamplesAndroid Application Components with Implementation & Examples
Android Application Components with Implementation & Examples
 

Decrease build time and application size

  • 1. Decrease build time and application size by 60% Keval Patel Android Developer @multidots 1
  • 2. Agenda - What is Gradle? How does it work? - Why decrease application build time? - How to decrease application build time? - Introduction to APK analyzer - Why is APK size so important? - How to trim your APK? 2
  • 3. What is Gradle? - Very powerful build system - Written in Groovy & Java - Open source - Flexible dependency management - Task based 3
  • 4. What Gradle does? - Building .dx files from java source. - Merge all the resources and assets. - Generates .apk from the source code. 4
  • 5. How to decrease application build time? PART 1: 5
  • 6. Why you should care about your build time? Let’s say, - You are generating 15 clean builds every day. - Each build takes 3 minutes to generate. - Total time: 15 * 3 = 45 mins. - If you work for 8 hours a day, that’s almost 10% of your work time. - That’s about 4 hours a week !!! 6
  • 7. Less build time More time for productive stuff Better app with less bugs Happy users Less Build Time = More Productivity Why you should care about your build time? 7
  • 8. Benchmarking with I/O’16 Application - Open source (https://github.com/google/iosched) - 25+ dependencies and third party libraries - 41538 method references 8
  • 9. - Two modules: - android: Contains android application classes and resources. - server: Contains codebase for backend server. - Minimum SDK - 16 - Target SDK - 22/23 - 9 different languages Benchmarking with I/O’16 Application 9
  • 10. Gradle build life cycle 1. Initialization 2. Configuration 3. Execution - Initialize resources - Start daemon thread - Figure out what to build - Evaluate your build.gradle script - Configure all the plugins - Evaluate the task graph. - Runs all the previously evaluated task - Build the application. 10
  • 11. --profile will tell gradle to measure the time taken to execute each task and dump that data into HTML file. --profile your gradle build time $ ./gradlew assembleDebug You can find that report under /[your project dir]/build/reports/profile directory. Sample 11 --profile
  • 14. - --dry-run tells gradle to evaluate the project but don’t run any task. - The last phase of the life cycle (execution phase) won’t get executed. - Tells us the time required by Gradle to configure the project and to start the build. Measure time required to configure 14
  • 15. Measure time required to configure $ ./gradlew assembleDebug --dry-run --profile 15
  • 16. — configure-on-demand $ ./gradlew assembleDebug --dry-run --configure-on-demand --profile 16
  • 18. Gradle Daemon - Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries to be load at initialization time. That's why it's slow to start. More info : https://docs.gradle.org/current/userguide/gradle_daemon.html - Gradle Daemon is a long-lived process that will run in the background. - Avoid the cost of JVM startup for every build, - Able to cache information about project structure, files, tasks, and more in memory. 18
  • 19. Commands for Gradle Daemon - To enable daemon for gradle build use --daemon. - To get a list of running Gradle Daemons and their statuses use the --status command. - To stop daemon use --stop command. 19More info : https://docs.gradle.org/current/userguide/gradle_daemon.html
  • 20. Gradle Daemon $ ./gradlew assembleDebug --dry-run --configure-on-demand --daemon --profile Initialization time decreased by 4.1 seconds than before after running the build for the second time. Speed improvements will apply for subsequent builds. 20
  • 22. Dex in the process - “Dex in process” is a process that allows to run multiple dex processes to run within the single VM that is also shared with the gradle. - Requirement: - Build tool version 23.0.2 or above. - Android Studio 2.0 or above. 22
  • 23. Dex in the process - Multiple dx processes runs on different VM instances. - These increases build time as each dx has to pass data between different VM instances. - Requires less per VM heap size as single VM is hosting only single dx process. https://www.youtube.com/watch?v=-SY5nkNVUn0 23
  • 24. Dex in the process Starting from AS 2.0, - All these dx processes run in the single VM and that VM is also shared with the gradle. - Require higher hip size to run as much as possible dx processes in a single instance. https://www.youtube.com/watch?v=-SY5nkNVUn0 24
  • 25. Dex in the process Add below line in your /gradle.properties file to re-configure the heap size. org.gradle.jvmargs=-Xmx3072m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 - Select the best suitable heap size for your system based on the amount of RAM available. - Generally, after 4GB, any further increase in heap size won’t affect the build time. 25
  • 26. Enable parallel builds - If you have multiple modules in you project, then by enabling this, gradle can run build operations for independent modules parallelly. - Gradle will only build modules parallelly if two modules are not interdependent. Google I/O project doesn’t have multiple modules, building parallel won’t make much difference. 26
  • 27. Enable parallel builds Add below line in your /gradle.properties file to enable parallel builds. org.gradle.parallel=true 27
  • 28. Other small things to remember… - Avoid heavy computations, like increasing the version code from git commits count or downloading some files from URL while building the project. - Always use latest gradle version. (Current is 3.4.1) - Don’t use dynamic dependency. compile 'com.android.support:appcompat-v7:23.0.+' compile 'com.android.support:appcompat-v7:23.0.1' 28
  • 29. TL;DR #Enable daemon org.gradle.daemon=true # Try and find out the best heap size for your project build. org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 # Modularise your project and enable parallel build org.gradle.parallel=true # Enable configure on demand. org.gradle.configureondemand=true - Add below line in your /gradle.properties. - Modularize your project. 29
  • 30. How to decrease application size? PART 2: 30
  • 31. Why APK size is so important? - Because you want to get your next billion users. - Mobile devices have limited amount of battery, limited storage, limited processing power, limited RAM, limited internet connectivity… - Currently, more than 11000 android devices are out in the market and most of them are having low-end configurations. - Two key things that are precious to every smartphone user, 1)Storage. 31
  • 32. Storage - Most low-mid range devices have 8GB to 16GB internal storage. - The less storage your application uses, the more free space user gets to store their videos and images. - You don’t want your user to uninstall your application because of “Storage out of space” notification. 32
  • 33. Network connectivity - Most of the android users are from developing countries (like India, Brazil or countries in Africa) where they have very limited 2G/3G connectivity. - If your application is large in size, it will take a longer time to download the application. - Most of the users have limited amount of data. - Every byte user uses to download the application, will affect your user’s wallet. 33
  • 34. Breakdown your APK using APK Analyser 34
  • 35. APK Analyser Provides immediate insight into the composition of your APK. Features: - View file size and the download size of the APK - View AndroidManifest.xml - View resources - View dex files and method counts for each dex files. - Compare two APK files. 35
  • 36. 36
  • 37. Breakdown of APK - APK file mainly contains 3 major components which utilize the maximum amount of size in whole apk package: 1.classes.dex - dex file which contains bytecode of your java classes. 2. res - Contains images, icons and raw files, menu files, and layouts. 3. resources.arsc - Holds all the value resources. This resource contains strings, dimensions, styles, integers, view ids etc. 37
  • 38. Reducing the size of classes.dex file 38
  • 39. Proguard - A Java class file shrinker, optimizer, obfuscator, and preverifier. - It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. - It renames the remaining classes, fields, and methods using short meaningless names to prevent reverse engineering of your apk file. 39
  • 40. How to apply proguard? - You have two default proguard files bundled in your SDK that you can apply. - proguard-android.txt - contains light-weight proguard rules. - proguard-android-optimize.txt - contains very aggressive obfuscation and shrinking rules. release { //Enable the proguard minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), "proguard-rules.pro" } 40
  • 41. Let’s see the results… Before: After: Size decreased from 3.1 MB to 1.98MB. (~50% decrease) 41
  • 42. Reducing the size of res folder and resources.arsc 42
  • 43. Enable “shrinkResources” - Many unused images and strings (e.g. resources for sign in with google button images) those are not used in your entire application. - Most of these unused images/resources are from third party libraries (e.g. Google Play Services). 43
  • 44. Enable “shrinkResources” - Remove all the resources (images/strings), those are not used anywhere in the project. - To enable this in your build, add below lines: release{ //... //... shrinkResources true //... } 44
  • 45. Set “resConfigs” - Libraries like Google Play Services contains resources from different locales. - Your application does not support all locales. - Provide the list of locales your application supports and “resConfigs” will remove all the resources for not supported locales. defaultConfig { //... //strip other than english resources resConfigs "en" } 45
  • 46. Convert your png images to webp - webp is image format like png but having less size than png image with the same quality. - webp is natively supported by Android since the beginning. So you can load webp images in ImageView same as other raster images. - Android Studio 2.3 provides a built-in tool that can convert your png images to webp. 46
  • 47. How to convert png images to webp? - Right-click on the image and select “Convert to webp”. - Select quality of the output image. - Select whether to convert transparent images to webp or not. - Hit “OK”. That’s it!! 47
  • 48. Let’s see the results… Before: 48
  • 49. Let’s see the results… After: res folder size decreased from 710KB to 597KB. (Decrease by 16%) 49
  • 50. Final comparison * This are the results from the current version of the Anti-Theft apk. So, numbers are different than previous slides. 50
  • 51. TL;DR Enable proguard in your project by adding following lines to your release build type. Enable “shrinkResources”. Strip down all the unused locale resources by adding required resources name in “resConfigs”. Convert all the images to the webp or vector drawable. 51
  • 52. How to decrease your Gradle build time by 65%? http://bit.ly/2jvi9XB How you can decrease application size by 60% (In only 5 minutes)? http://bit.ly/2mWQQIc 52 References:

Notas do Editor

  1. Introduction Ahmedabad 2 years of experience Designation AAD
  2. - First learn about gradle. - Made up of two parts. PART-1 - Why it is required to decrease build time? - How you can decrease? PART-2 - Basics and how to of APK analyser - Why APK size? - How can you decrease?
  3. Gradle is very powerful build system. Builds your application. Not just for android. We write build.gradle script. Open source Very flexible dependency management Kind of plug and play. Just write compile ----- Also, easy to add build type and product flavour Written groovey Nowadays java. (Display the screen shot) Tasks are everything in gradle assembleDebug is a task. Which depends on other tasks.
  4. Only for android prospective Builds .dx files from java file. Java ----> class -----> .dx Merge all the resources based on product flavour Complex process of generating apk.
  5. Why? Demonstrate simple math.
  6. Less Build time ⇒ Less time to see gradle building your project ⇒ More time for other work ⇒ Make some bug fixes ⇒ Implement some cool features ⇒ Happy users, the ultimate goal. Two Downside, Less coffee Less lunch/snack brakes.
  7. Before we go how to decrease time…. We need some benchmarking. We are software guys, we need proof. If applying those tricks, decreases time really? I/O’16 app project Open source Available on github. Name on GitHub ⇒ iosched. (weird) I/O’17 code is not available yet😊 25+ dependency And 41K methods Not multi dexed yet
  8. Let’s look in basic structure, App? Server? What are these modules , explain Min sdk - 16 Android 4.1 or above. Target SDK 23 (?Marshmallow). That was the latest while i/o 16. 9 different locales.
  9. Phases: Init Set resources. Init java instance in JVM. Pull required resources to JVM. Daemon thread to hold those resources. Config Evaluate script. Figure out what to do? Task graph Any plugins/external build scripts? (com.android.application) ⇒ For application. com.android.library ⇒ For libs module. Exe. Main phase. Run all tasks Compiles code to .dx. Generates APK.
  10. Sample Report : http://psychiatrist-sheep-25576.bitballoon.com Command to generate debug apk. --profile to create report in HTML Always good to run gradle with this tag. Later build time suddenly increase, provides you insight.
  11. Out of these three only Execution phase is important. Other two phases are useless.
  12. How we can measure the exact time required to config?
  13. Modularize project : https://medium.freecodecamp.com/how-modularisation-affects-build-time-of-an-android-application-43a984ce9968
  14. Explain what is in file proguard-rules.pro. This file is to add custom proguard rules
  15. Above results are from Anti-theft lock screen.
  16. This also removes all the images you find on android resources.