How to play multi-pack name apk with Android Studio

Source: Internet
Author: User

Issue: Different distribution channels in the project may need to pack multiple apk (same code), the package name may not be the same, if a modified package name to re-coding the APK is very troublesome, you can refer to the following steps to manipulate Gradle on Android studio to package different package name APK.

The goal of the example here is that the same code, packaged into Com.example.android.newsreader and Com.example.android.newsreaderdev two different package names apk

1. First add the following code to the Android node in Build.gradle

productflavors{    flavors_release{        = [str: "Releasestr", Package_name: " Com.example.android.newsreader "]        " Com.example.android.newsreader "    }    flavors_dev{        = [str: "Devstr", Package_name: "Com.example.android.newsreaderdev"]        " Com.example.android.newsreaderdev "    }}
The functionality of this productflavors is provided for packaging parameterization, and Flavors_release and Flavors_dev can be considered as two sets of variables of their own definition, The actual operation can be customized to note that there is a manifestplaceholders parameter, which is actually a few parameter variables and values, here the parameters can be used in the way of ${name} in Androidmanifest.xml. Originally wanted in the androidmanifest in the manifest node will be the package to parameterize to achieve the purpose of playing different packages name of the APK, that is:
<Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "${package_name}"Android:versioncode= "1"Android:versionname= "1.0">    <USES-SDKandroid:minsdkversion= "One"android:targetsdkversion= "+" />...

As a result, Android Studio Lint automatically gave me a hint:


Description Android Studio recommended that I use Set ApplicationID to parameterize the package name so I added ApplicationID settings in Flavors_release and Flavors_dev, namely: and

Let's see if it works.

2. This step requires manipulating the Gradle panel

At this point we click on the button refresh in the Gradle panel on the right side of Android studio, and the following items appear in the Build sub-column:


Where the assemble* starts with the APK option, the APK is generated in build/outputs/apk, where we can double-click to generate the corresponding APK

The apk corresponding to flavors_release and Flavors_dev should be the same as these two, then double-click them to wait for their own apk generation process

3. View the contents of the project panel below

Opening the project panel, we found that there were really several apk:


It can be seen that each double-click on one of the Gradle will generate three apk, we put newsreader-flavors_dev-debug.apk and Newsreader-flavors_ RELEASE-DEBUG.APK Take it out and check their package name with the AAPT command (command for AAPT dump badging {filename.apk})



Sure enough, the package name is not the same. So if you can install it at the same time, install the two apk in the simulator, which is sure to be possible:

Can see that there are two newsreader, in fact, are identical, but the package name is different

4. How to specify a signature when packaging

In the Android Studio menu bar Enter Build->generate signed APK:


After specifying your KeyStore, click Next:


Here will be found in the flavors there are two items, that is, we gradle inside the configuration of the two flavors parameter set, each selected, click Finish to generate the desired apk

5. There is a question, why modify the ApplicationID can modify the package name? What's the relationship between ApplicationID and PackageName?

In fact, the package represents the bundle name in the Java code. ApplicationID represents a unique identity in your app. and application signatures are used together to differentiate from other apps. I think that's why the Google market allows different applicationid to be used in the same way. The last hint: after packaging, it is best to thoroughly test before publishing, in order to avoid problems

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

How to play multi-pack name apk with Android Studio

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.