[Gradle] Gradle Building Android Apps FAQ Guide

Source: Internet
Author: User

Reprint Address: HTTP://WWW.CNBLOGS.COM/YOUXILUA/P/3348162.HTML1: Use the latest Gradle Android plugin

We used to write that when we wrote it.

dependencies {    classpath ' com.android.tools.build:gradle:0.5.0 '}

However, due to the development of Android Gradle plug-in is very active, and currently, there may be some pits we do not know, but, others stepped on, behind, the official repair, in order not to step on the pit, I recommend that Android Gradle always keep the latest version, the following wording:

dependencies {    classpath ' com.android.tools.build:gradle:0.5+ '}
2: Build failure due to inconsistent code encoding and compilation environment coding

Sometimes, our code is saved with Utf-8, but the environment for Gradle build is GBK, and the following error will be wrapped:

    15: Error: Encoding GBK non-mapped characters         * Credential empathize Juan Po Xuan Feng Open Kuang?

At this point we need to manually set the compile-time encoding type.

Tasks.withtype (Compile) {    options.encoding = "UTF-8"}apply plugin: ' Android ' Android {}
3:android Support V4 Duplicate reference issues
Unexpected top-level EXCEPTION:java.lang.IllegalArgumentException:already added:landroid/support/v4/app/ Activitycompathoneycomb;        At Com.android.dx.dex.file.ClassDefsSection.add (classdefssection.java:123) at        Com.android.dx.dex.file.DexFile.add (dexfile.java:163) at        Com.android.dx.command.dexer.Main.processClass ( main.java:490) at        com.android.dx.command.dexer.Main.processFileBytes (main.java:459)        at com.android.dx.command.dexer.main.access$400 (main.java:67) at        com.android.dx.command.dexer.main$1. Processfilebytes (main.java:398) at        com.android.dx.cf.direct.ClassPathOpener.processArchive ( classpathopener.java:245)

The cause of this problem is generally due to our writing:

dependencies {    Compile filetree (dir: ' Libs ', include: ' *.jar ')}

One of the same jar packages, copied to the build directory, causes the compilation to fail at compile time.

Because of this problem Android support V4 appear more, so the same type is categorized as V4 problem it.

To avoid this problem, we try to use less dependencies on all packages under a directory, after all, Android projects do not want Java Web projects to have a good dozens of jar package dependencies. To fix this v4, the principle is simple, and you can use MAVEN-dependent notation.

dependencies {    compile ' com.android.support:support-v4:13.0.0 '}
4: Missing *.so file after packaging

Packaging with the specified dependency package, we will find that the final packaged jar does not have a *.so file, and this time, we need to customize a tasks, write as follows:

Task Copynativelibs (type:copy) {from    (new file (' libs ')) {include ' **/*.so '}    to new file (BuildDir, ' Native-li BS ')}tasks.withtype (Compile) {compiletask, Compiletask.dependson copynativelibs}clean.dependson ' Cleancopynativelibs ' Tasks.withtype (com.android.build.gradle.tasks.PackageApplication) {Pkgtask    - Pkgtask.jnidir New File (BuildDir, ' Native-libs ')}

This will automatically copy the files from the Libs directory to the APK at compile time **/*.so .

5: Build multi-channel packages

In the latest version of Gradle 0.5.7, building multi-channel packages is much simpler than before, and you need to write this before:

Android {    Buildtypes {         hiapk {             packagenamesuffix ". hiapk"         }         playstore {             packagenamesuffix ". Playstore "        }     }    sourcesets {        hiapk {            manifest.srcfile ' hiapk/androidmanifest.xml '        }        Playstore {            manifest.srcfile ' Hiapk/androidmanifest.xml '}}    }

To replace a type of file you need to manually write, more channels, this code is imagined more, in 0.5.7, made a convention rules, build, channel package you just

Android {    Buildtypes {         hiapk {             packagenamesuffix ". hiapk"         }         playstore {             Packagenamesuffix ". Playstore"        }     }    sourcesets {         hiapk.setroot (' build-types/hiapk ')         Playstore.setroot (' Build-types/playstore ')    }}

Create a directory of build-types at the root of the project, create a subdirectory of the corresponding channel, and then put some, such as to replace Androidmanifest.xml , inside the Allies channel number, etc. Directly copy the XML into the line, Gradle in the construction of the project, will automatically take precedence of the directory files under the build-types , such as, according to different channels, different countries to change the program icon or something, just put it in the directory.

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.