Gradle in Android Studio
轉載請註明出處 : http://blog.csdn.net/hpu_zyh/article/details/48447539
博客主頁 | 簡書 | 知乎 | 微博 | github
Gradle (谷瑞豆) 官網, 點擊上面圖片看Google官方視頻 Introducing Gradle (Ep 2, Android Studio) in Youtube
來自Gradle的hello world
Android Studio中的Gradle
當創建一個項目後,Android studio 會自動創建以下的目錄
一般情況下,我們只修改app
moudle下的build.gradle即可滿足使用
android studio 目錄結構
庫管理機制
當創建一個項目時,生成以下的基本信息: app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22 // 這裏需要按照自己本機下載的版本填寫, 如果本機沒有下載,會報錯
buildToolsVersion "23.0.0 rc2" // Error:failed to find target android-22 : E:\android-sdk
defaultConfig {
applicationId "hanks.com.myapplication" //應用包名
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies { //庫依賴
compile fileTree(dir: 'libs', include: ['*.jar']) //引用本地的libs目錄下的所以jar包
compile 'com.android.support:appcompat-v7:22.2.0'
}
當需要添加一些第三方庫時, 直接在dependencies
添加引用
dependencies {
// Google Play Services
compile 'com.google.android.gms:play-services:7.8.0'
// Support Libraries
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.android.support:cardview-v7:23.0.0'
compile 'com.android.support:design:23.0.0'
compile 'com.android.support:gridlayout-v7:23.0.0'
compile 'com.android.support:leanback-v17:23.0.0'
compile 'com.android.support:mediarouter-v7:23.0.0'
compile 'com.android.support:palette-v7:23.0.0'
compile 'com.android.support:recyclerview-v7:23.0.0'
compile 'com.android.support:support-annotations:23.0.0'
compile 'com.android.support:support-v13:23.0.0'
compile 'com.android.support:support-v4:23.0.0'
// Note: these libraries require the "Google Repository" and "Android Repository"
// to be installed via the SDK manager.
}
添加引用後,Android Studio會提示我們Sync
, 然後會自動從遠程庫中下載我們引用的庫文件,本地已經緩存過的話會直接引用本地的,而不用下載
compile files('libs/gson-2.3.1.jar') //引用單個jar
-------------------------------------------------------------------------------
compile fileTree(include: ['*.jar'], dir: 'libs') // 引用libs下的全部jar
-------------------------------------------------------------------------------
compile project(':library:mylibrary') // 引用library目錄下的mylibrary
可以通過Android Studio
來添加引用, 比如引用 recyclerview-v7
使用Gradle
來管理庫,比以前引用.jar
(只包含Java Code)方便的多,也不用到處複製庫文件, 並且直接使用Gradle
可以直接引用aar
(包含庫文件的Java Code, Resource, Assets, AndroidMenifest.xml)
打包多個APK
利用Gradle的靈活性,你可以爲同一個項目來配置,創建不同的版本, 默認有debug
和release
兩種編譯類型
build variants
product flavors
代碼
MyApplication/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:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
jcenter
遠程倉庫默認是https
的,當使用代理時可能會在下載遠程庫時出現Error:Cause: peer not authenticated
, 解決方案是 使用http
代替 https
buildscript {
repositories {
jcenter{
url "http://jcenter.bintray.com"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
gradle下載報同樣的錯, 以下解決方案
settings.gradle
include ':app'
當我們創建一個library時
移動mylibrary目錄後