Creating a Custom Build for react native - facebook ads

Creating a Custom Build for react native - facebook ads

expo install react-native-fbsdk-next react-native-fbads
npm install -g eas-cli
 npx expo install expo-dev-client

In app.json add this

"plugins": [
      [
        "react-native-fbsdk-next",
        {
          "appID": "48127127xxxxxxxx",
          "clientToken": "c5078631e4065b60d7544a95xxxxxxxx",
          "displayName": "RN SDK Demo",
          "advertiserIDCollectionEnabled": false,
          "autoLogAppEventsEnabled": false,
          "isAutoInitEnabled": true,
          "iosUserTrackingPermission": "This identifier will be used to deliver personalized ads to you."
        }
      ],
      "./react-native-fbads.js"
    ],

Create a file named "react-native-fbads.js" and inside it paste

const {
    AndroidConfig,
    createRunOncePlugin,
    withAndroidManifest,
  } = require('@expo/config-plugins');

  const { getMainApplicationOrThrow, prefixAndroidKeys } = AndroidConfig.Manifest;

  const INTERSTITIAL_AD_ACTIVITY = 'com.facebook.ads.InterstitialAdActivity';

  const withFacebookManifest = (config) => {
    return withAndroidManifest(config, (config) => {
      config.modResults = setFacebookConfig(config.modResults);
      return config;
    });
  };

  function setFacebookConfig(androidManifest) {
    let mainApplication = getMainApplicationOrThrow(androidManifest);
    mainApplication = ensureFacebookActivity({ mainApplication });

    return androidManifest;
  }

  function ensureFacebookActivity({ mainApplication }) {
    if (Array.isArray(mainApplication.activity)) {
      // Remove all Facebook InterstitialAdActivity first
      mainApplication.activity = mainApplication.activity.filter((activity) => {
        return activity.$?.['android:name'] !== INTERSTITIAL_AD_ACTIVITY;
      });
    } else {
      mainApplication.activity = [];
    }

    mainApplication.activity.push(getFacebookAdActivity());
    return mainApplication;
  }

  function buildXMLItem({ head, children }) {
    return { ...(children ?? {}), $: head };
  }

  function getFacebookAdActivity() {
    /**
  <activity
    android:name="com.facebook.ads.InterstitialAdActivity"
    android:configChanges="keyboardHidden|orientation"
  />
     */
    return buildXMLItem({
      head: prefixAndroidKeys({
        name: INTERSTITIAL_AD_ACTIVITY,
        configChanges: 'keyboardHidden|orientation',
      }),
    });
  }

  /**
   * Apply react-native-fbads configuration for Expo SDK 44 projects.
   */
  const withReactNativeFbads = (config) => {
    return withFacebookManifest(config);
  };

  const pkg = require('./node_modules/react-native-fbads/package.json');

  module.exports = {
    withFacebookManifest,
    setFacebookConfig,
    default: createRunOncePlugin(withReactNativeFbads, pkg.name, pkg.version),
  };

Type in terminal

eas build --profile development --platform android