The Gradle For Android pack (2) through various channels and signature configuration “reprint please indicate the from silly child b_ mobile development (www.jianshu.com/users/d388b… Like can pay attention to me, not regular summary of the article! Your support is my motivation ha!
Gradle For Android
- Gradle For Android (1
Following Gradle Basics, how to configure multi-channel packaging and signing in Gradle
Google is known for being an app geek, so most of you haven’t seen the Google Play Store yet. Therefore, nowadays, there are many domestic application markets, such as Xiaomi, Huawei, 360, Baidu and so on, so we may need to release dozens of channel packages each time. Gradle offers productFlavors, which we can customize individually.
Oops, it went sideways. Gradle For Android, Gradle For Android, Gradle For Android, Gradle For Android, Gradle For Android
The premise
BuildTypes: BuildTypes. The Gradle component of AndroidStudio provides “debug” and “release” by default. BuildVariants: Each BuildType and flavor forms a Buildvariant
I. Multi-channel personalized customization
(I) Simple channel definition
You can add productFlavors to the Build. gradle, Android scope of the Application, with the following code:
Xiaomi {// Xiaomi channel} GooglePaly {// Google Play channel} Huawei {// Huawei channel} Baidu {// Baidu channel}}Copy the code
If there are no error cases, we will see the corresponding channel in Android Studio BuildVariant, as shown:
buildvariant.png
(2) Personality customization
Gradle components provide a number of convenient methods for multi-channel packaging. Developers can configure different APK packages for different channels, such as file names, package names, compile resources, and so on. Here’s an example:
1. Different channels compile different package names
// We can make a good decision about that. // We can make a good decision about that. } Googlepaly {// Google Play channel applicationId 'com.yuan. Agradle2 '}}Copy the code
When the package is finished, we print the package name to see the result:
package_google.png
package_xiaomi.png
2. Different channel compilations specify different resource types
From the first article, I’m not sure if I remember the sourceSets field, which allows you to customize the resource path for compilation. Can different channels compile different resources? The answer is yes, it must be! (Here I did not make demo examples, just notes)
Xiaomi {// xiaomi channel applicationId 'com.yuan. Agradle1 '// customization, Java.srcdirs = [' SRC /main1', SrcDirs = [' SRC /main1/ Java /'] res.srcdirs = [' SRC /main1/res'] assets1 = ['assets1']} Googlepaly {// Google Play channels ApplicationId 'com.yuan. Agradle2 '// the following specifies the compiled resource java.srcdirs = [' SRC /main2', 'src/main2/java/'] res.srcDirs = ['src/main2/res'] assets.srcDirs = ['assets2'] }Copy the code
3. Customize the package APK name
I’m not going to paste the two methods I wrote, but look at the demo
/ / modify output applicationVariants apk name. All {variant - > if (variant. BuildType. Name. Equals (" release ")) {variant. Outputs. Each { output -> def appName = 'AGradle' def oldFile = output.outputFile def buildName def releaseApkName variant.productFlavors.each { product -> buildName = product.name } releaseApkName = appName + getVersionByMainfest() + '-' + buildName + '-' + getNowTime() + '.apk' output.outputFile = new File(oldFile.parent, releaseApkName) } } }Copy the code
4. Unified configuration of channels
The productFlavors. All field can be used if the configuration is unified among different channels, for example:
productFlavors.all {
//...
}Copy the code
2. Signature Configuration
There are two signature modes: manual signature package and automatic signature. Here are two examples:
(1) Manual signature packaging
signed_1.png
Create a signature file and fill in the following:
signed_2.png
Using the file you just signed, enter your password for signature packaging:
signed_3.png
Choose packaging channels:
signed_4.png
Wait a while before you can see the result
signed_5.png
(2) Automatic signature
Build. Gradle: create a signature file. Create a signature file.
signingConfigs { debug { keyAlias 'yuan' keyPassword '12345678' storeFile file(".. /agradle.jks") StorePassword '12345678'} release {keyAlias 'yuan' keyPassword '12345678' storeFile file(".. /agradle.jks") storePassword '12345678' } }Copy the code
Hope to help some developers ~ specific view can be github demo, also welcome to join the development exchange group ha, details see personal profile. The next article is about gradle optimization and some tips for using it
DEMO
Gradle For Android (2) Multi-channel packaging and signing configuration
I write to you as you struggle on your way of growing up