写项目的时候常常会遇到下面的情况:
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_URL variable)

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 18buildToolsVersion "18.0.1"defaultConfig {minSdkVersion 15targetSdkVersion 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.
原文地址:http://tulipemoutarde.be/2013/10/06/gradle-build-variants-for-your-android-project.html

转载于:https://www.cnblogs.com/mfrbuaa/p/4734552.html

【Andrioid】在Gradle编译时生成一个不同的版本号,动态设置应用程序标题,应用程序图标,更换常数...相关推荐

  1. apt 根据注解,编译时生成代码

    apt: @Retention后面的值,设置的为CLASS,说明就是编译时动态处理的.一般这类注解会在编译的时候,根据注解标识,动态生成一些类或者生成一些xml都可以,在运行时期,这类注解是没有的~~ ...

  2. Android中使用AbstractProcessor在编译时生成代码

    Android中使用AbstractProcessor在编译时生成代码 发现这边不错的文章,忍不住转了过来,转自: http://blog.csdn.net/industriously/article ...

  3. Eclipse使用gradle编译时,使用固定的jdk版本进行编译(修改gradle的jdk编译版本)

    很多时候我们使用多个版本的jdk,比如说,用的是1.7 ,开发环境用1.8 那么经常碰到编译后的包是1.8版本的,虽然设置了1.7的,因为很多时候我们使用打包工具了. 1.7和1.8有个问题就是1.8 ...

  4. gradle 指定java版本_Eclipse使用gradle编译时,使用固定的jdk版本进行编译(修改gradle的jdk编译版本)...

    很多时候我们使用多个版本的jdk,比如说,用的是1.7 ,开发环境用1.8 那么经常碰到编译后的包是1.8版本的,虽然设置了1.7的,因为很多时候我们使用打包工具了. 1.7和1.8有个问题就是1.8 ...

  5. gradle 失败 编译项目_maven常见问题处理(3-3)Gradle编译时下载依赖失败解决方法...

    Gradle编译时在本地仓库中如果没有发现依赖,就会从远程仓库中下载, 默认的远程仓库为 mavenCentral(),即 http://repo1.maven.org/maven2/往往访问速度特别 ...

  6. maven常见问题处理(3-3)Gradle编译时下载依赖失败解决方法

    maven常见问题处理(3-3)Gradle编译时下载依赖失败解决方法 参考文章: (1)maven常见问题处理(3-3)Gradle编译时下载依赖失败解决方法 (2)https://www.cnbl ...

  7. Flutter编译时生成代码之 code_builder

    前言 之前学习原生开发的时候使用过各种编译自动生成模板代码的框架,例如ARouter,这些框架其实是借助了JavaPoet 这个框架来自动生成代码的,JavaPoet 可以在编译自动生成模板代码,在f ...

  8. linux内核编译的image,内核编译时生成uImage的办法

    有一个很简单的办法u-boot编译结束时,会在tool文件夹下面生成一个mkimage文件,将这个文件复制到交叉编译器目录下的bin文件夹下面,以后编译时就会生成uImage文件,省的用命令行的方式转 ...

  9. 匿名内部类编译时生成多个class文件

    由于线上代码出问题,需要增量上线.就遇到了以下问题:个性一个内部类后重新挂载对应的class文件,结果发现无效. 增量上线就是把你更改的java文件在本地编译成为.class文件,然后直接将.clas ...

  10. 关于Unity使用gradle编译时出现ABI with prefix: mips64el-linux-android的解决方案

    gradle编译报错如下: FAILURE: Build failed with an exception. * What went wrong: A problem occurred configu ...

最新文章

  1. Jira停售本地版,已购企业最大化降低损失的几种方法
  2. python编程免费教程电子书_python编程从入门到实践PDF电子书教程免费下载
  3. JVM系列之:详解java object对象在heap中的结构
  4. SAP Spartacus里的HTTPErrorInterceptor的单元测试设计原理
  5. 公积金10万元,为何却不能贷款买房?
  6. NDK/JNI01--NDK下载配置
  7. SpringMVC+Spring3+Hibernate4
  8. Python入门--流程控制语句continue
  9. SpringMVC+MyBatis整合——事务管理
  10. 电信私有云 oracle,行业观察:Oracle私有云升级
  11. MIS系统开发利器,实施、维护人员自定义报表的福音,AgileEAS.NET SOA平台动态报表指南...
  12. svn删除文件出错的经验总结
  13. 外盘琪貨正大国际:别放弃勤奋,再难,别丢掉善良
  14. 论文阅读笔记《Low-shot Visual Recognition by Shrinking and Hallucinating Features》
  15. 隐私空间伪装计算机,隐私空间app(文件夹隐藏) 6.1.9 免root
  16. IDEA 2018.3.6 修改背景图片
  17. springtask Cron表达式与fixedDelay的区别
  18. ant不是内部命令也不是外部命令问题的解决方法
  19. android 短信 8859 1,短信接口
  20. 利息积数的计算方法及应用

热门文章

  1. Linux笔记之shell script
  2. RHEL脚本更换YUM源(sohu)
  3. 企业如何选择最佳的SSL
  4. Android Wear缺少本质上的创新
  5. 【实用插件】精心挑选6款优秀的 jQuery Tooltip 插件
  6. 令人魂牵梦绕的香格里拉
  7. html 5拜年贺卡,HTML5+CSS3实现春节贺卡
  8. linux7 vnc 黑屏,51CTO博客-专业IT技术博客创作平台-技术成就梦想
  9. python3.6 asyncio paramiko_网工的Python之路:Concurrent.Futures
  10. 柱状图多维条形图vue_使用D3.js+Vue实现一个简单的柱形图