How the Android client program Gradle packaged _android

Source: Internet
Author: User
Tags create directory getdate stack trace

First, the preface

Android client development came to an end, responsible for SEO colleagues suddenly sent me a 45 release channels of the sad news, before only the release of their own channels of work (manual modification parameter packaging) has not met the demand, so introduced recently more popular Gradle packaging technology.

Gradle based on the groovy language, the introduction of the reason is also convenient to migrate from the current use of the Eclipse development environment to Android Studio, so blablabla ..., not much said, first dry.

Ii. preparatory work 

1. First, if you use eclipse as your development environment, right-click on the project and choose Export ... in the menu.
2, then, in the pop-up list, select "Android->generate gradle build Files"
3, choose "Next>"
4, then choose "Next>"
5, in the list of the need to create Gradle configuration script, continue to "next>"
6, choose "Finish" to this point, Eclipse has generated gradle configuration script, it may be necessary to right-click the project

Three, simple project Gradle package script configuration

Buildscript {repositories {jcenter ()} dependencies {classpath ' com.android.tools.build:gradle:0.12.+ '}}//project configuration version number, with APK output Directory Ext.appversioncode = 2 Ext.appversionname = "2.0" Ext.appreleasedir = "/users/freedoms/desktop/release" Apply PLU Gin: ' com.android.application '//Get timestamp def getDate () {def date = new Date () def formatteddate = Date.format (' yyyyMMdd ') r Eturn Formatteddate}//Package configuration Android {compilesdkversion buildtoolsversion "21.1.2" sourcesets {main {MANIFEST.SRCFI Le ' androidmanifest.xml ' java.srcdirs = [' src '] resources.srcdirs = [' src '] aidl.srcdirs = [' src '] renderscript.srcdirs = [' src '] res.srcdirs = [' res '] assets.srcdirs = [' Assets ']//project contains so packages, you need to increase the JNI-related directory configuration, otherwise the program will run to call so when the error jnilibs.srcdir ([' L
IBS ']} instrumenttest.setroot (' Tests ') debug.setroot (' Build-types/debug ') release.setroot (' Build-types/release ') ///packaging at the beginning, the lint run will have an error, when an exception occurs, this setting ignores the warning, continue to run the script, if omitted, the packaging process will be forced to terminate lintoptions {Abortonerror false}//Signature configuration Signingcon Figs {myconfig {storefile file("/users/freedoms/desktop/Product requirements Document/android.keystore") Storepassword "123123" Keyalias "Android.keystore" KeyPassword " 123123 "}}//Obfuscation configuration buildtypes{Release {signingconfig signingconfigs.myconfig//No confusion set to False Runproguard true p Roguardfiles getdefaultproguardfile (' proguard-android.txt '), ' proguard-project.txt '}///Custom configuration Productflavors {//Baidu B The aidu{//androidmanifest.xml configuration is defined in a placeholder complement configuration, in which "${umeng_channel_value}" is configured in Androidmanifest.xml, and after the script is run Baidu_ Android_channel automatically replaces this location, androidmanifest.xml other parameters that require dynamic configuration of the channel can refer to this practice action manifestplaceholders = [Umeng_channel_value: "
Baidu_android_channel ", Baidu_channel_value:" Baidu_baidu_android_channel "]}//360" 360 "{//If the channel name is configured to begin with a number, quotation marks must be added Manifestplaceholders = [Umeng_channel_value: "360_android_channel", Baidu_channel_value: "Baidu_360_android_channel

   "]}//Can be extended by reference to the above two items ...} Package rename algorithm, output xxx_android_v2.0_20160127_baidu.apk to the script configuration before the output directory Android.applicationVariants.all {variant-> def File = Variant.outputfile if (variant.buildType.naMe.equals (' release ')) {variant.outputfile = new File (Appreleasedir + '/', ' xxx_android_v ' + appversionname +getdate () + ' _ ' + Variant.productflavors[0].name + '. apk ')}}//Encoding configuration Tasks.withtype (Compile) {options.encoding = ' UTF-8 '}

Iv. gradle packaged script configuration with Project dependencies

1. Dependent projects

A If you use eclipse as your development environment, you first need to generate Gradle configuration scripts (see the second, preparation)
b Modify the Apply plugin for the following configuration in the Gradle script

Apply plugin: ' Android-library '

C other configuration with the main project, can refer to the simple Project Gradle script configuration

2. Main project

A If you use eclipse as your development environment, you first need to generate Gradle configuration scripts (see the second, preparation)

b Modify the Apply plugin for the following configuration in the Gradle script

Apply plugin: ' Com.android.application '

C Create the Setting.gradle text file under the main project root as a reference dependent project configuration

Introduce dependent project name include ' library '
include ' Library_pulltorefresh '
//Create directory reference, the final quotation mark is the absolute path of dependent item storage Project
(': Library '). ProjectDir = new File ('/users/freedoms/git/library ') 

D Add the following configuration to the Build.gradle of the main project

Dependent configuration
Dependencies {
compile filetree (dir: ' Libs ', include: ' *.jar ')
Compile project (': Library_ Pulltorefresh ') 
Compile project (': library ')
}

V. Run Build Script

1, the CD in the command line to the main project root directory
2, Input gradle clean implementation (cleaning Gradle generated check files and apk, you can not do, but do not guarantee the middle will have any strange problems, develop good habits)
3, Input gradle check Execution (check items, depending on the number of channels need different time)
4, Input Gradle build Execution (execute build script, start packing, depending on the number of channels need different time, 45 channels will take about 1 hours)
5, check the main project build.gradle configuration output directory, the channel bag is already in the inside

Vi. matters of concern (to be continued) 

Q1: When you perform a check or build, you may report the following error

Failure:build failed with a exception.
* What went wrong:
Execution failed for task ': Lint '.
> Lint found errors in the project; Aborting build.
Fix the issues identified by lint, or add the following to your builds script to proceed with errors:
...
Android {
Lintoptions {
abortonerror false
}
}
...
* Try:
Run with--STACKTRACE option to get the stack trace. Run with--info or--debug option to get more log output.
Build FAILED 
A1: Add
lintoptions {
abortonerror false
} in Build.gradle script

Q2: Depending on the number of channels, the Java virtual machine may not have enough memory to be packaged

The system is out of.
Consult the following stack trace for details.
Java.lang.OutOfMemoryError:Java heap space at
Com.sun.tools.javac.util.position$linemapimpl.build ( position.java:139) at
com.sun.tools.javac.util.Position.makeLineMap (position.java:63)
at Com.sun.tools.javadoc.DocCommentScanner.getLineMap (doccommentscanner.java:438) at
Com.sun.tools.javac.main.JavaCompiler.parse (javacompiler.java:512) at
Com.sun.tools.javac.main.JavaCompiler.parse (javacompiler.java:550) at
Com.sun.tools.javac.main.JavaCompiler.parseFiles (javacompiler.java:804) at
Com.sun.tools.javac.main.JavaCompiler.compile (javacompiler.java:727) at
Com.sun.tools.javac.main.Main.compile (main.java:353) at
Com.sun.tools.javac.api.JavacTaskImpl.call ( javactaskimpl.java:115)

The above is a small set for everyone to share the Android client program Gradle How to package the relevant knowledge, I hope to help.

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.