【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_URL variable)

You can download the project on Github.

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

演示样例项目非常easy。在不同的版本号中我们须要改动项目名称。项目图标,一些常量:url...,项目能够从Github 下载,效果图例如以下:

Android App drawer showing the two differents flavors

Android App drawer showing the two differents flavors

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,同一时候须要注意productionstaging。它们须要与后面的文件夹结构名字一致

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中配置productionstaging同样的文件夹,分别放入相应的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.

Android Studio IDE showing different build variants

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

The app drawer shows the two icons:

Android App drawer showing the two differents flavors

References





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/4734552.html,如需转载请自行联系原作者

相关文章
|
Android开发
IDEA编译gradle提示This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
IDEA编译gradle提示This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.3.1 or newer.
645 1
|
2月前
|
Java Linux 开发工具
Azkaban【部署 01】Linux环境 CentOS Linux release 7.5.1804安装配置azkaban-3.70.0编译阶段(附安装包及gradle-4.6资源)
【2月更文挑战第4天】Linux环境 CentOS Linux release 7.5.1804安装配置azkaban-3.70.0编译阶段(附安装包及gradle-4.6资源)
29 1
|
Java 开发工具 Android开发
【错误记录】Android Studio 编译报错 ( Invalid Gradle JDK configuration found )
【错误记录】Android Studio 编译报错 ( Invalid Gradle JDK configuration found )
614 0
【错误记录】Android Studio 编译报错 ( Invalid Gradle JDK configuration found )
|
4月前
|
开发工具 Android开发 开发者
Android 项目编译 Gradle 配置说明
Android 项目编译 Gradle 配置说明
142 0
|
6月前
|
Java 数据库连接 API
Gradle依赖管理:编译时和运行时依赖的区别
Gradle依赖管理:编译时和运行时依赖的区别
42 0
|
7月前
|
Java 开发工具 Maven
Android 编译 gradle 内存 OOM 解决之路(二)
Android 编译 gradle 内存 OOM 解决之路
|
7月前
|
Java Android开发
Android 编译 gradle 内存 OOM 解决之路(一)
Android 编译 gradle 内存 OOM 解决之路
|
Android开发
Android Studio 3.6.1设置gradle的离线模式(Offline Mode)
Android Studio 3.6.1设置gradle的离线模式(Offline Mode)
|
Java API Android开发
通过自定义Gradle插件修改编译后的class文件
通过自定义Gradle插件修改编译后的class文件
通过自定义Gradle插件修改编译后的class文件
|
Java Maven Android开发
使用idea和gradle编译spring5源码的方法步骤
使用idea和gradle编译spring5源码的方法步骤
使用idea和gradle编译spring5源码的方法步骤