Foreword
We introduce “AdMob” in Google and the first site will be the AdMob site, and the Google site. After switching to this one, they will start poaching you a little to the
dark side of Firebase (recently purchased by Google).
Firebase provides quite extensive analytics capabilities (at least for me, as a programmer, and not a marketer) advertising in your application (
height, weight , country, number of views, Android version where installed, etc., etc.).
The first thing the
post was found
was , but it was for Eclipse, and after the first attempts to make by example, warnings about depricated and, indeed, errors began.
')
After reading more information, it became clear that the transition of AdMob to firebase occurred recently and will have to figure it out for yourself.
Let's see how to live with it now.
Minimum requirements: (taking into account which the article was written).
- Android Studio 2.2.2
- Be registered with the Google Developer Console
- To be registered on AdMob
Brain
Step 1
Add dependencies to
build.gradle files. First of all in 'Module: app':
Now in the second ('Project: AdMobOnHabr'):
After that, it is recommended to do Tools → Android → “Sync Project with Gradle Files” and compile the project. There is still no visible result, but the main thing is that there are no errors? Fine! Go ahead.
Step 2
Log in to your account on
AdMob . Select the manual addition of the application (if your application has already been published on Google Play - use the search in the first tab).
Let's add an advertising
banner to our application (I suggest to get acquainted with other types by yourself). Update frequency I chose the minimum allowed - 30 seconds, the name of the ad unit at your discretion.
Further you will be offered “Configure Firebase Analytics (
optional )” - the desired word is an underline. So far this is no good -
skip it . Review the "integration instructions", click
Finish . After the redirect, we see the page and the long-awaited ad unit ID:
Add the
ad unit ID to
strings.xml :
Do not forget that for an ad unit on another Activity you need a new ID.
Step 3
Add an AdView element to
activity_admobbanner.xml :
In the attribute 'ads: adUnitId' we write the resource from
strings.xml with the ID of the ad unit.
Some tips and notes:
- Do not confuse ads and app when adding a namespace to the parent ViewGroup;
- At API <16, adding a View advertising element and mistakenly typing xmlns: app = 'http: //schemas.android.com/apk/res-auto' produced a simple rendering error due to the attribute 'adSize' (adSize was missing). I mention this as a similar error may occur in other cases. The solution that helped me: add xmlns: ads = 'http: //schemas.android.com/apk/res-auto' to the View tag of the advertising element ( Council taken from SO );
- If the value of the 'ads: adSize' attribute is set to BANNER, then the ad unit will most likely not be on the full screen of the device (in width). Such a circumcision did not suit me. The solution is simple - change the attribute value to SMART_BANNER. There is a small BUT - after adding this value, the banner can become large on the preview and close the useful elements, fix it by adding the attribute and setting the height you need:
Don't forget to add the namespace for tools to the parent ViewGroup:
Code Style Tag CouncilGoogle recommends writing a tag closing like this:
But,
AS says that the body of the tag is empty, and in the recommendations of good form for writing code, it is advisable to generally write "/>" from a new line. I recommend something in between, as an empty tag does not provide additional information, just by adding an extra line of code:
Step 4
Initializing the Google Mobile Ads SDK. For this you need an app ID taken from AdMob. Click the
gear in the upper right → Application Management:
Here
it is, happiness desired app ID:
We write, in fact, the initialization itself using our app ID:
Step 5
The last action on Google is to download the long-awaited ad in the AdView element:
On the emulator, following the AdMob policy, you cannot display real advertising (only test ads are shown). Pretty logical. But if you have a real device, you can test the advertisement in action on it and please yourself with success.
Test advertising on a real device- To display ads on a real test device, rewrite the code like this:
The question remains - where to get the magic DEVICE_ID_EMULATOR ? All in one voice recommend a very “trash” method: write any value instead of DEVICE_ID_EMULATOR, for example:
Then run the application on your device and see logcat. There you will find a log similar to this:
What to do next is clear - copy the resulting code to the method mentioned above and enjoy life.
Note :
It was at night, I wanted to sleep, in general, do not repeat my mistakes:
When searching for the ID of your real device - do not forget to select the logcat of the device on which the application is running (I, for example, had a few more emulators running and I could not understand why the ID that I needed was not displayed in the emulator log)
It seems that everything was done as it is written, everything should work, but it is time for us to
go to bed to work on the next feature but ...
Step 6
Somewhere
between the lines you should have read that without
google-services.json it will not work. Most likely, you still do not have this file. Let's search together.
Google kindly provides you with a “sheet” of instructions on how to do
google-services.json . But I had a question - do you really need to sit and write this file yourself, which, by the way, is likely to be pretty sample?
The answer is in the
Google Developer Console , where all the same thought about the desire of a lazy programmer.
Fill in the fields:
Click Continue. Choosing Google Sing-In. Further simple - You know your SHA-1 as a keepsake?
If you suddenly forgot, I can help. SHA-1 can be found out with the help of
keyltool or a little bit clumsy - through the gradle in
AS itself. Since you don’t want to do extra actions, we’ll choose the second method:
- Click on the side tab Gradle (right in the window AS );
- Choose your project (if necessary, click Refresh);
- Open Tasks -> android;
- Double click on signingReport;
- Switch to text display mode Run console (see screenshot below);
- Do not forget to choose your application for the build (and not signingReport);
And here is your SHA-1. Remember?
Insert the SHA-1 code, click "Enable Google Sign-In" and "Continue to Generate configuration files".
Well, you understand. Download the generated file and copy it to the application root directory:
We collect your project - that's it. By such simple manipulations in your application there is now an advertisement.
PS: Code in pictures, so that people at least remember something, and not just copy-paste in a few seconds.