Got Admob working for Android :)

EDIT: Added HoneyComb UA fix (check android ad htmls
EDIT: Change made for latest corona.

So, I saw alot of complaints about this and people really strugling with it. I finally found a bit of time looking at it.

I tested this on Corona 243, and daily 317. Other versions may have issues I don’t know.

EDIT: This has developed to a completely working solution on all devices! Instead of leaving the code fragmented across posts I’ve moved EVERYTHING you need to this first post:

Tested on Corona upto latest daily, and also including daily 319 for android.

Step 1)

You MUST add the internet permissions to the build.settings file. For the sake of completness here’s a build.settings file that you can drop in place:

settings = {  
 orientation =  
 {  
 default = "portrait",  
 supported =  
 {  
 "portrait",  
 },  
 },  
 androidPermissions =  
 {  
 "android.permission.INTERNET",  
 "android.permission.ACCESS\_NETWORK\_STATE"  
 },  
}  

Step 2) Sign up for admob and create a new ‘site’. Pick Android to get an android id - you’ll use this in step 3. Create a second site and pick ‘iphone’ to get an apple id.

Ignore the sdk download, as you can’t use it with corona. Edit the site and set the refresh rate to 20 seconds (this ensures an ad is refreshed on their side ready for when you http refresh your page).

Step 3) Create the following files from the code below:

main.lua - add this code to your main.lua and use DisplayAd(delay) to show and ad after ‘delay’ milliseconds. Use RemoveAd(delay) to remove it after delay milliseconds.

android_ad.html - This is the html file for showing ads on all android devices with zoom level <= 1.5. Change the admob id to the one you received when you created the '‘site’ in admob.

android_ad15.html - This is the html file for showing ads on all android devices with a zoom level > 1.5. Change the admob id to the one you received when you created the '‘site’ in admob. It should be the same one as above.

apple_ad.html - This handled ALL apple devices. I suggest you create a SECOND site in admob and use a seperate id for this, so you can see how much revenue you are generating from apple vs android.

You should also change the ‘test:’ line to ‘false’ when you want to see real ads.

– main.lua

local adSpace  
local isAndroid = "Android" == system.getInfo("platformName")  
  
function showAd\_Android(event)  
 -- Is the url a remote call?  
 if string.find(event.url, "android\_ad.html", 1, false) or string.find(event.url, "android\_ad15.html", 1, false) then  
 return true  
 else  
 system.openURL(string.gsub (event.url, "Corona:", ""))  
 -- Refresh ad  
 RemoveAd(0)  
 DisplayAd(0)  
 return true  
 end  
end  
  
local function showAd\_Apple(event)  
 -- Is the url a remote call?  
 if string.find(event.url, "http://", 1, false) == 1 then  
 -- Is it a call to the admob server?  
 if string.find(event.url, "c.admob.com", 1, false) == nil then  
 adSpace.url = event.url  
 else  
 -- an actual click on an ad, so open in Safari  
 system.openURL(event.url)  
 -- Refresh ad  
 RemoveAd(0)  
 DisplayAd(0)  
 end  
 else  
 -- Feb 1, 2011: if using the old version of this code, remove the  
 -- following line:  
 -- adSpace.url = event.url  
 -- and put in this line instead:  
 return true  
 end  
end  
  
function DisplayAd(t)  
 native.cancelWebPopup()  
 timer.performWithDelay(t, function()  
 local adfile = "apple\_ad.html"  
 local sizeX = 320  
 local sizeY = 48  
 local scale = 1/display.contentScaleY  
  
 if isAndroid then  
 if scale \> 1.5 then   
 adfile = "android\_ad15.html"  
 sizeX = sizeX\*(1.5/scale) + 1  
 sizeY = sizeY\*(1.5/scale) + 1  
 else  
 adfile = "android\_ad.html"  
 end  
 adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Android})  
 elseif system.getInfo( "environment" ) == "simulator" then  
 adSpace = display.newRect( (display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY )  
 display.getCurrentStage():insert(adSpace, false)  
 else  
 if scale \> 1.5 then   
 sizeX = sizeX\*(1.5/scale) + 1  
 sizeY = sizeY\*(1.5/scale) + 1  
 end  
 adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Apple})  
 end  
 end  
 )  
end  
  
function RemoveAd(t)  
 timer.performWithDelay(t, native.cancelWebPopup)  
 timer.performWithDelay(t, function() adSpace:removeSelf() end)  
end  

android_ad.html

br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; user-scalable=0;">  
<meta http-equiv="Refresh" content="30">

<title>ad</title>
  
  
  
<script type="text/javascript"><br>	var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> ama: false, // set to true and retain comma for the AdMob Adaptive Ad Unit, a special ad type designed for PC sites accessed from the iPhone. More info: http://developer.admob.com/wiki/IPhone#Web_Integration<br> test: false // test mode, set to false to receive live ads<br>	};<br>	</script>  
<script type="text/javascript"><br> var userAgentTemp = navigator.userAgent;<br> if (!(RegExp("Android (1|2).").test(userAgentTemp))) {<br> navigator = {}<br> if (userAgentTemp.search(/Android/) != -1) {<br> navigator.userAgent = userAgentTemp.replace(/Android 3[&#092;.&#092;d]*/, "Android 2.3.3");<br> } else {<br> navigator.userAgent = userAgentTemp + "; Android 2.3.3";<br> };<br> };<br> </script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br>	</script>  
  
[/html]  

android_ad15.html

br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; initial-scale=1.5; minimum-scale=1.5; maximum-scale=1.5; user-scalable=0;">
  
<meta http-equiv="Refresh" content="30">
  
<title>ad</title>
  
  
  
<script type="text/javascript"><br>	var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> ama: false, // set to true and retain comma for the AdMob Adaptive Ad Unit, a special ad type designed for PC sites accessed from the iPhone. More info: http://developer.admob.com/wiki/IPhone#Web_Integration<br> test: false // test mode, set to false to receive live ads<br>	};<br>	</script>  
<script type="text/javascript"><br> var userAgentTemp = navigator.userAgent;<br> if (!(RegExp("Android (1|2).").test(userAgentTemp))) {<br> navigator = {}<br> if (userAgentTemp.search(/Android/) != -1) {<br> navigator.userAgent = userAgentTemp.replace(/Android 3[&#092;.&#092;d]*/, "Android 2.3.3");<br> } else {<br> navigator.userAgent = userAgentTemp + "; Android 2.3.3";<br> };<br> };<br> </script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br>	</script>  
  
[/html]  

apple_ad.html

br\> "http://www.w3.org/TR/html4/loose.dtd"\>  
  
  
  
<meta name="viewport" content="width=320; user-scalable=0;">
  
<meta http-equiv="Refresh" content="30">
  
<title>ad</title>
  
  
  
<script type="text/javascript"><br>	var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> test: false // test mode, set to false to receive live ads<br>	};<br>	</script>  
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>  
  
[/html]  

[import]uid: 8872 topic_id: 8021 reply_id: 308021[/import]

Thanks for the tutorial, i will be giving it a shot once my app gets close to release. ill report how it goes. [import]uid: 19620 topic_id: 8021 reply_id: 28651[/import]

Thanks dude this seems to have nailed it. Now if only corona would fix multi-touch (only two touches allowed at once on android) , openAL and most of the other stuff that doesnt work on android! [import]uid: 11459 topic_id: 8021 reply_id: 28703[/import]

Multi touch on android is a bit of a mess tho actually. Different devices support different things in their driver. Some only handle two touches and anything more is a ‘palm’ touch. [import]uid: 8872 topic_id: 8021 reply_id: 28793[/import]

Hi kam187,

it works … thx for sharing…
it opens in a new webpopup!

Did you use it in any android app? [import]uid: 9592 topic_id: 8021 reply_id: 29200[/import]

It works fine but it is very, very slow.
And the function is never called.

Bruno from http://blueglutton.com [import]uid: 9097 topic_id: 8021 reply_id: 29613[/import]

alen.brkicic8: Not yet. I was going to add it to the lite version of Walkabout but decided not to just yet.

bruno_gallien: It works fine here, and definatly calls the function. What version of corona do you use? and which OS version of android do you build for?
[import]uid: 8872 topic_id: 8021 reply_id: 29624[/import]

Hi,
I use the last daily build and I build for 2.2 with a 2.3.2 phone.

Bruno from http://blueglutton.com [import]uid: 9097 topic_id: 8021 reply_id: 29642[/import]

My bad I discovered I had a typo.
But now the function is called even if i don’t click on the banner and in addition the banner is not appearing.

Bruno from http://blueglutton.com [import]uid: 9097 topic_id: 8021 reply_id: 29802[/import]

Hi bruno_gallien, what is your device name? Could you post your project here? [import]uid: 9190 topic_id: 8021 reply_id: 29814[/import]

Remember the ad.html has to be called exactly that. This line here checks if its loading the ad.html or its a click:

if not string.find(event.url, “ad.html”, 1, false) then

If you change the name of your ad.html you need to change this string too. If you renamed the file it will think there was a click as soon as the ad loads. [import]uid: 8872 topic_id: 8021 reply_id: 29842[/import]

In fact there’s something I don’t understand : should the function be called only if I click or is it also used to get the banner ?

Bruno from http://blueglutton.com [import]uid: 9097 topic_id: 8021 reply_id: 29846[/import]

Its called when the page loads (ad.html) to display the banner. and ALSO if its clicked.

That line of code checks if the url is the ad page. This is how it knows if the ad page is loading, or its been clicked.

You should use the code template I posted. If you modified it or removed/renamed the file it wont work. [import]uid: 8872 topic_id: 8021 reply_id: 29847[/import]

Ok so I messed up somewhere because it was loading the banner.

Bruno from http://blueglutton.com [import]uid: 9097 topic_id: 8021 reply_id: 29856[/import]

I got the ad working, but it seems very slow. It looks like bruno_gallien is experiencing the same problem. Any ideas as to why it’s slow? [import]uid: 14218 topic_id: 8021 reply_id: 31177[/import]

Thanks for the example…i got it working but now im wondering if there is a way to remove the banner or if theres a way to add it to a group. [import]uid: 27671 topic_id: 8021 reply_id: 33330[/import]

I figured out a way to remove the ad banner…it’s probably not the best way but it works for me.

In addition to ad.html i also have a file named noad.html which is the same file provided here(http://www.ludicroussoftware.com/corona/admob-ad-support-in-corona/) that has a bunch of x’s instead of my actual pubid. it’s more of a hack but it works. [import]uid: 27671 topic_id: 8021 reply_id: 33336[/import]

What’s your fill rate with AdMob? [import]uid: 14218 topic_id: 8021 reply_id: 33385[/import]

My fill rate for the last 30 days is 89%. it’s 56% for the lats 90 days and its 89% for the last 2 years. Currently all of my apps are for Android. [import]uid: 27671 topic_id: 8021 reply_id: 33520[/import]

Was your fill rate low when you first started with admob? I just placed ads in my app last week and my fill rate is at 30%. With your high fill rate, I wonder if it just takes a little bit to get the fill rate up. [import]uid: 14218 topic_id: 8021 reply_id: 33565[/import]