Yuma 4×4

Media and Communications

AdMob + Kotlin – Mobile Ads Garage #13

AdMob + Kotlin – Mobile Ads Garage #13

[MUSIC PLAYING] ANDREW BROGDON: Hey, everybody. I’m Andrew Brogdon, and welcome
back to the Mobile Ads Garage. Today, with the help of my
partner, Gary the graphics guy, we get to cover
something awesome– Kotlin. One of the biggest cheers
at Google I/O this year came when Stephanie
Cuthbertson announced that Android Studio
3.0 would include built-in support for Kotlin. And it was for good reason. Kotlin compiles to good
old-fashioned JVM bytecode, but as a modern
language it includes a ton of features
that could help you code faster and better. Things like single abstract
method conversions, explicit nullability, default
parameters, the Elvis operator, and a bunch more. Plus, because everything’s
still running on the JVM, Kotlin inneroperates with Java. That means the
same Mobile Ads SDK you’ve been using to display
ads from AdMob and DoubleClick and your Android
apps works right out of the box with Kotlin. Now, Gary and I are part
of Developer Relations. Our end of things is
creating developer resources. So the big
announcement for us is that we’ve updated our
stuff to support Kotlin, and the changes are live. Our developer site now
features Kotlin code snippets alongside Java for
its publisher guides. You can just click
back and forth to see what a particular
technique looks like in either language, and
you can cut and paste code right into your own integrations. We also created Kotlin versions
of our open source samples, not only to show you how
the code works, but also because I wanted to write
a bunch of stuff in Kotlin. Seriously, it’s
a great language. All the code got
smaller and tighter. Actually, you don’t have to. Let’s dive right in with a
simple banner implementation in Android Studio. OK, so here’s what
I’m starting with. Just a simple hello
world app in Kotlin. And I’m going to put a
banner across the bottom. So step one, let’s get the
Mobile Ads SDK imported. Now, there is nothing different
here between Java and Kotlin. It’s the same SDK. So it’s the same dependency
in your build dot Gradle file. There we go. And now I can come into
my activity class, which is in Kotlin, and call
the initialize method for Mobile Ads. And the only difference here is
that I don’t put a semi-colon on the end of the line, really. Now, I do need an app ID
for the second parameter. And since I haven’t
registered this little sample, I’m going to use the sample app
ID from our quickstart guide, which you can use, too. This is great if you’re
just experimenting. You can cut and paste it
just like I’m doing here. There we go. OK, so the SDK is
imported and initialized. Let’s get a banner
into the layout. This is the XML layout
file for my activity. And I’m just going to
drop in an ad view tag and put wrap content on
both the height and width. There we go. Next up, I need an ID so
I can reference it later. And I’m just going
to call it ad view. There we go. And now for some
layout constraints that’ll put my banner centered
at the bottom of the screen. As you can see, I’m using
constraint layout, which is the new hotness, and it uses
these individual constraints to get everything
locked in place. So I’m locking start to
the beginning of my parent, and to the end of my
parent, and so on. And there we go. All right, let’s get the
ad size attribute in place. And as you can
see, Android Studio isn’t giving me any
intelligence on that attribute, and that’s because
I haven’t added the XML namespace for ads. So let me pop up
here and add it. And I’m going to copy
the URL from app up here. And this res-auto URL
you see, that just tells the system it can find
the namespace definition inside the final APK. The Google Mobile Ads
SDK actually includes it. So the namespaces attributes
are built into your app automatically. Cool. Now we can come back down and
put in an ad size or banner. And also, let’s get
the ad unit in place. And here, again, I haven’t
registered this out, because I’m just messing around. So I’ll use the test ad unit
from our banner guide, which again, you can use this, too. It’ll always return test ads. So you can bang away on it
without worrying about loading a live production ad. Cool. Now it’s back to the activity. All right, now in Java, to use
a UI element from your layout, you have to go find it with a
fine view by ID call like this. But with Kotlin come
these really handy Android extensions. They’re generated automatically,
and you can import them with a statement like this one. So Kotlin x Android,
and then synthetic, and then main for my source set,
and the name of my activity. Once that set of
classes is imported, I get these automatically
populated properties in the activity
class that correspond to the elements of my layout. So I could just call
load ad right here and give it a request object. I know there are a bunch
of open source projects that produce a similar effect,
many using annotations, but this one runs through
a Gradle plugin, which means it can really encapsulate
away a lot of the wiring and produce a great result.
All right, with my banner in place and a call to load
ad, let’s run this sucker. And there’s my banner. Job done. All right, so there’s
a banner in Kotlin. I think the Android extensions
alone make it easier. Exactly. All right, let’s step it
up and do an interstitial. That will give me
a chance to talk about Kotlin’s null safety. Back into Android Studio. So here’s my app. It’s dead simple. Just two buttons, one for
loading an interstitial, and one for showing it. To save time, as
you can see, I’ve already got the SDK imported. I’m already calling initialize. And my buttons, of course, are
right here in the layout file. So all that’s setup to go. All right, first
things first, let’s get an interstitial ad property
to hold a reference to my ad object. Now, you’ll notice that
question mark, which declares this is a
nullable property, and I’m required to
explicitly initialize it, which I’ll do with a null. I could actually call the
interstitial ad constructor right there in the
property definition rather than using null
and doing it later. But after so many years of
instantiating everything in onCreate, that just
feels wrong to me, like it would
somehow be bad luck. It totally works though. So feel free to try
it in your own apps. So like I said, I’m
going to instantiate it down here in onCreate. And I need to set the
add unit ID property. But since this is just an
app I’m fooling around with and I haven’t
registered it, I’ll cut and paste the one from
our interstitial guide. And just like banners, you
can use this ad unit as well in your own experiments. It always returns test ads. So you won’t get
yourself into trouble by loading and
showing ads with it. And there we go. Now, there’s a couple of
other things of note here. First, this is the
safe call operator, and it ensures that if
this variable were null, rather than getting a
null pointer exception, the call to set the property
would just be ignored instead. So it’s a built-in null check. Also, you can see I’m setting
a property called ad unit ID here. Fun fact– that
property does not exist. If you looked at the class
definition for interstitial ad, there’s no field or
property called ad unit ID. There are two methods called
get ad unit ID and set ad unit ID, though, which
Kotlin synthesizes into this property for you. When you assign to it, you’re
really calling set ad unit ID under the hood. And when you read it, you’re
calling get ad unit ID. But your actual
code is simplified. So with the object in
place and the ad unit set, let’s get the
buttons in play. For that, I’m going to use
the Android extensions again. There we go. Now I can come down here and put
it OnClickListener on the Load button. And this is another
cool Kotlin feature. OnClickListener is a
single method interface, and Kotlin will automatically
convert this lambda into an anonymous class
implementing the interface. Plus, you can use these cool
braces instead of parentheses. So in here, I’ll drop
a safe call to load ad. There we go. Then I’ll scroll down and
I’ll put an OnClickListener on the Show button. And inside, I’ll call the show
method on my interstitial. Now there’s just one piece left. If you recall, the Show
button starts off disabled. So I need to enable
it when an ad loads. For that, I need to assign
an ad listener implementation to the ad listener property
on my interstitial ad. And this is another one of
those synthesized properties. In Java, you’d call
set ad lister instead. And I’m just going to do
a basic implementation that just overrides
on ad loaded, and sets is enabled
on the Show button. And that’s another
synthesized property. There we go. And up here, I do
need the question mark that makes the assignment
a safe call, but that’s it. Let’s run it. All right, I’m
going to click Load. And when the ad loads,
there goes my Show button. And there’s my test ad. OK, I have one last
trick to show you. I’ve been using a
nullable variable to hold the reference
to my interstitial ad, but I can get around that by
calling the interstitial ad constructor in the
declaration, or I can use this cool late inti keyword. This tells Kotlin, hey, I’m
marking this as non-nullable, but I’m not going to
assign a value just yet. Just trust me that
I’ll assign one before I try to read this value. With that in place, I can get
rid of the save call operators and just do the normal
non-null save calls. Because I know I’m
instantiating the ad in OnCreate before I do any of
this other stuff, I’m still safe from null
reference exceptions. So you’ve got a couple
of different ways to structure the code,
and either way, we’ve got our ad showing. So job done. So there’s an interstitial. That’s two ad
formats in one video. Can’t beat that. Hopefully this has
inspired you to mess around with Kotlin a little bit. It really is a great language. And as always,
I’ve got some links to resources for you down in
the description for this video. First up are a couple of
good videos about Kotlin from Google I/O 2017. I’ve also got links to
our guides for banners, interstitials, and
the other formats, which have those Kotlin
updates I mentioned. Plus, a link to our
GitHub repo where you can see all our Kotlin samples. As always, if you’ve got a
question about this episode or an idea for something
you’d like us to cover, leave a comment below, and Gary
and I will see you next time. [MUSIC PLAYING]

13 thoughts on “AdMob + Kotlin – Mobile Ads Garage #13

  1. helo sir… your admob polecy not show in indain langwadge, i am not understand in english please fill in hindi langwadge on admob sating.

Leave comment

Your email address will not be published. Required fields are marked with *.