文档章节

Gradle Build Variants for your android project

daveztong
 daveztong
发布于 2014/11/06 21:25
字数 532
阅读 57
收藏 0

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

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:

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.

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. 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 case a BASE_URL variable)
You can download the project on  Github. Here are two screenshots of the generated apps. Nothing really fancy:

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.

File structure

In the src folder, 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 staging version defines new icons while both flavors defines a Constants.java. The app name is defined in the string.xml files.

├── 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 in the Build variants tab of the IDE. Android Studio has some trouble identifying the resources for a non-active flavors.

We are using the production flavor, Studio does not understand that the staging folder 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

这种方式区分产品与开发环境实在是爽爆了!再也不用去手动改配置文件了!

© 著作权归作者所有

daveztong
粉丝 4
博文 93
码字总数 21482
作品 0
成都
程序员
私信 提问
The module is an Android project without build variants, and cannot be built

导入 安卓项目报错 Error:The module 'app' is an Android project without build variants, and cannot be built. Please fix the module's configuration in the build.gradle file and s......

之渊
2018/12/03
6.4K
0
Windows环境下Android Studio系列4—界面介绍

参考资料 [1] Android Studio常用功能介绍, http://ask.android-studio.org/?/article/23 [2] Windows环境下Android Studio系列1—下载与安装, http://my.oschina.net/1pei/blog/467210 [3] ......

一配
2015/06/23
1K
0
Android Studio, gradle and NDK integration

With the recent changes (release 0.7.3 around Dec 27), the new Android Build System starts to be really interesting also if you are using the NDK! Now this is really easy to int......

Jerikc
2015/05/28
1K
0
Android Gradle Build Tools 0.5.5

Android Gradle Build Tools 0.5.5 发布了,这是一个用 Gradle 来构建 Android 项目的工具,主要改进内容包括: Fix issue preventing to use Build Tools 18.0.1 access to the variants co......

打杂程序猿
2013/08/02
1K
1
Android Studio 3.3 Canary 发布,包含重要的 bug 修复

Android Studio 3.3 Canary 已发布。该版本带来了不少的新特性,还修复了一些重要的 bug,例如在打包 APK 时,Gradle 会消耗大量内存,从而导致出现 Java 堆栈溢出的错误。 新特性 The flag...

局长
2018/06/30
1K
1

没有更多内容

加载失败,请刷新页面

加载更多

360常用小工具分享 独立版 360驱动网卡版

有些朋友为了修复断网或者修复LSP,都会选择去下载360,用完再删除有些麻烦。所以自己收集了18个实用小工具独立版本方便坛友使用。只做分享,不喜勿喷。 360驱动网卡版:链接: https://pan.b...

xiaogg
3分钟前
1
0
批量删除表中某字段

select * from information_schema.columns where table_schema='erp-zyjsgx-v2' and column_name='manageflag'; alter table jz_xiangmudj drop column manageflag;......

glen_xu
5分钟前
1
0
JavaScript面试题总结系列(八)

8. JavaScript - this对象 this对象 this总是指向函数的直接调用者(而非间接调用者) 如果有new关键字,this指向new出来的那个对象 在事件中,this指向触发这个事件的对象,特殊的是,IE中的...

张森ZS
8分钟前
2
0
Apache Camel Spring Boot

Camel应用初始化 Apache Camel 采用的是组件化的设计思想,通过Camel Component对接第三方的应用,Camel核心模块会扫描classpath 加载这些Camel Component。 Camel应用在启动的过程中,需要将...

WillemJiang
9分钟前
2
0
数据管理必看!Kendo UI for jQuery过滤器状态保持

Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery、Kendo UI for Angular、Kendo UI Support for React和Kendo UI Support for Vue四个控件。Kendo UI for jQue......

FILA6666
22分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部