Android Packaging Tool Gradle kick

Source: Internet
Author: User

When the traditional manual packaging approach to the same application, multi-channel/multi-package name and a variety of guide page/icon and so on differences, it becomes a miserable pure physical life. But with the Gradle this is no longer a problem, Gradle makes it all the same.

The main introduction to eclipse in the use of gradle packaging to solve multi-channel problems, the other problems almost. Using Gradle in eclipse requires only the following kick:

First axe, generate gradle corresponding file

1.1 Select the appropriate item, right-click Export-generategradle build files, and then go down all the way next.


1.2 After you go down, finish, you can generate the following directories and files



In the generated file, there are two need to follow configuration modification gradle-wrapper.properties and build.gradle files, other files can be fixed.

Second axe, download Gradle and configure the appropriate environment variables

2.1 Double hit Open gradle-wrapper.properties, copy out http://services.gradle.org/distributions address, visit this website


2.2 Download the corresponding Gradle version, the individual downloaded the 2.1 version, find and download Gradle-2.1-all.zip, modify the above Distributionurl to

Distributionurl=http\://services.gradle.org/distributions/gradle-2.1-all.zip


2.3 Configure Gradle environment changes, extract the newly downloaded zip package out of gradle-2.1, configure gradle_home separately and append path after path

Gradle_home, refer to gradle-2.1 directory, here is D:\Program files\android\gradle-2.1

Path refers to the bin directory, here is D:\Program Files\android\gradle-2.1\bin


In addition, you need to configure the Android_home environment, in Eclipse-preferences-android, find the SDK location

Add the Android_home variable name to the environment configuration and point to the path to the SDK location in eclipse.


Configured, cmd Check under grade is configured, input gradle-w command check.

Third axe, gradle Configuration and Package command generation

3.1 Gradle The packaging process is slow, and it may be possible to have oom when the memory is consumed, especially when multiple versions of multiple n channels are packaged together. You can add org.gradle.jvmargs=-xmx2048m-xx:maxpermsize=512m-xx:+ to the Gradle-wrapper.properties file by adding the Heapdumponoutofmemoryerror

3.2 Modify the Build.gradle------A more critical profile, configured for multi-channel/multi-pack names and various variations under the Build.gradle file.

This is where the configuration of the personal demo project is first and last, and then the key points are explained.

----------------------------------------build.gradle file starts-----------------------------------------

Buildscript {

repositories {

Mavencentral ()

}

dependencies {

Classpath ' com.android.tools.build:gradle:0.14.+ '

}

Tasks.withtype (javacompile) {options.encoding = "UTF-8"}

}

Apply plugin: ' Android '

dependencies {

Compile Filetree (dir: ' Libs ', include: ' *.jar ')

}

Android {

Compilesdkversion 21

Buildtoolsversion "21.1.2"

Defaultconfig {

ApplicationID "Com.test.drawdemo"

MinSdkVersion10

Targetsdkversion 12

versionCode200

Versionname "v2.0.5"

Testapplicationid "Com.test.drawdemo"

Testinstrumentationrunner "Android.test.InstrumentationTestRunner"

The default is the offical channel, which is the official version

Manifestplaceholders = [Umeng_channel_value: "Offical"]

}

App signature File

Signingconfigs {

Release {

StoreFile file ("Demo.keystore")

Storepassword "123456"

Keyalias "Demo.keystore"

Keypassword "123456"

}

}

Buildtypes {

Release {

Signingconfigsigningconfigs.release

Do not display log

Buildconfigfield "Boolean", "Log_debug", "false"

minifyenabled true

Proguardfiles ' Proguard-project.txt '

}

}

Remove the error from the lint check

lintoptions {

Abortonerror false

Ignorewarnings true

}

Friend Alliance multi-channel packaging

productflavors {

Offical {}

Appchina {}

Anzhi {}

Mumayi {}

Lenovo {}

QQ {}

Huawei {}

Xiaomi {}

Baidu {}

Meizu {}

Taobao {}

huijia360 {}

hiapk {}

}

Productflavors.all {flavor->

Flavor.manifestplaceholders = [Umeng_channel_value:name]

}

------------loading so library start------------

Task Copynativelibs (type:copy) {

From (New File (' libs ')) {include ' **/*.so '}

Into NewFile (BuildDir, ' native-libs ')

}

Tasks.withtype (javacompile) {compiletask-Compiletask.dependson copynativelibs}

Clean.dependson ' Cleancopynativelibs '

Tasks.withtype (com.android.build.gradle.tasks.PackageApplication) {

Pkgtask, Pkgtask.jnifolders =new hashset<file> ()

PkgTask.jniFolders.add (NewFile (BuildDir, ' native-libs '))

}

---------------loading so library end---------

sourcesets {

Main {

Manifest.srcfile ' Androidmanifest.xml '

Java.srcdirs = [' src ']

Resources.srcdirs = [' src ']

Aidl.srcdirs = [' src ']

Renderscript.srcdirs = [' src ']

Res.srcdirs = [' res ']

Assets.srcdirs = [' Assets ']

}

}

}

----------------------------------------build.gradle file Ends-----------------------------------------

3.3 Execute gradlewclean. Open CMD,CD to the project directory, enter Gradlew clean first. Here Gradlew is the Gradle command packaging, Gradlew clean can make the gradle will be self-synchronization download Gradle the corresponding dependency, and can be the last packaging of the clearing to re-hit, will delete the build directory and all the files in the build directory.

3.4 Execute the Gradlew build command to complete the packaging process.

Next you can see that the build directory is generated under the project, and in this case the build-->outputs-->apk of each channel is generated.

You can see that a bunch of packages have been generated, with release and debug versions, signed and unsigned. If you do not want the DEBUGT version of the APK, you can enter the Gradlew ar command when packaging. AR is a shorthand for assemblerelease. Of course, you can generate only the debug version (Gradlew assembledebug)

The merit is caused, how is not very easy ah, hehe ... Below is a brief introduction to Build.gradle.

Supplemental instructions--About build.gradle configuration

1. Buildscript Elements

In addition to Dependencies-classpath in the Gradle version needs to change, basically do not need to modify.

1.1 Dependencies {

Classpath ' com.android.tools.build:gradle:0.12.+ '

}

gradle:0.12.+ modified to gradle:0.14.+, the corresponding Gradle 2.1 with the new Gradle version, more critical.

1.2 Tasks.withtype (javacompile) {options.encoding = "UTF-8"}, where the old version gradle is compile and needs to be modified to Javacompile

2, Applyplugin: ' Android '

-----Do not make changes

3. Dependencies elements

dependencies{

Compilefiletree (dir: ' Libs ', include: ' *.jar ')

Specify the corresponding jar dependencies in the project, note:

3.1 Configuration instructions are configured here, and the dependency jar package is reconfigured in-libraryjars Libs/xxxx.jar conflict with the Proguard obfuscation configuration. The-libraryjars configuration in the Proguard obfuscation file can be commented out and #-libraryjars ...

3.2 Dependencies only instructs the jar package to be dependent, and does not have the command so library dependent, so library relies on the individual configuration ..., followed by the introduction: But there is a perverted method is can be the so library files, all zip into a file such as So.zip, and then renamed So.zip So.jar so do not need to order so library, compared to abnormal bar. Oh....

4. Android elements

Here is the heavy weight, the configuration is cumbersome,

4.1 Defaultconfig elements

ApplicationID instruction package name, old version is PackageName instruction

Other configurable version information

4.2 Signingconfigs Elements

Can configure signature file information, file location alias command, etc.

4.3 buildtypes elements

The generated apk has two type types, Debug and release versions, which can be configured separately, configure which signature configuration to use (Signingconfigs), confusion, and so on

Minifyenabled is the new version of the element, the old version of Runproguard has been deprecated.

The Proguardfiles ' proguard-project.txt ' directive uses Proguard-project.txt to confuse files with Build.gradle in the same directory.

4.4 Lintoptions Elements

Under configuration, ignore lint corresponding warning ...

4.5 flavors mechanism,,

Mainly by, this mechanism for multi-channel, multi-package name packaging ....,

The rest of the load so library, directly copied under, do not change, take it, thank you.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Packaging Tool Gradle kick

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.