【Android組件開發(fā)】統(tǒng)一配置文件

由于組件化實(shí)踐中模塊比較多,因此配置gradle,添加依賴庫時(shí),需要考慮簡化工作。

第一步:在Project目錄下點(diǎn)擊鼠標(biāo)右鍵,依次new——>File


在這里插入圖片描述

第二步:然后輸入統(tǒng)一配置文件的名稱,以.gradle結(jié)尾


在這里插入圖片描述

第三步:在新建的統(tǒng)一配置文件fjs.gradle里面配置要管理的項(xiàng),代碼如下所示
ext{
    isApplication = false //false:作為Lib組件存在, true:作為application存在
    android = [
            compileSdkVersion :28,
            buildToolsVersion :"29.0.0",
            minSdkVersion :15,
            targetSdkVersion :28,
            versionCode :1,
            versionName :"1.0"http://必須是int或者float,否則影響線上升級
    ]

    version = [
            androidSupportSdkVersion:"28.0.0",

    ]

    dependencies = [
            "appcompat-v7":"com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}",
    ]
}

第四步:在project目錄下的build.gradle里添加下面一句話

apply from: "fjs.gradle"

build.gradle完整代碼如下所示:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "fjs.gradle"
buildscript {
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.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來統(tǒng)一配置,如在app目錄下的build.gradle里面使用,代碼如下:

apply plugin: 'com.android.application'

android {
    compileSdkVersion rootProject.ext.android["compileSdkVersion"]
    buildToolsVersion rootProject.ext.android["buildToolsVersion"]
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion rootProject.ext.android["minSdkVersion"]
        targetSdkVersion rootProject.ext.android["targetSdkVersion"]
        versionCode rootProject.ext.android["versionCode"]
        versionName rootProject.ext.android["versionName"]
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    api rootProject.ext.dependencies["appcompat-v7"]
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

最后點(diǎn)擊Sync Now


在這里插入圖片描述

結(jié)果如下所示:


在這里插入圖片描述

統(tǒng)一配置文件是為了讓各個(gè)模塊的各個(gè)參數(shù)達(dá)到一致,利于以后統(tǒng)一修改、管理。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容