Skip to content
Advertisement

Multiple sourcesets configurations

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:

  1. app/src/main/java/...
  2. 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 BLEUtil class in dev as well, with the same API, perhaps implemented as a no-op; or

  • Have two MainActivity implementations, one in dev and one in ble, where only the one in ble uses BLEUtil, and perhaps with some common base class in main that has the common functionality; or

  • Have MainActivity inherit from a FlavoredActivity, where you have implementations of FlavoredActivity in both dev and ble, where the latter uses BLEUtil

There may be other approaches as well.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement