欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

首先我們來看一下,正常情況下我們的項目目錄的 build.gradle 情況:

先看 app 下的 build.gradle:

//說明module的類型,com.android.application為程序,com.android.library為庫 
apply plugin: 'com.android.application'  
android { 
 
  //編譯的 SDK 版本 
  compileSdkVersion 25 
 
  //編譯的 Tools 版本 
  buildToolsVersion "25.0.2" 
 
  //默認配置 
  defaultConfig { 
 
    //應用程序的包名 
    applicationId "com.example.qiudengjiao.activitytest" 
 
    //支持 SDK 的最低版本 
    minSdkVersion 15 
 
    //支持 SDK 的目標版本 
    targetSdkVersion 25 
 
    //版本號 
    versionCode 1 
 
    //版本名 
    versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
  } 
 
  //build 類型 
  buildTypes { 
    release { 
      //混淆是否開啟,返回true則開啟 
      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' 
 
  //support v7 支持庫 
  compile 'com.android.support:appcompat-v7:25.1.0' 
} 

接下來我們再來看一下項目根目錄下的 build.gradle:

//構建腳本 
buildscript { 
   repositories { 
     //依賴的倉庫 
    jcenter() 
  } 
  dependencies { 
     
    //項目依賴的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)在我們添加一個 Module 庫,來看一下我們 Module 庫下的 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' 
} 

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

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

Module 庫下的 build.gradle 是:apply plugin:com.android.library

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

解決辦法:

方法一

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

/*在根目錄中配置公用供子模塊調用*/ 
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 情況:

//構建腳本 
buildscript { 
 
  repositories { 
 
    //依賴的倉庫 
    jcenter() 
  } 
  dependencies { 
 
    //項目依賴的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 
} 
 
/*在根目錄中配置公用供子模塊調用*/ 
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}", 
  ] 
} 

接下來我們在 app 的 build.gradle 中進行調用如下:

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 中進行調用如下:

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 對項目中 app 下的 build.gradle 和 Module 中的 build.gradle 依賴進行統(tǒng)一配置的解決,以此類推,更多的 Module 也是如此配置,以后需要版本的更改我們只需要去根目錄 build.gradle 修改即可

方法二

因為每個人都有自己的配置習慣,這里我們再提供一種配置以供大家參考,這里我們在主項目的根目錄下創(chuàng)建 config.gradle 來配置需要的相關配置信息如下:

config.gradle 里面的配置信息:

/** 
 * 在主項目的根目錄下創(chuàng)建config.gradle文件 
 * 在這里單獨處理統(tǒng)一依賴問題 
 * 注意需要在根目錄的build.gradle中進行引入 
 */ 
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 引入進來,這里特別注意是在根目錄的 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 
} 

接下來我們就可以在 Module 中引入使用了,如下:

apply plugin: 'com.android.library' 
 
//android配置 
def config = rootProject.ext.android 
 
//相關庫依賴 
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' 
 
  //在這里使用庫的依賴 
  compile librarys.supportAppcompat 
  compile librarys.supportV4 
  compile librarys.suppoutDesign 
} 

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

相關文章

  • Android實現(xiàn)頭像上傳功能

    Android實現(xiàn)頭像上傳功能

    這篇文章主要為大家詳細介紹了Android實現(xiàn)頭像上傳功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • ViewPager 與 Fragment相結合實現(xiàn)微信界面實例代碼

    ViewPager 與 Fragment相結合實現(xiàn)微信界面實例代碼

    這篇文章主要介紹了ViewPager 與 Fragment相結合實現(xiàn)微信界面實例代碼的相關資料,需要的朋友可以參考下
    2016-07-07
  • Android9?雙屏異顯實現(xiàn)方式思路

    Android9?雙屏異顯實現(xiàn)方式思路

    這篇文章主要為大家介紹了Android9?雙屏異顯實現(xiàn)方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Android中解決RecyclerView各種點擊事件的方法

    Android中解決RecyclerView各種點擊事件的方法

    這篇文章主要介紹了Android中解決RecyclerView各種點擊事件的方法,完美解決RecyclerView點擊事件、長按事件、子項點擊事件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android開發(fā)中多進程共享數據簡析

    Android開發(fā)中多進程共享數據簡析

    這篇文章主要為大家簡單分析Android開發(fā)中多進程共享數據,怎么做才能讓這兩邊共享數據,感興趣的小伙伴們可以參考一下
    2016-04-04
  • android簡易文件管理器實例(列表式文件目錄)

    android簡易文件管理器實例(列表式文件目錄)

    下面小編就為大家?guī)硪黄猘ndroid簡易文件管理器實例(列表式文件目錄)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android學習之AppWidget高級效果

    Android學習之AppWidget高級效果

    這篇文章主要為大家詳細介紹了Android學習之AppWidget高級效果的相關資料,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android開發(fā)實現(xiàn)抽屜菜單

    Android開發(fā)實現(xiàn)抽屜菜單

    這篇文章主要為大家詳細介紹了Android開發(fā)實現(xiàn)抽屜菜單,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 如何利用Flutter實現(xiàn)酷狗流暢Tabbar效果

    如何利用Flutter實現(xiàn)酷狗流暢Tabbar效果

    這篇文章主要給大家介紹了關于如何利用Flutter實現(xiàn)酷狗流暢Tabbar效果的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-02-02
  • Android AES加密工具類分享

    Android AES加密工具類分享

    這篇文章主要介紹了Android AES加密工具類分享,本文給出了實現(xiàn)代碼和使用例子,本文使用PKCS5Padding加密方式實現(xiàn),需要的朋友可以參考下
    2014-10-10

最新評論