Preface
Android gradle plug-in has been developed to 0.5.7, and gradle itself has reached 1.8. Compared with two months ago, android gradle is faster, more complete, and easier to use, in order to allow androider to use the gradle artifact as soon as possible, I would like to write a special guide on how to solve some wonderful errors in gradle.
Use the latest gradle android plug-in
We used to write this in the past.
dependencies { classpath 'com.android.tools.build:gradle:0.5.0'}
However, the android gradle plug-in development is still very active, and there may still be some pitfalls we don't know, but someone else stepped on it, and then officially fixed it, to avoid this issue, we recommend that you keep the latest version of android gradle as follows:
dependencies { classpath 'com.android.tools.build:gradle:0.5+'}
The build fails because the code encoding is inconsistent with the compiling environment encoding.
Sometimes, our code is saved using UTF-8. However, the gradle build environment is gbk, and the following error is returned:
15: Error: Non- able characters encoded in GBK
* Are you sure you want to keep yourself updated?
At this time, we need to manually set the encoding type during compilation.
tasks.withType(Compile) { options.encoding = "UTF-8"}apply plugin: 'android'android {}
Duplicate android support v4 references
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)
This problem is generally caused by the following code:
dependencies { compile fileTree(dir: 'libs', include: '*.jar')}
An identical jar package is copied to the build directory, causing repeated compilation to fail during compilation,
Android support v4 has many problems.
To avoid this problem, we should try to use as little as possible to rely on all the packages in a directory. After all, the android project does not want the java web project to have dozens of jar packages to rely on. to fix this v4, the principle is very simple. You can use the maven-dependent method.
dependencies { compile 'com.android.support:support-v4:13.0.0'}
*. So file missing after Packaging
If you use the specified dependency package, we will find that the *. so file is missing in the final jar package. At this time, we need to customize a tasks and write it as follows:
task copyNativeLibs(type: Copy) { from(new File('libs')) { include '**/*.so' } into new File(buildDir, 'native-libs')}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')}
In this way,**/*.so
The file is copied to the apk.
Build multi-channel packages
In the latest version of gradle 0.5.7, it is much easier to build multi-channel packages than before. In the past, you need to write:
android { buildTypes { hiapk { packageNameSuffix ".hiapk" } playstore { packageNameSuffix ".playstore" } } sourceSets { hiapk { manifest.srcFile 'hiapk/AndroidManifest.xml' } playstore { manifest.srcFile 'hiapk/AndroidManifest.xml' } }}
To replace a certain type of file, you need to manually write the file by yourself, and there are more channels. The amount of code is as much as you can imagine. In 0.5.7, you have made an agreed rule to build the channel package. You only need
android { buildTypes { hiapk { packageNameSuffix ".hiapk" } playstore { packageNameSuffix ".playstore" } } sourceSets { hiapk.setRoot('build-types/hiapk') playstore.setRoot('build-types/playstore') }}
Createbuild-types
Directory, create sub-directories of the corresponding channel, and then replace some, suchAndroidManifest.xml
The umeng channel number or something in it, just copy the xml directly, gradle will automatically give priority to use when building the projectbuild-types
Directory of directory files, such as changing program icons in different countries based on different channels.
This article is from the "game a chai" blog, please be sure to keep this source http://youxilua.blog.51cto.com/3357586/1303922