How to exclude gradle dependency from specific build flavour on Android

93 views Asked by At

I have React-Native application and trying to integrate facebook sdk. My app on android side has different build flavours, let's name one of them C, which actually doesn't need any 3rd party dependencies and I need to keep this integration only for all other flavours, let's name them A and B.

According to official documentation I need to include this dependency into build.gradle file

implementation 'com.facebook.android:facebook-android-sdk:latest.release'

and do some work for AndroidManifest.xml

<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/><meta-data android:name="com.facebook.sdk.ClientToken" android:value="@string/facebook_client_token"/>

in RN to include this native module, I need to create file

import com.facebook.FacebookSdk;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

class FacebookModule(reactContext: ReactApplicationContext) :
    ReactContextBaseJavaModule(reactContext) {

    override fun getName() = "FacebookModule"

    @ReactMethod
    fun setAutoLogAppEventsEnabled(isEnabled: Boolean) {
        FacebookSdk.setAutoLogAppEventsEnabled(isEnabled)
    }
}

I need to also include this new one to packages list

import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
//...

class AppPackages : ReactPackage {
    override fun createNativeModules(reactContext: ReactApplicationContext): MutableList<NativeModule> =
        listOf(
            SomeModule(reactContext),
            FacebookModule(reactContext),
        ).toMutableList()
    //...
}

which is usually connected in MainApplication.java

//...
        @Override
        protected List<ReactPackage> getPackages() {
          List<ReactPackage> packages = new PackageList(this).getPackages();
          // packages.add(new MyReactNativePackage());
            packages.add(new AppPackages());
          return packages;
        }
//...

MainApplication.java and AppPackages files are created inside main folder (which is basically shared for all flavours). But I have own OtherApplication.java inside folder for flavour C (it has also own main activity, let's name OtherActivity)

If I keep it as is, the flavours A and B run succesfully, but I got error for flavour C that facebook sdk is not initialised, which means basically that gradle includes this dependency while compiling, but final build has no connection to FacebookModule file and no initialisation happens.


So, I want to ask an advice, what are the possible ways to include specified dependency in build.gradle for all flavours but excluding flavour C?

1

There are 1 answers

0
Deepak On

Try adding no-op in the dependency on that flavor in which you want to exclude example:

    if (project.env.get("ENV") == "C") {
        implementation 'com.facebook.android:facebook-android-sdk-no-op:latest.release'
    } else {
        implementation 'com.facebook.android:facebook-android-sdk:latest.release'
    }