EddyVerbruggen / nativescript-admob

NativeScript plugin to earn some precious :moneybag::moneybag: with ads by Google AdMob
MIT License
69 stars 26 forks source link

'Error in admob.createBanner: TypeError: Cannot read property \'android\' of undefined' #60

Closed iamnailbiter closed 5 years ago

iamnailbiter commented 5 years ago

Hi All,

I'm facing issue on my nativescript-vue apps.

please check my code below

<script>
    // import Vue from 'nativescript-vue';
    import { mapState } from 'vuex';
    import Vuex from 'vuex'
    import Login from '@/components/Auth/Login'
    import Header from '@/components/Header'
    import Home from '@/components/Home'
    import Footer from '@/components/Footer'
    import Searching from '@/components/Searching'
    import Account from '@/components/Account'

    const platform = require("tns-core-modules/platform");
    const application = require("tns-core-modules/application");
    const admob = require("nativescript-admob");

    export default {
        data() {
            return {
                platform: "",
                isSearching: false,
                width: 0,
                height: 0
            };
        },
        computed: {
            token(){
                return this.$store.state.token
            },
            pageIndex(){
                return this.$store.state.pageIndex
            }
        },
        methods: {
            checkAuth(){
                if(!this.$store.getters.isLoggedIn){
                    this.$navigateTo(Login)
                }
            },
            signout() {
                this.$store.dispatch('clearData')
                this.$navigateTo(Login)
            },
            toggleSearch() {
                this.isSearching = !this.isSearching;
            }
        },
        mounted() {
            this.width = platform.screen.mainScreen.widthDIPs;
            this.height = platform.screen.mainScreen.heightDIPs;
            if (application.ios) {
                this.platform = "ios";
            } else if (application.android) {
                this.platform = "android";
            }
            setTimeout( function() {
                admob.createBanner({
                    testing: true,
                    size: admob.AD_SIZE.SMART_BANNER, // anything in admob.AD_SIZE, like admob.AD_SIZE.SMART_BANNER
                    androidBannerId: "ca-app-pub-4518453906571567/7738847196", // add your own
                    margins: {
                        // if both are set, top wins
                        //top: 10
                        bottom: 50
                    },
                }).then(
                    function() {
                        console.log("admob createBanner done");
                    },
                    function(error) {
                        console.log("admob createBanner error: " + error);
                    }
                )
            }, 4000);
        },
        components: {
            Header,
            Footer,
            Home,
            Searching,
            Account
        }
    };
</script>

error message below

JS: 'Error in admob.createBanner: TypeError: Cannot read property \'android\' of undefined'
JS: 'admob createBanner error: TypeError: Cannot read property \'android\' of undefined'

i'm already tried to put timeout but no luck.

Please help me :D

EddyVerbruggen commented 5 years ago

Are you deploying to a device or emulator? And are you using Sidekick perhaps?

iamnailbiter commented 5 years ago

It deployed to a device and yes I'm using sidekick also

this is my package.json

{
  "nativescript": {
    "id": "org.nativescript.xxx",
    "tns-ios": {
      "version": "5.3.1"
    },
    "tns-android": {
      "version": "5.3.1"
    }
  },
  "description": "xxx",
  "license": "SEE LICENSE IN <your-license-filename>",
  "repository": "<fill-your-repository-here>",
  "dependencies": {
    "nativescript-theme-core": "~1.0.4",
    "nativescript-vue": "2.2.2",
    "tns-core-modules": "5.3.1",
    "vuex": "^3.1.0"
  },
  "devDependencies": {
    "@babel/core": "7.4.3",
    "@babel/preset-env": "7.4.3",
    "axios": "0.18.0",
    "babel-loader": "8.0.5",
    "nativescript-admob": "3.3.2",
    "nativescript-dev-webpack": "~0.21.0",
    "nativescript-dom": "2.0.2",
    "nativescript-globalevents": "1.2.2",
    "nativescript-localstorage": "2.0.0",
    "nativescript-orientation": "2.2.1",
    "nativescript-platform": "1.2.4",
    "nativescript-vue-template-compiler": "2.2.2",
    "node-sass": "~4.11.0",
    "vue-loader": "15.7.0"
  },
  "gitHead": "803cec054c44c99eaa59cdc267993e6346f6cb3a",
  "readme": "NativeScript Application"
}
kidz55 commented 5 years ago

same here , using emulator and sidekick

EddyVerbruggen commented 5 years ago

Has to be this line:

https://github.com/EddyVerbruggen/nativescript-admob/blob/402c9ffc63a9e8a8972d55697a3e665e27da8a99/src/admob.android.js#L103

Not sure why that happens.. you're not running this inside the Preview app, right?

kidz55 commented 5 years ago

I finally found a solution to my problem: It is an issue with google play services version for my part.

I solved this problem by replacing the line in /nodes_module/nativescript-admob/platforms/android/include.gradle by

def googlePlayServicesVersion = project.hasProperty('googlePlayServicesVersion') ? project.googlePlayServicesVersion : '16.0.0'

iamnailbiter commented 5 years ago

Hi @EddyVerbruggen , it running on my device, not preview app and emulator.

@ThibaultGloaguen , i just tried your solution, but still no luck.. thank you

EddyVerbruggen commented 5 years ago

@iamnailbiter Did you remove the platforms/android folder after making that change, and before rebuilding the app?

Alternatively, you can try adding this:

project.ext {
    googlePlayServicesVersion = "16.0.0" // or whatever version you fancy
}

To the root of /app/App_Resources/Android/app.gradle

iamnailbiter commented 5 years ago

Hi @EddyVerbruggen ,

I did it, removing the android platform then add it again.

just trying add project.ext { googlePlayServicesVersion = "16.0.0" // or whatever version you fancy }

but seems no changes..

EddyVerbruggen commented 5 years ago

Dunno man.. did you update your Android repositories through Android Studio yet?

iamnailbiter commented 5 years ago

Hi @EddyVerbruggen ,

After update Android Repositories, this error gone JS: 'Error in admob.createBanner: TypeError: Cannot read property \'android\' of undefined' JS: 'admob createBanner error: TypeError: Cannot read property \'android\' of undefined'

it mention that admob create banner done, but no banner showed up. kindly look below logs JS: 'Treating this deviceId as testdevice: 27D5AC885D33871BBAE8D18268264E64' JS: 'admob createBanner done' chromium: [INFO:library_loader_hooks.cc(50)] Chromium logging enabled: level = 0, default verbosity = 0

EddyVerbruggen commented 5 years ago

Does the demo app work? If so, double-check your AdMob settings.

iamnailbiter commented 5 years ago

i can't run demo app on my device and emulator. it crashed..

EddyVerbruggen commented 5 years ago

I don't think I can help you without looking at your project, but that's more of a consultancy job than a plugin issue IMO.

iamnailbiter commented 5 years ago

well then.. anyway thank you @EddyVerbruggen I'll contact you on slack to discuss about consultancy service ed..