最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解Android使用Gradle統(tǒng)一配置依賴(lài)管理

 更新時(shí)間:2018年01月12日 09:48:20   作者:QDJdeveloper  
本篇文章主要介紹了詳解Android 使用 Gradle 統(tǒng)一配置依賴(lài)管理,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

在介紹使用 Gradle 統(tǒng)一配置依賴(lài)管理前我們先來(lái)簡(jiǎn)單介紹一下 Gradle, Gradle 是一個(gè)基于 JVM 的構(gòu)建工具,也是一款非常靈活強(qiáng)大的構(gòu)建工具,支持  jcenter、maven、Ivy 倉(cāng)庫(kù),支持傳遞性依賴(lài)管理(即 A 依賴(lài) B,B 依賴(lài) C,那么 A 也就可以依賴(lài) C,不用再單獨(dú)去依賴(lài)),而不需要遠(yuǎn)程倉(cāng)庫(kù)或者是 pom.xml 和 ivy.xml 配置文件,拋棄了各種繁瑣,基于 Groovy,build 腳本使用 Groovy 編寫(xiě)

而在我們的 Android studio 中默認(rèn)就是使用 Gradle 來(lái)構(gòu)建管理我們的工程的,在我們的工程構(gòu)建過(guò)程中通常會(huì)創(chuàng)建很多個(gè) Module 來(lái)對(duì)我們的工程進(jìn)行功能以及業(yè)務(wù)上的解耦(也就是模塊化開(kāi)發(fā)),這時(shí)候可能就會(huì)存在一個(gè)問(wèn)題,就是每個(gè) Module 以及 Module 中一些公用庫(kù)的依賴(lài)可能會(huì)出現(xiàn)版本不統(tǒng)一的問(wèn)題,包括使用的編譯版本,SDK 的版本等,導(dǎo)致不能打包,這里可以使用 Gradle 統(tǒng)一配置文件來(lái)解決我們的問(wèn)題

首先我們來(lái)看一下,正常情況下我們的項(xiàng)目目錄的 build.gradle 情況:

先看 app 下的 build.gradle:

//說(shuō)明module的類(lèi)型,com.android.application為程序,com.android.library為庫(kù) 
apply plugin: 'com.android.application'  
android { 
 
  //編譯的 SDK 版本 
  compileSdkVersion 25 
 
  //編譯的 Tools 版本 
  buildToolsVersion "25.0.2" 
 
  //默認(rèn)配置 
  defaultConfig { 
 
    //應(yīng)用程序的包名 
    applicationId "com.example.qiudengjiao.activitytest" 
 
    //支持 SDK 的最低版本 
    minSdkVersion 15 
 
    //支持 SDK 的目標(biāo)版本 
    targetSdkVersion 25 
 
    //版本號(hào) 
    versionCode 1 
 
    //版本名 
    versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
  } 
 
  //build 類(lèi)型 
  buildTypes { 
    release { 
      //混淆是否開(kāi)啟,返回true則開(kāi)啟 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
  } 
} 
 
//在這里進(jìn)行庫(kù)的依賴(lài) 
dependencies { 
  compile fileTree(dir: 'libs', include: ['*.jar']) 
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
  }) 
 
  testCompile 'junit:junit:4.12' 
 
  //support v7 支持庫(kù) 
  compile 'com.android.support:appcompat-v7:25.1.0' 
} 

接下來(lái)我們?cè)賮?lái)看一下項(xiàng)目根目錄下的 build.gradle:

//構(gòu)建腳本 
buildscript { 
   repositories { 
     //依賴(lài)的倉(cāng)庫(kù) 
    jcenter() 
  } 
  dependencies { 
     
    //項(xiàng)目依賴(lài)的Gradle版本 
    classpath 'com.android.tools.build:gradle:2.2.3' 
 
    // NOTE: Do not place your application dependencies here; they belong 
    // in the individual module build.gradle files 
  } 
} 
 
allprojects { 
  repositories { 
    jcenter() 
  } 
} 
 
task clean(type: Delete) { 
  delete rootProject.buildDir 
} 

現(xiàn)在我們添加一個(gè) Module 庫(kù),來(lái)看一下我們 Module 庫(kù)下的 build.gradle:

apply plugin: 'com.android.library' 
android { 
  compileSdkVersion 23 
  buildToolsVersion "23.0.2" 
 
  defaultConfig { 
    minSdkVersion 13 
    targetSdkVersion 23 
    versionCode 1 
    versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
 
  } 
  buildTypes { 
    release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
  } 
} 
 
dependencies { 
  compile fileTree(dir: 'libs', include: ['*.jar']) 
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
  }) 
  compile 'com.android.support:appcompat-v7:25.0.0' 
  testCompile 'junit:junit:4.12' 
} 

這里我們來(lái)看一下和 app 目錄下的 build.gradle 有什么區(qū)別:

app 目錄下的 build.gradle 是:apply plugin:com.android.application

Module 庫(kù)下的 build.gradle 是:apply plugin:com.android.library

其它的就是版本的不一樣了,要素是一樣的,這里就是我們今天著重要來(lái)介紹的,這里我們看到編譯的 SDK 版本和編譯的 Tools 版本以及支持 SDK 的最低版本等的版本號(hào)都是不一樣的,這里我們就需要來(lái)統(tǒng)一,而我們總不能每次都來(lái)手動(dòng)配置,當(dāng) Module 增多時(shí)則容易出錯(cuò)

解決辦法:

方法一

在項(xiàng)目的根目錄的 build.gradle 里進(jìn)行統(tǒng)一配置如下:

/*在根目錄中配置公用供子模塊調(diào)用*/ 
ext { 
  //Android 
  compileSdkVersion = 25 
  buildToolsVersion = "25.0.2" 
  minSdkVersion = 15 
  targetSdkVersion = 25 
 
  //Version 
  supportLibrary = "25.1.0" 
 
  //supportLibraries dependencies 
  supportDependencies = [ 
      supportAppcompat: "com.android.support:appcompat-v7:${supportLibrary}", 
  ] 
} 

配置完后工程根目錄的 build.gradle 情況:

//構(gòu)建腳本 
buildscript { 
 
  repositories { 
 
    //依賴(lài)的倉(cāng)庫(kù) 
    jcenter() 
  } 
  dependencies { 
 
    //項(xiàng)目依賴(lài)的Gradle版本 
    classpath 'com.android.tools.build:gradle:2.2.3' 
 
    // NOTE: Do not place your application dependencies here; they belong 
    // in the individual module build.gradle files 
  } 
} 
 
allprojects { 
  repositories { 
    jcenter() 
  } 
} 
 
task clean(type: Delete) { 
  delete rootProject.buildDir 
} 
 
/*在根目錄中配置公用供子模塊調(diào)用*/ 
ext { 
  //Android 
  compileSdkVersion = 25 
  buildToolsVersion = "25.0.2" 
  minSdkVersion = 15 
  targetSdkVersion = 25 
 
  //Version 
  supportLibrary = "25.1.0" 
 
  //supportLibraries dependencies 
  supportDependencies = [ 
      supportAppcompat: "com.android.support:appcompat-v7:${supportLibrary}", 
  ] 
} 

接下來(lái)我們?cè)?app 的 build.gradle 中進(jìn)行調(diào)用如下:

apply plugin: 'com.android.application' 
android {  
  compileSdkVersion rootProject.ext.compileSdkVersion 
  buildToolsVersion rootProject.ext.buildToolsVersion  
  defaultConfig { 
 
    applicationId "com.example.qiudengjiao.activitytest" 
    minSdkVersion rootProject.ext.minSdkVersion 
    targetSdkVersion rootProject.ext.targetSdkVersion 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
  } 
 
  buildTypes { 
    release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
  } 
} 
 
dependencies { 
  compile fileTree(include: ['*.jar'], dir: 'libs') 
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
  }) 
  compile 'junit:junit:4.12' 
  compile rootProject.ext.supportDependencies.supportAppcompat 
} 

在 Module 的 build.gradle 中進(jìn)行調(diào)用如下:

apply plugin: 'com.android.library'  
android { 
 
  compileSdkVersion rootProject.ext.compileSdkVersion 
  buildToolsVersion rootProject.ext.buildToolsVersion 
 
  defaultConfig { 
 
    minSdkVersion rootProject.ext.minSdkVersion 
    targetSdkVersion rootProject.ext.targetSdkVersion 
    versionCode 1 
    versionName "1.0" 
 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
 
  } 
 
  buildTypes { 
    release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
  } 
} 
 
dependencies { 
  compile fileTree(dir: 'libs', include: ['*.jar']) 
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
  }) 
  testCompile 'junit:junit:4.12'  
  compile rootProject.ext.supportDependencies.supportAppcompat 
} 

這樣我們就完成了使用 Gradle 對(duì)項(xiàng)目中 app 下的 build.gradle 和 Module 中的 build.gradle 依賴(lài)進(jìn)行統(tǒng)一配置的解決,以此類(lèi)推,更多的 Module 也是如此配置,以后需要版本的更改我們只需要去根目錄 build.gradle 修改即可

方法二

因?yàn)槊總€(gè)人都有自己的配置習(xí)慣,這里我們?cè)偬峁┮环N配置以供大家參考,這里我們?cè)谥黜?xiàng)目的根目錄下創(chuàng)建 config.gradle 來(lái)配置需要的相關(guān)配置信息如下:

config.gradle 里面的配置信息:

/** 
 * 在主項(xiàng)目的根目錄下創(chuàng)建config.gradle文件 
 * 在這里單獨(dú)處理統(tǒng)一依賴(lài)問(wèn)題 
 * 注意需要在根目錄的build.gradle中進(jìn)行引入 
 */ 
ext { 
  android = [ 
      compileSdkVersion: 25, 
      buildToolsVersion: "25.0.2", 
      minSdkVersion  : 15, 
      targetSdkVersion : 25 
  ] 
 
  //Version 
  supportLibrary = "25.1.0" 
 
  //supportLibraries dependencies 
  supportDependencies = [ 
      supportAppcompat: "com.android.support:appcompat-v7:${supportLibrary}", 
      supportV4    : "com.android.support:support-v4:${supportLibrary}", 
      suppoutDesign  : "com.android.support:design:${supportLibrary}" 
  ] 
} 

然后我們需要在根目錄的 build.gradle 中把 config.gradle 引入進(jìn)來(lái),這里特別注意是在根目錄的 build.gradle 中引入
引入的代碼為:

apply from: "config.gradle"  

引入后的根目錄 build.gradle 如下:

//在這里引入config.gradle 
apply from: "config.gradle" 
 
buildscript { 
  repositories { 
    jcenter() 
  } 
  dependencies { 
    classpath 'com.android.tools.build:gradle:2.2.3' 
 
    // NOTE: Do not place your application dependencies here; they belong 
    // in the individual module build.gradle files 
  } 
} 
 
allprojects { 
  repositories { 
    jcenter() 
  } 
} 
 
task clean(type: Delete) { 
  delete rootProject.buildDir 
} 

接下來(lái)我們就可以在 Module 中引入使用了,如下:

apply plugin: 'com.android.library' 
 
//android配置 
def config = rootProject.ext.android 
 
//相關(guān)庫(kù)依賴(lài) 
def librarys = rootProject.ext.supportDependencies 
 
android { 
  compileSdkVersion config.compileSdkVersion 
  buildToolsVersion config.buildToolsVersion 
 
  defaultConfig { 
    minSdkVersion config.minSdkVersion 
    targetSdkVersion config.targetSdkVersion 
    versionCode 1 
    versionName "1.0" 
 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
 
  } 
  buildTypes { 
    release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
  } 
} 
 
dependencies { 
  compile fileTree(dir: 'libs', include: ['*.jar']) 
  androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
    exclude group: 'com.android.support', module: 'support-annotations' 
  }) 
  testCompile 'junit:junit:4.12' 
 
  //在這里使用庫(kù)的依賴(lài) 
  compile librarys.supportAppcompat 
  compile librarys.supportV4 
  compile librarys.suppoutDesign 
} 

到這里我們就成功的引入到了 Module 的 build.gradle 中,以后每個(gè) Module 中的引入都是這樣,實(shí)現(xiàn)了和方法一 同樣的功能,個(gè)人感覺(jué)第二種更好一點(diǎn),大家自己選擇吧,畢竟各有所好,好了,到這里就給大家分享完了在項(xiàng)目中使用 Gradle 統(tǒng)一配置依賴(lài),希望對(duì)大家有用,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 自定義標(biāo)題欄背景

    Android 自定義標(biāo)題欄背景

    最近在做android項(xiàng)目,需要做一個(gè)自定義的標(biāo)題欄(操作欄)。去網(wǎng)上找了很多demo,發(fā)現(xiàn)都有很多問(wèn)題。例如使用自定義的style。下面來(lái)分享下個(gè)人最終的解決方案吧
    2016-01-01
  • Android仿微信拍攝短視頻

    Android仿微信拍攝短視頻

    本文主要對(duì)Android仿微信拍攝短視頻的實(shí)現(xiàn)方法進(jìn)行介紹,其功能設(shè)置為類(lèi)似于微信,點(diǎn)擊開(kāi)始拍攝,設(shè)置最長(zhǎng)拍攝時(shí)間。具有很好的參考價(jià)值,需要的朋友一起來(lái)看下吧
    2016-12-12
  • Android 實(shí)現(xiàn)永久性開(kāi)啟adb 的root權(quán)限

    Android 實(shí)現(xiàn)永久性開(kāi)啟adb 的root權(quán)限

    這篇文章主要介紹了Android 實(shí)現(xiàn)永久性開(kāi)啟adb 的root權(quán)限,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • android簡(jiǎn)單自定義View實(shí)現(xiàn)五子棋

    android簡(jiǎn)單自定義View實(shí)現(xiàn)五子棋

    這篇文章主要為大家詳細(xì)介紹了android簡(jiǎn)單自定義View實(shí)現(xiàn)五子棋,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Android-App增量更新的使用姿勢(shì)

    Android-App增量更新的使用姿勢(shì)

    增量更新根據(jù)字面理解就是下載增加的那部分來(lái)達(dá)到更新的目獲取舊的Apk安裝包的簽名和已合并成新的Apk安裝包的簽名,對(duì)比簽名是否一致當(dāng)你下載差異文件時(shí),可以讓服務(wù)器給你返回新的Apk合并成功后文件的md5,當(dāng)你合并成功后,通過(guò)校驗(yàn)文件的md5值,達(dá)到校驗(yàn)文件完整性。
    2016-04-04
  • Android自定義View之繪制圓形頭像功能

    Android自定義View之繪制圓形頭像功能

    這篇文章主要介紹了Android自定義View之繪制圓形頭像功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-09-09
  • android中實(shí)現(xiàn)背景圖片顏色漸變方法

    android中實(shí)現(xiàn)背景圖片顏色漸變方法

    這篇文章主要介紹了android中實(shí)現(xiàn)背景圖片顏色漸變方法,本文直接使用配置文件實(shí)現(xiàn)了這個(gè)效果,需要的朋友可以參考下
    2015-05-05
  • Android中再按一次退出提醒實(shí)現(xiàn)的兩種方法

    Android中再按一次退出提醒實(shí)現(xiàn)的兩種方法

    今天小編就為大家分享一篇關(guān)于Android中再按一次退出提醒實(shí)現(xiàn)的兩種方法,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-04-04
  • RecyclerView上拉加載封裝代碼

    RecyclerView上拉加載封裝代碼

    這篇文章主要為大家詳細(xì)介紹了RecyclerView上拉加載封裝代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android動(dòng)態(tài)加載布局實(shí)現(xiàn)技巧介紹

    Android動(dòng)態(tài)加載布局實(shí)現(xiàn)技巧介紹

    通過(guò)使用LayoutInflater 每次點(diǎn)擊按鈕時(shí)候去讀取布局文件,然后找到布局文件里面的各個(gè)VIEW 操作完VIEW 后加載進(jìn)我們setContentView 方面里面的要放的布局文件里面,每次動(dòng)態(tài)加載文件必需調(diào)用 removeAllViews方法,清除之前的加載進(jìn)來(lái)的View
    2022-12-12

最新評(píng)論

遵化市| 新野县| 山西省| 前郭尔| 望谟县| 渭源县| 新巴尔虎左旗| 永平县| 桃园市| 清河县| 临洮县| 城口县| 靖西县| 黎平县| 铜鼓县| 新密市| 科尔| 邢台县| 全州县| 平江县| 洛宁县| 无为县| 永登县| 南溪县| 阿拉善盟| 青州市| 靖远县| 隆子县| 宁安市| 股票| 南溪县| 托克逊县| 蒙自县| 彭州市| 台中县| 淮北市| 云浮市| 吴江市| 洛浦县| 安化县| 邛崃市|