Skip to content

Backelite/AndroidVariantSelectorGradlePlugin

Repository files navigation

AndroidVariantSelector Gradle Plugin

A plugin for Gradle that aims to simplify dynamic build of android variants. The plugin's task reads a BUILD_TYPE and a FLAVOR variable to determine the variant to build.

It also can move and/or rename your generated apk.

Why ?

The Android plugin for Gradle lets you build different variants of an application using dynamically generated tasks (like assembleDebug and assembleRelease for example). However, you need to know those tasks in advance to build a variant.
Consequently, when building your application via a script, you need to hardcode the variant you want to build in your script. You would need as many scripts as the number of variants you are building.
This is especially problematic when using a continuous integration server to build your application. On Jenkins, you would need to create a job for each build variant.

This plugin allows to select dynamically which variant you want to build in your script by using input variables. On Jenkins for example, you simply need to add parameters to your job. Each build can then be parameterized to build the variant you need.

In order to easily retrieve the application you built, the plugin also gives you the opportunity to move the generated apk in the directory of your choice (without touching the original apk created by Android).

The last feature lets you prefix the generated apk name with the date and hour of the build, to be able to identify your application more easily.

Usage

Apply the plugin in your build.gradle and configure it if needed, using the 'androidVariantSelector' closure :

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.backelite:android-variantSelector-gradle-plugin:1.0'
  }
}

apply plugin: 'com.backelite.android.variantSelector'

androidVariantSelector {
  moveOutputEnabled true
  outputDirectoryPath "myDeliveryFolder"
  renameOutputEnabled false
}

You can then use the plugin with the gradle command line :

$ gradle assembleVariant -DBUILD_TYPE=myBuildType -DFLAVOR=myFlavor

Parameters

The assembleVariant task can take two properties :
BUILD_TYPE :
The gradle build type of the variant to assemble.
FLAVOR :
The gradle flavor of the variant to assemble.
This property is optional if you don’t have any flavor in your application.

You can customize the following parameters in your build.gradle, using the 'androidVariantSelector' closure :
renameOutputEnabled :
Specifies if the final apk will be renamed. Currently, the rename function simply prefixes the apk name with the build date.
The default value is false.
moveOutputEnabled :
Specifies if we need to move the generated apk outside of the defaut build directory.
The default value is false.
outputDirectoryPath :
This variable is used only if moveOutputEnabled is true.
It specifies the directory in which we will copy the final apk. The path is relative to the project build directory.

Example with Jenkins

The plugin can easily be used with Jenkins to allow non-technical users to build your application, with the right build type and flavor.
To do this, you need to :

  1. Configure the job to retrieve your project via your favorite version control system

  2. Add two choice parameters to the job :
    Jenkins Choice Parameters

  3. Add a gradle build step that uses the assembleVariant task
    Jenkins Gradle Task

  4. That's it, you can now start your build and choose the parameters

Contributing

Contributions for bug fixing or improvements are welcomed. Feel free to submit a pull request.

Licence

AndroidVariantSelector is available under the MIT license. See the LICENSE file for more info.

Analytics

About

Gradle plugin that helps building android variants dynamically

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages