【Andrioid】在Gradle中編譯一次產生不同的版本,動態設定應用標題,應用表徵圖,替換常量,andrioidgradle

來源:互聯網
上載者:User

【Andrioid】在Gradle中編譯一次產生不同的版本,動態設定應用標題,應用表徵圖,替換常量,andrioidgradle

寫項目的時候經常會遇到以下的情況:
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:

樣本項目很簡單,在不同的版本中我們需要修改項目名稱,項目表徵圖,一些常量: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.

修改很簡單:核心配置是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。這個比較容易理解哈。

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.