android - 产品 flavor : Duplicate class found

我有一个问题,但几个小时以来我一直坐在我的应用程序前,但我不明白问题出在哪里。

我有一个 android 应用程序(用 kotlin 编写),我想制作两种产品风格并覆盖产品风格中的类/文件:

所以我的 gradle 脚本是这样的:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'


android {
  ...
  productFlavors {
    foo {
      applicationId "com.foo"
    }
  }
}

我的文件结构如下:

 - src
    - androidTest
    - foo
      - java
        - com
          - example
            - Bar.kt
    - main
      - java
        - com
          - example
            - Bar.kt
    - test

所以基本上我想在 foo 产品风格中覆盖 Bar.kt 文件,但不知何故它不起作用:它说类 Bar 是重复的。

有什么提示吗?

最佳答案

The documentation for variants状态(强调我的):

Note: For a given build variant, Gradle throws a build error if it encounters two or more source set directories that have defined the same Java class. For example, when building a debug APK, you cannot define both src/debug/Utility.java and src/main/Utility.java. This is because Gradle looks at both these directories during the build process and throws a 'duplicate class' error. If you want different versions of Utility.java for different build types, you can have each build type define its own version of the file and not include it in the main/ source set.

因此解决方案是为每个变体拥有自己的 Bar.kt 版本,并将其从主要源集中排除。

关于android - 产品 flavor : Duplicate class found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37439120/

相关文章:

string - 为什么 Kotlin 中 null + null 的类型是隐式 String?

android - Kotlin 错误 : Dagger does not support inje

android - 使用 Kotlin 关闭/隐藏 Android 软键盘

android - 使用 Kotlin 组合整数标志的最佳方法?

kotlin - 从 lambdas 或 Kotlin : 'return' is not allo

android - kotlin 中的重载解析歧义错误

android - 从 Activity Kotlin 中获取额外的字符串

kotlin - 从 Kotlin 中的密封类扩展数据类

java - 如何在 Java 中访问 Kotlin 伴随对象?

kotlin - 做任何==对象