简化 build.gradle配置

作者: mouekz | 来源:发表于2018-01-24 10:33 被阅读297次

    一般在项目较小的时候,我们的build.gradle文件看着比较直爽,简介。
    但是项目慢慢变大,依赖配置越来越多的时候在去浏览,修改build.gradle会觉得头大。

    这是谷歌实例的一个项目,其app build.gradle配置(这里面只是做了几个简单配置)如下:

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion rootProject.ext.compileSdkVersion
        buildToolsVersion rootProject.ext.buildToolsVersion
    
        defaultConfig {
            applicationId "com.example.android.architecture.blueprints.todomvpdagger"
            minSdkVersion rootProject.ext.minSdkVersion
            targetSdkVersion rootProject.ext.targetSdkVersion
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
        }
    
        buildTypes {
            debug {
                minifyEnabled true
                // Uses new built-in shrinker http://tools.android.com/tech-docs/new-build-system/built-in-shrinker
                useProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguardTest-rules.pro'
            }
    
            release {
                minifyEnabled true
                useProguard true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                testProguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguardTest-rules.pro'
            }
        }
    
        flavorDimensions "default"
    
        // If you need to add more flavors, consider using flavor dimensions.
        productFlavors {
            mock {
                dimension "default"
                applicationIdSuffix = ".mock"
            }
            prod {
                dimension "default"
            }
        }
    
        // Remove mockRelease as it's not needed.
        android.variantFilter { variant ->
            if (variant.buildType.name == 'release'
                    && variant.getFlavors().get(0).name == 'mock') {
                variant.setIgnore(true)
            }
        }
    
        // Always show the result of every unit test, even if it passes.
        testOptions.unitTests.all {
            testLogging {
                events 'passed', 'skipped', 'failed', 'standardOut', 'standardError'
            }
        }
    }
    
    /*
     Dependency versions are defined in the top level build.gradle file. This helps keeping track of
     all versions in a single place. This improves readability and helps managing project complexity.
     */
    dependencies {
        // App's dependencies, including test
        compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
        compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
        compile "com.android.support:design:$rootProject.supportLibraryVersion"
        compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
        compile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
        compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.espressoVersion"
        compile "com.google.guava:guava:$rootProject.guavaVersion"
        compile "android.arch.persistence.room:runtime:$rootProject.roomVersion"
        annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
    
        // Dagger dependencies
        annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
        provided 'org.glassfish:javax.annotation:10.0-b28'
        compile "com.google.dagger:dagger:$rootProject.daggerVersion"
        compile "com.google.dagger:dagger-android:$rootProject.daggerVersion"
        compile "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
        annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    
        // Dependencies for local unit tests
        testCompile "junit:junit:$rootProject.ext.junitVersion"
        testCompile "org.mockito:mockito-all:$rootProject.ext.mockitoVersion"
        testCompile "org.hamcrest:hamcrest-all:$rootProject.ext.hamcrestVersion"
    
        // Android Testing Support Library's runner and rules
        androidTestCompile "com.android.support.test:runner:$rootProject.ext.runnerVersion"
        androidTestCompile "com.android.support.test:rules:$rootProject.ext.rulesVersion"
    
        androidTestCompile "android.arch.persistence.room:testing:$rootProject.roomVersion"
    
        // Dependencies for Android unit tests
        androidTestCompile "junit:junit:$rootProject.ext.junitVersion"
        androidTestCompile "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
        androidTestCompile "com.google.dexmaker:dexmaker:$rootProject.ext.dexmakerVersion"
        androidTestCompile "com.google.dexmaker:dexmaker-mockito:$rootProject.ext.dexmakerVersion"
    
        // Espresso UI Testing
        androidTestCompile "com.android.support.test.espresso:espresso-core:$rootProject.espressoVersion"
        androidTestCompile "com.android.support.test.espresso:espresso-contrib:$rootProject.espressoVersion"
        androidTestCompile "com.android.support.test.espresso:espresso-intents:$rootProject.espressoVersion"
        androidTestCompile "com.android.support.test.espresso.idling:idling-concurrent:$rootProject.espressoVersion"
        compile "com.android.support.test.espresso:espresso-idling-resource:$rootProject.espressoVersion"
    
        // Resolve conflicts between main and test APK:
        androidTestCompile "com.android.support:support-annotations:$rootProject.supportLibraryVersion"
        androidTestCompile "com.android.support:support-v4:$rootProject.supportLibraryVersion"
        androidTestCompile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"
        androidTestCompile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
        androidTestCompile "com.android.support:design:$rootProject.supportLibraryVersion"
        androidTestCompile "com.google.code.findbugs:jsr305:3.0.1"
    
    }
    
    

    其中配置中引用的配置在项目build.gradle中配置

    buildscript {
        repositories {
            jcenter()
            google()
        }
        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 {
            jcenter()
            google()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    
    // Define versions in a single place
    ext {
        // Sdk and tools
        // Support library and architecture components support minSdk 14 and above.
        minSdkVersion = 14
        targetSdkVersion = 26
        compileSdkVersion = 26
        buildToolsVersion = '26.0.2'
    
        // App dependencies
        supportLibraryVersion = '26.1.0'
        guavaVersion = '18.0'
        junitVersion = '4.12'
        mockitoVersion = '1.10.19'
        powerMockito = '1.6.2'
        hamcrestVersion = '1.3'
        runnerVersion = '1.0.1'
        rulesVersion = '1.0.1'
        espressoVersion = '3.0.1'
        roomVersion = "1.0.0"
        daggerVersion = '2.11'
        dexmakerVersion = '1.2'
    }
    
    //When there is an error in code generation you will may see
    //lots of errors due too missing generated code in your logs, we increase
    //how many errors are shown here so that the real error is visible when it is near the end
    allprojects {
        afterEvaluate {
            tasks.withType(JavaCompile.class) {
                options.compilerArgs << "-Xmaxerrs" << "500"
            }
        }
    }
    
    

    可以看到谷歌并没有对其进行太多的抽离只做了些配置版本的抽离,并未对其进行实质性的有优化。
    下面是一个小型项目其中里面用到很多三方库依赖。其app build.gradle配置如下:

    apply plugin: 'com.android.application'
    //apply plugin: 'me.tatarka.retrolambda'
    
    android {
        compileSdkVersion rootProject.ext.android.compileSdkVersion
        buildToolsVersion rootProject.ext.android.buildToolsVersion
    
        defaultConfig {
            applicationId rootProject.ext.android.applicationId
            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.txt'), 'proguard-rules.pro'
            }
        }
    
    //    compileOptions {
    //        sourceCompatibility JavaVersion.VERSION_1_8
    //        targetCompatibility JavaVersion.VERSION_1_8
    //    }
    
        buildToolsVersion '26.0.2'
    }
    
    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 rootProject.ext.dependencies["appcompat-v7"]
        compile rootProject.ext.dependencies["support-v4"]
        compile rootProject.ext.dependencies["cardview"]
        compile rootProject.ext.dependencies["design"]
        compile rootProject.ext.dependencies["recyclerview"]
        compile rootProject.ext.dependencies["retrofit2"]
        compile rootProject.ext.dependencies["converter-scalars"]
        compile rootProject.ext.dependencies["converter-gson"]
        compile rootProject.ext.dependencies["adapter-rxjava2"]
        compile rootProject.ext.dependencies.rxlifecycle2
        compile rootProject.ext.dependencies.rxlifecomponents
        compile rootProject.ext.dependencies["dagger"]
        annotationProcessor rootProject.ext.dependencies["dagger-compiler"]
    
        compile rootProject.ext.dependencies["rxjava"]
        compile rootProject.ext.dependencies["rxandroid"]
        compile rootProject.ext.dependencies["rxbinding2"]
        compile rootProject.ext.dependencies["constraint-layout"]
        compile rootProject.ext.dependencies["ultra-ptr"]
        compile rootProject.ext.dependencies["fragmentation"]
        compile rootProject.ext.dependencies["butterknife"]
        annotationProcessor rootProject.ext.dependencies["butterknife-compiler"]
        compile rootProject.ext.dependencies["BaseAdapterHelper"]
        compile rootProject.ext.dependencies["glide"]
        compile rootProject.ext.dependencies["banner"]
        compile rootProject.ext.dependencies["viewanimator"]
        compile rootProject.ext.dependencies["FlycoDialog"]
        compile rootProject.ext.dependencies["PhotoView"]
        compile rootProject.ext.dependencies["videoplayer"]
        compile rootProject.ext.dependencies["lottie"]
        compile rootProject.ext.dependencies["progressbar"]
        compile rootProject.ext.dependencies["swipebacklayout"]
        compile rootProject.ext.dependencies["tablayout"]
        compile rootProject.ext.dependencies["androideventbus"]
        compile rootProject.ext.dependencies["gif-drawable"]
    
        compile rootProject.ext.dependencies["litepal"]
        compile 'com.android.support.constraint:constraint-layout:1.0.2'
        testCompile 'junit:junit:4.12'
    }
    repositories {
        mavenCentral()
    }
    
    

    看到这里会发现这里面的东西已经和我们平常看到的变化很大了
    其这里做的是吧真正依赖的库放到了一个confid.gradle(位置是和项目的build.gradle同级)的文件中

    
    ext {
    
        android = [
                compileSdkVersion      : 25,
                buildToolsVersion      : "25.0.2",
                applicationId          : "com.will.weiyue",
                minSdkVersion          : 19,
                targetSdkVersion       : 25,
                versionCode            : 1,
                versionName            : "1.0"
        ]
    
        dependVersion = [
                support: "25.3.1"
        ]
    
        dependencies = [
                //android-support
                "support-v4"            : "com.android.support:support-v4:${dependVersion.support}",
                "appcompat-v7"          : "com.android.support:appcompat-v7:${dependVersion.support}",
                "design"                : "com.android.support:design:${dependVersion.support}",
                "recyclerview"          : "com.android.support:recyclerview-v7:${dependVersion.support}",
                "cardview"              : "com.android.support:cardview-v7:${dependVersion.support}",
    
                //http
                "retrofit2"             : "com.squareup.retrofit2:retrofit:2.1.0",
                "converter-scalars"     : "com.squareup.retrofit2:converter-scalars:2.1.0",
                "converter-gson"        : "com.squareup.retrofit2:converter-gson:2.1.0",
                "adapter-rxjava2"       : "com.squareup.retrofit2:adapter-rxjava2:2.2.0",
    
                //httpcache
                "rxCache"               : "com.github.VictorAlbertos.RxCache:runtime:1.8.0-2.x",
                "Jolyglot-gson"         : "com.github.VictorAlbertos.Jolyglot:gson:0.0.3",
    
                //rxjava
                "rxjava"                : "io.reactivex.rxjava2:rxjava:2.0.6",
                "rxandroid"             : "io.reactivex.rxjava2:rxandroid:2.0.1",
                "rxbinding2"            : "com.jakewharton.rxbinding2:rxbinding:2.0.0",
                rxlifecycle2            : "com.trello.rxlifecycle2:rxlifecycle:2.1.0",
                rxlifecomponents        : "com.trello.rxlifecycle2:rxlifecycle-components:2.1.0",
    
                //dagger2
                "dagger"                : "com.google.dagger:dagger:2.7",
                "dagger-compiler"       : "com.google.dagger:dagger-compiler:2.7",
    
                //ui
                "constraint-layout"     : "com.android.support.constraint:constraint-layout:1.0.2",
                "fragmentation"         : "me.yokeyword:fragmentation:1.0.0",
                "butterknife"           : "com.jakewharton:butterknife:8.8.1",
                "butterknife-compiler"  : "com.jakewharton:butterknife-compiler:8.8.1",
                "ultra-ptr"             : "in.srain.cube:ultra-ptr:1.0.11",
                "bottom-bar"            : "com.roughike:bottom-bar:2.3.1",
                "BaseAdapterHelper"     : "com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.24",
                "glide"                 : "com.github.bumptech.glide:glide:4.0.0-RC1",
                "banner"                : "com.youth.banner:banner:1.4.9",
                "viewanimator"          : "com.github.florent37:viewanimator:1.0.5",
                "tablayout"             : "com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar",
                "FlycoDialog"           : "com.flyco.dialog:FlycoDialog_Lib:1.3.2@aar",
                "richtext"              : "com.zzhoujay.richtext:richtext:2.5.2",
                "transformations"       : "jp.wasabeef:glide-transformations:2.0.2",
                "PhotoView"             : "com.githu  b.chrisbanes:PhotoView:2.1.2",
                "videoplayer"           : "fm.jiecao:jiecaovideoplayer:5.7",
                "lottie"                : "com.airbnb.android:lottie:2.1.0",
                "progressbar"           : "me.zhanghai.android.materialprogressbar:library:1.4.1",
                "transitionhelper"      : "me.immortalz:transitionhelper:1.0.6",
                "swipebacklayout"       : "cn.bingoogolapple:bga-swipebacklayout:1.1.0@aar",
                "gif-drawable"          : "pl.droidsonroids.gif:android-gif-drawable:1.2.8",
    
                //Event
                "androideventbus"       : "org.simple:androideventbus:1.0.5.1",
    
                //db
                "litepal"               : "org.litepal.android:core:1.5.1",
                "retrolambda"           : "me.tatarka:gradle-retrolambda:3.7.0",
    
        ]
    }
    

    相关文章

      网友评论

        本文标题:简化 build.gradle配置

        本文链接:https://www.haomeiwen.com/subject/jabyaxtx.html