Android Gradle One of the tricks: Build Variant Related

Source: Internet
Author: User

Build Variant

Android Gradle plugin that allows the final package to be combined in multiple dimensions.

BuildVariant = ProductFlavor x BuildType
Two dimensions

The most common is this:

    productFlavors {        pro {        }        fre {        }    }    lintOptions {        abortOnError false    }    buildTypes {        debug {        }        release {        }    }

Among them, Buildtypes General will have debug or release, marking the type of compilation, usually in the confusion of code, adjustable, resource compression to do some of the distinction.
Productflavor to meet "the same project, according to a very small distinction, to play different packages" this demand.

The combination of these two dimensions produces the following package:

    • Prodebug
    • Prorelease
    • Fredebug
    • Prorelease
More dimensions
    flavorDimensions ‘abi‘, ‘version‘    productFlavors {        pro {            dimension ‘version‘        }        fre {            dimension ‘version‘        }        arm {            dimension ‘abi‘        }        mips {            dimension ‘abi‘        }    }    buildTypes {        debug {        }        release {        }    }

Productflavor itself defines 2 dimensions, BuildType, then there are three dimensions that produce the following packages:

    • Armprodebug
    • Armprorelease
    • Armfredebug
    • Armfrerelease
    • Mipsprodebug
    • Mipsprorelease
    • Mipsfredebug
    • Mipsfrerelease

Each of these dimension combinations can set its own dependency, test source. Let's take an example.

Flavor and dependency demand

There are several flavors in the module, such as Fre and Pro, depending on the libraries, which are either local jar libraries or remote libraries.

Scheme

Traversing Build variant requirements

Bugtags's Android SDK, which has an automatic upload symbol table feature, was initially configured like this:

  apply plugin: ‘com.bugtags.library.plugin‘  bugtags {      appKey "APP_KEY"      appSecret  "APP_SECRET"      mappingUploadEnabled false  }

Later, we added a beta-live mechanism to differentiate between the test and the online app, so that there are two sets of App_key and App_secret in the same app, and it is obvious that the configuration above is not applicable.

Scheme

The Android Gradle plugin provides a android.applicationvariants index to traverse all the build variants
Later, we took a scenario that iterates through the Build Variant and sets extension information to be compatible with this requirement.

  afterEvaluate {      android.applicationVariants.each { variant ->          def bugtagsAppKey = null;          def bugtagsAppSecret = null;          if (variant.name.contains("debug")) {              bugtagsAppKey = ‘APP_KEY_BETA‘              bugtagsAppSecret = ‘APP_SECRET_BETA‘          } else if (variant.name.contains("release")) {              bugtagsAppKey = ‘APP_KEY_LIVE‘              bugtagsAppSecret = ‘APP_SECRET_LIVE‘          }          variant.ext.bugtagsAppKey = bugtagsAppKey          variant.ext.bugtagsAppSecret = bugtagsAppSecret      }  }  apply plugin: ‘com.bugtags.library.plugin‘
Summarize

This article mainly introduces the concept of build variant, and also introduces two daily application cases. Hope to be of help to everyone.

Resources

Android-build-tool

You got a problem? In the article under the message or add QQ Group: 453503476, hope to help you.

To receive the latest blog post in time, please pay attention to:

"Mobdev" Public number QR code

?

Android Gradle One of the tricks: Build Variant Related

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.