Detailed explanation of multiple APK instances with different package names packaged in one project of Android studio

Recently, the company has a special need to compile several different applications by slightly modifying the same set of code (such as different package names, different icons, different application names, etc.). Just finished reconstructing the project with as, consulted some materials on the Internet, and finally got it!! Record it here.

As mainly uses gradle to realize this requirement. The specific methods are as follows:

Modify the build.gradle file of the app

Suppose we compile two apps with the same set of code: app1 and app2

Notice, there's a pit here. We wrote it on it

After running, as will automatically generate @ string / APP_ Name, the content is like this

So, here's the problem. If you also define it in the strings.xml file of the app

There will be problems when compiling, because there are two apps_ Name, so we need to remove app strings.xml, and the compilation will be normal.

Note again that we can only configure the values of each version in the build.gradle file of the app, such as

If you configure other sub modules, there will be messy errors during compilation!!

If the sub module needs to configure the value, you can define the static variable in the public module. After the app module takes out the configuration value, set it to the static variable defined in the public module. In this way, each module can get it!!

Modify the androidmanifest.xml file

Note that the value of the icon attribute is ${app_icon}. Although it is red, it doesn't matter. We added this code in the build.gradle file:

The function here is that even if an error is reported in the project, the packaging will not stop.

Pack and sign apk

When we pack, two apps will appear:

If we choose two apps, as will pack two different apks for us. It's that simple!!

Pit filling

Today, I tried in the development project according to the above steps. Although different versions of APK can be packaged, they cannot be installed on the same device at the same time. Tips: XX has the same components. Then check whether the package names of the two apks are the same. As a result, the package names of the two apks are different. I checked this problem for a whole day. Sure enough, I found it because I defined a provider in the androidmanifest file:

Remember, the value of the authorities attribute of the provider tag must be unique. If the values of the authorities attribute of two apps are the same, it will prompt that the installation fails. Remember!!

How to view the package name:

Because the APK cannot be installed on the mobile phone at this time, we only have the APK package. At this time, we use the AAPT command (AAPT is a self-contained tool of SDK, in the SDK \ builds tools \ directory):

summary

The above is a detailed explanation of the APK examples of Android studio packaged with multiple package names introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>