Xiamen Tour the first gradle multi-channel packaging (Dynamic settings app name, app icon, background image, status bar color)

Source: Internet
Author: User

I am not a poet, can't write the beauty of Xiamen, I am not a singer, can't sing the sentiment of Xiamen, I am not a painter, can't draw the scenery of Xiamen.

I am just a programmer, I can only use my eyes, my heart to see to feel, the long-lost sea breeze blowing on the cheek, as if the heart of a part of irritability and heavy also with the sea breeze drifting to a distance ...

One, Gradle multi-channel packaging reasons

Time goes back two years, when I was still using eclipse development tools. Brother Division has a product that needs to be packed (packaged into an app using WebView) and puts forward the relevant requirements:

    • For different business addresses (webview load URL), app name, app icon, welcome page background, status bar color (background same color) are different, and version number is different.

    • For different businesses, they must be packaged as standalone apps without overwriting the installation.

For the above requirements each packaging needs to replace the app name, app icon, background and other resources, you must also modify the package name (resulting in a series of errors). Later put forward the upgrade function, but also to the resources, package name changes back, a toss really want to die. Then I simply each time the new project packaging, business logic does not change only the resources have changed, the code is copied one after another, and a lot of businesses, maintenance is really looking for the most, in addition to the customer proposed new features, I need to repeat every business! Oh, God, who's going to save me?

Second, the road of self-salvation

In the subsequent contact AndroidStudio (hereinafter referred to as), gradually transferred to the AS development, indeed eclipse faster and more efficient and convenient. The gradle build function is really very powerful, the default is debug, release version, we can do different configurations for different versions, this does not just solve the problem I have encountered. The gradle nodes are provided with productFlavors different configurations for different versions.

For example, there are "Four seasons easy to buy" "Humanity Home" "Library Tesco" business needs packaging.

I reconfigure this property under each branch so that the applicationId package name of the packaged apk is different, avoiding overwriting the installation.

Of course, for different business app name, the icon is not the same. At this point we need to configure the manifestPlaceholders property, which manifestPlaceholders is a similar HashMap container, so manifestPlaceholders you can configure multiple properties. We can configure this as follows:

Gradle configuration:

Androidmanifest configuration:

Note: the configuration of the node Meta-data.

${icon}${app_name}a value that refers to manifestPlaceholders icon the container app_name .

Customer needs not only change the app name and icon, but also need to modify the webview loading address, welcome page background image, etc., specific configuration such as:

Note: The new configuration also needs to add nodes to the manifest file meta-data .

        <meta-data            android:name="welcome_bg"            android:value="${welcome_bg}"/>        <meta-data            android:name="tint_color"            android:value="${tint_color}"/>        <meta-data            android:name="load_url"            android:value="${load_url}"/>

We can get the meta-data value under the node through Java code android:value .

In general, MyApplication get the value of the node:

ApplicationInfo info = null;try {info = this. Getpackagemanager(). Getapplicationinfo(Getpackagename (), Packagemanager. GET_meta_data);int Tintcolor = info. MetaData. GetInt("Tint_color");String Loadurl=info. MetaData. getString("Load_url");String Welcomepath = info. MetaData. getString("WELCOME_BG");} catch (Packagemanager. Namenotfoundexceptione) {E. Printstacktrace();}

Note: Set welcome_bg:"@mipmap/klg_welcome" , I'm int welcomeRes = info.metaData.getInt("welcome_bg"); going to take the value here, return 0 each time, but tint_color the value is normal. @mipmap/klg_welcomethe value returned by normal thinking int , then I getString actually get the value: res/mipmap-hdpi-v4/sjyg_welcome.png . Gets the path value, the background of the picture resource cannot be used directly.

Then we convert the path resources into int resources, and here we use the reflection of Java. The specific code is as follows:

Class C = R. Mipmap. Class;field[] Fields = C. GetFields();for (field Field:fields) {if (field. GetName(). Equals(Welcomepath. Substring(Welcomepath. LastIndexOf("/") +1, Welcomepath. LastIndexOf("."))) {This. Welcomebgres= (int) field. Get(c. newinstance());             Break;}    }

Intercept path string Gets the name of the picture, based on "name" in the "R.mipmap.class" class, and finds the value of the property.

This allows us to save different configuration information to the MyApplication middle and call it directly when we use it.

There is also a way to easily get the data from the gradle configuration file in Java code:

By code: context.getResources().getString(R.string.key); Gets the value.

Note In this way strings.xml , the file cannot have a property with the same name key . Otherwise, the same resource reference error will be reported.

Third, build apk

If you want to generate packages for all channels:

The terminal command enters the following instruction:

gradlew assembleRelease

Build success in the following directory:

If you want to package for a channel:

Way One:

Configure different channel base information.

Way two:

Change applicationId , add a manifestPlaceholders collection.

Multi-channel packaging, do not need to change Baidu, gold and other third-party key .

Multi-channel packaging, to solve the past deep sorrow.

Source Address

Xiamen Tour first Gradle multi-channel packaging (Dynamic settings app name, app icon, background image, status bar color)

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.