[Andrioid] generate different versions at a time in Gradle, dynamically set the application title, application icon, replace constant, andrioidgradle

Source: Internet
Author: User

[Andrioid] generate different versions at a time in Gradle, dynamically set the application title, application icon, replace constant, andrioidgradle

When writing a project, you may encounter the following situations:
1. You need to generate an apk for the test version and the official version.
2. The Beta and official versions have different URLs.
3. The package names of the test version and the official version must be inconsistent so that they can be installed on the same mobile phone.
4. Different apk APPs must have different names, different icons, and different constants ....
If you have the above requirements, read this article.

When developing an app, you usually have extends slightly different versions of this app. The most common example is probably the backend you want to use: production or staging.

During development, we often need to generate multiple versions of the app. The most common ones are beta and official versions.

You usually define the base URLs with the other constants of the app. Switching from one environment to the other is done by (un) commenting the right lines:

We often need to define some constants in the application. When the application is officially released, we often comment out the test part and release the formal part, as shown below:

public static String BASE_URL = "http://staging.tamere.be"//public static String BASE_URL = "http://production.tamere.be"

The process is manual, boring and error prone but hey, changing one line is not that bad, is it?

Then you add more and more features that depends on the environment. You maybe want a different icon and then different input validation rules and then...

That's where your build tool can help. Let's see how we can automate the process of generating different APKs for different environment with Gradle.

The above steps are boring and tasteless. It's okay to modify a place. When the code is too much to write, it's just like egg_pain. At this time, Gragle will be on the stage.

Build variants

Gradle has the concepts of Build Types and Build Flavors. When combining the two, you get a Build Variant.

There two default build types: release and debug. We won't change them in our example but we will create two new flavors: production and staging.

Gradle has two versions by default: release and debug. Here we have added the production and staging. combinations are the following four versions.

As a result, we'll have four different variants:

  • ProductionDebug
  • ProductionRelease
  • StagingDebug
  • StagingRelease
Sample project

The project is pretty simple but shows how you can define for each build variant:

  • An app name
  • An icon
  • Constants (in our caseBASE_URLVariable)

You can download the project on Github.

Here are two screenshots of the generated apps. Nothing really fancy:

The example project is very simple. In different versions, we need to modify the project name, project icon, and some constants: url.... The project can be downloaded from Github, as shown below:

Build. gradle file
buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath 'com.android.tools.build:gradle:0.5.+'    }}apply plugin: 'android'repositories {    mavenCentral()}android {    compileSdkVersion 18    buildToolsVersion "18.0.1"    defaultConfig {        minSdkVersion 15        targetSdkVersion 18    }    productFlavors {        production {            packageName "be.tamere.gradlebuildtypesexample"        }        staging {            packageName "be.tamere.gradlebuildtypesexample.staging"        }    }}dependencies {    compile 'com.android.support:appcompat-v7:18.0.0'}

The definition of the flavors is super simple, all the magic will happen in their folders.

The modification is simple: the Core configuration is productFlavors. At the same time, you must pay attention to production and staging. They must be consistent with the subsequent directory structure names.

File structure modified File structure

InsrcFolder, we 've created two directories whose names must match the flavors. We will define all the flavor-specific values. Only specific values are necessary.

The modification is also relatively simple. Under src (main directory at the same level), create a directory with the same production and staging as configured in productFlavors above, and put the corresponding Constants. java respectively. The attributes and functions of these two directories are the same as those of the default main directory.

Summary:

1. Two directories, production and staging, respectively store one copy of Constants. java.

2. Configure the application icon and Application name under the res directory. This topic re-configures ic_launcher.png and string. xml for staging. If the production function is not configured, use the res under the default main. This is easier to understand.

The staging version defines new icons while both flavors definesConstants.java. The app name is defined instring.xmlFiles.

├── main│   ├── AndroidManifest.xml│   ├── ic_launcher-web.png│   ├── java│   │   └── be│   │       └── tamere│   │           └── gradlebuildtypesexample│   │               └── MainActivity.java│   └── res│       ├── drawable-hdpi│       │   └── ic_launcher.png│       ├── drawable-mdpi│       │   └── ic_launcher.png│       ├── drawable-xhdpi│       │   └── ic_launcher.png│       ├── drawable-xxhdpi│       │   └── ic_launcher.png│       ├── layout│       │   └── activity_main.xml│       ├── menu│       │   └── main.xml│       ├── values│       │   ├── dimens.xml│       │   ├── strings.xml│       │   └── styles.xml│       ├── values-v11│       │   └── styles.xml│       └── values-v14│           └── styles.xml├── production│   └── java│       └── be│           └── tamere│               └── gradlebuildtypesexample│                   └── Constants.java└── staging    ├── java    │   └── be    │       └── tamere    │           └── gradlebuildtypesexample    │               └── Constants.java    └── res        ├── drawable-hdpi        │   └── ic_launcher.png        ├── drawable-mdpi        │   └── ic_launcher.png        ├── drawable-xhdpi        │   └── ic_launcher.png        ├── drawable-xxhdpi        │   └── ic_launcher.png        └── values            └── string.xml
Android Studio

You can switch between the two flavors inBuild variantsTab of the IDE. Android Studio has some trouble identifying the resources for a non-active flavors.

We are usingproductionFlavor, Studio does not understand thatstagingFolder contains source code. Don't worry, it's normal, it will catch up when you switch to the staging variant.

Launch the app with the different flavors to see the result.

The app drawer shows the two icons:

References
  • Http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors
  • Xav's answer on this Stack overflow topic is special helpful.
Address: http://tulipemoutarde.be/2013/10/06/gradle-build-variants-for-your-android-project.html

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.