[Question] : Android - Shipping dependencies depending on flavors / BuildType
We can read in the documentation that it should be possible to use platforms.android.buildTypes to specify a build type (like “debug”) to “[...] avoid shipping the library in a release build”. But it seems that this config only have an effect of the build.gradle file that import the dependency in the project, but not on the PackageList.java file that instantiate the library. Resulting in a build failure if we want to ship a package only in debug, release or for a specific flavor.
Is it something that is supposed to work ? Am I missing something in the documentation ? I don't really see how this can works if we don't ship a native module in our build, but this module is still referenced in the packageList :/
Context
We are building an app with 2 flavors “public” and “intenal” and would like our “internal” users to have a fast in-app bug reporting solution. We think it’s important to avoid shipping this library to our end users, even deactivated. We would like to keep our bundle size as low as possible.
It’s relatively easy to not bundle a JS feature, but it’s a bit more complexe when it comes to native module.
Details
It seems that the platforms.android.buildTypes is used by the following function to generate "${buildVariant}Implementation"
But not in the function that generates the packageList:
I see 3 possible differents methods to handle this problem :
- Generate as much “packageList” files as there are build variant (
build/generated/rncli/src/public/release/java,build/generated/rncli/src/public/debug/java,build/generated/rncli/src/internal/debug/java,build/generated/rncli/src/internal/release/java) - Find the buildType and flavor for the current build, and filter the package in packageList, (keep only one generated file in
build/generated/rncli/src/main/java) - Use java reflection as soon as a library have a buildType property set in
react-native.config.js
I would be glad to help and submit a pull request to fix this (IF it needs to be fixed ...I might be missing something)
(Also I’m quite new to android development/gradle/groovy and stuff so I might need some help)
Alternative
For now we use platforms: { android: null } to disable the automatic linking of the library, then we import the library by hand in build.gradle file (using InternalImplementation) and use java reflection to try to import the library and add it the the packageList if the BuildConfig.FLAVOR is equal to “internal”.