Problem:
You want to improve the efficiency of gradle construction.
Solution:
Use the recommended technology portfolio below.
Discuss:
First, there is no suggestion that can affect the performance of your app. There are a lot of things you can do to improve the app, many of which are related to the Android obfuscation tool. This section is not about this, it's about improving the efficiency of the building itself.
This section discusses the settings that can be added to the Gradle.properties file in the app's root directory. If you prefer the global settings, add a gradle.properties file under the subfolders of the. Gradle folder under the home directory.
Gradle Daemon Process
The Gradle daemon is a background process that maintains, caches data and code between builds. When you run the command prompt, the latest gradle automatically starts the daemon.
By default, Android Studio launches a Gradle daemon within your project that lasts for three hours and is sufficient for most development tasks. If you run Gradle on the command line, you may not be able to automatically start the daemon.
To ensure the daemon starts, you can add the following configuration:
Org.gradle.daemon=true
Daemons can be started or stopped by command line flags. Using--daemon and--no-daemon can be enabled or disabled in a private configuration often. If you are concerned about an internal cache expiration or if you are testing, the shutdown daemon is valid. If you want to stop a running daemon, you can use the--stop parameter.
Parallel compilation:
Gradle has a "hatch" option to compile standalone projects in parallel. Use it to add a line to the gradle.properties:
Org.gradle.parallel=true
This may not be useful because most of the modules in Android projects are related, which makes it difficult to derive benefits from parallel compilation.
Configure on Demand:
General Gradle All tasks are configured for all projects before some tasks are excluded. This is not very effective for a project that has many sub-projects and many tasks. All can try to configure only those items that are related to the requested task.
Use "Configure on Demand" in Gradle.properties:
Org.gradle.configureondemand=true
Most Android apps have very few sub-projects, so this feature is not always useful.
This is a latent feature, so specific details may change in the new version of Gradle.
Excluded tasks:
As discussed in section 4.3, the-X flag can be used to exclude a specified task, such as lint, which is time consuming but not required to run each build.
The section also shows how to exclude specified tasks from the task map.
To change the JVM settings:
Essentially a Gradle build is run on the Java process, and all the parameters that affect the JVM also affect the performance of Gradle. The following shows some of the JVM settings (in gradle.properties):
org.gradle.jvmargs=-xmx2048m-xx:maxpermsize=512m -xx:+heapdumponoutofmemoryerror
-XMX sets the maximum memory used by the Java process. -XMS sets the memory that is initially allocated to the process. This example also changes the persistent space size, and when Java.lang.OutOfMemoryError throws, the stack is stored inside the file.
You can view more details on the Java HotSpot VM page.
Use only the dependent libraries you need:
This is especially true for the Google Play service, which often requires a large library and uses a separate module.
For example, using Google Maps, you're accustomed to adding the full Google Play service at compile time:
dependencies { compile ' com.google.android.gms:play-service:7.8.0 '}
This is a huge library that has a lot of dependencies, and once the Google Play service is added the following series of libraries will be added:
Due to the 65K method name limitations of Android, you need to add a lot of methods you don't need to handle. Instead, you can just add a map:
dependencies { compile ' com.google.android.gms:play-service-maps:7.8.0 '}
The difference between the two is dramatic:
Using the DEX option:
The Android block allows you to specify parameters for the "Dex" process. The Dexoptions block contains the following options:
dexoptions { incremental true javamaxheapsize ' 2g ' Jumbomode = True Predexlibraries = true}
The incremental option sets whether incremental mode is enabled. The document says "There are very many restrictions that may not take effect." Use cautiously. "
Use Javamaxheapsize as an optional method for specifying XMX.
Enabling "Jumbo Mode" allows more strings to appear in the Dex file. If this is a problem, you should spend more time configuring confusion.
Predexlibraries will allow the DX process in advance. Document Description: "This can improve incremental build efficiency, but clean will become slow".
These settings can improve or degrade performance, so experiment before you decide to use them.
List the configuration overview:
Use--profile to run gradle at the command line to generate useful information for the build. The results are generated in HTML under the Build/reports/profile directory.
For example, run a multi-flavor Assembledebug task, as follows:
>./gradlew--profile assembleDebug:app:preBuild up-to-date:app:prearrogantstarkdebugbuild Up-to-date:app: CheckArrogantStarkDebugManifest:app:preArrogantStarkReleaseBuild Up-to-date:app:prearrogantwaynedebugbuild Up-to-date:app:prearrogantwaynereleasebuild up-to-date:app:prefriendlystarkdebugbuild UP-TO-DATE:app: Prefriendlystarkreleasebuild up-to-date:app:prefriendlywaynedebugbuild Up-to-date:app: Prefriendlywaynereleasebuild up-to-date:app:preobsequiousstarkdebugbuild Up-to-date:app: Preobsequiousstarkreleasebuild up-to-date:app:preobsequiouswaynedebugbuild Up-to-date:app: Preobsequiouswaynereleasebuild up-to-date//... tons of other tasks ...: App:assembleObsequiousWayneDebug:app: Assembledebug
The output file is under the Build/reports/profile folder, similar to the "profile-yyyy-mm-dd-hh-mm-ss.html" file name.
There is not much to see for small projects, but for large projects, this is a good way to find bottlenecks.
Android Development: "Gradle Recipes for Android" reading notes (translation) 6.1--recommended configuration