Can’t add AdMob to my project: My project is ReactNative and AdMob needs JAVA

Ok so it looks as though you’re trying to put Java and JavaScript code in the same file, which won’t work.

In your included javascript project snippet, this whole section:

//problems start from here. This is the copied text from https://developers.google.com/admob/android/quick-start?hl=es#import_the_mobile_ads_sdk
package ...
import ...
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
    }
}
//this is where they end

Would need to be in a separate Java file.

Now, I see you’re using react-native. I would need to know if you’re using standard react-native or an expo managed project.

If you are using an expo managed project, there is already a package made for admob, just follow the instructions in the documentation here: https://docs.expo.io/versions/v39.0.0/sdk/admob/

If you are just using react native, it gets quite a bit more complicated. The docs that you provided are intended for Java / Kotlin native development opposed to native development through react, so it will be hard for you to get it working using the code from that document. This link should help you get it working for react-native: https://dev.to/srajesh636/how-to-show-ads-in-react-native-lcj

*note that in the included docs provided by google, where you see package ... and import ..., you’re expected to fill in your actual package name, and any other imports you need for the class.

Hopefully this has provided some insight on your problem, let me know if there are still any issues.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top