Android: a variety of items are packaged

Scenario: work often encounter situations of a project requires a variety of packaging. Changing only a few parameters, or replace a few pictures, you need to re-make a package. If only a two, but fortunately, once or twice, if there is such a demand frequently, think about the child skull hurt ......
To solve this problem, the use of gradle comes productFlavors tools to achieve multi-channel, multi app pack way .

What productFlavors that?

Is the literal translation of product characteristics, the role is similar, with different characteristics to generate the same set of source products.
There is no need to go into the other, in short, like a few points to remember:

  1. This is gradle own tools, configured in build.gradle years, mainly for multi-channel package;
  2. Can be configured very many parameters, applicationId, versionCode, versionName, signature, resource files, constants, code files, and so on.
  3. Select Configure when packaging.

Simple to use

If only a few constants need to be modified, then two steps on the line:
The first step: add the following code in the module build.gradle under android.

    flavorDimensions "default"
    productFlavors {
        oneApk {
            buildConfigField "String", "myString", '"第一个apk的文本"'  
            buildConfigField "int", "myint", '1'
            buildConfigField "boolean", "myboolean", 'false'
        }
        twoApk {
            buildConfigField "String", "myString", '"第二个apk的文本"'  
            buildConfigField "int", "myint", '2'
            buildConfigField "boolean", "myboolean", 'true'
        }
        threeApk {
            buildConfigField "String", "myString", '"第三个apk的文本"'  
            buildConfigField "int", "myint", '3'
            buildConfigField "boolean", "myboolean", 'false'
        }
    }

Step Two: After clean recompiling the code can be used directly in the

Log.e("mystring",BuildConfig.myString);

Build Variants find bar at the bottom left of Android Studio before packing, select the corresponding Build Variant pack.

Use higher-order

Modify applicationId, versionCode, versionName, signature ......

Modification part of the code line on the basis of the above, each of a corresponding set of productFlavors.
In order to be able to copy and paste use, that I replicate about it!

 	flavorDimensions "default"
    productFlavors {
        oneApk {
            applicationId "com.xx.xxone"
            versionCode 1
            versionName "1.1"
            signingConfig signingConfigs.oneRelease
            
            //....
        }

        twoApk {
            applicationId "com.xx.xxtwo"
            versionCode 2
            versionName "1.2"
            signingConfig signingConfigs.twoRelease

            //....
        }

        threeApk {
            applicationId "com.xx.xxthree"
            versionCode 3
            versionName "1.3"
            signingConfig signingConfigs.threeRelease

             //....
        }
    }
    // 因为多了签名,所以还得多一个标签,如下:
    signingConfigs {
        oneRelease {
            storeFile file("xx/xx/one.jks") // 签名文件路径
            storePassword "123456"
            keyAlias "one"
            keyPassword "123456"
        }
        twoRelease {
            storeFile file("xx/xx/two.jks")
            storePassword "123456"
            keyAlias "two"
            keyPassword "123456"
        }
        threeRelease {
            storeFile file("xx/xx/three.jks")
            storePassword "123456"
            keyAlias "three"
            keyPassword "123456"
        }
    }

If you modify the signature is not successful, or use the default signature, then the next buildTypes signingConfig set to null.

 buildTypes {
        release {
            signingConfig null
            //... 省略其它
        }
        debug {
            signingConfig null
             //... 省略其它
        }
    }

other

More channels, it is easy to mess packed, so when packaged distinguish between apk file name.

Added buiild.gradle outermost label Method

static def releaseTime() {
    return new Date().format("yyyy-MM-dd-HH.mm", TimeZone.getTimeZone("GMT+8"))
}

android under the label:

    applicationVariants.all {
        variant ->
            variant.outputs.all {
                outputFileName = "${variant.productFlavors[0].name}-v${variant.productFlavors[0].versionName}-${releaseTime()}.apk"
            }
    }
Published 70 original articles · won praise 176 · views 310 000 +

Guess you like

Origin blog.csdn.net/zheng_weichao/article/details/85300917