如何将我的一个模块或库用于特定的产品口味

时间:2018-10-17 11:58:05

标签: android dependencies build.gradle android-productflavors

我有五个产品风格的android项目,

 productFlavors {
    'Dev' {
        manifestPlaceholders = [appName: "Product"]
    }

    'Diamond' {
        manifestPlaceholders = [appName: "Product-Diamond"]
        applicationId GROUP.toString() + ".oc"
    }

    'Gold' {
        manifestPlaceholders = [appName: "Product-Gold"]
        applicationId GROUP.toString() + ".Gold"
    }

    'Silver' {
        manifestPlaceholders = [appName: "Product-Silver"]
        applicationId GROUP.toString() + ".Silver"
    }

    'Bronze' {
        manifestPlaceholders = [appName: "Product-bronze"]
        applicationId GROUP.toString() + ".bronze"
    }
 }

我有一个名为premimum的模块名称,我将使用以下名称将该模块添加到我的主项目中:

dependencies {
 implementation project(path: ':premimum')
}

我面临的问题是,我只想在premimumDev风格中使用此模块Diamond,我只想添加premimum依赖项对于这两种口味,我该怎么做。

我的gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.binarybuff.appgo"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    flavorDimensions "default"
    productFlavors {
        Dev {
            manifestPlaceholders = [appName: "Product"]
        }

        Diamond {
            manifestPlaceholders = [appName: "Product-Diamond"]
            applicationId "com.binarybuff.appgo" + ".oc"
        }

        Gold {
            manifestPlaceholders = [appName: "Product-Gold"]
            applicationId "com.binarybuff.appgo" + ".Gold"
        }

        Silver {
            manifestPlaceholders = [appName: "Product-Silver"]
            applicationId "com.binarybuff.appgo" + ".Silver"
        }

        Bronze {
            manifestPlaceholders = [appName: "Product-bronze"]
            applicationId "com.binarybuff.appgo" + ".bronze"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    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'
//    implementation project(path: ':premium')
//    DiamondImplementation project(":premium")
    DiamondImplementation project(path: ':premium' )
}

我已经找到了并发现: Library Publication

Gradle: add dependency for a specific flavour of the library

我点击了上述链接,但失败了。请帮忙。

3 个答案:

答案 0 :(得分:0)

dependencies {
 implementationPremium project(path: ':premimum')
}

您可以通过编写 variantName +实现来实现。如果源目录适合您的部分依赖关系,则可能无法编译您的项目。

您可以通过写作避免它

dependencies {
 provided project(path: ':premimum')
}

但是请确保不要对变体不执行此操作,否则它就没有ClassNotFoundException

答案 1 :(得分:0)

要为特定的构建变体配置依赖项,您可以在Implementation关键字之前为构建变体的名称加上前缀,如下例所示:

dependencies {
    // Adds the local "mylibrary" module as a dependency to the "free" flavor.
    freeImplementation project(":mylibrary")
}

请像上面的示例一样修改您的build.gradle文件。

已编辑:

Dev {  //not 'Dev'
    manifestPlaceholders = [appName: "Product"]
}

答案 2 :(得分:0)

也尝试提供配置。由于您的库模块中没有样式,因此将配置设置为“默认”应该适合您。这是您要添加的两行代码

DevImplementation project(path: ':premimum', configuration: 'default')
DiamondImplementation project(path: ':premimum', configuration: 'default')