【Andrioid】在Gradle编译时生成一个不同的版本号,动态设置应用程序标题,应用程序图标,更换常数
写项目的时候常常会遇到下面的情况:
1.须要生成測试版本号和正式版本号的apk
2.測试版本号和正式版本号的URL是不一样的
3.測试版本号和正式版本号的包名须要不一致,这样才干安装到同一部手机上面。
4.不同apk须要应用名不同,图标不同,某些常量不同....
假设你有以上的需求。看这篇文章就对了
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.
当我们做开发的时候。常常会须要生成多个版本号的app。
最常见的就是測试版和正式版。
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.
上面的步骤是烦躁。无味的,改动一个地方还好。代码写多了以后正过来整过去就egg_pain了,这个时候我们Gragle就闪亮登场了
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默认有release和debug两个版本号。
我们这里添加了production 和staging.两两组合就是以下4个版本号了。
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_URLvariable)
You can download the project on Github.
Here are two screenshots of the generated apps. Nothing really fancy:
演示样例项目非常easy。在不同的版本号中我们须要改动项目名称。项目图标,一些常量:url...,项目能够从Github 下载,效果图例如以下:


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.
改动非常easy:核心配置是productFlavors,同一时候须要注意production和staging。它们须要与后面的文件夹结构名字一致
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.
改动也比較简单,在src以下(main同级的文件夹)新建和上面productFlavors中配置production和staging同样的文件夹,分别放入相应的Constants.java。这两个文件夹的属性和功能与系统默认创建的main是一样的。
总结一下就是:
1.production和staging两个文件夹,相应着各存放一份Constants.java
2.对于应用图标和应用名字信息配置在res文件夹以下的。这个地方针对staging又一次配置了一份ic_launcher.png和string.xml。production没配置的话就是用默认main以下的res。
这个比較easy理解哈。
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
- http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors
- Xav's answer on this Stack
overflow topic is particularly helpful.
【Andrioid】在Gradle编译时生成一个不同的版本号,动态设置应用程序标题,应用程序图标,更换常数的更多相关文章
- apt 根据注解,编译时生成代码
apt: @Retention后面的值,设置的为CLASS,说明就是编译时动态处理的.一般这类注解会在编译的时候,根据注解标识,动态生成一些类或者生成一些xml都可以,在运行时期,这类注解是没有的~~ ...
- .net postsharp编译时生成的代码?
使用PostSharp进行AOP框架设计:一个简单的原型 AOP已经不是一个什么新名词了,在博客园使用关键字搜索可以查出n多条关于AOP的介绍,这里就不再赘述了. 在Bruce Zhang's B ...
- maven常见问题处理(3-3)Gradle编译时下载依赖失败解决方法
Gradle编译时在本地仓库中如果没有发现依赖,就会从远程仓库中下载, 默认的远程仓库为 mavenCentral(),即 http://repo1.maven.org/maven2/往往访问速度特别 ...
- Android Studio Gradle编译时『No resource found that matches the given name』解决方法(windows系统的坑)
* 最近帮团队同事配置gradle时,发现一个非常奇怪的问题:> * 同样的gradle配置的项目,只是修改了一个项目的名称,竟然会出现以下奇怪问题: ## 现象1. 一个编译完全OK,另外一个 ...
- qmake.exe是在Qt安装编译时生成的,里面内嵌了Qt相关的一些路径(最简单的方法是保持一样的安装路径,最方便的办法是设置qt.conf文件)
在网上直接下载别人编译好的Qt库,为自己使用省了不少事.但往往也会遇到些问题,其中Qt version is not properly installed,please run make instal ...
- 解决Gradle编译时出现: 编码GBK的不可映射字符
解决Gradle编译时出现: 编码GBK的不可映射字符 在build.gradle文件中加入如下内容: [compileJava, compileTestJava]*.options*.encodin ...
- Gradle 编译时选择不同的 google-services.json
在做的安卓应用需要在 debug 和 release build中使用不同的谷歌服务账号,要用到不同的google-serivces.json ,手动替换的话太费时费力,好在万能的gradle可以完成 ...
- Gradle编译时下载依赖失败解决方法
如果Gradle在编译的时候没有在本地仓库中发现依赖,就会从远程仓库中下载,默认的远程仓库为mavenCentral(),也就是http://repo1.maven.org/maven2/,但是往往访 ...
- Android 打造编译时注解解析框架 这只是一个开始
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43452969 ,本文出自:[张鸿洋的博客] 1.概述 记得很久以前,写过几篇博客 ...
随机推荐
- hdu1151 Air Raid,DAG图的最小路径覆盖
点击打开链接 有向无环图的最小路径覆盖 = 顶点数- 最大匹配 #include <queue> #include <cstdio> #include <cstring& ...
- CentOS 7 命令备忘录
1 查看目录下有什么文件/目录 >ls //list 列出目录文件信息 >ls -l 或ll //以“详细信息”查看目录文件 >ls -a //-all 查看目录“全部”(包含隐藏文 ...
- SVN的switch命令
语法就不说了,文档有的是,主要是两个常用的用法: . 切换资源库(svn sw --relocate) [plain] view plaincopy svn sw --relocate <fro ...
- coco2d-x CCScrollView实现背包翻页,仅供参考
#include "CCCGameScrollView.h" USING_NS_CC; USING_NS_CC_EXT; CCCGameScrollView::CCCGameScr ...
- 灰度图像阈值化分割常见方法总结及VC实现
转载地址:http://blog.csdn.net/likezhaobin/article/details/6915755 在图像处理领域,二值图像运算量小,并且能够体现图像的关键特征,因此被广泛使用 ...
- java实现简单web服务器(分析+源代码)
在日常的开发中,我们用过很多开源的web服务器,例如tomcat.apache等等.现在我们自己实现一个简单的web服务器,基本的功能就是用户点击要访问的资源,服务器将资源发送到客户端的浏览器.为了简 ...
- Android画图监听接口OnPreDrawListener具体解释
public static interface ViewTreeObserver.OnPreDrawListener 我们先看下API中的定义: 类概述: 为即将绘制视图树时运行的回调函数定义的接口. ...
- 彻底理解Javascript原型继承
彻底理解Javascript原型继承 之前写过一篇Javascript继承主题的文章,这篇文章作为一篇读书笔记,分析的不够深入. 本文试图进一步思考,争取彻底理解Javascript继承原理 实例成员 ...
- c语言移位操作
应该先看看C语言是指所有的位二进制算术位计算.即使输入的是十进制的数,在存储器存储为二进制形式. “<<”使用方法: 的格式是:a<<m,a和m式,要求m>=0. 功能: ...
- iOS设备定位
一.iOS谈到定位 1.SignInSignOutViewController.h @interface SignInSignOutViewController : UIViewController& ...