Android gradle buildTypes: Duplicate class

痞子三分冷 提交于 2020-02-12 08:24:46

问题


I'm converting my app to use gradle, and I'm trying to use the buildTypes. I have a Constants class which I wish to modify for my release build. So I have a file at src/main/java/my/package/name/Constants.java and at src/release/java/my/package/name/Constants.java.

When I try to build this, gradle tells me the build failed on the Constants file in my release buildtype, with the error that it's a duplicate class.

I also tried adding a different sourceSet for this in my build.gradle like this:

sourceSets {
    main {
        java.srcDirs = ['src/main/java'];
        //...
    }
    release {
        java.srcDirs = ['src/release/java'];
    }
}

But this still gives me the same error. So I'm wondering, what am I doing wrong here?


回答1:


You can not have a class in main and release. You need to split it into something like debug and release.

gradle will merge the source sets for each buildType with main.
This is the reason, why the class gets duplicated in your release build.

So the rule is: put a class into main, or in every buildType but not both.




回答2:


The answer from "fix" helped me on the way, but I got an error from the main Gradle, saying a constant was missing (in my class Config). This since I had my class only in paid and free version and not in main. Could not find the Config class. Im not sure if this is a bug in Studio... I finally solved it with the following:

buildTypes {

    release {
        ...
        buildConfig "public static final boolean XYZ = false;"
    }

}

And the instead of using my Config.XYZ class constant I used buildConfig.XYZ



来源:https://stackoverflow.com/questions/18782368/android-gradle-buildtypes-duplicate-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!