如何共享不同的Android构建变体之间的依赖关系?

时间:2017-03-16 16:40:35

标签: android gradle groovy android-gradle build.gradle

我有一个Android库项目(RealCoolLib)。 RealCoolLib的用户有时希望将依赖项打包在aar中,有时他们希望以正常的gradle方式使用它。我的构建gradle看起来像这样:

apply plugin: 'com.android.library'
android {
    compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_COMPILE_SDK_VERSION)
    buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
    defaultConfig {
        minSdkVersion Integer.parseInt(project.ANDROID_BUILD_MIN_SDK_VERSION)
        targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
        versionCode 1
        versionName 1.0.0
    }

    buildTypes {
        debug {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'client-proguard.txt'
        }
        releaseNoPro {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'client-proguard.txt'
        }
        releaseWithJars {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'client-proguard.txt'
        }
        releaseWithJarsNoPro {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'client-proguard.txt'
        }
    }
}

dependencies {
        compile 'com.googlelib.everyone.needs.this:1.0.0'

        debugCompile 'com.tools.debugversion:0.0.5'
        debugCompile 'org.awesome.stuff.debug:2.0.0'
        debugCompile 'com.another.tool.needed:3.0.0'        

        releaseCompile 'com.tools.releaseversion:1.0.0'
        releaseCompile 'org.awesome.stuff:2.0.0'
        releaseCompile 'com.another.tool.needed:3.0.0'

        releaseNoProCompile 'com.tools.releaseversion:1.0.0'
        releaseNoProCompile 'org.awesome.stuff:2.0.0'
        releaseNoProCompile 'com.another.tool.needed:3.0.0'

        releaseWithJarsProvided file('stuff/com_tools_releaseversion.jar') 
        releaseWithJarsCompile file('stuff/org_awesome_stuff.jar')
        releaseWithJarsCompile file('stuff/com_another_tool_needed.jar')

        releaseWithJarsNoProProvided file('stuff/com_tools_releaseversion.jar') 
        releaseWithJarsNoProCompile file('stuff/org_awesome_stuff.jar')
        releaseWithJarsNoProCompile file('stuff/com_another_tool_needed.jar')
}

这导致了许多重复的依赖声明。有没有办法创建一个坚持jar列表的单个“单位” 依赖关系和另一个正常的符号?基本上我正在寻找一些方法来减少重复。

1 个答案:

答案 0 :(得分:0)

你可以这样做:

configurations {
    baseConfiguration
    configurationA {
        extendsFrom(baseConfiguration)
    }
    configurationB {
        extendsFrom(baseConfiguration)
    }
}

dependencies {
    baseConfiguration('junit:junit:4.12')
}

然后在运行./gradlew dependencies时,您可以看到configurationAconfigurationB包含与baseConfiguration相同的依赖关系:

baseConfiguration
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

configurationA
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

configurationB
\--- junit:junit:4.12
     \--- org.hamcrest:hamcrest-core:1.3

文档:extendsFrom