I need to generate AAR and APK from the same module base on different build flavor.
However APK requires an applicationId, while library doesn't. I have to add if-case inside build.gradle. Is there any better way to achieve the same outcome ?
My current gradle file:
productFlavors {
generateAar {
versionCode buildVersionCode()
versionName getPackageName()
}
generateApk {
if (!library) {
applicationId "appId"
}
versionCode buildVersionCode()
versionName getPackageName()
}
A module can either be a library or application, never both.
You should probably consider wrapping all your code into a library module which can be used by the application module (only manifest and launcher resource drawables)
That way your logic can be packaged as an aar artifact from the library and your app module is able to use it either directly if in the same project or as a packaged artifact if in a separate project.
PS: when working with a local aar the transitive dependencies on the artifact need to be explicitly defined in the application module that’s dependent on it.
Additionally, depending on your use case you might want to consider how you approach modularisation of your project either by features or layer (data, domain, presentation etc)