Gradle is an open-source automation build tool which Android Studio uses for Android Apps. Today, we will be covering the following:
- Gradle profile reports
- Why we need Gradle profiles?
- How to run a profile?
- Understanding the profile output
Gradle profile reports
Gradle profile reports are HTML reports which highlight the performance of the build. How much time each task or process has taken to execute. Gradle has a list of tasks to execute before running the Android application on an emulator or physical device. A Gradle profile report can be generated at any time a task runs. It can be saved permanently to record a history of the build performance which is useful for understanding and optimizing build times. Gradle profiling is already available in Gradle so we don’t need to worry about installing or adding it. Gradle profile reports are generated during build execution by a special command. The process first builds the project and then generates a report based on that build.
Why we need Gradle profile reports?
We can use the report to understand why our builds are taking too long so we can optimize our build time for a faster development process. Gradle profile reports help us understand what tasks ran and what time duration they took. How many modules or dependencies are within our Android Project.
How to generate a Gradle build profile?
- Open up any project in Android Studio that compiles and runs successfully or a new one can suffice.
- Open your desired Command Line Interface (CLI) program and navigate to the root of the project or use the current terminal available in Android Studio. Within the Android Studio terminal, you don’t need to navigate to the project.
- Run the following command to generate a profiling report. Don’t worry about any task warnings because they won’t be a problem while generating a profiling report.
gradle build --profile
- After the build is successful, a static web page will be generated and the path to the profiling report will be given in the line which starts with “See the profiling report at: [path]”
Navigate to Android Studio and select Project scope rather than Android scope. Navigate to the build folder under the project name and then reports > profile and open the HTML file in Android Studio. Right-click on the HTML file and choose Open in browser and select your browser to view the file.
Understanding the Profile report
Let’s go through the generated file. At the top of the file is the name build name and below it is the time and date of when the build process started. The profile report includes five tabs which are the following:
- Dependency Resolution
- Artifact Transforms
- Task Execution
The summary tab contains all the high-level phases, for example, the first item in the list is the Total Build Time which reports about the total time taken by Gradle to build the whole project.
The configuration tab lists all the Gradle Projects (Modules) within the project and their duration of execution
This tab lists all the dependencies within the app module build.gradle file and their duration of execution. It’s the list of tasks that happen when we build the project in Android Studio
Artifacts in Gradle are executable or compressed files which are generated by Gradle during the build process. We can list all the Artifacts in Android Studio under External Libraries within Project Scope in Android Studio.
Task execution tab lists all the modules and their tasks, their execution duration and result. Gradle finds every module within the project and lists all the related tasks accordingly along with their duration of execution.
Understanding Gradle profile reports is an essential task of an Android Developer because it helps in optimizing build process. On a large and complex project dependencies, tasks and builds have a big role to play due to which understanding the basic of it is a must.
Comment, like and share if you learnt something new today!