Original Android Packaged Gradle pack

Source: Internet
Author: User

Having recently tried Android's Gradle package, it has been noted that it is much more convenient than ant packaging.

Note: Android Gradle now plug-in version is already 0.14.3, for some old methods and APIs, some have been removed, can not be used (http://tools.android.com/tech-docs/ new-build-system/migrating-to-1-0-0), the following properties have been renamed, and the original is not available:

Runproguard, Applicationidjnidebugbuild,   Jnidebuggablerenderscriptdebug,renderscriptdebuggablerenderscriptsupportmode , Renderscriptsupportmodeenabledrenderscriptndkmode  Renderscriptndkmodeenabledvariant.packageapplication/zipalign/createzipaligntask/outputfile/ Processresources/processmanifest using Variant.out instead

1. Convert Eclipse Project to Gradle project:

In project, right-->export->generate Gradle build files, so you'll have to make a file out of project:

  

├──build.gradle├──gradle│└──wrapper│     ├──gradle-wrapper.jar│     └──gradle-WRAPPER.PR Operties├──gradlew├──gradlew.bat

Gradlew and Gradlew.bat are scripts that can be executed under *nix and under Windows, gradle-wrapper.properties to specify the version of Gradle that needs to be downloaded, as follows:

Ten :distributionbase=gradle_user_homedistributionpath =wrapper/distszipstorebase=gradle_user_homezipstorepath=wrapper/  Distsdistributionurl=http\://services.gradle.org/distributions/gradle-2.2.1-all.zip

Build.gradle is the file used to configure compilation options, with a compile script that runs successfully:

Import Java.text.DateFormatimport java.text.SimpleDateFormatimport java.util.regex.Patternbuildscript { repositories {mavencentral ()}//specifying the Gradle plug-in versionDependencies {classpath'com.android.tools.build:gradle:0.14.+'}}apply Plugin:'Android'//Add Android Dependency LibsDependencies {compile filetree (dir:'Libs', include: ['*.jar'])}//Get system Timedef buildtime () {defDF= New SimpleDateFormat ("yyyyMMdd '-' HHmm")       DF. Settimezone (Timezone.getdefault ()) returnDF. Format (New Date ())}def Apkname="Androidgradle"Android {compilesdkversion -buildtoolsversion"20.0.0"Sourcesets {main {manifest.srcfile'Androidmanifest.xml'Java.srcdirs= ['src'] Resources.srcdirs= ['src'] Aidl.srcdirs= ['src'] Renderscript.srcdirs= ['src'] Res.srcdirs= ['Res'] Assets.srcdirs= ['Assets']        }        //Move The tests to Tests/java, tests/res, etc ...Instrumenttest.setroot ('tests')    }        //SignatureSigningconfigs {release {storefilefile('Release.keystore') Storepassword'123456'Keyalias'Demo'Keypassword'111111'        }    }        //Channelproductflavors { Free{ApplicationID'Com.clarck.httpclientnew.free'} premium {ApplicationID'Com.clarck.httpclientnew.premium'        }    }        //Lint Checklintoptions {abortonerrorfalse} buildtypes {release {//Blur SwitchMinifyenabledtrue            //whether the zip is alignedZipalignenabledtrue            //whether to open the debuggable switchDebuggablefalse            //whether to open the jnidebuggable switchJnidebuggablefalse            //Confusing configuration FilesProguardfiles Getdefaultproguardfile ('Proguard-android.txt')            //Signature Configurationsigningconfig Signingconfigs.release} android.applicationVariants.all {Variant 
    -def Manifestfile=file("Androidmanifest.xml") def pattern= Pattern.compile ("versionname=\ "(. +) \"") def Manifesttext=Manifestfile.gettext () def Matcher=Pattern.matcher (Manifesttext) matcher.Find() def versionname= Matcher.group (1) Pattern= Pattern.compile ("versioncode=\ "(. +) \"") Matcher=Pattern.matcher (Manifesttext) matcher.Find() def Versioncode= Matcher.group (1)                                    if(variant.buildType.zipAlignEnabled) {variant.outputs.each {output-def OutputFile=Output.outputfileif(OutputFile! =NULL&& OutputFile.name.endsWith ('. apk') {def fileName="apk/"+ ApplicationID +"- v"+ Versionname +"-"+ Versioncode +"-"+ Variant.name +"-"+ buildtime () +". apk"Output.outputfile=New File (Outputfile.parent, FileName)}} } Else{variant.outputs.each {output-def OutputFile=Output.outputfileif(OutputFile! =NULL&& OutputFile.name.endsWith ('. apk') {def fileName="apk/"+ ApplicationID +"- v"+ Versionname +"-"+ Versioncode +"-"+ Variant.name +"-"+ buildtime () +"-unaligned.apk"Output.outputfile=New File (Outputfile.parent, FileName)}} }          }    }}

The final execution of the./gradlew build is packaged, and the following results indicate a successful package:

:p reBuild:compileFreeDebugNdk:preFreeDebugBuild:checkFreeDebugManifest:prepareFreeDebugDependencies: CompileFreeDebugAidl:compileFreeDebugRenderscript:generateFreeDebugBuildConfig:generateFreeDebugAssets up-to-DATE:mergeFreeDebugAssets:generateFreeDebugResValues up-to-DATE:generateFreeDebugResources:mergeFreeDebugResources:processFreeDebugManifest:processFreeDebugResources : GenerateFreeDebugSources:compileFreeDebugJava:preDexFreeDebug:dexFreeDebug:processFreeDebugJavaRes up-to-DATE:VALIDATEDEBUGSIGNING:PACKAGEFREEDEBUG:ZIPALIGNFREEDEBUG:ASSEMBLEFREEDEBUG:COMPILEPREMIUMDEBUGNDK: PrePremiumDebugBuild:checkPremiumDebugManifest:preparePremiumDebugDependencies:compilePremiumDebugAidl: CompilePremiumDebugRenderscript:generatePremiumDebugBuildConfig:generatePremiumDebugAssets up-to-DATE:mergePremiumDebugAssets:generatePremiumDebugResValues up-to-DATE:generatePremiumDebugResources:mergePremiumDebugResources:processPremiumDebugManifest: ProcessPremiumDebugResources:generatePremiumDebugSources:compilePremiumDebugJava:preDexPremiumDebug: Dexpremiumdebug:processpremiumdebugjavares up-to-DATE:packagePremiumDebug:zipalignPremiumDebug:assemblePremiumDebug:assembleDebug:preFreeReleaseBuild: CheckFreeReleaseManifest:prepareFreeReleaseDependencies:compileFreeReleaseAidl:compileFreeReleaseRenderscript: Generatefreereleasebuildconfig:generatefreereleaseassets up-to-DATE:mergeFreeReleaseAssets:generateFreeReleaseResValues up-to-DATE:generateFreeReleaseResources:mergeFreeReleaseResources:processFreeReleaseManifest: ProcessFreeReleaseResources:generateFreeReleaseSources:compileFreeReleaseJava:lintVitalFreeRelease: CompileFreeReleaseNdk:proguardFreeRelease:dexFreeRelease:processFreeReleaseJavaRes up-to-DATE:validateReleaseSigning:packageFreeRelease:zipalignFreeRelease:assembleFreeRelease: PrePremiumReleaseBuild:checkPremiumReleaseManifest:preparePremiumReleaseDependencies:compilePremiumReleaseAidl : CompilePremiumReleaseRenderscript:generatePremiumReleaseBuildConfig:generatePremiumReleaseAssets up-to-DATE:mergePremiumReleaseAssets:generatePremiumReleaseResValues up-to-DATE:generatePremiumReleaseResources:mergePremiumReleaseResources:processPremiumReleaseManifest: ProcessPremiumReleaseResources:generatePremiumReleaseSources:compilePremiumReleaseJava:lintVitalPremiumRelease SKIPPED:compilePremiumReleaseNdk:proguardPremiumRelease:dexPremiumRelease:processPremiumReleaseJavaRes up-to-DATE:packagePremiumRelease:zipalignPremiumRelease:assemblePremiumRelease:assembleRelease:assemble: Compilelint:lintran Lint on variant premiumrelease:5issues Foundran Lint on variant freedebug:5issues Foundran Lint on variant premiumdebug:5issues Foundran Lint on variant freerelease:5issues foundwrote HTML report tofile:/data/gitwork/autobuildproject/build/outputs/lint-results.htmlwrote XML report to/data/gitwork/autobuildproject/build/outputs/lint-Results.xml:check:buildBUILD successfultotal Time:41.892secs

Original Android Packaged Gradle pack

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.