I have two productFlavors
productFlavors {
dev {
...
}
ble {
...
}
}
Now, I created a different source set for ble where I added few extra Java classes.
So I have two sets of directories now:
app/src/main/java/...app/src/ble/java/...
In the ble set, I have a Java class called BLEUtil.java
app/src/ble/java/.../BLEUtil.java
I want to call a method that is in BLEUtil.java from my MainActivity
BLEUtil.startScan();
When the build variant is set to bleDebug, it works great because BLEUtil.java exist.
However, when I change my build variant to devDebug, BLEUtil.java does not exist so BLEUtil.startScan() throws an error and I can’t build the app.
error: cannot find symbol class BLEUtil
Do I have to comment and uncomment those lines out manually or is there any other way?
Advertisement
Answer
Assuming that MainActivity is in main, you can:
Have a
BLEUtilclass indevas well, with the same API, perhaps implemented as a no-op; orHave two
MainActivityimplementations, one indevand one inble, where only the one inbleusesBLEUtil, and perhaps with some common base class inmainthat has the common functionality; orHave
MainActivityinherit from aFlavoredActivity, where you have implementations ofFlavoredActivityin bothdevandble, where the latter usesBLEUtil
There may be other approaches as well.