AdMob Not Being Integrated

I tried integrating AdMob to my app but I keep getting error messages while it’s building for android. The code I implemented was: 

settings =

{

    {

       plugins =

       {

            [“plugin.google.play.services”] =

           {

            publisherId = “com.coronalabs”

           },

       },      

    },

     local ads = require( “ads” )

     local adProvider = “admob”

     local function adListener( event )

     if ( system.getInfo( “platformName” ) == “Android” ) then

         bannerAppID = “pub-3054113816306190”  --for Android banner

     end

    ads.init( adProvider, appID, adListener )

    --(more on this later)

    ads.show( “banner”, { x=0, y=0, appId=bannerAppID } )

    end

android =

    {

        usesPermissions =

        {

            “android.permission.INTERNET”,

            “android.permission.ACCESS_NETWORK_STATE”,

        },

    },

}

The error says “unexpected symbol near local,” referring to the “local ads”.

@ben5stars

  1. Please put your code in code blocks for future posts (see image below).   No big deal this time, but it will help us to help you and that is the goal.

formatyourcode.jpg

  1. Question: Is the code you showed all in one file?  If so, that is not right.

This goes in build.settings (a file with this exact name)

-- For more details on this file and what you can do with it, look here: -- https://docs.coronalabs.com/guide/distribution/buildSettings/index.html -- Find more permissions here: http://developer.android.com/reference/android/Manifest.permission.html settings = { plugins = { -- ================================= -- Ads -- ================================= ["plugin.google.play.services"] = { publisherId = "com.coronalabs" }, }, android = { usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_NETWORK\_STATE", -- Find more permissions here: -- http://developer.android.com/reference/android/Manifest.permission.html }, }, }

The other code you had was…well it didn’t look right.  Try the code you’ll find here: https://forums.coronalabs.com/topic/57277-google-admob-test-ads-are-not-showing-up-in-device/

 

See my “07 June 2015 - 09:24 AM” entry in this post.

Also, please remember to test with testmode enabled (also talked about in the thread I posted a link to).

If you run ads on your device and click them, Google WILL cancel your account.  Maybe not the first time, but they notice this and will whack you in a serious way.  

So, don’t risk it.

Just remember to disable test mode before submitting to the store.

Yes, it’s all in the “build.settings”. But the thing is, when I’m finished and start “build for android,” it comes back with an error I described in the end. I looked at all the suggestions I could find about AdMob, but you’re the first to suggest an edit in the permissions. Also, I don’t think I should specify the coordinates for the banner ad; I just leave it (0,0). If you could dive deeper with my errors like if there is any I made in the “locals”, I’d really appreciate it. Thanks. 

Also, the test mode I copied from one of your replies isn’t working.

  1. Use the build settings file I supplied above, not yours.

  2. My code definitely works. I’m using it and two other folks are using it just recently.  (Seems admob is suddenly a popular forums question theme)  Please check for typos in things like your app id, etc.

For example this does not look right:

"pub-3054113816306190" 

Unless you custom specified the ID when you created the admob app, it should look something like this:

"ca-app-pub-2064790293916744/655311234"

PS - In case it is not clear, all the code you posted is not supposed to be in build.settings.  You are mixing settings and functional code.

You need these files in your project at a minimum:

  • config.lua
  • build.settings - Try my exact code above, then add more settings after you get this working.
  • main.lua - This is where the calls to require, and showAd(), etc should be (assuming there are no additional files in your app/game).

So this is my updated build.settings:

settings = {    plugins =       {       -- =================================       -- Ads       -- =================================       ["plugin.google.play.services"] = { publisherId = "com.coronalabs" },     },    android =     {       usesPermissions =        {          "android.permission.INTERNET",          "android.permission.ACCESS\_NETWORK\_STATE",       },    },         local ads=require("ads")     local function adListener( event )     for k,v in pairs( event )       print("adListener ", k, v ) -- so you see everything you get      end     end ads.init("admob", bannerID, adListener ) ads:setCurrentProvider("admob") ads.load("admob", { appId = bannerID, testMode = true } ) showAd( "top", true )            local unusedWidth = display.actualContentWidth - display.contentWidth local unusedHeight = display.actualContentHeight - display.contentHeight local left = 0 - unusedWidth/2 local top = 0 - unusedHeight/2 local right = display.contentWidth + unusedWidth/2 local bottom = display.contentHeight + unusedHeight/2     }

What’s missing? Also, I couldn’t get it to test mode.

You’re not reading my instructions carefully enough, so I’ll give you explicit code.

Put this in build.settings

settings = { plugins = { ["plugin.google.play.services"] = { publisherId = "com.coronalabs" }, }, android = { usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_NETWORK\_STATE", }, }, }

Make a main.lua file with ONLY this in it (so you can see whether it works):

local unusedWidth = display.actualContentWidth - display.contentWidth local unusedHeight = display.actualContentHeight - display.contentHeight local left = 0 - unusedWidth/2 local top = 0 - unusedHeight/2 local right = display.contentWidth + unusedWidth/2 local bottom = display.contentHeight + unusedHeight/2 local bannerID = "PUT YOUR ID CODE BETWEEN THESE QUOTES" local testMode = true local ads=require("ads") local function adListener( event ) for k,v in pairs( event ) do print("adListener ", k, v ) -- so you see everything you get end end ads.init("admob", bannerID, adListener ) ads:setCurrentProvider("admob") ads.load("admob", { appId = bannerID, testMode = testMode } ) local function showAd( adPosition, isTestMode ) local xPos, yPos local adPosition = "top" -- "top", "bottom" if adPosition == "top" then xPos, yPos = display.screenOriginX, top elseif adPosition == "bottom" then xPos, yPos = display.screenOriginX, bottom end ads:setCurrentProvider("admob") ads.show( "banner", { x = xPos, y = yPos, appId = bannerID, testMode = isTestMode } ) end showAd( "top", testMode )

Remember to put your id in this line of code:

local bannerID = "PUT YOUR ID CODE BETWEEN THESE QUOTES"

Finally, test it on your device.  This can’t be tested in the simulator.

How do you disable test mode?

Also, it’s not showing. The main.lua is:

local unusedWidth     = display.actualContentWidth - display.contentWidth local unusedHeight    = display.actualContentHeight - display.contentHeight local left            = 0 - unusedWidth/2 local top             = 0 - unusedHeight/2 local right           = display.contentWidth + unusedWidth/2 local bottom          = display.contentHeight + unusedHeight/2 local bannerID = "ca-app-pub-3054113816306190/1367368068" local testMode = true local ads=require("ads") local function adListener( event )    for k,v in pairs( event )       do print("adListener ", k, v ) -- so you see everything you get    end end ads.init( "admob", bannerID, adListener ) ads:setCurrentProvider("admob") ads.load( "admob", { appId = bannerID, testMode = testMode } ) local function showAd( adPosition, isTestMode )    local xPos, yPos    local adPosition = "top" -- "top", "bottom"    if adPosition == "top" then       xPos, yPos = display.screenOriginX, top    elseif adPosition == "bottom" then       xPos, yPos = display.screenOriginX, bottom    end    ads:setCurrentProvider("admob")    ads.show( "banner", { x = xPos, y = yPos, appId = bannerID, testMode = isTestMode } ) end showAd( "top", testMode )

Given the code above, simply change:

local testMode = true

to

local testMode = false

Rob

Off topic: the icon isn’t showing after it’s installed on a device. It looks just like your avatar, @Rob.

If you’re getting a corona logo as the Icon then you probably don’t have the right Icon files in the folder with your main.lua.  Please see:

https://docs.coronalabs.com/guide/distribution/buildSettings/index.html#appicons

And go down to the Android section.

Rob

Well, I put the code which I gave you in a blank project.  It ran perfectly (after fixing a missing ‘do’)  and showed a banner at the top.

Download this, build it, install it on your device , and run it.

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2015/06/admob.zip

If this does not work for you, then I’m stumped.  It is either something with your test device or maybe your internet connection… I really don’t know, but this code is solid and now…tested on a Gen 1 Nexus 7.

It worked. Apparently, I didn’t insert the right format of my ID. But it is displaying on top which is blocking some details. How do I get it to display at the bottom?   

Read the code I gave you.  It has everything you need, spelled out.

The function I wrote: showAd() takes a position parameter.

Well like @roaminggamer said, it appears you are not reading the instructions or even the comments.

See from the code you already have:

   local adPosition = “top” – “top”, “bottom”

Now what do you see :slight_smile: ?

@ben5stars

  1. Please put your code in code blocks for future posts (see image below).   No big deal this time, but it will help us to help you and that is the goal.

formatyourcode.jpg

  1. Question: Is the code you showed all in one file?  If so, that is not right.

This goes in build.settings (a file with this exact name)

-- For more details on this file and what you can do with it, look here: -- https://docs.coronalabs.com/guide/distribution/buildSettings/index.html -- Find more permissions here: http://developer.android.com/reference/android/Manifest.permission.html settings = { plugins = { -- ================================= -- Ads -- ================================= ["plugin.google.play.services"] = { publisherId = "com.coronalabs" }, }, android = { usesPermissions = { "android.permission.INTERNET", "android.permission.ACCESS\_NETWORK\_STATE", -- Find more permissions here: -- http://developer.android.com/reference/android/Manifest.permission.html }, }, }

The other code you had was…well it didn’t look right.  Try the code you’ll find here: https://forums.coronalabs.com/topic/57277-google-admob-test-ads-are-not-showing-up-in-device/

 

See my “07 June 2015 - 09:24 AM” entry in this post.

Also, please remember to test with testmode enabled (also talked about in the thread I posted a link to).

If you run ads on your device and click them, Google WILL cancel your account.  Maybe not the first time, but they notice this and will whack you in a serious way.  

So, don’t risk it.

Just remember to disable test mode before submitting to the store.

Yes, it’s all in the “build.settings”. But the thing is, when I’m finished and start “build for android,” it comes back with an error I described in the end. I looked at all the suggestions I could find about AdMob, but you’re the first to suggest an edit in the permissions. Also, I don’t think I should specify the coordinates for the banner ad; I just leave it (0,0). If you could dive deeper with my errors like if there is any I made in the “locals”, I’d really appreciate it. Thanks.