{"id":29316,"date":"2017-01-16T10:55:20","date_gmt":"2017-01-16T18:55:20","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=29316"},"modified":"2020-01-17T10:41:28","modified_gmt":"2020-01-17T18:41:28","slug":"lightweight-ads-for-android-apps","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/lightweight-ads-for-android-apps\/","title":{"rendered":"Lightweight Ads for Android Apps"},"content":{"rendered":"<p>\t\t\t\t<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/play-services-ads-lite_128x128.png\" alt=\"play-services-ads-lite_128x128\" width=\"128\" height=\"128\" class=\"alignright size-full wp-image-29317\" \/>The easiest way to start monetizing your mobile apps is to connect with advertisers and show relevant ads within them. It&#8217;s as simple as displaying an ad, your users click on it, and you make money. The main staple in the industry is AdMob from Google, which offers SDKs for both <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.Firebase.iOS.AdMob\/\" target=\"_blank\" rel=\"noopener noreferrer\">iOS<\/a> and <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.GooglePlayServices.Ads\/\" target=\"_blank\" rel=\"noopener noreferrer\">Android<\/a> applications and can be integrated in minutes. However, one drawback to the Android library is that its part of Google Play services, which can increase the size of your app. Today we&#8217;re going to look at a new SDK for Android, Ads Lite, that gives us a lightweight new version to help reduce app size and decrease the number of dependencies required. This can be extremely useful if you&#8217;re running into the <a href=\"https:\/\/developer.android.com\/studio\/build\/multidex.html#about\" target=\"_blank\" rel=\"noopener noreferrer\">64K reference limit<\/a> and have to multidex your application.<\/p>\n<h2>Monetize App in AdMob<\/h2>\n<p>Before we can integrate the SDK, we&#8217;ll need to set up our app in AdMob. If you haven&#8217;t signed up for an <a href=\"https:\/\/www.google.com\/admob\/\" target=\"_blank\" rel=\"noopener noreferrer\">AdMob account<\/a>, you&#8217;ll want to do so at this time to start the monetization process. Once you&#8217;ve created an account, we can simply click on the Monetize App button to find our app in the app store or add it manually. Select the Manual process if you haven&#8217;t launched your app in Google Play.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AddAppAdMob.png\" alt=\"addappadmob\" width=\"816\" height=\"622\" class=\"aligncenter size-full wp-image-29319\" \/><\/p>\n<p>The next step is to select the ad format that we want to integrate into the app. The easiest format to integrate is a Banner, which is placed throughout the apps. We have control over different refresh rates along with the type of ads we want to display. We can then link to Firebase for advanced analytics, or skip over this process.<\/p>\n<p>We can add additional ad units or get setup instructions at this point. If we wanted to add ad banners to multiple Activities, it&#8217;s best practice to create an ad unit for each. Back on the main app screen, we&#8217;ll want to write down our App ID and Ad unit ID to integrate into our code:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AppID1.png\" alt=\"appid\" width=\"659\" height=\"323\" class=\"aligncenter size-full wp-image-29323\" \/><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AdUnitId1.png\" alt=\"adunitid\" width=\"595\" height=\"260\" class=\"aligncenter size-full wp-image-29322\" \/><\/p>\n<h2>Getting Started with Ads Lite SDK<\/h2>\n<p>Ads Lite is a brand new SDK that was introduced with Google Play services 9.6.1. It has far fewer dependencies than the standard Ads SDK, but still requires a minimum version of 24.2.1 of the Android Support Libraries. This means that, as of this blog, Ads Lite is currently not compatible with Xamarin.Forms applications (<a href=\"http:\/\/motzcod.es\/post\/154607061227\/xamarinforms-google-admob-ads-in-android\" target=\"_blank\" rel=\"noopener noreferrer\">see my blog on adding Ads to Xamarin.Forms apps<\/a>), but can be added to traditional Xamarin.Android applications easily through the <a href=\"https:\/\/www.nuget.org\/packages\/Xamarin.GooglePlayServices.Ads.Lite\" target=\"_blank\" rel=\"noopener noreferrer\">NuGet package<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/AdNuget.png\" alt=\"adnuget\" width=\"969\" height=\"209\" class=\"aligncenter size-full wp-image-29318\" \/><\/p>\n<h2>Add Permissions and Ads Activity<\/h2>\n<p>Once the SDK has been added, we must update our Android Manifest with a few permissions and a default Ads activity that can be launched. <\/p>\n<p>The Google Mobile Ads Lite SDK requires the Internet and Access Network State permissions to work correctly. We can add these with the following assembly level attributes:<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\r\n[assembly: UsesPermission (Android.Manifest.Permission.Internet)]\r\n[assembly: UsesPermission (Android.Manifest.Permission.AccessNetworkState)]\r\n<\/pre>\n<p>Or, we can add them directly inside the Android Manifest manually. We can find our Android Manifest source under the Properties folder in Visual Studio or Visual Studio for Mac.<\/p>\n<pre class=\"theme:vs2012 lang:xml decode:true\">\r\n\t<uses-permission android:name=\"android.permission.INTERNET\" \/>\r\n\t<uses-permission android:name=\"android.permission.ACCESS_NETWORK_STATE\" \/>\r\n<\/pre>\n<p>Inside of the application tag we will add this very specific activity node:<\/p>\n<pre class=\"theme:vs2012 lang:xml decode:true\">\r\n    <activity android:name=\"com.google.android.gms.ads.AdActivity\"\r\n            android:configChanges=\"keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize\"\r\n            android:theme=\"@android:style\/Theme.Translucent\" \/>\r\n\r\n    <meta-data android:name=\"com.google.android.gms.version\" android:value=\"@integer\/google_play_services_version\" \/>\r\n<\/pre>\n<h2>Integrate AdView<\/h2>\n<p>We can now start to add the AdViews throughout our application. Simply open the layout file where the ad will live, and in the root node we&#8217;ll add a custom xmlns for our ad under the <code>xmlns:android<\/code> attribute:<\/p>\n<pre class=\"theme:vs2012 lang:xml decode:true\">\r\nxmlns:ads=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n<\/pre>\n<p>Next, we can add the AdView control:<\/p>\n<pre class=\"theme:vs2012 lang:xml decode:true\">\r\n  <com.google.android.gms.ads.AdView\r\n   android:id=\"@+id\/adView\"\r\n   android:layout_width=\"wrap_content\"\r\n   android:layout_height=\"wrap_content\"\r\n   local:adSize=\"SMART_BANNER\"\r\n   local:adUnitId=\"@string\/banner_ad_unit_id\">\r\n  <\/com.google.android.gms.ads.AdView>\r\n<\/pre>\n<p>Note that we&#8217;re using the <code>SMART_BANNER<\/code> keyword for the <a href=\"https:\/\/firebase.google.com\/docs\/admob\/android\/banner\" target=\"_blank\" rel=\"noopener noreferrer\">adSize<\/a> to dynamically fill the width and height and by referencing <code>@string\/banner_ad_unit_id<\/code> for the <code>adUnitId<\/code>, which we get from AdMob. The ID should be placed in the strings.xml resources folder. This ID starts with <code>ca-app-pub<\/code> and has a <code>\/<\/code> in it.<\/p>\n<pre class=\"theme:vs2012 lang:xml decode:true\">\r\nYOUR_AD_UNIT_ID\r\n<\/pre>\n<h2>Initialize the Ads SDK<\/h2>\n<p>Our AdView is ready to start displaying ads, but we must ensure that the SDK has been initialized with our publisher app ID. It&#8217;s recommended to initialize at app launch, such as inside the OnCreate of our MainActivity. This ID starts with <code>ca-app-pub<\/code> and has a <code>~<\/code> in it.<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\r\nvar id = \"YOUR_ADMOB_APP_ID\";\r\nAndroid.Gms.Ads.MobileAds.Initialize(ApplicationContext, id);\r\n<\/pre>\n<p>Finally, we must send a request for the ad in the code behind where the layout is loaded:<\/p>\n<pre class=\"theme:vs2012 lang:csharp decode:true\">\r\nvar adView = FindViewById(Resource.Id.adView);\r\nvar adRequest = new AdRequest.Builder().Build();\r\nadView.LoadAd(adRequest);\r\n<\/pre>\n<h2>See it in Action<\/h2>\n<p>At this point, we&#8217;re ready to launch and run our application. We must use a Google Android emulator with Google Play services or a physical device.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/Screenshot_1484006501.png\" alt=\"screenshot_1484006501\" width=\"360\" height=\"640\" class=\"aligncenter size-full wp-image-29326\" \/><\/p>\n<p>This should look very similar to standard AdMob test ads if you have used the standard Ads SDK in your app. In fact, you could simply replace the standard Ads SDK with Ads Lite SDK with absolutely no code changes!<\/p>\n<h2>Limitations<\/h2>\n<p>Ads Lite has a reduced footprint because it is unlike the standard Ads SDK, which includes the Google Play services client libraries. Ads Lite strips away the implementation and leaves only an interface for us to develop against. This means that Google Play services must be installed and updated on the device that your app is running on. It&#8217;s only recommended to use the Lite SDK if distributing through the Google Play store. If a device doesn&#8217;t have Google Play services, it will log an error and an ad will not be displayed.<\/p>\n<h2>Learn More<\/h2>\n<p>To learn more about the Ads Lite SDK, be sure to read through the <a href=\"https:\/\/firebase.google.com\/docs\/admob\/android\/lite-sdk\" target=\"_blank\" rel=\"noopener noreferrer\">Firebase documentation<\/a> and the full <a href=\"https:\/\/firebase.google.com\/docs\/admob\/android\/quick-start\" target=\"_blank\" rel=\"noopener noreferrer\">Ads SDK documentation<\/a> on integrating ads. You can find a full sample of integrating Ads Lite SDK in my <a href=\"https:\/\/github.com\/jamesmontemagno\/MonkeysApp-AppIndexing\" target=\"_blank\" rel=\"noopener noreferrer\">Monkeys App on GitHub<\/a>. If you&#8217;re looking to integrate AdMob ads in your Xamarin.Forms applications, read through my posts on integrating AdMob for both <a href=\"http:\/\/motzcod.es\/post\/154696375922\/xamarinforms-google-admob-ads-in-ios\" target=\"_blank\" rel=\"noopener noreferrer\">iOS<\/a> and <a href=\"http:\/\/motzcod.es\/post\/154607061227\/xamarinforms-google-admob-ads-in-android\" target=\"_blank\" rel=\"noopener noreferrer\">Android<\/a> Xamarin.Forms apps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The easiest way to start monetizing your mobile apps is to connect with advertisers and show relevant ads within them. It&#8217;s as simple as displaying an ad, your users click on it, and you make money.  Today we&#8217;re going to look at a new SDK for Android, Ads Lite.<\/p>\n","protected":false},"author":544,"featured_media":41053,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313,2,556],"tags":[5,429,1176,1448,4],"class_list":["post-29316","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-developers","category-integrations","tag-android","tag-components","tag-google-play","tag-monetization","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>The easiest way to start monetizing your mobile apps is to connect with advertisers and show relevant ads within them. It&#8217;s as simple as displaying an ad, your users click on it, and you make money.  Today we&#8217;re going to look at a new SDK for Android, Ads Lite.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/29316","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=29316"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/29316\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/41053"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=29316"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=29316"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=29316"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}