提问者:小点点

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


我的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')
}

我面临的问题是,我只想使用这个模块premimum,在DevDiamondflavor中,我只想为这两个flavor添加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' )
}

我已经找过了,找到了:图书馆出版物

Gradle:为库的特定风格添加依赖项

我遵循上面的链接,但失败了。请帮忙。


共3个答案

匿名用户

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

您可以通过编写variantName实现来实现这一点。如果源目录适合您的部分依赖项,则可能不会编译您的项目。

你可以通过写作来避免

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

但是确保不要执行它,因为变体没有依赖关系,否则你就有了ClassNotFoundExcema

匿名用户

要配置特定构建变体的依赖项,可以在实现关键字之前添加构建变体的名称,如下所示:

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

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

编辑:

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

匿名用户

试着给出配置。由于您的库模块中没有样式,所以将配置设置为“默认”应该适合您。下面是您要添加的两行代码

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