ArticlesBlog

Binding Kotlin Libraries | The Xamarin Show

Binding Kotlin Libraries | The Xamarin Show


>>Tune in to this week’s Xamarin
Show where my good friend, Alexei comes on to
show us how to bind Kotlin libraries into your
Xamarin Android applications. [MUSIC]>>Welcome back everyone
to The Xamarin Show. I’m your host, James Matzo Magno. Today, back for the 80th time, my good friend and best friend
possibly in the entire world, Alexei. How’s it going buddy?>>Hi, guys. Hi, James. It is going good this
beautiful day in Redmond.>>It is. It is stunning outside. You saw me in this beautiful
Hawaiian shirts to match our.>>Very relevant.>>Yes, very dark gray and rainy. Perfect. Well, if people
don’t know who you are, you are from our Mobile
Customer Advisory Team. We do all sorts of crazy
stuff with customers. But also, are always solving
really complex problems. You want to show it off on the show? So what are you going
to talk about today?>>So today, James, we’re going to talk about
native world again. I’m working with customers and sometimes they come into
us and then saying, “Oh, we have this interesting
library to use in our Xamarin app, but we don’t know how to do that. We have this archive, which is Android Archive. We don’t have tools
like DROP Archive, and it just works out of the box. You have to do these libraries, you have to do some magic.” Today, I’m going to
talk about this magic.>>Got you.>>So specifically,
Android libraries.>>We’re talking about
Android in general, but today we’ll be
specific about Kotlin. Because Android with Java
was there for a long time already while Kotlin is
relatively new language, and these libraries won’t be bind
automatically out-of-the-box. So today, I’m going to do a step-by-step instructions on
how to bind a Kotlin library as well as what tricks you need to do relatively to the
native Java libraries.>>Beautiful. This is my
favorite part about Xamarin, because you can take any
native iOS or Android library, bring it into your
Xamarin application. We’ve been doing it forever
for Objective-C and for Java libraries and find
this is how Xamarin works, finding the native libraries. You’re using these already
if you’ve ever used like Android Support Library,
material design.>>Yes. Absolutely.>>All of them. But Kotlin is a little bit different
because you said it’s new, it’s special, but it’s still just an Android library that you
get from the internet, right?>>Yes. Absolutely. Kotlin
is just a language. It’s slightly different
from Java so that’s why it generates different API set. So we need to take
these into account.>>Got it.>>Half a year ago, we wouldn’t even be able to just drop these AR library because
our tooling would crash. But now, we took a lot of
knowledge about Kotlin, put it inside of Kotlin
support library, and providing it to work as
a NuGet package for you.>>This is great because
it’s really future proof. People ask about how the
technology is going. You can see the investment
because as native iOS and Android development goes from older
languages to newer languages, everything [inaudible] in Xamarin?>>Yes.>>Cool.>>The Xamarin teams still
supports all the developers.>>Nice. Beautiful. Let’s get
into them, let’s show it off.>>So to show this concept, I’ve just picked a random
library from the GitHub.>>It’s my favorite way of doing it, beside I just Google
the random library.>>Yes. It will turn out to
be a Bubble-Picker library.>>This is cool, look
at that. That’s nice.>>Thank you, Arthur
for this library. We’re going to use in Xamarin. So most of developers, when
developing third-party libraries, thinking about Xamarin and providing
those bindings automatically. But whenever they don’t, you would like to take
care about yourself.>>Because there’s tons
of libraries and bindings already done for Xamarin
developers today. But not every single library, because there’s new
libraries all the time.>>That’s right. So this
Bubble-Picker library is a regular library source code on GitHub, in public,
you can download it. Unfortunately, there is no binaries. So in our case, we need to build
this library on ourselves. For this we will need Android Studio.>>Okay. So if they provided
a release and a library, then you could just use that. But in this case, it’s
just source code.>>Yes, just a source code. We download source code, we open it in Android Studio. We go to Gradle to set up
some of version numbers, always use the latest Kotlin version. What is your favorite Kotlin version?>>1.4.9.>>It should be 1.3.>>Oh, 1.3.5 as of today.>>Yes. You also would like to
connect to monitoring repository, which is a Google repository
for their libraries.>>Kind of like NuGet.>>Yeah. But specifically
for Google dependencies.>>Got it.>>There are also third-party
repositories where you could find those
kind of binaries. So once you connect everything
and done everything, you just go here to Gradle, open the Bubble-Picker target and hit the “Build”,
double-click on it. It will give you an AAR file. So what is AAR? It’s an archive. It used to be just jar files, but now it is AAR, which is archive over
archive. I would say it so. It includes additional things like dependencies, styles,
metadata, whatever. Now, we have these nice little
tool called Java Decompiler.>>Oh, fancy.>>We will drop this AAR file here.>>It is like a free
tool on the Internet.>>Yes, all free.>>I love random free tools
on the Internet [inaudible].>>It will show you that it has some dependencies
inside of our AAR file. It also show which
classes are exposed here, and those are classes, right? It’s not a Java class
or Kotlin class, it’s a class, it’s a
bytecode decompiled by JD.>>Okay.>>So once it’s AAR, we know nothing about Kotlin, we think in bytecode.>>Bytecode.>>But this bytecode is different
whenever you create and build in a Java code. It would be different whenever
you build in a Kotlin, because it will provide
to you different classes, namings, and small tricks you
need to take into account.>>Got it.>>So next step for this, we need to say Xamarin
tool and how to use this. So there is a Bubble-Picker
namespace and Bubble-Picker class listener
view, Bubble-Picker view. So we need to say, our Xamarin tools and how to use it. For this, we’re going to use our Xamarin tools,
Xamarin bindings project. So to do this, you will go ahead with
Visual Studio, File, New, Visual Studio project,
Xamarin Android binding.>>Okay. So this is different than library. It’s a binding library.>>Yes.>>So magical glue library for stuff.>>Make sure you choose
specifically Android binding, because there’s also
some other platforms you would like to take care.>>Got you.>>First step is to create this binding project
and define metadata.>>Okay.>>So I’ve already defined
this metadata for you, but what you would like to specify here is how your
interface would look like. So basically Xamarin iOS, Xamarin Android tool would try to expose every member of this library.>>Got you.>>So whatever it discovers, it tries to generate a class. In most cases, it will try to generate even these
dependencies classes,>>Oh, no.>>and we will actually fail on this.>>Not good.>>Because lots of
classes, lots of tricks, and you would like to expose
only whatever you want.>>Yeah, you don’t
need all that stuff. You only need, I’m looking
at a few, not very many.>>Yes, that’s right. You actually
need only this namespace here.>>Beautiful.>>That’s what I did. I’ve
just removed these node for JBox2D and SLF4J APIs. So I’ve just removed this
dependency by removing these node.>>Got you. This looks the
same as when I would bind Java libraries in the past in the documentation like all this
metadata stuff similar-ish?>>Yes. That because,
like I said before, we just work in this bytecode. We don’t know about Kotlin.>>Got you.>>But if you have some
Kotlin specific stuff, you would like to take
care here in the metadata. Let me talk about the specifics. So there are a few things you
would like to take into account. Whenever you have unsigned types like unsigned integer
or unsigned byte, those will be generated with these
strange suffix in our bytecode.>>Weird.>>So that’s how it is. Whenever you are going
to use it in C-Sharp, you will be getting these
names, weird names.>>I don’t like that.>>Those won’t be
translated automatically. Our tools already is
trying to handle this, but we don’t remove this name. You would like to apply metadata
to rename those classes, so you will be having
these nice overloads whenever you have a method
to a stream and integer, you won’t be getting ended up
with two different message. You will still have one
method that is overload.>>Kind of see, you
can prettify it up.>>Yes. That’s metadata
will let you control this. We have lots of information
how to define this metadata. You will be able to rename
classes, add members, remove members, just expose
whatever you want to do.>>Okay.>>That’s a fine level of control. Let’s talk about the
other tricks of Kotlin. Also we would like to
start out on the arrays, arrays of unsigned types. Those will be generated as well
with some weird suffix here.>>So if you don’t do anything there might be some weird
gobbledygook in there.>>Or it could just doesn’t work. It could just fail on compilation, or it will be compiled, but you won’t be able to find it.>>I see>>Another trick is generics. So generics are not supported out
of the box by Kotlin and Java. I mean, whenever they
defined in Kotlin and Java, they’re not supported
by Xamarin tooling.>>Got it.>>Those will be bind, but metadata generated won’t let you bind specific
generic type arguments. It will be just a regular
C-Sharp classes with objects.>>Oh, I see, so you get
a bunch of objects back.>>Yeah.>>Got it.>>Unfortunately, unbound
[inaudible] that’s strongly type, but they always will
be able to create a [inaudible] class defining those objects in generic type manner on C-Sharp and just use those.>>Got you.>>That’s all the tricks
you need to know about the Kotlin and as well unlike Java, you have to enable these Java
Xamarin.Kotlin.StdLib. What is that?>>What is that? I was about to ask. What is that? Standard
library of sorts.>>Yeah, it’s kind of
a standard library. Actually, you can write a Kotlin code without
using Kotlin classes. You can use just like integers, for example, and do a simple math. It will work out of the box actually, without even the standard library, you will be able to bind it, to use it and it won’t
crash. It will work.>>Got it.>>But whenever you start to
use Kotlin specific classes, those won’t define in your
app and your app will crash.>>Got it.>>So just a simple
rule in this package, it is also updated with every
Kotlin version updates. So whenever there is
a new Kotlin version, you would like to update it. We were building our AR
with 1.3.50, remember?>>I see it right there, 1.3.50.>>That’s exactly same version.>>Nice.>>So you add in this dependency, and it works out of the box.>>Nice.>>Then you just build it
and all the classes you saw here in namespace will be generated. We’re going to use these
bubble picker class, which is a view and Android to you. We’re going to create an instance. So the final step->>It looks like you have that
ARA in that library too, right? Inside of the binding library?>>Which one?>>Inside of Visual Studio for Mac in a jar folder there.
I saw that in there.>>Oh yeah, you’re right. You remember this name>>That’s what you built, right?>>That’s right.>>Okay.>>Sorry for not mentioning this. Of course, you’re binding
libraries not only on metadata but also how to call
and what to call for this.>>Got it.>>That’s native code, which will be embedded into the binding and we’ll
be able to call it. The binding project itself will
expose a DLL as a regular .NET DLL. It’s not a regular.>>Special Android DLL.>>You will be able
to reference it on the Android project and that’s
what we’re going to do.>>Okay. Are there any
special properties that we need to know
about for that thing?>>Not at all. It will
be added automatically. It will be a library project Z. Whenever you drop an AAR, it will be specified like that.>>All right.>>Nothing specific to Kotlin here, nothing specific to
Kotlin in metadata. You just need to know about all these tricks with classes
and you’re good to go.>>And that package, perfect.>>Yeah. So this project is actually compilable already and
once you build it, it will generate you a DLL
with all the classes inside. Let’s try to use this. For this, we will file
new Android project, just regular Android project. Add this reference to bubble picker binding, no
other dependencies. You also want to add this StdLib as well and
Xamarin.Essentials of course. Not related, but you
won’t like those, right?>>Yes.>>You also mentioned
support libraries. Those are maintained by Microsoft
and all similar kind of bindings and records or with the Android to provide
the support libraries. Once you have this set up, you go to your main activity, create a similar bubble picker class.>>So that came from
the Kotlin library?>>Yes. That’s actually a class
which came from the library. Let me go and show this class. It’s in this namespace. You see, we have lots of
classes that we want to use. Because we didn’t provide any
metadata to hide any of these, we will get every line. Everything.>>Except of those dependencies
which AAR file has it on its own.>>Perfect.>>So we don’t have those.>>Nice.>>So our main activity would
create this bubble picker class. Just find the reference. It is created in the layout. Actually, the nice thing about that, you can use it in layout
as regularly out, right? So let’s go to “Layout”, “activity_main” and you
will be able to see this. Let me make it bigger. Why do you show me this. So we are including this
content main in activity. Our content main is few
textbox with this dependency.>>So you can just use it right there because it’s
just a normal control.>>Yes, we specify namespace, specifying the name of
class and using it as is. It’s the same way
where you would use in Native world in Android
Studio without even Xamarin.>>Got you. Then if you
probably wouldn’t even use it in a Xamarin forms project, you would just create
your Xamarin forms and then this would be your render?>>Yes. In render, you would be able to just create
an instance bubble picker, initialize it with all the
properties you need and use it.>>Cool.>>So let’s say we have
this instance already here. Then we said in some properties like bubble size adapter
with all our classes, we’d just generate a 10 random
bubbles and just try to run it. Let’s see if it works. What are your bets? Will it crash?>>Well I’m assuming
because I saw you around earlier before I
recorded, I bet it works. I mean, that’s just my assumption unless something magically
happened between then and now.>>That sounds like a cheat.>>You’ll never know.>>Let’s see.>>Yeah. This is super cool though because it looks like pretty minimal work to
actually get it in there. You just define a few
things and then that’s it.>>I would say it’s a simple
library in terms of bindings. We were working on much
more complicated libraries with lots of classes and
lots of dependencies, and those won’t work out of the box.>>Got to do more of
that metadata stuff.>>You would like to actually learn the library from
here, what is inside. That’s a very convenient
tool to do that. Then you see it works.>>Got it though.>>It’s nice.>>That’s great though because I
mean, this is a control that to me you said most of all, there’s a lot in that
project, actually. Lot of classes, a lot of
different things that are being referenced and now
you have it in an app.>>Yeah, and all this
information with step-by-step is defined on a GitHub documentation. We’ll publish a blog post soon and you will be able to follow these, all the steps we’ve just defined
and all the important links.>>Beautiful.>>Yeah.>>I like that. That’s
absolutely amazing. I love it.>>Thank you.>>Yeah. Thank you for
coming and showing this off.>>Thank you for having me.>>Absolutely. Well, also, don’t forget to go rewatch a
whole bunch of this series. We have stuff on binding Swift
libraries, bringing C++ libraries. We’ll link to all the documentation. We’ll link to all the blog posts and all the source code and
all that good stuff.>>That’s great.>>Yeah. Thank you so much again.>>Thank you, James.>>Well, thanks everyone for tuning in and don’t forget to subscribe. Ding the bell, do the things. You know what to do, who knows? You’ll figure it out. Have fun. Thanks for watching
and have a great day.>>Thank you guys.>>[MUSIC]>>Hey, James here.
Just wanted to check in and thank you for
watching this video. Now, do all the things that you
know you want to do such as like, subscribe, and ding
that notification bell, become part of the
notification squad. While you’re here, check out all these awesome videos
that I’ve already recorded. Click on that thing. Click
it. Watch it. Do it.

Comments (4)

  1. awesome video it was really good

  2. What about Xamarin forms

Comment here