capacitor-community / admob

Community plugin for using Google AdMob
MIT License
209 stars 67 forks source link

TypeError: Cannot read property 'initialize' of undefined #60

Closed inspire22 closed 3 years ago

inspire22 commented 3 years ago

Describe the bug I've followed all the steps, editing MainActivity.java, etc but AdMob is still undefined upon importing. Looking for tips on how to debug further, as I've hit a wall.

import {Plugins} from "@capacitor/core"; const {AdMob, App, Device} = Plugins;

Then in created() I call: AdMob.initialize();

It compiles just fine and starts up on the emulator or my phone but then gives this error: 2021-01-09 16:17:15.684 15158-15158/com.kevins.app E/Capacitor/Console: File: http://localhost/js/chunk-vendors.458f2ac8.js - Line 7 - Msg: TypeError: Cannot read property 'initialize' of undefined

I'm using vue 2.x, but I don't think it matters.

Does android studio need to build the plugin or something first? I've checked after 'ionic cap sync android' and there's a 'capacitor-cordova-android-plugins' directory, but I'm not sure what to look for in there.

Plugin Issue or your implements issue? I've tried to adapt the code examples for vue, so I think they're pretty correct. It feels like the problem is happening pretty early in the startup process.

There's no errors or warnings in the android build output.

On the web mode, it does show: web-runtime.js:38 Uncaught (in promise) AdMob does not have web implementation.

Expected behavior Had hoped an ad would show :)

Logcat

2021-01-09 16:17:15.064 15158-15158/com.kevins.app I/Capacitor/Console: File: http://localhost/js/app.f79a5628.js - Line 1 - Msg: admob init 2021-01-09 16:17:15.067 15158-15299/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-198e3942.22b01d4d.js 2021-01-09 16:17:15.081 15158-15335/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->getUnsafe()Lsun/misc/Unsafe; (greylist,core-platform-api, linking, allowed) 2021-01-09 16:17:15.082 15158-15335/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->objectFieldOffset(Ljava/lang/reflect/Field;)J (greylist,core-platform-api, linking, allowed) 2021-01-09 16:17:15.082 15158-15335/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->putObject(Ljava/lang/Object;JLjava/lang/Object;)V (greylist, linking, allowed) 2021-01-09 16:17:15.082 15158-15335/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed) 2021-01-09 16:17:15.089 15158-15450/com.kevins.app D/NetworkSecurityConfig: No Network Security Config specified, using platform default 2021-01-09 16:17:15.089 15158-15451/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed) 2021-01-09 16:17:15.093 15158-15299/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-11e5012f.f0be9f85.js 2021-01-09 16:17:15.094 15158-15299/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-198e3942.22b01d4d.js 2021-01-09 16:17:15.096 15158-15302/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-1bef398e.8f7a182e.js 2021-01-09 16:17:15.104 15158-15302/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-1f16206c.252d54f9.js 2021-01-09 16:17:15.108 15158-15318/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-1bef398e.8f7a182e.js 2021-01-09 16:17:15.111 15158-15335/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed) 2021-01-09 16:17:15.112 15158-15335/com.kevins.app I/chatty: uid=10133(com.kevins.app) CapacitorPlugin identical 1 line 2021-01-09 16:17:15.121 15158-15335/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed) 2021-01-09 16:17:15.130 15158-15318/com.kevins.app D/Capacitor: Handling local request: http://localhost/js/chunk-1f16206c.252d54f9.js 2021-01-09 16:17:15.133 15158-15455/com.kevins.app W/m.kevins.ap: Accessing hidden method Lsun/misc/Unsafe;->compareAndSwapObject(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z (greylist, linking, allowed)

.. some more chunk loading 2021-01-09 16:17:15.684 15158-15158/com.kevins.app E/Capacitor/Console: File: http://localhost/js/chunk-vendors.458f2ac8.js - Line 7 - Msg: TypeError: Cannot read property 'initialize' of undefined

Thank you!

distante commented 3 years ago

You have to import the plugin from capacitor community, not core.

inspire22 commented 3 years ago

Hmm, the readme says core for the main Plugins access. AdSize/AdPosition are in @capacitor-community/admob though

import {Plugins} from "@capacitor/core"; const {AdMob} = Plugins;

Right?

My theory was that maybe something's wrong with android autoloading my plugins for some reason at startup, but I don't get any error messages and can't figure out how to debug it...

distante commented 3 years ago

Yes, sorry you are right. IT should be imported from Plugins. I was wrong.

Did you logged your Plugins object to see what does it has?

mnkprs commented 3 years ago

Any fixes on that?

inspire22 commented 3 years ago

I'd been using logcat to view messages which doesn't show objects. I just figured out I can inspect with chrome using chrome://inspect/#devices

The plugins object definitely doesn't include AdMob in it - any idea how to debug the loading of plugins?

My bootup does include the line "E/Capacitor: Unable to read file at path public/plugins" twice - which is probably related. plugins is {"App":{},"Accessibility":{},"Geolocation":{},"Device":{},"Keyboard":{},"StatusBar":{},"PushNotifications":{},"BackgroundTask":{},"Photos":{},"Storage":{},"LocalNotifications":{},"Toast":{},"Modals":{},"Network":{},"Permissions":{},"SplashScreen":{},"Camera":{},"Clipboard":{},"Haptics":{},"Filesystem":{},"WebView":{},"Share":{},"Browser":{},"Motion":{"config":{"name":"Motion"},"loaded":false,"listeners":{},"windowListeners":{"accel":{"registered":false,"windowEventName":"devicemotion","pluginEventName":"accel"},"orientation":{"registered":false,"windowEventName":"deviceorientation","pluginEventName":"orientation"}}}}

I'm using "@capacitor-community/admob": "^1.2.6", "@capacitor/android": "^2.1.2", "@ionic-native/core": "^5.1", "@ionic/core": "^5.1.1", "@ionic/vue": "^0.0.9" "vue": "^2.6.11",

distante commented 3 years ago

Capacitor: Unable to read file at path public/plugins

Maybe this fixes? https://github.com/ionic-team/capacitor/issues/1889#issuecomment-731573768

arthurrmp commented 3 years ago

Been having the same problem here on Android Emulator. I don't have an real Android device to test but can confirm it is working on iOS.

Capacitor: Unable to read file at path public/plugins

Maybe this fixes? ionic-team/capacitor#1889 (comment)

I've tried but got no luck.

rdlabo commented 3 years ago

Sorry, this trouble could not be reproduced. If you still have trouble,

  1. Please try demo: https://github.com/capacitor-community/admob/tree/master/demo/angular
  2. Please introduce the repository to reproduce

Thanks.

juanmafont commented 3 years ago

I fixed this way:

https://github.com/capacitor-community/admob/issues/48#issuecomment-732331879

import { AdMobPlugin } from '@capacitor-community/admob';
import { Plugins } from '@capacitor/core';

const adMobPlugin = Plugins.AdMob as AdMobPlugin;
rdlabo commented 3 years ago

It seems to depend on the tsconfig setting, but I'm glad you solved it. Thanks!