【问题标题】:Android Studio Issue with gradle flavors in library module库模块中的 gradle 风格的 Android Studio 问题
【发布时间】:2017-01-09 22:02:14
【问题描述】:

我有一个项目,其中包含多个库模块。我想在一个模块中创建独特的风格,但我遇到了一个我无法解决的奇怪错误。

在我的模块的 gradle 文件中,我有以下内容:

android {
    defaultConfig {
        testApplicationId "com.mytestApplication"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    productFlavors {
       flav1 {
            buildConfigField 'String', 'PROJECT', '"Flav1"'
        }
        flav2{
            buildConfigField 'String', 'PROJECT', '"Flav2"'
        }
    }
}

但我不断收到错误:

Error:Cannot add task ':myaccessorModule:jarRelease' as a task with that name already exists. 

如果我的 build.gradle 文件是这样写的:

android {
    defaultConfig {
        testApplicationId "com.mytestApplication"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    productFlavors {
       flav1 {
            buildConfigField 'String', 'PROJECT', '"Flav1"'
        }
    }
}

然后它实际上会在 Build Variant 窗口中显示 flav1Debug 和 flav1Release 选项。

我想知道为什么尝试使用两种或多种口味时会失败?我没有在我的任何其他模块中定义任何风格。我见过的所有文档都让这看起来很容易。我还尝试通过项目结构 GUI 将这些风格添加到我的模块中,但我得到了相同的结果。我如何让这个模块有多种口味?

干杯。

【问题讨论】:

    标签: android android-studio android-gradle-plugin build.gradle


    【解决方案1】:

    通常使用产品风格来创建具有相同代码的多个 apk 版本。

    因此,每种口味都应该有唯一的applicationIds。

    尝试使用

    productFlavors {
           flav1 {
                applicationId "com.mytestApplication.flav1"
            }
            flav2{
                applicationId "com.mytestApplication.flav2"
            }
        }
    

    欲了解更多信息,https://developer.android.com/studio/build/build-variants.html

    【讨论】:

    • 感谢 Jaswanth 的回复。我之前实际上已经尝试过,只是为了看看它是否会起作用,但不幸的是它没有。我刚刚发现不同的风格只适用于应用程序而不是库。 (使用 apply plugin: 'com.android.application' 而不是 apply plugin: 'com.android.library'" 所以不幸的是,目前看来库项目只能是一种风格。:(
    • 刚刚想起一件事。你能看看这个答案吗? stackoverflow.com/a/24316133/1852441.
    • 类似于在穿戴模块中添加风味依赖(如果你有穿戴应用的经验)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-02
    • 2017-10-21
    • 2022-11-09
    相关资源
    最近更新 更多