Android Studio's Gradle Automated build package

Source: Internet
Author: User

Most of the work I have learned in the last year is that the test is always asking me to test the package, I am really complain unbearable. Today there is a work gap, and some accidents have seen similar posts, just in this work Gap time realized window run Gradle script automatically build Android project.

First, the development environment:

System: Win7

Gradle Version: 4.2

Android studio:2.2.2

The configuration fields for Gradle under android{} are described below:

defaultconfig{} default configuration, is the Productflavor type. It is shared with other productflavor to use    
sourcesets{} Source file directory settings, is the Androidsourceset type.
buildtypes{} buildtype type
signingconfigs{} signature configuration, Signingconfig type
productflavors{} Product style configuration, Productflavor type
testoptions{} Test configuration, testoptions type
aaptoptions{} aapt configuration, aaptoptions type
lintoptions{} Lint configuration, lintoptions type
dexoptions{} dex configuration, dexoptions type
compileoptions{} Compile configuration, compileoptions type
packagingoptions{} packagingoptions type
jacoco{} jacocoextension type. For setting the Jacoco version
splits{} splits type

The script build works as follows:

About Gradle Configuration compilation apk related signature is as follows:  

Android {    Signingconfigs {        release {            storefile file (// absolute path            Storepassword "12345678            " "Alias"            "12345678"}}    }

  Build Type (the test debug package is continually submitted to the tester for testing during development)

The following code is for reference only:

buildtypes {release {minifyenabledfalseshrinkresourcesfalsezipalignenabledfalseproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' proguard-rules.txt '} debug {minifyenabledfalseshrinkresourcesfalsezipalignenabledfalse} custom {minifyenabledfalseshrinkresourcesfalsezipalignenabledfalse} sourcesets {main {jni.srcdirs= []            }        }    }

  Multi-channel packaging to build multiple types of different channels apk

The different channels here generate the corresponding APK based on the build type, which corresponds to the number of channel types * Build compilation type number equals the number of compiled apk.

    /* Multi-Channel package */     productflavors{        tencent{            "com.domin.armn            "            "1.0" "1.2.0" = [AppName: "Tencent"]        }                baidu{            "com.domin.armn"            "1.0              "1.2.0"            = [appName: "Baidu"]        }    }             

  Custom generated APK package name

Example: module_flavor-version-time-buildtype.apk

Under the Build.gradle root directory under the app, declare the following:

// Get current Time def getcurrenttime () {    returnnew Date (). Format ("Yyyy-mm-dd", Timezone.gettimezone (" UTC "))}

In the android{} directory, declare the following: 

/*apk Rename*/Android.applicationVariants.all {Variant-Variant.outputs.each {Output-def OutputFile=Output.outputfileif(OutputFile! =NULL&& outputFile.name.endsWith ('. apk ')) {def buildtype=Variant.buildType.name//here to modify the APK filename, in the format module_flavor-version-time-buildtype.apkdef fileName = "Gsafety_${variant.productflavors[0].name}-v${defaultconfig.versionname}-${getcurrenttime ()}-${ buildtype}.apk "Output.outputfile=NewFile (Outputfile.parent, FileName)}} }

Advanced Configuration Items

All of the above settings do not fully satisfy the automated compilation, still require the user to manually enter the command to run the compilation APK package, which is certainly not our most wanted results.

Gradle executes the following commands:     

Note: Before using the compile command, make sure that Gradle has been added to the environment variable, and if not, you can use the./gradlew instead of the Gradle (only available under the project root directory)

1 compile all productflavor and corresponding apk for all buildtype.
$gradle Assemble//Perform only the task set required for project packaging
$gradle Build//Execute task set required for project packaging, and perform automated tests, so it will be slower
If the current project contains more than one module, executing gradle assemble at the project root compiles all the module

2 Compiling the APK for the specified Productflavor and BuildType
$gradle Assemble[productflavor][buildtype]
If a parameter is missing, all of the configuration for that parameter is compiled, that is, if you run Gradle Assembleflavor, the apk that flavor all BuildType is compiled .
For example:
$gradle Assemble
$gradle assembleflavorrelease
$gradle Assembleflavordebug
Note: Gradle supports command abbreviations, the above two commands can also be written in the following format
$gradle a
$gradle
$gradle AR
$gradle Assflavorr
$gradle AD
$gradle Assflavord

Create a specified folder under the project root (for compiling a run script), for example,. config, and then in that directory distinguish between creating a. bat file (a batch suffix file on Win7) and signing files

The batch files are as follows BUILD.bat:

Note: This may appear in the cmd window in Chinese garbled, this is due to the convenience of the problem, does not affect the compilation.

#快速编译打包apk脚本echo  " Package start ************************** "1-E" ************************** package complete * "#桌面右上角弹出通知notify-send build.sh" package complete!

To create a signature file Sign.txt:

The signature file is placed outside here in order to better protect the signature file, as little as possible to other people know < security considerations >.

Android {    Signingconfigs {        release {            storefile file (// absolute path            Storepassword "12345678            " "Alias"            "12345678"}}    }

Apply the signature file to android{} under the app's Build.gradle file:

// Loading signature information    File ConfigFile = File ('.. /.config/sign.txt ')    if  (configfile.exists ()) {        = configfile.newreader (). ReadLine (). Trim ()        ' lucher, Path: ' + signgradlepath        if  (file ( Signgradlepath). Exists ()) {            apply From:signgradlepath        }    }

Finally, the developer benefits, on the window machine directly click on the. bat file to execute Batch command, you can get the corresponding APK package in the project build directory under the APK directory.

    

Android Studio's Gradle Automated build package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.