gradle 編譯android項目 Eclipse

來源:互聯網
上載者:User

標籤:android   gradle   eclipse   

gradle

gradle環境配置


1.工程目錄結構


說明:如果項目有多個library工程並且有可能重複引用了相同的jar包,如support-4等,需要將這些jar單獨拎出來作為一個BaseLibray,其他library引用這個BaseLibrary,如果不這樣做很有可能在編譯的時候遇到下面這樣的錯誤:

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


2.Eclipse下自動產生相關檔案

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


如果沒有Generate這個選項,可以嘗試升級到高版本的ADT 筆者使用的ADT-23

選中主工程



然後finish即可。完成之後會自動在工程目錄中產生一些build檔案


說明:在目錄下添加local.properties檔案,內容如:

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


即指定Android SDK的目錄,gradle在編譯工程的時候會調用該目錄下的打包工具,由於我已經把SDK目錄配置在了環境變數中,這裡可以不需要local.properties檔案了。

開啟build.gradle我們可以看到:

// 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.+'    }}

修改這個檔案:

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories{mavenCentral()}dependencies{//筆者使用的gradle版本為2.4classpath 'com.android.tools.build:gradle:1.1.3'}/***tasks.withType(Compile){options.encoding = "UTF-8"}**/tasks.withType(JavaCompile) { options.encoding = "UTF-8" }}

由於使用了ADT自動產生build files的功能,庫工程和主工程下都會有build.gradle檔案

主工程下build.gradle

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')    }}


直接命令列切換到主工程目錄:執行gradle clean

執行gradle build


至此這個基本流程就算完成了。

3.打多個不同包名,使用不同資源不同渠道定製包。

 以umeng多渠道為例,修改android:value

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

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

applicationId為包名,manifestPlaceholders會在打包時替換AndroidManifest.xml中<meta-data/>節點下的CHANNEL_NAME,

在src目錄下新增目錄 src/aa/res  src/bb/res


如想重新命名應用程式名稱,只需修改對應路徑下的string.xml就行了,換應用的icon或其他資源也同理,保證資源名和主工程下的一致,同時放到對應路徑,gradle在打包的時候就會替換為新的資源檔。【這兒有個問題需要注意,就是如果主工程下有自訂的attr,在使用自訂屬性的xml中的schemas中使用xmlns:app="http://schemas.android.com/apk/res-auto" 否則在打其他包名的時候提示找不到attribute】


貼上一份還算完整的build.gradle

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"defaultConfig {applicationId 'com.example.testgradle' minSdkVersion 9targetSdkVersion 20versionCode 1versionName '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")//指定keystore的路徑 這兒為當前工程目錄下storePassword "123321"keyAlias "alias_name"keyPassword "123321"}}buildTypes{release{//runProguard true  //開啟混淆開關//proguardFile 'proguard.txt.txt'  //配置單個檔案這樣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 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')    }}

相關技術文章

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

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

gradle 編譯android項目 Eclipse

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.