How to import flavour specific service from main/shared code?

19 Views Asked by At

I have a multi-flavor project which looks like this :

+ src
    + main // common code
        + java 
            - MainActivity
    + flavorOne
        + java
            - MyService // buildvariant- specific implementation of a Service
    + flavorTwo
        + java
            - MyService // again a buildvariant-specific implementation of a Service

The Service also has some static functions, and some constants, for example :

 @JvmStatic
    fun hasDataConnectivity(): Boolean

const val CUSTOM_INTENT = "nl.mycompany.myproduct.intent.action.CUSTOM_INTENT"

I am having the following issue when building one of the variants : that any main code which references the static fun hasDataConnectivity() or the const val will need an import for MyService. This import (packagename) obviously looks different for both variants, so I am only able to build one.

I thought that placing a flavour-specific implementation of MyService in the flavour's source set would work, but unfortunately now I am having this issue.

I'm probably not the first one encountering this situation so is there a way to solve this ?

I tried an implementation using an Interface which is defined in main and inherited in the MyService implementations, but I was still struggling with the imports because I need to create an instance/object of the Service implementing the Interface.

Last but not least : I still have import-trouble because this being a Service which is started from another part in this manner :

ContextCompat.startForegroundService(
                    this,
                    Intent(this, MyService::class.java)
                )

Again, how do I import a flavour specific Service (or class for that matter) implementation from within main code ?

0

There are 0 best solutions below