AppLovin / AppLovin-MAX-React-Native

MIT License
55 stars 29 forks source link

App crashes after adding AppLovin code #124

Closed SohelKabir closed 1 year ago

SohelKabir commented 1 year ago

MAX Module Version

4.0.0

React Native Version

0.63.3

Device/Platform Info

Android 12

Current Behavior

When i add the code to my app after installing applovin sdk to my react native project it crashes without any error.

The code i have placed in one of my screen which is a react component.

  AppLovinMAX.initialize(
    'sdkkey',
    configuration => {
      // SDK is initialized, start loading ads
      // initializeInterstitialAds();
    },
  );
  const INTERSTITIAL_AD_UNIT_ID = Platform.select({
    android: 'adId',
    ios: 'YOUR_IOS_INTERSTITIAL_AD_UNIT_ID',
  });

  if (AppLovinMAX.isInterstitialReady(INTERSTITIAL_AD_UNIT_ID)) {
    AppLovinMAX.showInterstitial(INTERSTITIAL_AD_UNIT_ID);
  }

  function loadInterstitial() {
    AppLovinMAX.loadInterstitial(INTERSTITIAL_AD_UNIT_ID);
  }

  const [retryAttempt, setRetryAttempt] = useState(0);

  function initializeInterstitialAds() {
    AppLovinMAX.addEventListener('OnInterstitialLoadedEvent', () => {
      // Interstitial ad is ready to be shown. AppLovinMAX.isInterstitialReady(INTERSTITIAL_AD_UNIT_ID) will now return 'true'

      // Reset retry attempt
      setRetryAttempt(0);
    });
    AppLovinMAX.addEventListener('OnInterstitialLoadFailedEvent', () => {
      // Interstitial ad failed to load
      // We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds)

      setRetryAttempt(retryAttempt + 1);
      var retryDelay = Math.pow(2, Math.min(6, retryAttempt));

      console.log(
        'Interstitial ad failed to load - retrying in ' + retryDelay + 's',
      );

      setTimeout(function () {
        loadInterstitial();
      }, retryDelay * 1000);
    });

    AppLovinMAX.addEventListener('OnInterstitialHiddenEvent', () => {
      loadInterstitial();
    });

    // Load the first interstitial
    loadInterstitial();
  }
  useEffect(() => {
    initializeInterstitialAds();
  }, []);

Expected Behavior

No response

How to Reproduce

Just install the latest sdk and add the code to any component.

Additional Info

No response

thomasmso commented 1 year ago

@SohelKabir - What is the crash you are experiencing?

SohelKabir commented 1 year ago

@thomasmso there is no errors showing when it crashes. I also checked our crashlytics dashboard in firebase, did not see any crash report. Crash happens only I add above code in our code base.

alhiwatan commented 1 year ago

@SohelKabir , has AppLovinMAX been initialized? Can you use AppLovinMAX.isInitialized() to verify the initialization before loading the ads?

SohelKabir commented 1 year ago

I have solved the problem, it was problem with this statement,

  if (AppLovinMAX.isInterstitialReady(INTERSTITIAL_AD_UNIT_ID)) {
    AppLovinMAX.showInterstitial(INTERSTITIAL_AD_UNIT_ID);
  }

moving this "if" statement inside the eventlistener "OnInterstitialLoadedEvent" block solved the crashing issue.

thomasmso commented 1 year ago

@alhiwatan - we should probably still not crashed if our APIs are used incorrectly as above

alhiwatan commented 1 year ago

Added guard for accessing the MAX components before the SDK initialization.