關於Android Studio裡的Gradle,你所需要知道的都在這裡了

來源:互聯網
上載者:User

標籤:

Gradle介紹

Gradle是一個先進的build toolkit,可以方便的管理依賴包和定義自己的build邏輯。到底有多先進,Android Studio官方整合Gradle,Google還專門寫了Android Plugin for Gradle,你們感受一下。

基礎配置

Android Studio中有一個頂級的build.gradle檔案,每一個module還有一個自己的build.gradle。這個檔案是使用Groovy文法和Android Plugin for Gradle元素的設定檔。通常我們只需要修改module的build檔案就可以了。 下面是一個簡單的例子

apply plugin: ‘com.android.application‘android {    compileSdkVersion 19    buildToolsVersion "19.0.0"    defaultConfig {        applicationId "com.example.my.app"        minSdkVersion 8        targetSdkVersion 19        versionCode 1        versionName "1.0"    }    buildTypes {        release {            minifyEnabled true            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }    }}dependencies {     // Module dependency    compile project(":lib")    // Remote binary dependency    compile ‘com.android.support:appcompat-v7:19.0.1‘    // Local binary dependency    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])}

第一句apply plugin: ‘com.android.application’說明在這個build檔案中使用Android plugin for Gradle,有了這一句後,那些針對Android的元素才有意義。比如android {…}這個就是配置所有針對Android項目build時的選項。

  • compileSdkVersion: 編譯時間用的sdk版本
  • buildToolsVersion: build工具的版本
  • defaultConfig: 動態在build時配置AndroidManifest.xml裡的項目,defaultConfig裡的配置可以覆蓋manifest裡的配置。
  • buildTypes: 配置如何構建和打包你的App,預設有debug和release兩個類型。debug類型包含調試時的資訊,並且有debug key簽名。release預設是不含簽名的。本例中release版本用了ProGuard。
  • dependencies: 在android {…}這個元素之外,配置此模組的依賴。
依賴
dependencies {     // Module dependency    compile project(":lib")    // Remote binary dependency    compile ‘com.android.support:appcompat-v7:19.0.1‘    // Local binary dependency    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])}
  • 模組依賴: compile project(“:lib”),表明本模組依賴於lib模組,編譯的時候系統會把lib模組包含進來
  • 遠端二進位依賴: compile ‘com.android.support:appcompat-v7:19.0.1’,當本地沒有此依賴時會預設從Maven Central Repository下載相應的依賴。從哪裡下載可以在頂級的build檔案中配置。
  • *本地二進位依賴:**compile fileTree(dir: ‘libs’, include: [‘.jar’]),把jar包拷貝到/libs檔案夾下,compile fileTree(dir: ‘libs’, include: [‘*.jar’])意思是,app/libs下的所有jar包都包含進來
ProGuard

ProGuard的作用是在byte層級對你的app進行分析最佳化,使得你的App變得更小,更快。值得一提的是,當你使用某些開源項目時,他們會提醒你把一些包排除在ProGuard裡,防止出錯。

android {    ...    buildTypes {        release {            minifyEnabled true            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘        }    }}

getDefaultProguardFile(‘proguard-android.txt’)獲得預設的ProGuard配置。每個模組也都有模組層級別的ProGuard規則,在proguard-rules.pro這個檔案裡,當然你也可以自己配置ProGuard規則。關於ProGuard的詳細配置,請參考ProGuard Manual

Application ID

Application ID是用來唯一標識要發行的應用的。在build.gradle裡設定。

   defaultConfig {        applicationId "com.example.my.app"        minSdkVersion 15        targetSdkVersion 19        versionCode 1        versionName "1.0"    }

系統允許你對不同的產品類型設定不同的id,比如免費版和進階版。

productFlavors {        pro {            applicationId = "com.example.my.pkg.pro"        }        free {            applicationId = "com.example.my.pkg.free"        }    }    buildTypes {        debug {            applicationIdSuffix ".debug"        }    }
配置簽名

debug版本與release版本的區別在於是否可以在一個安全的裝置上debug和APK如何被簽名的。在debug版本中,系統會提供一個預設的key來簽名和已知的認證來授權App,避免在構建的時候出現密碼提示。但是在release版本中,除非你主動提供一個key,不然系統是不會構建此項目的。

Release簽名步驟
  1. 建立keystore。keystore是一個包含一系列私密金鑰的二進位檔案,這個檔案必須妥善保管。
  2. 建立一個私密金鑰。一個私密金鑰代表著一個app實體。
  3. 在build檔案中配置
android {    ...    defaultConfig { ... }    signingConfigs {        release {            storeFile file("myreleasekey.keystore")            storePassword "password"            keyAlias "MyReleaseKey"            keyPassword "password"        }    }    buildTypes {        release {            ...            signingConfig signingConfigs.release        }    }}

4.在Android Studio中啟動assembleRelease。app/build/apk/app-release.apk這個檔案就是包含你release key的apk了。 註:一般不會直接在build檔案中寫入密碼的,密碼資訊可以寫在環境變數中

storePassword System.getenv("KSTOREPWD")keyPassword System.getenv("KEYPWD")

或者用命令列輸入

storePassword System.console().readLine("\nKeystore password: ")keyPassword System.console().readLine("\nKey password: ")
在Android Studio中配置簽名
  1. 功能表列裡,點擊Build > Generate Signed APK.
  2. 在彈出的視窗裡點擊Create new
  3. 填寫相關資訊
  4. Build Type 選擇release

到此為止,你就可以去應用市場發布你的應用啦 ^_^

Build變數定義productFlavors

有時你需要對同一個App發行不同的版本,如免費版、付費版之類的。這時你就需要用到productFlavors了。

android {    ...    defaultConfig { ... }    signingConfigs { ... }    buildTypes { ... }    productFlavors {        demo {            applicationId "com.buildsystemexample.app.demo"            versionName "1.0-demo"        }        full {            applicationId "com.buildsystemexample.app.full"            versionName "1.0-full"        }    }}

productFlavors裡的配置會覆蓋defaultConfig裡的配置,如上配置後,系統會為每一個不同版本使用一個不同的id。

給每一個版本添加對應的資源

當你有不同的版本的時候,不同版本之間肯定有不同,所以需要添加不同的資源。以demo版為例,在src目錄下建立以下目錄

把需要的Activity,String.xml等資源加入到對應檔案夾下。 在IDE左側的Build Variants視窗裡就可以看到如下四個Build變數

這樣你需要哪個版本,直接選擇就可以了。

最後

看到這裡,再次開啟build.gradle這個檔案也不是感覺也沒那麼複雜了?是不是更愛Android Studio了?如有什麼問題歡迎留言討論。喜歡的話別忘了點個“頂”啊 ?(^∀^●)?

轉載請註明:Android開發中文站 » 關於Android Studio裡的Gradle,你所需要知道的都在這裡了

關於Android Studio裡的Gradle,你所需要知道的都在這裡了

聯繫我們

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