I’m having trouble with integrating AdMob in my React Native app. After initializing AdMob, the Android app crashes.
The initialization appears to be successful ( from console log ), but then the app crashes on Android devices.
Code:
import React, { useRef } from 'react';
import { View, Platform } from 'react-native';
import mobileAds, { BannerAd, BannerAdSize, TestIds, useForeground } from 'react-native-google-mobile-ads';
const adUnitId = __DEV__ ? TestIds.BANNER : process.env.BANNER_AD_UNIT_ID || '';
function App() {
const [loaded, setLoaded] = React.useState(false);
const bannerRef = useRef<BannerAd>(null);
React.useEffect(() => {
mobileAds().initialize()
.then(() => {
setLoaded(true);
console.log('AdMob initialized successfully');
})
.catch((error) => console.error('AdMob initialization error:', error));
}, []);
// Only use useForeground for iOS
React.useEffect(() => {
if (Platform.OS === 'ios') {
useForeground(() => {
bannerRef.current?.load();
});
}
}, []);
if (!loaded) return null;
if (!adUnitId) {
console.log('Ad Unit ID is not set');
return null;
}
return (
<View style={{ height: 50 }}>
<BannerAd
ref={bannerRef}
unitId={adUnitId}
size={BannerAdSize.BANNER}
/>
</View>
);
}
export default App;
I have correctly configured android_app_id in app.json.
I have also set the environment variable BANNER_AD_UNIT_ID correctly.
Logcat shows “AdMob initialized successfully” but then the app crashes.
2