1. 程式人生 > >Android studio 統一管理依賴版本號

Android studio 統一管理依賴版本號

方式一(推薦)

1.在根目錄下的build.gradle檔案下新增 ext{ …. } 中的內容

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
} } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } ext{ //SDK minSdkVersion = 15 targetSdkVersion = 26 compileSdkVersion = 26 //dependencies supportLibraryVersion ='26.1.0' butterknifeVersion = '8.8.1'
}

注:在android studio3.0之後,buildToolsVersion去掉了,等號也別忘記了,要不然會報: Gradle DSL method not found: ‘minSdkVersion()’

2.在app目錄下build.gradle中使用

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    defaultConfig {
        applicationId "com.asialyf.zhiqu"
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 { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion" implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion" implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.1' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' //Butterknife implementation "com.jakewharton:butterknife:$rootProject.butterknifeVersion" }

注意:引用的依賴使用的是雙引號

方式二

1.在專案根目錄下建立.gradle檔案,如:config.gradle
2.在根目錄下的build.gradle檔案中引入建立的配置檔案
apply from:”config.gradle”
3.最後config.gradle中檔案內容可以自己定義

ext {  
    // 用於編譯的SDK版本  
    COMPILE_SDK_VERSION = 23  

    // 用於Gradle編譯專案的工具版本  
    BUILD_TOOLS_VERSION = "24.0.2"  

    // 最低支援Android版本  
    MIN_SDK_VERSION = 14  

    // 目標版本  
    TARGET_SDK_VERSION = 23  

    APPCOMPAT_VERSION = '23.2.1'  

} 

4.在app目錄下的build.gradle中使用

dependencies {  
    compile fileTree(include: ['*.jar'], dir: 'libs')  
    compile "com.android.support:cardview-v7:${APPCOMPAT_VERSION}"  
    compile "com.android.support:appcompat-v7:${APPCOMPAT_VERSION}"  
    compile "com.android.support:design:${APPCOMPAT_VERSION}"  
    compile 'com.github.bumptech.glide:glide:3.7.0'  
}