使用gradle構建android項目(續)

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   使用   sp   java   

在幾個月之前,我已經寫過一篇使用gradle構建android項目的部落格了http://blog.isming.me/2014/05/20/android4gradle/,那篇文章已經介紹了如何使用gradle進行項目構建,以及為Google會推薦使用gradle。當時android的gradle外掛程式是0.11.0,現在外掛程式的版本已經是0.14.3了,對於一些老的方法和api,有一些已經被移除,無法使用。因此有必要再寫一篇部落格介紹這些被移除的部分和替代方案。同時由於個人學識原因,當時沒有介紹的一些技巧,其他功能,也會在本文中進行介紹。

和上一篇文章相比不相容的地方

沒有看過我另一篇文章的,建議去看一下。

以下這些屬性改名,原先的不能用:

runProguard -> minifyEnabled (是否混淆)
zipAlign -> zipALignEnabled (是否zip對齊)
packageName -> applicationId
jniDebugBuild-> jniDebuggable
renderscriptDebug->renderscriptDebuggable
renderscriptSupportMode->renderscriptSupportModeEnabled
renderscriptNdkMode->renderscriptNdkModeEnabled
Variant.packageApplication/zipAlign/createZipAlignTask/outputFile/processResources/processManifest使用variant.out代替,具體使用,看後面代碼

這些被移除替換的,在最新版的gradle外掛程式中,已經不會提示過時,直接報錯,請警惕啊!!!!

新功能 multiDexEnabled 多dex支援 shrinkResources 移除未使用的資源 支援定義BuildConfig值和res的值,比如:
applicationVariants.all { variant ->    variant.buildConfigField "int", "VALUE", "1"    variant.resValue "string", "name", "value"}

還可以在defaultConfig,buildType,productFlavors中定義,比如:

buildTypes {        debug {            applicationIdSuffix ".debug"            signingConfig signingConfigs.myConfig            buildConfigField "String", "FOO", "\"bar1\""            buildConfigField "String", "FOO", "\"bar\""            resValue "string", "foo", "foo2"        }    }

通過這樣,我們可以對我們產生的最終程式,進行多樣劃的定製了。

Manifest檔案內容預留位置

這樣可以打包的時候,對Manifest進行自訂配置,使用方法:

  1. 在Manifest檔案中定義一個預留位置,比如以我們之前寫的umeng打包的例子為例,${UMENG_CHANNEL},這種格式.

  2. 在gradle設定檔中加替換,可以在defaultConfig,buildType,productFlavors中配置,比如:

    defaultConfig {manifestPlaceholders = [ UMENG_CHANNEL:"defaultName"]}

同時,還可以直接在Manifest檔案中加包名的替換,直接使用${applicationId}即可。

 其他技巧免費附送

如果使用過程中經常出現OOM,那麼在gradle.properties檔案中增加一下記憶體,讓gradle可以使用更多記憶體:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError

如果因為中文問題,出現錯誤,最好在org.gradle.jvmargs後面再加上-Dfile.encoding=UTF-8,那麼這個時候和在一起就是:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

如果,因為一些錯誤,不得不終止,再進來之後,無法進行編譯,去projectpath/.gradle/<gradle-version>/taskArtifacts/目錄下看有沒有*.lock的檔案,刪掉再重試。

關於android studio和gradle

android studio(以下簡稱as)今天發布了1.0RC版,意味著正式版本的即將到來,同時在社區,QQ群也可以看到越來越多的人開始在使用android studio。經常也有很多人會問到升級的時候會遇到一些問題,主要原因就是android studio的一些大版本升級後,一般有一個推薦gradle外掛程式的版本,比如,as0.9要求0.14.+版本,as0.8要求0.12+版本。兩者是互相依賴的,gradle外掛程式的版本同時對於as也有最低版本要求。這樣,我們升級as後也必須修改gradle的設定檔,提高外掛程式版本,同時一些不能向下相容的配置也需要修改。

在升級gradle和外掛程式版本後,一般都會重新下載gradle,這樣會消耗你一點時間。

最後,福利

奉上我最近的妹子圖的gradle配置:

buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath ‘com.android.tools.build:gradle:0.14.+‘    }}apply plugin: ‘com.android.application‘android {    compileSdkVersion 21    buildToolsVersion "21.1.0"    defaultConfig {        minSdkVersion 9        targetSdkVersion 21        versionCode 3        versionName "1.1.1"        multiDexEnabled false        manifestPlaceholders = [ UMENG_CHANNEL_VALUE:"default_channel" ]        buildConfigField "boolean", "ISDEBUG", "true"    }    lintOptions {        abortOnError false    }//簽名    signingConfigs {        debug {            //storeFile file("/home/sam/.android/debug.keystore")        }        //你自己的keystore資訊        release {            //storeFile file("/home/sam/sangmingming.keystore")            //storePassword ""            //keyAlias "sam"            //keyPassword ""        }    }    buildTypes {        debug {            signingConfig signingConfigs.debug            buildConfigField "boolean", "ISDEBUG", "true"        }        release {            buildConfigField "boolean", "ISDEBUG", "true"            signingConfig signingConfigs.release            minifyEnabled true            zipAlignEnabled true            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.txt‘        }    }    //渠道Flavors,我這裡寫了一些常用的,你們自己改    productFlavors {        //GooglePlay{}        //NDuo{}        xiaomi {}        umeng {}    }    compileOptions {        sourceCompatibility JavaVersion.VERSION_1_7        targetCompatibility JavaVersion.VERSION_1_7    }    productFlavors.all { flavor ->        flavor.manifestPlaceholders = [ UMENG_CHANNEL_VALUE:name ]    }    applicationVariants.all { variant ->        variant.outputs.each { output ->            def outputFile = output.outputFile            if (outputFile != null && outputFile.name.endsWith(‘.apk‘)) {                def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")                output.outputFile = new File(outputFile.parent, fileName)            }        }    }}dependencies {    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])    compile ‘com.android.support:appcompat-v7:21.+‘    compile ‘com.android.support:support-v4:21.+‘    compile ‘com.android.support:cardview-v7:21.+‘    compile ‘com.android.support:recyclerview-v7:21.+‘}

然後,我把Google最新的gradle配置的樣本也拿回來了,分享給大家:點擊下載

參考資料:http://tools.android.com/tech-docs/new-build-system

原文地址:http://blog.isming.me/2014/11/21/use-gradle-new/,轉載請註明出處。

使用gradle構建android項目(續)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.