Current location - Quotes Website - Signature design - How to package multiple apk at the same time with android studio?
How to package multiple apk at the same time with android studio?
Problem: Different distribution channels in the project may need to package multiple apk (same code), and the package names may be different. If it is troublesome to modify the package names one by one and re-edit apk, you can refer to the following steps, manipulate Gradle on Android Studio and package APK with different package names.

The goal of the example here is to package the same code into two apks with different package names: com.example.android.newsreader and com.example.android.newsreader dev.

1. First, add the following code to the android node of build.gradle

Product flavor {

Taste _ release {

manifest placeholders =[str:" release str ",package _ name:" com . example . Android . news reader "]

application id " com . example . Android . news reader "

}

Taste _ development {

manifest placeholders =[str:" devStr ",package _ name:" com . example . Android . newsreaderdev "]

application id " com . example . Android . newsreaderdev "

}

}

The function of productFlavors is parametric packaging, and flavors_release and flavors_dev can be regarded as two sets of variables defined by themselves, which can be customized in actual operation.

Note that there is a parameter of manifestPlaceholders, which actually provides several parameter variables and values. All the parameters here can be used in AndroidManifest.xml by means of ${name}. Originally, I wanted to parameterize the packages in the node of the manifest in AndroidManifest to achieve the purpose of playing apk with different package names, namely:

& lt listing xmlns: Android = "/apk/res/Android"

Package = "$ {package name}"

android:versionCode=" 1 "

Android:version name = " 1.0 " & gt;

& lt purpose-SDK Android: minsdkversion = "11"Android: targetsdversion = "14"/>

...

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

Displaying Android Studio recommended me to parameterize the package name by setting the applicationId.

Therefore, I added the setting of applicationId in flavors_release and flavors_dev, namely:

and

Let's see if it works.

2. This step requires operating the Gradle panel.

At this point, we click the button in the gradle panel on the right side of Android Studio to refresh, so that the following contents will appear in the build sub-column:

Items starting with assemble* are all options for generating apk, and the generated apk is in build/outputs/apk. Here, we can double-click any item to generate the corresponding apk.

The apk corresponding to flavors_release and flavors_dev should be AND, so double-click them to wait for their respective processes to generate apk.

3. Check the contents in the project panel below.

When we opened the project panel, we found that there were actually several apks:

As you can see, every time you double-click one of gradle, three apks will be generated. Let's take out newsreader-flavors _ dev-debug.apk and newsreader-flavors _ release-debug.apk, and check their package names with the aapt command (the command is aapt dump badging {filename.apk}).

Sure enough, the package name is different.

So can I install it at the same time? Here, you can install two apks in the simulator:

You can see that there are two news readers, exactly the same, but with different package names.

4. How to specify the signature when packaging?

Enter Build-& gt;; In the menu bar of Android Studio. Generate signature Apk:

Specify your keystore, and then click Next:

Here, you will find two items in flavors, namely, two sets of Flavors parameters that we configured in gradle. Select them separately and click Finish to generate the required apk.

5. There is a question, why can I modify the package name by modifying the applicationId? What is the relationship between applicationID and packagename?

In fact, package represents the package name in java code. The applicationId represents a unique identification in the application. Used with application signature to distinguish it from other applications. I think this is why Google market can allow the same application to have different application IDs.

One last tip: after packaging, it is best to conduct a comprehensive test before release to avoid problems.