Build Android Projects with Gradle (cont.)

Source: Internet
Author: User

A few months ago, I had written a blog about building Android projects using Gradle http://blog.isming.me/2014/05/20/android4gradle/, The article already describes how to use Gradle for project building and for Google to recommend the use of Gradle. At that time the Android Gradle plug-in is 0.11.0, now the plug-in version is 0.14.3, for some of the old methods and APIs, some have been removed, can not be used. It is therefore necessary to write another blog about these removed parts and alternatives. At the same time, because of personal knowledge, there are not some skills introduced at the time, other functions, will also be introduced in this article.

Incompatible places compared to the previous article

Have not read my other article, suggest to look.

The following properties have been renamed, and the original is not available:

Runproguard-minifyenabled (whether confused)
Zipalign-zipalignenabled (whether zip-aligned)
PackageName-ApplicationID
Jnidebugbuild-> jnidebuggable
Renderscriptdebug->renderscriptdebuggable
Renderscriptsupportmode->renderscriptsupportmodeenabled
Renderscriptndkmode->renderscriptndkmodeenabled
variant.packageapplication/zipalign/createzipaligntask/outputfile/processresources/ Processmanifest use Variant.out Instead, use, look at the code behind

These are removed to replace, in the latest version of the Gradle plug-in, has no hint of outdated, direct error, please be vigilant!!!!

New featuresMultidexenabled Multi-Dex SupportShrinkresources Removing unused resourcesSupports defining values for buildconfig values and res, such as:
Applicationvariants.all {Variant    -variant.buildconfigfield "int", "VALUE", "1"    variant.resvalue "string" "," name "," value "}

can also be defaultConfig defined in, buildType ,, productFlavors for example:

buildtypes {        Debug {            applicationidsuffix ". Debug"            signingconfig signingconfigs.myconfig            Buildconfigfield "string", "foo", "\" bar1\ ""            Buildconfigfield "string", "foo", "\" Bar\ ""            resvalue "string", " Foo "," Foo2 "        }    }

By doing so, we can customize the final program that we generate, with a variety of strokes.

Manifest file Content placeholder

This allows you to customize the manifest when you package, using the method:

    1. Define a placeholder in the manifest file, for example, ${umeng_channel}, in the case of the Umeng package we wrote earlier.

    2. Replace in the Gradle configuration file, which can be configured in,, defaultConfig buildType productFlavors for example:

      defaultconfig {manifestplaceholders = [umeng_channel: ' DefaultName ']}

At the same time, can also directly in the manifest file to add the name of the replacement, direct use ${applicationId} can be.

Other Tips for free

If oom occurs frequently during use, gradle.properties add memory to the file so that Gradle can use more memory:

Org.gradle.jvmargs=-xmx2048m-xx:maxpermsize=512m-xx:+heapdumponoutofmemoryerror

If because of the Chinese problem, there is an error, it is best to add after the Org.gradle.jvmargs -Dfile.encoding=UTF-8 , then this time and together is:

Org.gradle.jvmargs=-xmx2048m-xx:maxpermsize=512m-xx:+heapdumponoutofmemoryerror-dfile.encoding=utf-8

If, because of some errors, have to terminate, and then come in, can not be compiled, go to projectpath/.gradle/<gradle-version>/taskArtifacts/ the directory to see if there *.lock are files, delete and try again.

About Android Studio and Gradle

Android Studio (hereinafter referred to as) released the 1.0RC version today, meaning that the official version of the upcoming, while in the community, QQ group can also see more and more people began to use Android Studio. Often, many people will be asked to upgrade when there are some problems, the main reason is that some large version of Android Studio upgrade, there is generally a recommended version of the Gradle plug-in, for example, as0.9 requirements 0.14.+ version, as0.8 requirements 0.12+ version. The two are interdependent, and the version of the Gradle plugin has a minimum version requirement for as as well. This way, we must also modify the Gradle configuration file after upgrading as, improve the plug-in version, while some incompatible configuration needs to be modified.

After upgrading Gradle and plug-in versions, it is common to re-download Gradle, which consumes a bit of your time.

Finally, the welfare

The Gradle configuration of my nearest sister figure:

Buildscript {repositories {mavencentral ()} dependencies {classpath ' Com.android.tools.build:gra    dle:0.14.+ '}}apply plugin: ' Com.android.application ' android {compilesdkversion buildtoolsversion ' 21.1.0 '        Defaultconfig {minsdkversion 9 targetsdkversion versioncode 3 versionname "1.1.1" multidexenabled false manifestplaceholders = [Umeng_channel_value: "Default_channel"] buildconfigfield "Boo            Lean "," Isdebug "," true "} lintoptions {Abortonerror false}//signature Signingconfigs {debug { StoreFile file ("/home/sam/.android/debug.keystore")}//Your own KeyStore information release {/ /storefile file ("/home/sam/sangmingming.keystore")//storepassword ""//keyalias "Sam"// Keypassword ""}} buildtypes {debug {signingconfig signingconfigs.debug Build Configfield "Boolean", "Isdebug "," true "} release {Buildconfigfield" boolean "," Isdebug "," true "signingconf IG Signingconfigs.release minifyenabled true zipalignenabled true Proguardfiles Getdefaul Tproguardfile (' Proguard-android.txt '), ' proguard-rules.txt '}//channel flavors, I have written here some common, you change Productflavor s {//googleplay{}//nduo{} Xiaomi {} Umeng {}} compileoptions {Sourcecompatibi        Lity javaversion.version_1_7 targetcompatibility Javaversion.version_1_7} productflavors.all {Flavor Flavor.manifestplaceholders = [Umeng_channel_value:name]} applicationvariants.all {variant-V Ariant.outputs.each {output-def outputFile = Output.outputfile if (outputFile! = null &AMP;&A mp OutputFile.name.endsWith ('. apk ')) {def fileName = OutputFile.name.replace (". apk", "-${defaultconfig.versio        nname}.apk ")        Output.outputfile = new File (outputfile.parent, FileName)}}}}dependencies {compile Fil Etree (dir: ' Libs ', include: [' *.jar ']) compile ' com.android.support:appcompat-v7:21.+ ' compile ' com.android.support: support-v4:21.+ ' compile ' com.android.support:cardview-v7:21.+ ' compile ' com.android.support:recyclerview-v7:21.+ ' }

Then I took back the example of Google's latest gradle configuration and shared it with everyone: click to download

Reference: Http://tools.android.com/tech-docs/new-build-system

Original address: http://blog.isming.me/2014/11/21/use-gradle-new/, reproduced please indicate the source.

Build Android Projects with Gradle (cont.)

Related Article

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.