Build Flavours are commonly used in Android applications through the android gradle plugin. This allows a project to have a directory structure
src
- main
- com.stack.A.java
- debug
- com.stack.B.java
- release
- com.stack.B.java
This will only compile the correct B.java
depending on the release type that has been selected.
Is there a way to mirror this functionality without using the android gradle plugin and just using the java plugin?
It is called sourceSet in Java plugin, see https://docs.gradle.org/current/userguide/java_plugin.html
main
and test
sourceSets are created automatically, to add more sourceSets you can do something like below. Basically both releases and debug also uses codes in main
apply plugin: 'java'
sourceSets {
release {
java {
srcDirs 'src/main/java', 'src/release/java'
}
}
debug {
java {
srcDirs 'src/main/java', 'src/debug/java'
}
}
}
I've written a plugin to achieve this. Eg
apply plugin: 'com.lazan.javaflavours'
javaFlavours {
flavours = ['free', 'paid']
}
Then sources in src/free/java
and src/paid/java
and resources in src/free/resources
and src/paid/resources
Project on github: https://github.com/uklance/gradle-java-flavours
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With