{"id":1464,"date":"2024-04-06T11:01:04","date_gmt":"2024-04-06T05:31:04","guid":{"rendered":"http:\/\/codetheory.in\/?p=1464---3af4a8f4-82c2-491b-9ce0-66ef09690bcd"},"modified":"2024-04-06T11:01:04","modified_gmt":"2024-04-06T05:31:04","slug":"admob-native-android-sdk-integration-with-phonegap-cordova-without-plugins","status":"publish","type":"post","link":"https:\/\/codetheory.in\/admob-native-android-sdk-integration-with-phonegap-cordova-without-plugins\/","title":{"rendered":"Admob Native Android SDK Integration with Phonegap\/Cordova without Plugins"},"content":{"rendered":"
Just like we’ve Google’s AdSense<\/a> program for all web publishers to generate revenue off their content, for mobile app developers there’s Google’s AdMob<\/a> ads platform to monetize and promote their web app. PhoneGap is a great tool to convert JavaScript rich web apps to native mobile app. But when it comes to monetizing via ads, just putting Adsense code is not the solution as its meant for web usage only and is against Google’s terms and conditions. We’ll go through a simple process that I came across to integrate the native admob android sdk to our cordova apps and games without using any phonegap plugins.<\/p>\n <\/p>\n Let’s just follow these few steps.<\/p>\n Download the Android Admob SDK<\/a>, extract it and copy <\/p>\n Now refresh your project directory in Eclipse and add the newly downloaded SDK’s jar file to the project’s build path. You can do this by following these simple steps:<\/p>\n <\/p>\n Add the following lines in your Here, the first import contains the functions related to ads while the second one will help us in inserting the ads into the screen.<\/p>\n Now, inside the main class of your Make sure to replace Below the newly added string, add one more private variable This will be the container for the ad. Now, inside the This should be enough for the ads to show up in a real device. However, if you want to test the ads on a Android Virtual Machine, then you’ll have to add this line above Make sure to comment out the above added line before publishing the app for obvious reasons!<\/p>\n So the entire code file should look like this:<\/p>\n The last step is to define the newly created Ads activity into the Now you must be able to see the ads when running the app in an emulator or on a real device. If you run into a problem or doesn’t get it to work then drop us a comment here and we’ll help you! \ud83d\ude42<\/p>\n Reference: https:\/\/github.com\/sainttex\/PhoneGap-Android-Native-AdMob<\/a><\/p>\n","protected":false},"excerpt":{"rendered":" Just like we’ve Google’s AdSense program for all web publishers to generate revenue off their content, for mobile app developers there’s Google’s AdMob ads platform to monetize and promote their web app. PhoneGap is a great tool to convert JavaScript rich web apps to native mobile app. But when it comes to monetizing via ads, … Continue reading “Admob Native Android SDK Integration with Phonegap\/Cordova without Plugins”<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[105,110,109],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1464"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=1464"}],"version-history":[{"count":30,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1464\/revisions"}],"predecessor-version":[{"id":1535,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1464\/revisions\/1535"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}Install the Native Android SDK<\/h2>\n
GoogleAdMobAdsSdk-*.*.*.jar<\/code> to the PhoneGap project’s
libs<\/code> directory.<\/p>\n
\n
yourProject\/libs\/GoogleAdMobAdsSdk-*.*.*.jar<\/code><\/li>\n<\/ul>\n
Include Admob’s Library<\/h2>\n
MainActivity.java<\/code> right below other imports. <\/p>\n
\r\nimport com.google.ads.*;\r\nimport android.widget.LinearLayout;\r\n<\/pre>\n
Configure your AdMob ad unit ID<\/h2>\n
MainActivity.java<\/code> file, create a new private string
AdMob_Ad_Unit<\/code> like this:<\/p>\n
\r\nprivate static final String AdMob_Ad_Unit = "xxxxxxxxxxxxxxx";\r\n<\/pre>\n
xxxxxxxxxxxxxxx<\/code> with your own ID as putting incorrect ID there will result in no ads showing up. Now we are ready to show the ads in our app!<\/p>\n
Create the Ad View<\/h2>\n
adView<\/code> of AdView type like this:<\/p>\n
\r\nprivate AdView adView;\r\n<\/pre>\n
onCreate<\/code> function, add the following lines at the bottom:<\/p>\n
\r\nadView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit); \r\nLinearLayout layout = super.root; \r\nlayout.addView(adView); \r\nAdRequest request = new AdRequest();\r\n\r\nadView.loadAd(request); \r\n<\/pre>\n
adView.loadAd(request);<\/code> and ads will start showing up in the AVDs.<\/p>\n
\r\nrequest.addTestDevice(AdRequest.TEST_EMULATOR);\r\n<\/pre>\n
\r\n\/*\r\n Licensed to the Apache Software Foundation (ASF) under one\r\n or more contributor license agreements. See the NOTICE file\r\n distributed with this work for additional information\r\n regarding copyright ownership. The ASF licenses this file\r\n to you under the Apache License, Version 2.0 (the\r\n "License"); you may not use this file except in compliance\r\n with the License. You may obtain a copy of the License at\r\n\r\n http:\/\/www.apache.org\/licenses\/LICENSE-2.0\r\n\r\n Unless required by applicable law or agreed to in writing,\r\n software distributed under the License is distributed on an\r\n "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r\n KIND, either express or implied. See the License for the\r\n specific language governing permissions and limitations\r\n under the License.\r\n *\/\r\n\r\npackage com.project.android;\r\n\r\nimport android.os.Bundle; \r\nimport org.apache.cordova.*;\r\nimport com.google.ads.*;\r\nimport android.widget.LinearLayout;\r\n\r\npublic class MainActivity extends DroidGap\r\n{\r\n\tprivate static final String AdMob_Ad_Unit = "xxxxxxxxxxxxxxx";\r\n\tprivate AdView adView;\r\n\r\n @Override\r\n public void onCreate(Bundle savedInstanceState)\r\n {\r\n \t\r\n super.onCreate(savedInstanceState);\r\n \/\/ Set by <content src="index.html" \/> in config.xml\r\n super.loadUrl(Config.getStartUrl());\r\n \/\/super.loadUrl("file:\/\/\/android_asset\/www\/index.html")\r\n \r\n adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit); \r\n LinearLayout layout = super.root; \r\n layout.addView(adView); \r\n AdRequest request = new AdRequest();\r\n \r\n \/\/ Comment this out before publishing.\r\n \/\/ request.addTestDevice(AdRequest.TEST_EMULATOR);\r\n adView.loadAd(request); \r\n \r\n }\r\n}\r\n<\/pre>\n
Final Steps<\/h2>\n
AndroidManifest.xml<\/code> file. Add this line in your manifest file after the closing tag of your main activity.<\/p>\n
\r\n<activity android:name="com.google.ads.AdActivity"\r\n android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"\/>\r\n<\/pre>\n