Gradle compiles the android project Eclipse and gradleandroid

Source: Internet
Author: User

Gradle compiles the android project Eclipse and gradleandroid

Gradle

Gradle environment Configuration


1. project directory structure


Note: If the project has multiple library projects and the same jar package may be referenced repeatedly, such as support-4, these jar packages must be separately extracted as a BaseLibray, other libraries reference this BaseLibrary. If you do not do this, you may encounter the following errors during compilation:

UNEXPECTED TOP-LEVEL EXCEPTION:    java.lang.IllegalArgumentException: already added: Landroid/support/v4


2. automatically generate related files in Eclipse

File-> Export-> Android-> Generate Gradle build files


If you do not have the Generate option, you can try upgrading to a later version of the adt I used ADT-23

Select the main project



Then finish. Some build files are automatically generated in the project directory.


Note: add the local. properties file to the directory. The content is as follows:

sdk.dir=D:\\dev\\adt-bundle-windows-x86_64-20140702\\sdk


That is, to specify the directory of the Android SDK, gradle will call the packaging tool under this directory when compiling the project, because I have configured the SDK directory in the environment variable, local is not required here. properties file. 

Open build. gradle and we can see:

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:0.12.+'    }}

Modify this file:

// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript {repositories {mavenCentral ()} dependencies {// The gradle version used by the author is 2.4 classpath 'com. android. tools. build: gradle: 1.1.3 '}/*** tasks. withType (Compile) {options. encoding = "UTF-8"} **/tasks. withType (JavaCompile) {options. encoding = "UTF-8 "}}

Because the ADT function is used to automatically generate build files, both the Library Project and the main project will have the build. gradle File

Build. gradle in the main project

apply plugin: 'com.android.application'dependencies {    compile fileTree(dir: 'libs', include: '*.jar')    compile project(':appcompat_v7')    compile project(':AndroidSupportLibrary')    compile project(':AndroidSupportLibrary2')}android {    compileSdkVersion 22    buildToolsVersion "22.0.1"    sourceSets {        main {            manifest.srcFile 'AndroidManifest.xml'            java.srcDirs = ['src']            resources.srcDirs = ['src']            aidl.srcDirs = ['src']            renderscript.srcDirs = ['src']            res.srcDirs = ['res']            assets.srcDirs = ['assets']        }        // Move the tests to tests/java, tests/res, etc...        instrumentTest.setRoot('tests')        // Move the build types to build-types/<type>        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...        // This moves them out of them default location under src/<type>/... which would        // conflict with src/ being used by the main source set.        // Adding new build types or product flavors should be accompanied        // by a similar customization.        debug.setRoot('build-types/debug')        release.setRoot('build-types/release')    }}


Directly switch to the main project directory through the command line: Execute gradle clean

Execute gradle build


Now this basic process is complete.

3. Name multiple packages and use different resources and channels to customize packages.

Take umeng multi-channel as an example to modify android: value

<meta-data            android:name="UMENG_CHANNEL"            android:value="${CHANNEL_NAME}" > </meta-data>
Add in build. gradle

 productFlavors {        aa {            applicationId 'com.example.testgradle.aa'manifestPlaceholders = [ CHANNEL_NAME:"aa"]        }        bb {            applicationId 'com.example.testgradle.bb'manifestPlaceholders = [ CHANNEL_NAME:"bb"]        }    }

ApplicationId is the package name, And manifestPlaceholders replaces CHANNEL_NAME under the <meta-data/> node in AndroidManifest. xml during packaging,

Add the src/aa/res src/bb/res directory under the src directory.


To rename the application name, you only need to modify the string in the corresponding path. xml is enough. The application icon or other resources are the same. Ensure that the resource name and the master project are consistent and put in the corresponding path, gradle is replaced with a new resource file during packaging. [Note: If a custom attr exists in the main project, use xmlns in schemas in xml that uses custom attributes: app = "http://schemas.android.com/apk/res-auto" otherwise the system prompts that the attribute cannot be found when other package names are called]


Paste a complete build. gradle

Apply plugin: 'com. android. application 'dependencies {compile fileTree (dir: 'libs', include :'*. jar ') compile project (': appcompat_v7') compile project (': androidsupplibrary library') compile project (': androidsupplibrlibrary2 ')} android {compileSdkVersion 22 buildToolsVersion "22.0.1" defaultConfig {applicationId 'com. example. testgradle 'minsdkversion 9 targetSdkVersion 20 versionCode 1 versionname' 1. 0'} productFlavors {aa {applicationId 'com. example. testgradle. aa 'manifestplaceholders = [CHANNEL_NAME: "aa"]} bb {applicationId 'com. example. testgradle. bb 'manifestplaceholders = [CHANNEL_NAME: "bb"]} signingConfigs {myConfig {storeFile file ("test. keystore ") // specify the keystore path. Here, storePassword "123321" keyAlias "alias_name" keyPassword "123321" }}buildtypes {release {// runProguard true // enable the obfuscation switch/ /proguardFile 'proguard.txt.txt '// configure a single file such as signingConfig signingConfigs. myConfig} sourceSets {main {manifest. srcFile 'androidmanifest. xml 'java. srcDirs = ['src'] resources. srcDirs = ['src'] aidl. srcDirs = ['src'] renderscript. srcDirs = ['src'] res. srcDirs = ['res'] assets. srcDirs = ['assets'] }}// Move the tests to tests/java, tests/res, etc... instrumentTest. setRoot ('tests') // Move the build types to build-types/<type> // For instance, build-types/debug/java, build-types/debug/AndroidManifest. xml ,... // This moves them out of them default location under src/<type> /... which wocould // conflict with src/being used by the main source set. // Adding new build types or product flavors shoshould be accompanied // by a similar customization. debug. setRoot ('Build-types/debug') release. setRoot ('Build-types/release ')}}

Related Technical Articles

Http://tech.meituan.com/mt-apk-adaptation.html

Http://stackoverflow.com/questions/17571427/gradle-no-resource-identifier-found-for-attribute-when-using-flavors-and-packag

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.