Build Android Projects with Gradle

Source: Internet
Author: User
Tags jcenter

Gradle is a building tool based on the groovy language, built on the DSL syntax, which customizes the engineering build process through plug-ins. Google has developed an Android Gradle plugin that uses Gradle to build Android projects. Gradle build Android projects that we can use in Android Studio, in the command line, or in the persistence integration tool. Gradle the configuration name of the Android project is Build.gradle, which is stored in the project's root directory. An Android Project (project) consists of one or more components (module), and the engineering and components have separate build.gradle Android Engineering's Build.gradleIn general, we do not need to modify the Android engineering Build.gradle, engineering level Build.gradle define the basic configuration of Gradle. Gradle dependent Android Plugin and version number are declared in Buildscript->dependencies
Buildscript {
repositories {
Jcenter ()
}
dependencies {
Classpath ' com.android.tools.build:gradle:1.3.0 '

Note:do not place your application dependencies here; They belong
In the individual module Build.gradle files
}
}

allprojects {
repositories {
Jcenter ()
}
}
Build.gradle for Android componentsIn general, there are two types of Android components that we use, application and library
The following is an example of building a application Build.gradle
 Apply plugin: ' com.android.application ' 

android {
Buildtoolsversion "21.1.2"

Defaultconfig { BR clear= "None" > ApplicationID "COM.HAN.MYEXAMPLEAPP2"
Minsdkversion 8
Targetsdkversion 23
Versioncode 1
Versionname "1.0"
}
Buildtypes {
release {
minifyenabled true
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '
}
}
}

dependencies {
com Pile Filetree (dir: ' Libs ', include: [' *.jar '])
compile ' com.android.support:appcompat-v7:23.0.1 '
}
In the example above, Apply plugin: ' Com.android.application 'Indicates that Gradle is to be built using the Android plugin.
Define Android-related build options in android{}
compilesdkversion Indicates the version of the Android SDK to use
Buildtoolsversion represents the version of the compilation tool, and the compilation tool uses SDK Manager management (Android STUDIO->TOOLS->ANDROID->SDK Manager)
defaultconfig is used to configure basic build properties and Manifest (Androidmanifest.xml) properties. You can configure the properties as shown in the following table, and if the properties are configured in Defaultconfig, the settings in the corresponding androidmanifest.xml are overwritten.
The ApplicationID "COM.HAN.MYEXAMPLEAPP2" ApplicationID property is used to identify the unique identity of the Android application.
About the difference between ApplicationID and Androidmanifest.xml PackageName in Build.gradle, this article can be consulted
Https://chaosleong.gitbooks.io/gradle-for-android/content/appendix/applicationid_versus_packagename.html


The following table is the properties that Defaultconfig can configure:
Property Name Default value in DSL object Default value
Versioncode -1 Value from manifest if present
Versionname Null Value from manifest if present
Minsdkversion -1 Value from manifest if present
Targetsdkversion -1 Value from manifest if present
ApplicationID Null Value from manifest if present
Testapplicationid Null ApplicationID + ". Test"
Testinstrumentationrunner Null Android.test.InstrumentationTestRunner
Signingconfig Null Null
Proguardfile N/A (set only) N/A (set only)
Proguardfiles N/A (set only) N/A (set only)

buildtypes Configure how components are built and packaged, by default, Gradle builds two types: release and debug
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '
Proguardfiles declares the Proguard file used,
Getdefaultproguardfile (' Proguard-android.txt ') contains the default Proguard configuration.
In the ' Proguard-rules.pro ' file, we can add a custom Proguard configuration,
The ' Proguard-rules.pro ' file is in the component root directory.
minifyenabled Control gradle code obfuscation at build time, true for code obfuscation at build time

The following table is the properties that Buildtypes can configure
Property name Default values for debug Default values for Release/other
Debuggable True False
Jnidebuggable False False
Renderscriptdebuggable False False
Renderscriptoptimlevel 3 3
Applicationidsuffix Null Null
Versionnamesuffix Null Null
Signingconfig Android.signingConfigs.debug Null
Zipalignenabled False True
Minifyenabled False False
Proguardfile N/A (set only) N/A (set only)
Proguardfiles N/A (set only) N/A (set only)

The dependencies element declares the component dependencies to be built.
Compile Filetree (dir: ' Libs ', include: [' *.jar ']) indicates that all. jar files are dependent on the Libs directory
Compile ' com.android.support:appcompat-v7:23.0.1 ' identity dependent version is 23.0.1 's Android support library
dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Compile ' com.android.support:appcompat-v7:23.0.1 '
}
Examples of dependent SUPPORT-V4 libraries
Compile ' com.android.support:support-v4:23.0.1 '
Example of dependent library component The library path is the Lib folder under the project root directory
Compile project (": Lib")
  Signing ConfigurationDeclaring the signature configuration of Signingconfigs.release, using the signature in Buildtypes.release
Android {
Signingconfigs {
Release {
StoreFile file (' Release.keystore ')
}
}

    Buildtypes {
Release {
...
Signingconfig Signingconfigs.release
}
}
}
Product FlavorWhen the same app component needs to build two different versions, it needs to use product flavor, the following example declares two product Flavor,demo and full, two product flavor build different apps, the package name is " Com.buildsystemexample.app.demo "and" Com.buildsystemexample.app.full "
Android {
...
Defaultconfig {...}
Signingconfigs {...}
Buildtypes {...}
productflavors {
Demo {
ApplicationID "Com.buildsystemexample.app.demo"
Versionname "1.0-demo"
}
Full {
ApplicationID "Com.buildsystemexample.app.full"
Versionname "1.0-full"
}
}
}
Productflavors and Defaultconfig are the same type, that is, the configurable properties are the same. In real-world applications, the DEFAULTCONFIG configuration flavor the same properties, flavor different attributes are declared in their flavor. Build VariantProduct Flavor + Build type = Buildvariantgradle based on user-selected buildvariant, such as product flavor has Flavor1 and flavor2 two kinds, build type There are two types of debug and release, then there will be four variants of the build variant, namely Flavor1debug, Flavor1release, Flavor2debug, Flavor2relese if the project does not define product Flavor, Gradle will use the default flavor, and the build variant is the build type name. Source Files DirectorySrc/main default source Files directory src/<buildtype> build <buildType> use source file directory, optional src/<productflavor> build < The source file directory used for productflavor>, optional Build.gradle's relationship with Android studioRight-clicking on the component in Android Studio, as shown in Android studio, shows the component properties corresponding to Build.gradle and modifies the configuration in Android studio to automatically update to Build.gradle; Perform the buildThere are two ways to gradle the execution of a build 1. In Android Studio, select the Gradle tab, double-click Assemble<build_type&gt, and then do the build. For example in this article, under the app component, choose Assembledebug, Build the debug version of the APK select Assemblerelease, build the release version of APK2. On the command line, execute build in terminal, CD to the root of the project./gradlew:app:assembledebug, build the debug version of the app component. There may be multiple components under a project, so use the:< component name > to choose which component is built to build the entire project, execute./gradlew assembledebug perform Clean./gradlew clean View all Tasks
./gradlew Tasks

Build Android Projects with Gradle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.