admob
admob copied to clipboard
Apps crashes when downloading from Google PlayStore (works fine on computer, emulator and psychical device)
I have an app tested on my phone and emulators and this is working fine. When I upload the app to the google playstore and download it from their the app crash instantly. I have a log file from the Pre-launch report:
Exception java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.addView(android.view.View)' on a null object reference
at com.getcapacitor.community.admob.banner.BannerExecutor.lambda$createNewAdView$4 (BannerExecutor.java)
at com.getcapacitor.community.admob.banner.BannerExecutor.$r8$lambda$SRErdRYNdwgeYcdHD6kfXLEFvHY (BannerExecutor.java)
at com.getcapacitor.community.admob.banner.BannerExecutor$$InternalSyntheticLambda$3$24b47d65758d39384fe06c20a9cb1f6bdbad8c0af55ff1611fe89e660be5c42c$0.run (BannerExecutor.java)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:201)
at android.os.Looper.loop (Looper.java:288)
at android.app.ActivityThread.main (ActivityThread.java:7839)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1003)
I saw this issue and I got the same problem:
https://github.com/capacitor-community/admob/issues/57
I initialize the plugin like this (demo code)
import { Component } from '@angular/core';
import { AdMob } from '@capacitor-community/admob';
import { Platform } from '@ionic/angular';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss'],
})
export class AppComponent {
constructor(private platform: Platform,) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
/**
* initialize() require after platform.ready();
*/
AdMob.initialize({
requestTrackingAuthorization: true,
testingDevices: ['2077ef9a63d2b398840261c8221a0c9b'],
initializeForTesting: true,
});
});
}
}
This is my admob service.ts:
import { Injectable } from '@angular/core';
import { AdMob, BannerAdOptions, BannerAdSize, BannerAdPosition, BannerAdPluginEvents } from '@capacitor-community/admob';
@Injectable({
providedIn: 'root'
})
export class AdMobService {
appMargin: number = 0;
constructor() { }
public async bannerAd(admobId: string) {
AdMob.addListener(BannerAdPluginEvents.SizeChanged, (info: any) => {
// Subscribe Change Banner Size
this.appMargin = parseInt(info.height, 10);
if (this.appMargin > 0) {
const app: HTMLElement = document.querySelector('ion-router-outlet');
app.style.marginBottom = this.appMargin + 'px';
}
});
const options: BannerAdOptions = {
adId: admobId,
adSize: BannerAdSize.ADAPTIVE_BANNER,
position: BannerAdPosition.BOTTOM_CENTER,
margin: 0,
isTesting: true,
npa: true
};
await AdMob.showBanner(options).catch(err => console.error('Could not init Admob banner', err));
}
}
Package.json
"dependencies": {
"@angular/animations": "^14.2.5",
"@angular/common": "~14.2.5",
"@angular/core": "~14.2.5",
"@angular/forms": "~14.2.5",
"@angular/platform-browser": "~14.2.5",
"@angular/platform-browser-dynamic": "~14.2.5",
"@angular/router": "~14.2.5",
"@awesome-cordova-plugins/firebase-analytics": "^5.45.0",
"@awesome-cordova-plugins/firebase-dynamic-links": "^5.45.0",
"@awesome-cordova-plugins/social-sharing": "^5.45.0",
"@capacitor-community/admob": "^4.0.0-1",
"@capacitor/android": "^4.3.0",
"@capacitor/app": "4.0.1",
"@capacitor/core": "4.3.0",
"@capacitor/haptics": "4.0.1",
"@capacitor/keyboard": "4.0.1",
"@capacitor/status-bar": "4.0.1",
"@ionic/angular": "^6.3.0",
"@ionic/storage-angular": "^3.0.6",
"animate.css": "^4.1.1",
"chart.js": "^3.9.1",
"cordova-plugin-firebase-analytics": "^7.0.5",
"cordova-plugin-firebase-dynamiclinks": "^7.0.2",
"cordova-plugin-x-socialsharing": "^6.0.4",
"cordova-sqlite-storage": "^6.0.0",
"cordova-support-android-plugin": "^2.0.4",
"es6-promise-plugin": "^4.2.2",
"localforage-cordovasqlitedriver": "^1.8.0",
"primeicons": "^6.0.1",
"primeng": "^14.1.2",
"rxjs": "~7.5.7",
"tslib": "^2.4.0",
"zone.js": "~0.11.8"
},
"devDependencies": {
"@angular-devkit/build-angular": "~14.2.5",
"@angular-eslint/builder": "~14.1.2",
"@angular-eslint/eslint-plugin": "~14.1.2",
"@angular-eslint/eslint-plugin-template": "~14.1.2",
"@angular-eslint/template-parser": "~14.1.2",
"@angular/cli": "~14.2.5",
"@angular/compiler": "~14.2.5",
"@angular/compiler-cli": "~14.2.5",
"@angular/language-service": "~14.2.5",
"@capacitor/cli": "4.3.0",
"@ionic/angular-toolkit": "^7.0.0",
"@types/jasmine": "~4.3.0",
"@types/jasminewd2": "~2.0.10",
"@types/node": "^18.8.3",
"@typescript-eslint/eslint-plugin": "5.39.0",
"@typescript-eslint/parser": "5.39.0",
"eslint": "^8.25.0",
"eslint-plugin-import": "2.26.0",
"eslint-plugin-jsdoc": "39.3.6",
"eslint-plugin-prefer-arrow": "1.2.3",
"jasmine-core": "~4.4.0",
"jasmine-spec-reporter": "~7.0.0",
"karma": "~6.4.1",
"karma-chrome-launcher": "~3.1.1",
"karma-coverage": "~2.2.0",
"karma-coverage-istanbul-reporter": "~3.0.3",
"karma-jasmine": "~5.1.0",
"karma-jasmine-html-reporter": "^2.0.0",
"protractor": "~7.0.0",
"ts-node": "~10.9.1",
"typescript": "~4.8.4"
},
MainActivity
import android.os.Bundle;
import com.getcapacitor.BridgeActivity;
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
registerPlugin(com.getcapacitor.community.admob.AdMob.class);
super.onCreate(savedInstanceState);
}
}
Test ads are working fine I also get this in my LogCat
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.webkit.PacProcessor"
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.webkit.WebViewRenderProcessClient"
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.app.AppOpsManager$OnOpActiveChangedListener"
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.displayhash.DisplayHashResultCallback"
I have tried to work with the ProGuard rules but no luck :(
Can someone point me in the right direction?
you need to initialize the admod first then display ad.
Hello @kingsleylow ,
I did that in my app.module.ts like the demo code. I have found a solution: I have to set minify on false and then it works. But I wanna use the minify on true because it makes the app smaller. Are there any pro guard rules for this?
Same here
i put setTimeout(this._ad.showBanner, 1000, this.adId) this works for me
i put
setTimeout(this._ad.showBanner, 1000, this.adId)this works for me
I had the same error and this works for me. It seems that when it tries to display the banner it hasn't initialized yet and causes an error.