This shows how can you utilize Java reflection and proxies to avoid repeating code. The interface provided is a very trivial example but can definitely be expanded to more complex scenarios/use-cases. This is especially useful if you are working with multiple variants since it allows you to implement interfaces only when necessary
You can try to run the app module to see the differences between the variants. Only the paid
variant has the implementation, other variants are obtaining a proxy instance.
SDKInterface.kt - main source set
This essentially is the entrypoint for any feature that you want to "split" the implementation among the variants. The explanation is detailed in the source of this file. The overall pattern is essentially:
interface InterfaceName {
fun funcName(...)
companion object {
fun getInstance(...) {
// try to find the implementation classes
// if not found, return the default impl or a Proxy
}
}
}
SDKInterfaceImpl.kt - paid source set
The actual implementation. It is only implemented here and not anywhere else on other flavors
This will definitely work with Proguard and R8. It is not included here, I leave that as an assignment to the reader
Hint: Learn about @Keep
and -keepclassmembers