간편한build.gradle 구성

16723 단어
일반적으로 프로젝트가 비교적 작을 때 우리의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()
}


여기를 보면 이 안에 있는 것들이 우리가 평소에 봤던 것과 많이 달라진 것을 발견할 수 있습니다.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",

    ]
}

좋은 웹페이지 즐겨찾기