安卓java类重复,Android的gradle这个buildTypes:重复类

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?

解决方案

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

gradle will merge the source sets for each buildType with main.

This is the reason, why the class get's duplicated in your release build.

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