Build Android Project with Gradle

Source: Internet
Author: User

Now with the Friend Alliance such statistical tools in the project integration, according to different channels to play different APK package also become one of the daily work. God horse? You're still manually changing the channel number one by one pack?! Then you must be a very diligent programmer--! Just trying to use Android studio, a little bit more about the build tool used under Android Studio: Gradle.

What is Gradle

You can take a look at the Gradle homepage .

Simply put, Gradle is an automated compilation deployment testing tool. Gradle content is very wide, there is a special book to introduce Gradle. and Android Studio uses a subset of the features in Gradle.

Build.gradle

Build.gradle is the Gradle file used by Android Studio, here's a simple example of build.gradle:

Buildscript {    repositories {        mavencentral ()    }    dependencies {        classpath ' com.android.tools.build:gradle:0.6.+ '    }}apply plugin: ' Android ' Android {    compilesdkversion 17}

Buildscript {...} to configure the compiled code, this part is basically not how to edit;

Apply plugin: ' Android ' is indicated with Android plugin;

Android {...} is the configuration of the Android plugin. Specific configuration and values can be seen here

Build Task

A task is used in Gradle to represent a series of operations, somewhat similar to the target in makefile. The common tasks are:

    • Assemble
    • Build
    • Check
    • Clean

The task function is basically known by name, where build = assemble + check.

Executing a task is directly executing gradle [task] at the command line, for example: Gradle assemble

Gradle Assemblerelease

Build Parameter Configuration

The parameters in the Gradle script are DSL specifications, that is, the use of a Java package name is the same way:

A.B.C = 1a {    b {      c 1            }}

The above two methods represent a meaning. Parameters that can be configured are not detailed here, please refer to http://tools.android.com/tech-docs/new-build-system/user-guide.

Here are a few of the main parameters to use.

Build Types

Todo

Signing Configurations

When you build the APK, one step is to sign the APK with a key, and the configuration items for the signature are all in Signingconfigs:

Signingconfigs {    myconfig {        storefile file ("Path-to-keystore")        Storepassword "******"        keyalias "* * * "        Keypassword" ****** "    }}
Then reference in the compilation type:
Buildtypes {    Release {        ...        Signingconfig signingconfigs.myconfig    }}
So the signature configuration is good. However, as configured above, there is a problem: Build.gradle is often submitted to the code management system, and obviously put key plaintext in the code base is not very safe (especially if you write an open source project). An alternative is to enter a password, or store the password in an environment variable, see this link: http://stackoverflow.com/questio ... k-file-using-gradle
... signingconfigs {    release {        storefile file (system.getenv ("KEYSTORE"))        Storepassword system.getenv (" Keystore_password ")        keyalias system.getenv (" Key_alias ")        Keypassword system.getenv (" Key_password ")    }}

Product Flavors

If you have a project that wants to generate different APK packages with different package names or different resources, then this is the time to use product flavors.

Android {    ...    .. productflavors {        Flavor1 {            packagename "Com.example.flavor1"            versioncode        }        flavor2 {            PackageName "Com.example.flavor2"            minsdkversion        }}    }

The above example generates two different APK packages with different package names and attributes. Product flavors the properties that can be set inside the reference http://tools.android.com/tech-docs/new-build-system/user-guide.

Friend Alliance multi-channel packaging

Seeing so much above, now come an example: Write a build.gradle to complete the Friend Alliance multi-channel packaging. The AU channel differentiation is achieved through the <meta-data> in Androidmanifest.xml:

<meta-data android:value= "Umeng_channel_value" android:name= "Umeng_channel"/>
So the basic idea is to replace the string in the Androidmanifest.xml with a different APK package when packing. Build.gradle key sections are as follows:
Android {compilesdkversion buildtoolsversion ' 19.0.0 ' defaultconfig {minsdkversion 7 targetsdk Version 19}//signature information exists in the environment variable Signingconfigs {release {StoreFile file (system.getenv ("KEYSTORE            ")) Storepassword system.getenv (" Keystore_password ") Keyalias system.getenv (" Key_alias ")            Keypassword system.getenv ("Keystore_password")}} buildtypes {release {Runproguard true Proguardfile getdefaultproguardfile (' proguard-android-optimize.txt ') signingconfig signingConfigs.re        Lease//Use the above signature information}} productflavors {defaultflavor {proguardfile ' proguard-rules.txt ' }//need to play different versions Xiaomi {} LeTV {}}}//The following code is excerpted from <a href= "https://github.com/ Umeng/umeng-muti-channel-build-tool/tree/master/gradle "target=" _blank "style=" color:rgb (0, 136, 204); Text-decoration:none; " >https://github.com/umeng/umeng-muti-channel-build-tool/tree/master/gradle</a>//implementation with Productflavor name replacement Androidmanifest.xml            Umeng_channel_value string Android.applicationVariants.all {variant-variant.processmanifest.dolast{copy{            From ("${builddir}/manifests") {include "${variant.dirname}/androidmanifest.xml"} Into ("${builddir}/manifests/$variant. Name") filter{String Line, Line.replaceall ("U Meng_channel_value "," ${variant.productflavors[0].name} ")} Variant.processResources.manifestFile = File ("${builddir}/manifests/${variant.name}/${variant.dirname}/androidmanifest.xml")}}}

After modifying Build.gradle, you need to synchronize your engineering settings with the Sync project with Gradle files feature in Android Studio. After that, enter Gradle build (Windows is Gradlew build) in Android Studio terminal, and wait a moment, all the generated apk is under the build/apk of your project!

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.