Gradle is a build tool based on java. There are some prerequisites that are required to be installed before installing the Gradle frame work. Show
Prerequisites to install GradleJDK and Groovy are the prerequisites for Gradle installation. Gradle requires JDK version 6 or later to be installed in the system. It uses the JDK libraries which are installed, and sets to the JAVA_HOME environmental variable. Gradle carries its own Groovy library, therefore, we need not install Groovy explicitly. If it is installed, that is ignored by Gradle. The steps to install Gradle in your system are explained below. Step 1 − Verify JAVA InstallationFirst of all, you need to have Java Software Development Kit (SDK) installed on your system. To verify this, execute Java –version command in any of the platform you are working on. In WindowsExecute the following command to verify Java installation. I have installed JDK 1.8 in my system. C:\> java - versionOutput The output is as follows − java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)In LinuxExecute the following command to verify Java installation.We have installed JDK 1.8 in the system. $ java - versionOutput java version "1.8.0_66" Java(TM) SE Runtime Environment (build 1.8.0_66-b18) Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)We assume the readers of this tutorial have Java SDK version 1.8.0_66 installed on their system. Step 2 − Download Gradle Build FileDownload the latest version of Gradle from the link available at https://gradle.org/install/. In the reference page, click on the Complete Distribution link. This step is common for any platform. For this you will get the complete distribution file into your Downloads folder. Step 3 − Set Up Environment for GradleSetting up environment means, we have to extract the distribution file and copy the library files into proper location. Set up GRADLE_HOME and PATH environmental variables. This step is platform dependent. In WindowsExtract the downloaded zip file named gradle-2.11-all.zip and copy the distribution files from Downloads\gradle-2.11\ to C:\gradle\ location. After that, add the C:\gradle and C:\gradle\bin directories to the GRADLE_HOME and PATH system variables. Follow the given instructions − Right Click On My Computers -> Click On Properties -> Advanced System Settings -> Click On Environmental Variables. There you will find a dialog box for creating and editing system variables. Click on new button for creating GRADLE_HOME variable (follow the left side screenshot). Click on Edit for editing the existing Path system variable (follow the right side screenshot). Follow the below given screenshots. In LinuxExtract the downloaded zip file named gradle-2.11-all.zip then you will find an extracted file named gradle-2.11. You can use the following to move the distribution files from Downloads/gradle-2.11/ to /opt/gradle/ location. Execute this operation from the Downloads directory. $ sudo mv gradle-2.11 /opt/gradleEdit the ~/.bashrc file and paste the following content to it and save it. export ORIENT_HOME = /opt/gradle export PATH = $PATH:Execute the following command to execute ~/.bashrc file. $ source ~/.bashrcStep 4 − Verify the Gradle installationIn windowsYou can execute the following command in command prompt. C:\> gradle –vOutput Here you will find the Gradle version. ------------------------------------------------------------ Gradle 2.11 ------------------------------------------------------------ Build time: 2016-02-08 07:59:16 UTC Build number: none Revision: 584db1c7c90bdd1de1d1c4c51271c665bfcba978 Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.7.0_60 (Oracle Corporation 24.60-b09) OS: Windows 8.1 6.3 amd64In LinuxYou can execute the following command in terminal. $ gradle –vOutput Here you will find the Gradle version. ------------------------------------------------------------ Gradle 2.11 ------------------------------------------------------------ Build time: 2016-02-08 07:59:16 UTC Build number: none Revision: 584db1c7c90bdd1de1d1c4c51271c665bfcba978 Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.7.0_60 (Oracle Corporation 24.60-b09) OS: Windows 8.1 6.3 amd64In LinuxYou can execute the following command in terminal. $ gradle –vOutput You will find the Gradle version. ------------------------------------------------------------ Gradle 2.11 ------------------------------------------------------------ Build time: 2016-02-08 07:59:16 UTC Build number: none Revision: 584db1c7c90bdd1de1d1c4c51271c665bfcba978 Groovy: 2.4.4 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.7.0_60 (Oracle Corporation 24.60-b09) OS: Linux 3.13.0-74-generic amd64
The Android build system compiles app resources and source code and packages them into APKs or Android App Bundles that you can test, deploy, sign, and distribute. Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process while letting you define flexible, custom build configurations. Each build configuration can define its own set of code and resources while reusing the parts common to all versions of your app. The Android Gradle plugin works with the build toolkit to provide processes and configurable settings that are specific to building and testing Android apps. Gradle and the Android Gradle plugin run independent of Android Studio. This means that you can build your Android apps from within Android Studio, the command line on your machine, or on machines where Android Studio is not installed, such as continuous integration servers. If you aren't using Android Studio, you can learn how to build and run your app from the command line. The output of the build is the same whether you are building a project from the command line, on a remote machine, or using Android Studio. Note: Because Gradle and the Android Gradle plugin run independently from Android Studio, you need to update the build tools separately. Read the release notes to learn how to update Gradle and the Android Gradle plugin. The flexibility of the Android build system lets you create custom build configurations without modifying your app's core source files. This page helps you understand how the Android build system works and how it can help you customize and automate multiple build configurations. If you want to learn more about deploying your app, see Build and run your app. To start creating custom build configurations right away using Android Studio, see Configure build variants. The build processThe build process involves many tools and processes that convert your project into an Android Application Package (APK) or Android App Bundle (AAB). The Android Gradle plugin does much of the build process for you, but it can be useful to understand certain aspects of the build process so you can adjust the build to meet your requirements. Different projects may have different build goals. For example, the build for a third-party library produces AAR or JAR libraries. However, an app is the most common type of project, and the build for an app project produces a debug or release APK or AAB of your app that you can deploy, test, or release to external users. This page focuses on app development, but many of the build steps and concepts are common to most build types. Android build glossaryGradle and the Android Gradle plugin help you configure the following aspects of your build: Build typesBuild types define certain properties that Gradle uses when building and packaging your app. Build types are typically configured for different stages of your development lifecycle. For example, the debug build type enables debug options and signs the app with the debug key, while the release build type may shrink, obfuscate, and sign your app with a release key for distribution. You must define at least one build type to build your app. Android Studio creates the debug and release build types by default. To start customizing packaging settings for your app, learn how to configure build types. Product flavors Product flavors represent different versions of your app that you can release to users, such as free and paid versions. You can customize product flavors to use different code and resources while sharing and reusing the parts that are common to all versions of your app. Product flavors are optional, and you must create them manually. To start creating different versions of your app, learn how to configure product flavors. Build variants A build variant is a cross-product of build type and product flavor and is the configuration Gradle uses to build your app. Using build variants, you can build the debug version of your product flavors during development and signed release versions of your product flavors for distribution. Although you don't configure build variants directly, you configure the build types and product flavors that form them. Creating additional build types or product flavors also creates additional build variants. To learn how to create and manage build variants, read the Configure build variants overview. Manifest entries You can specify values for some properties of the manifest file in the build variant configuration. These build values override the existing values in the manifest file. This is useful if you want to generate multiple variants of your app with a different application name, minimum SDK version, or target SDK version. When multiple manifests are present, the manifest merger tool merges manifest settings. Dependencies The build system manages project dependencies from your local file system and from remote repositories. This means you don't have to manually search, download, and copy binary packages of your dependencies into your project directory. To find out more, see Add build dependencies. Signing The build system lets you specify signing settings in the build configuration, and it can automatically sign your app during the build process. The build system signs the debug version with a default key and certificate using known credentials to avoid a password prompt at build time. The build system does not sign the release version unless you explicitly define a signing configuration for this build. If you don't have a release key, you can generate one as described in Sign your app. Signed release builds are required for distributing apps through most app stores. Code and resource shrinking The build system lets you specify a different ProGuard rules file for each build variant. When building your app, the build system applies the appropriate set of rules to shrink your code and resources using its built-in shrinking tools, such as R8. Shrinking your code and resources can help reduce your APK or AAB size. Multiple APK support The build system lets you automatically build different APKs that each contain only the code and resources needed for a specific screen density or Application Binary Interface (ABI). For more information see Build multiple APKs. However, releasing a single AAB is the recommended approach, as it offers splitting by language in addition to screen density and ABI, while avoiding the need to upload multiple artifacts to Google Play. All new apps submitted after August 2021 are required to use AABs.Build configuration filesCreating custom build configurations requires you to make changes to one or more build configuration files or build.gradle files. These plain-text files use Domain Specific Language (DSL) to describe and manipulate the build logic using either Groovy, which is a dynamic language for the Java Virtual Machine (JVM), or Kotlin script, which is a flavor of the Kotlin language. You don't need to know Groovy or Kotlin script to start configuring your build, because the Android Gradle plugin introduces most of the DSL elements you need. To learn more about the Android Gradle plugin DSL, read the DSL reference documentation for Groovy. Kotlin script also relies on the underlying Gradle Kotlin DSL. When starting a new project, Android Studio automatically creates some of these files for you, as shown in figure 1, and populates them based on sensible defaults. Figure 1. The default project structure for an Android app module.There are a few Gradle build configuration files that are part of the standard project structure for an Android app. Before you can start configuring your build, it's important to understand the scope and purpose of each of these files and the basic DSL elements they define. The Gradle settings fileThe settings.gradle file (for Groovy) or settings.gradle.kts file (for Kotlin script) is located in the root project directory. This settings file defines project-level repository settings and informs Gradle which modules it should include when building your app. Multi-module projects need to specify each module that should go into the final build. For most projects, the file looks like the following by default:
The top-level build fileThe top-level build.gradle file (for Groovy) or build.gradle.kts file (for Kotlin script) is located in the root project directory. It defines dependencies that apply to all modules in your project. By default, the top-level build file uses the plugins block to define the Gradle dependencies that are common to all modules in the project. In addition, the top-level build file contains code to clean your build directory. The following code sample describes the default settings and DSL elements in the top-level build.gradle file after creating a new project:
Configure project-wide propertiesFor Android projects that include multiple modules, it might be useful to define certain properties at the project level and share them across all the modules. You can do this by adding extra properties to the ext block in the top-level build.gradle file (for Groovy) or build.gradle.kts file (for Kotlin script):
To access these properties from a module in the same project, use the following syntax in the module's build.gradle file.
Note: Although Gradle lets you define project-wide properties at the module level, avoid doing so, because it causes the modules that share those properties to be coupled. Module coupling makes it more difficult to later export a module as a standalone project and prevents Gradle from using parallel project execution to speed up multi-module builds. The module-level build fileThe module-level build.gradle file (for Groovy) or build.gradle.kts (for Kotlin script) is located in each project/module/ directory. It lets you configure build settings for the specific module it is located in. Configuring these build settings lets you provide custom packaging options, such as additional build types and product flavors, and override settings in the main/ app manifest or top-level build.gradle or build.gradle.kts file. This sample Android app module build.gradle file outlines some of the basic DSL elements and settings:
Gradle properties filesGradle also includes two properties files, located in your root project directory, that you can use to specify settings for the Gradle build toolkit itself: gradle.properties This is where you can configure project-wide Gradle settings, such as the Gradle daemon's maximum heap size. For more information, see Build Environment. local.properties Configures local environment properties for the build system, including the following:
Remap the NDK to a shorter path (Windows only)In Windows, tools in the installed NDK folder, such as ld.exe, end up with long paths. The tools don't support long paths well. To create a shorter path, in local.properties, set the property ndk.symlinkdir to request that the Android Gradle plugin create a symlink to the NDK. The path of that symlink can be shorter than the existing NDK folder. For example, ndk.symlinkdir = C:\ will result in the following symlink: C:\ndk\19.0.5232133 Sync project with Gradle filesWhen you make changes to the build configuration files in your project, Android Studio requires that you sync your project files so that it can import your build configuration changes and run some checks to make sure your configuration doesn't create build errors. To sync your project files, click Sync Now in the notification bar that appears when you make a change, as shown in figure 2, or click Sync Project from the menu bar. If Android Studio finds any errors with your configuration—for example, your source code uses API features that are only available in an API level higher than your compileSdkVersion—the Messages window describes the issue. Figure 2. Sync the project with build configuration files in Android Studio.Source setsAndroid Studio logically groups source code and resources for each module into source sets. When you create a new module, Android Studio creates a main/ source set within the module. A module’s main/ source set includes the code and resources used by all its build variants. Additional source set directories are optional, and Android Studio doesn't automatically create them for you when you configure new build variants. However, creating source sets, similar to main/, helps organize files and resources that Gradle should only use when building certain versions of your app: src/main/ This source set includes code and resources common to all build variants. src/buildType/ Create this source set to include code and resources only for a specific build type. src/productFlavor/ Create this source set to include code and resources only for a specific product flavor.Note: If you configure your build to combine multiple product flavors, you can create source set directories for each combination of product flavors between the flavor dimensions: src/productFlavor1ProductFlavor2/. src/productFlavorBuildType/ Create this source set to include code and resources only for a specific build variant.For example, to generate the "fullDebug" version of your app, the build system merges code, settings, and resources from following source sets:
Note: When you create a new file or directory in Android Studio, use the File > New menu options to create it for a specific source set. The source sets you can choose from are based on your build configurations, and Android Studio automatically creates the required directories if they don't already exist. If different source sets contain different versions of the same file, Gradle uses the following priority order when deciding which file to use. Source sets on the left override the files and settings of source sets to the right: build variant > build type > product flavor > main source set > library dependencies This allows Gradle to use files that are specific to the build variant you are trying to build while reusing activities, application logic, and resources that are common to other versions of your app. When merging multiple manifests, Gradle uses the same priority order so each build variant can define different components or permissions in the final manifest. To learn more about creating custom source sets, read Create source sets. Other build systemsBuilding Android apps with Bazel is possible but not officially supported. Android Studio does not officially support Bazel projects. To better understand the current limitations of building with Bazel, see the known issues. |