Use Gradle to dynamically set the value of <meta-data> variables in Android resvalue/buildconfig/manifes at compile time

Source: Internet
Author: User

Reprint please indicate source: http://blog.csdn.net/xx326664162/article/details/49247815 article from: Shire's Blog

You can also view my other similar articles. It will give you a certain receipt.

about using Gradle to control the version number and generate different version numbers of the code. I have summed up three articles, which are more fragmented on the Internet. I was studying this before. It is not known that there is such a method at all. So it's not scary to say that I don't know what I don't know. I believe these three articles will give you a lot of inspiration

Gradle Build Control log switch--buildconfig\ own definition

Use Gradle to dynamically set the value of the < meta-data> variable in Android resvalue/buildconfig/manifes at compile time

Gradle compilation generates a different version number. Dynamic Settings app title/app icon/replace constant

Preface

In the project, we sometimes need to use different project variables depending on the project environment,

Example:

    • The server address that is required to access the app in a test environment is a test server
    • The official release of the app requires an interview with the official server.

The following three technologies can be used to meet the requirements:

use Gradle to dynamically set up Android Resvalue at compile time

The way to change res value. For example, change the value of AppName in the Strings.xml file

In your Gradle file buildtypes or productflavors below, such as release body written similar:

"string""AppName""app1"

Change the value of string value named AppName to App1

use Gradle to dynamically set up Android buildconfig at compile time

In your Gradle file buildtypes or productflavors below, such as release body written similar:

"String""ENDPOINT""\"http://example.com\""

Gradle sync, Buildconfig.endpoint will be assigned to http://example.com to be able to call the Java code.

Here's how to set a variable in a tag, how to use a variable in a tag, see here

use Gradle to dynamically set up Android Manifest at compile timeMethod One: (not recommended)

In the previous version number of the Gradle, most of the idea is to define a variable in the androidmanifest. Then, at compile time, find the variable in the androidmanifest and replace it.

For example, in the original Androidmanifest.xml file, you would add a multi-channel package:

<meta-data android:value="UMENG_CHANNEL_VALUE"               android:name="UMENG_CHANNEL"/>

Then use the Build.gradle to find the value of Umeng_channel_value, and replace

Android.applicationvariants.all{Variant-println"${variant.productflavors[0].name}"variant.processmanifest.dolast{copy{from ("${builddir}/manifests") {include"${variant.dirname}/androidmanifest.xml"} into ("${builddir}/manifests/$variant. Name") filter{String Line->line.replaceall ("Umeng_channel_value","${variant.productflavors[0].name}")} Variant.processResources.manifestFile = File ("${builddir}/manifests/${variant.name}/${variant.dirname}/androidmanifest.xml" )        }   }}
Method Two: Use Manifestplaceholders to change < meta-data> (recommended)

In the new version number of the Gradle, processmanifest This method is removed, and provides a new function manifestplaceholders. We are able to define a variable in androidmanifest. It is very convenient to replace it dynamically in Build.gradle. The syntax is also very simple.

In contrast to the above function, we need to dynamically replace the appkey of the Friend Alliance, we need to define a variable in androidmanifest

<meta-data         android:name="UMENG_APPKEY"         android:value="${umeng_app_key}"/>

Next, we are based on different environments in the Build.gradle file. Create a different Appkey apk.

buildTypes {        debug {         "你替代的内容"]        }        release {     "你替代的内容"]        }        develop {    "你替代的内容"]        }    }

Note: Here's "what you substituted for". cannot be a special keyword, for example: TRUE, otherwise the value in Meta-data is not obtained in Java code.

Sample Error Demo:

manifestPlaceholders = [oem: "TRUE"]

Executes the Gralde clean build. You will be able to generate different Appkey apk, is not feeling much better. ^ ^

Suppose you want to replace multiple variables, if you need to replace two variables, you need to follow the following form

<meta-data         android:name="UMENG_APPKEY"         android:value="${umeng_app_key}"/><meta-data          android:name="UMENG_SECRET"    android:value="${umeng_app_secret}"/>
buildTypes {        debug {    "你替代的内容",umeng_app_secret:"你要替换的内容"]        }        ...    }

How to get the value of the < meta-data> element in manifest in Android

References
http://hong1024.com/2015/02/10/%E4%BD%BF%E7%94%A8manifestPlaceholders%E6%9B%BF%E6%8D%A2%E5%8F%98%E9%87%8F/
http://write.blog.csdn.net/mdeditor#!postId=49247815

Use Gradle to dynamically set the value of &lt;meta-data&gt; variables in Android resvalue/buildconfig/manifes at compile time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.