Gradle packaging tutorial, gradle Packaging
Requirement: I developed an App and sold it to Company A (baidustore). At this time, Company B (lestore) came to the door and wanted to customize + additional function modules ......
Let's take a look at the differences between the two projects.
Company Name |
Baidustore |
Lestore |
Package name |
Com. baudustore. test |
Com. lestore. test |
Umeng Channel No. |
BAIDUSTORE |
LESTORE |
Application name |
AndroidDemoForBaidu |
AndroidDemoForLeStore |
Resources |
Res |
Res |
Version Number |
1.0 _ baidu |
1.0 _ lestore |
Signature |
Huangyanan. keystore |
Huangyn1.keystore |
Function |
Yes |
None |
Function B |
None |
Yes |
Functions such as CDEFG |
Yes |
Yes |
...... |
...... |
...... |
We can see that most of the functions are the same. We can do all of the functions first. In the future, we will use configuration items to create apps that meet the needs of Company B of Company, it can also be customized for CDE companies ......
Gradle can do this, but now there are two ways to know:
1. File Creation Method
2. You do not need to create a file. Use the manifestPlaceholders method of gradle.
Main steps:
1. Add Different signing
Open the project structure and select the signing tab to add different signature keys, such:
2. Add different packaging information
Open the flavors tab, add flavor, and add the package name, signature configuration, and version name, for example:
After the modification, open the gradle. build File and you can see the following content:
Android {signingConfigs {huangyanan {storeFile file ('/Users/huangyanan. keystore') keyAlias 'huangyanan 'keypassword' 111111 'storepassword' 111111 '} huangyn1 {storeFile file ('/Users/huangyanan/Documents/huangyn1.keystore ') keyAlias 'huangyn1 'keypassword '000000' storePassword '000000 '}}...... Omitted ...... ProductFlavors {baidustore {minSdkVersion 21 applicationId 'com. baudustore. test 'signingconfig signingConfigs. huangyanan targetSdkVersion 21 versionCode 1 versionname' 1. 0_baidu '} lestore {minSdkVersion 21 applicationId' test. lestore. test 'signingconfig signingConfigs. huangyn1 targetSdkVersion 21 versionCode 1 versionname' 1. 0_lestore '}}}...... Omitted ......
In this case, different package names correspond to the release packages of different signature keys.
3. Modify the umeng Channel Number:
Open the AndroidManifest. xml file and add the following code:
<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL}"/>
Open the build. gradle file of the app and add it to {} of lestore and baidustore in productFlavors.
manifestPlaceholders = [UMENG_CHANNEL: "LESTORE"]
manifestPlaceholders = [UMENG_CHANNEL: "BAIDUSTORE"]
Here the manifestPlaceholders method of gradle is used.
4. modify the application name and Resource
Create a folder with the same name as Flavor in the app/src directory, for example:
Put the resources we want to replace or modify according to the directory structure:
Here, the launcher_icon and app_name of the image are modified.
You can also use this method to modify the channel number to replace some node items in AndroidManifest. xml.
5. Packaging
First clean project, and then enter the command in the directory where the project is located
Mac and linux Input
./gradlew build
Windows
gradlew.bat build
After the execution is completed, you can view the packaged app in the app/build/outputs/apk directory.
6. Problems
Some people say that they can use the manifestPlaceholders method to replace the activity name. After trying it, they will not succeed.
Demo
Refer:
Http://tools.android.com/tech-docs/new-build-system/user-guide
Gradle Android Plug-In User Guide