Developing for Android Things Using Android Studio

11 Likes comments off

[MUSIC PLAYING]

DAVE SMITH: Good
afternoon, everybody.

How’s I/O far?

Oh come on.

Come on.

Come on.

Come on.

Give me something.

[APPLAUSE]

There we go.

Awesome.

Awesome.

Well, thank you so
much for joining us.

My name is Dave Smith.

I’m a developer advocate
on the IoT platform team

here at Google.

RENATO MANGENI:
I’m Renato Mangeni

and I’m a Develop Blog
programs engineer.

I work in [INAUDIBLE]
here at Google

DAVE SMITH: And we’re
here to talk to you today

a little bit about how you can
easily get started developing

for Android Things using a
tool you probably already know,

which is Android Studio.

So let’s talk a little
bit about Android Things

for just a minute Android Things
is an extension of the Android

platform to a brand
new form factor,

in this case for
Embedded in IoT.

This is something
that we’ve done

in the past with other
form factors such as Where,

Android TV, Android Auto.

And Android Things is just
yet another form factor

where we’re bringing all the
power of the Android platform

in the framework
that you’re already

familiar with to
this new form factor

in building new use cases.

This is a really great
thing for embedded in IoT,

specifically because it brings
Android’s best-in-class tools

to a space that traditionally
hasn’t had very well integrated

tooling.

You know, often
developing environments,

debugging environments in
Embedded, are decoupled,

they’re separated.

There’s not a real well
integrated experience

in a lot of cases.

And that’s what makes Android
Things and Android Studio

so awesome, is the fact that
we have this fully integrated

development
experience, where you

can use these powerful
tools like Android Studio

to develop, debug, and
deploy your applications

to your devices for development
and then again into production.

Basically, it makes embedded
development as simple

as mobile development
has become because

of these powerful tools.

So most of you
are probably– let

me just see a show of hands.

How many of you are Android
developers in the room?

How many of you use Android
Studio roughly every day?

OK, great.

So this is awesome, because you
guys already know this tool.

And you know all of the
amazing things that it

can do for you, in terms of
development and deployment,

but just to give you some
background or some idea here.

This means that now you can use
some of these amazing Android

Studio features
that you’re already

familiar with like Instant Run,
the UI Layout Builders, and all

of these great tools to
now build applications

for embedded devices.

This also means that
you can leverage

all of the deep integrations
that are already in Android

Studio for Google services.

The deep integrations for
things like Firebase as well as

the Google Cloud Platform.

And in addition
to these features,

Android Studio also has these
fully featured debugging

and profiling tools.

You have this amazing
step through debugger

that you can set watch
points, execute arbitrary

code expressions.

You can do all of these
things on your target device

the same way you do when you
do your Android development

and apps today.

But in addition, we also have
all of these new profiling

tools.

You saw some of them were just
announced at the developer

keynote yesterday, for profiling
CPU performance, memory, object

allocations, and tracking
down issues in your program

and performance problems.

So all of these tools that
we’ve announced here at IO,

as well as the
ones you’re already

familiar with using
in Android Studio,

you can now apply
those same tools

to developing for
embedded and for IoT.

All right, so that’s enough
about talking about what

you probably already know.

Let’s talk about the new stuff.

So as you probably
heard yesterday,

we announced the new preview
of Android Studio 3.0,

and one of the new features
in Android Studio 3.0

is direct support for Android
Things as a device form factor.

So let’s take a look
at what we’ve added.

So now if you are using the
Preview of Android Studio

and you create a new project,
what you’ll see is Android

Things is now listed as
one of those form factors

that you can select when
you build a new project.

Now the interesting
thing to note

here is that it’s not mutually
exclusive with phone and tablet

or any of the others.

So in theory, you could
build an application

that targets both phone and
tablet and Android Things.

Interesting use case
for this might be,

maybe you want to build a
companion application that

goes along with what’s
running on Android Things

and you’d like to share
as much code as possible.

You could actually build them
into the same application

project, just using
different entry points

via different activities.

We’ll talk about that a
little bit more in a bit.

All right, so what
does that actually do?

When I check that little
Android Things box,

what is being added
to my project?

So what that does,
is it walks through,

and when it creates
the new project,

it automatically adds the
Android Things support

libraries to that
project for you,

in the form of the
Gradle Dependency,

as well as the required
manifest element there.

If you’ve been through
our documentation,

you’ve seen us telling
you to manually add

these things if you’ve been
playing around with the preview

before, and now
Android Studio is

going to add them for
you automatically.

So if you’re not familiar with
Android Things development

directly, this support
library essentially

adds a bunch of new APIs that
are specific to Android Things

for interacting with low
level peripheral devices.

And these APIs have names
like GPIO, PWM, SPI,

and all the other ones
that we have listed there.

And maybe you’ve
heard of those things.

Maybe you haven’t.

But essentially
they’re all interfaces

that are used to connect low
level hardware like buttons,

LEDs, sensors, and
things like that.

So now you can
communicate with those

directly inside of your
app, and the support library

allows you to do that,
and now Android Studio

will automatically add
that to your project

if you target Android things.

The next thing that
you’ll see when

you create a new project
targeting Android Things

is an activity dialog to
add a new empty activity

to your project.

Now this is also important
because an Android Things

application must have at least
one main entry point activity.

So to discuss that a little
bit, let me kind of walk you

through what happens when
Android Things starts up

for the first time.

So Android Things
is a little bit

different than
traditional Android

in that there’s not a launcher
application on the device

that the user would choose
to pick a particular app

and then launch that so that
they can interact with it.

It’s not to say that
Android Things doesn’t

have a graphical display,
because it might.

  About

But what it does mean, is that
we have to somehow provide

a mechanism, so that
Android can automatically

launch whatever the
primary application is

that you’ve developed, once
this system fully boots.

So the way that we do that,
is once Android is finished

booting, there is an
intermediary app that we’ve

built into the system
called the IoT launcher.

And that launcher is looking
for a very specific activity

of intent on the
system that it can

launch as the first
main application that’s

ready to run whatever
code you’ve developed.

Now again, if you’ve done some
Android Things development

before, you might have seen
this in our documentation.

But essentially,
it means that you

are creating an
activity that has

an intent filter that includes
this IoT launcher category.

And the IoT launcher
app will look

for an activity
that has that filter

and automatically launch it
as the primary application

on the device.

Now from that point, you
can do whatever you like.

You could start other services.

You could even launch
other applications

if you have multiple APKs.

But that single entry point that
we require to get your app up

and running.

And again when you use
the new activity template

in Android Studio,
what it’s going to do

is automatically add
all of that boilerplate

to the manifest for
you, so that you

don’t have to
remember to do that

for that specific activity.

All right, so one more
thing about Android Studio.

I mentioned before that there
is a possibility for having

a display on an Android Things
device, but it’s not required.

We say that essentially displays
are optional in Android Things.

Because of that there are
some Android behaviors that

might work a little
bit differently

than traditional Android, and
one of those that’s very common

are APIs that would typically
show a dialog to the user.

One example is runtime
permissions, probably

the most common.

Now, the issue with showing
a runtime permissions dialog

to require access
to particular APIs

is we can’t necessarily do
that if there’s no display.

So in Android Things these
dialogs do not display

and permissions are granted
to applications by default,

even if they’re
dangerous permissions.

We say that they’re
granted at install time,

but it turns out there’s a
little bit of an implementation

difference in here
that some of you

may have experienced if you’ve
done the development so far.

It turns out that
those permissions

are granted when the when
the device first boots.

So on reboot, the device will
grant application permissions

based on what they’ve
requested in their manifest.

So there’s no dialogue, but
during development, this

creates a bit of a gap.

There’s actually a period of
time between when you install

your app on the
device the first time,

and when it may or may
not have been rebooted,

where that permission is not
granted to you and that can

cause some friction when
you’re doing development .

There are ways around this by
manually rebooting the device,

or in some cases,
you can manually

install the APK using
the grant permissions

flag to override all of this.

In Android Studio 3.0 preview,
this is now done for you.

When we detect that you are
connecting to an Android Things

target device and you
download your code out

of Android Studio,
we will automatically

grant those permissions
at install time the way

you expect, so that you don’t
have to do these manual work

arounds, or reboot the device.

All this is now much smoother,
much easier to work with,

now that we’ve got
full integration

with Android Studio.

OK, so now I’d like to show you
some of this stuff in practice.

So I’m going to hand
it over to Renato

who’s going to show you a demo.

Can we switch the demo please?

RENATO MANGENI: Hi, everyone.

Are you ready for
a live coding demo?

[APPLAUSE]

Hold your claps for later.

Let’s check if everything
works as expected.

I just launched the
Android Studio 3.0,

which we’ve just launched.

We just announced
that yesterday.

You can download it right now.

It’s public, but it’s
on Canary Channel.

So it’s not stable.

That means this is risky.

What we are going to do here.

I’m going to start a new
project, a new Android Things

project, from scratch, using
the support that we just

added to Android Studio,
and then from there

we’re going to do
something else.

 

I will enable form factor
support for Android Things.

I will add a empty activity,
just like Dave showed,

and I don’t want to
deal with UI now,

because I would actually
run this app on this device

as you can see here
and this device has

no UI, no traditional UI,
so let’s disable that.

 

OK, now it’s
generating a project.

I will explain a little
bit of what this device is.

This is one of the many boards
that we support Android Things

on, and on top of
that you can see

a, what we call a
hat, which is just

a board with a bunch of sensors
and buttons and segments

plays, etc.

We are going to play
with that a little bit.

OK, here we have the activity.

If I switch to my
[? build gradle, ?]

you can see that the Android
Things support library

is added automatically.

So I’m ready to do something.

What do you want me to do, Dave?

DAVE SMITH: Well, I
guess the first project

everybody does with
hardware is a blinky light.

That’s kind of the “Hello
world” of hardware.

So let’s-

RENATO MANGENI:
Hello world, right?

DAVE SMITH: Yep.

RENATO MANGENI: OK, let’s
do a Hello world here.

First thing.

We have this notion of a
driver on Android Things.

We don’t want to you guys
to do all the low level

stuff for every
standard peripheral,

every standard
sensor, or display.

So we provide that and
we count on the community

to provide more of
those high level drivers

for these sensors.

So you don’t have to
deal with I squared S,

I squared C, SPI, and
those low level protocols

every time you just
want your blinky LED.

For this particular hat,
for this particular board

with a lot of sensors, we
created a method driver

that contains every single
sensor or peripheral

that has in that board.

 

So I would start by adding
the dependency on that driver.

 

Is it better?

 

OK, so I just added dependency.

This library is
published on J Center,

so it means that when
I sync, it will add

all the transity of dependency.

And why it does that?

I will go back to my activity.

 

OK.

Now– I will start
with Open the LED.

See, there are three LEDs
here that are currently on.

There is a red, green, and blue.

And by using this
class from the driver,

I can open any of these.

I opened the red.

Now, there is an exception.

  Storage access with Android 11

I would just handle it
the default way and–

 

set the value.

I also added the LED
direction as out.

So this is something that
we send the signal to,

not something that we
collect the signal from.

And I would throw
that in the loop.

I’m pretty sure
Dave has something

to complain about that.

We’ll talk about that later.

 

OK, if you are an
Android developer,

you can see that what
we are doing here–

we open up the LED.

We got into a loop.

And we set the value
every 300 milliseconds

to an alternate value.

And now, I will run the app.

Here are the devices I have.

The only device I have
connected is the IMX 7D,

which is this board.

 

Cross your fingers, please.

If all goes well
after some time,

the red LED will start blinking.

 

It’s working.

[APPLAUSE]

 

Awesome, right?

I’m done.

DAVE SMITH: So it works.

That’s great.

But you’re a pretty
experienced Android developer.

I’m not real sure that
putting an infinite

While loop and On Create is
the best way to handle this.

You think maybe we can fix that?

RENATO MANGENI: I told you guys.

Yeah.

I knew he would
complain about that.

OK, so that’s a very good point.

Despite not having a UI, Android
Things should not do it–

you have to follow the
same rules of Android.

You should not block
the main triad.

Why?

If you don’t have a
why, you don’t need

to refresh your why, right?

However, you have events
like keyboard events

that can still be sent
to your applications,

to your main triad.

You have sensor events.

So you have a bunch
of other stuff.

So you should not
block the main triad.

That’s a main route for Android.

And that is something
we are doing here.

So let’s fix that.

The way I’m going to fix this–

there are multiple
ways you should do it.

You guys probably know
you can create a service.

You can create a triad.

I would do it the
simplest way here.

I’d just create a handler,
attach that handler

to the main triad,
and add each set value

to the end of the looper–

of the main triad looper.

It’s going to look less
complicated than it sounds.

 

DAVE SMITH: So as
Renato mentioned,

one of the things
that we’re doing

is we’re ensuring that
the main thread in Android

continues to remain free so
that system events can still

come into your
application, whether those

are lifecycle events or
he mentioned input events.

This also means
that, in general,

if you have to do any
regular polling of an input

or something along
those lines, you

want to make sure that you
don’t do that on the main thread

either.

So in this case, we’re
scheduling an output.

So the way that we’re
doing this is fine.

But if we need to pull a
sensor or read some value

from another input
on a regular basis,

we might actually want to use
a background thread to do that.

Now, it could be
a handler thread.

That seems to be the simplest
way to sort of modify

what’s being done in this case.

Or you can use AsyncTask
or other things

that suit your
development workflow,

because it’s just Android.

They all work the
same way you expect.

But it’s important to
realize that when you’re

working with some
of this hardware,

it’s still good to offload those
regular, repeated, long-running

operations of the main thread,
even though it’s not a network

access or something that you
might otherwise traditionally

consider as one of those
blocking operations.

RENATO MANGENI: OK, good job.

Yep.

DAVE SMITH: Sorry.

RENATO MANGENI: And
now, we have the code

that supposedly should work.

Again, let’s cross
the figures again.

Actually, first, let me just
go over it pretty quickly.

I have the handler.

The difference
between now and before

is that now I have a handler.

And the handler sliced out the
walk that the looper was doing,

the While loop was
doing, into this method–

Run and Reschedule.

It does exactly the same.

However, at the end,
it posted itself

at the end of the looper.

So it can do whatever has
to do like reading a sensor

or reading keys,
processing any event that’s

come into the main triad.

And then it does another step.

And then it reschedules again.

Again, it is not specific
for Android Things.

This is Android.

And, oh, you won’t
know if it’s working

or not, because it
was blinking before.

I would change the
LED to the green one.

So you don’t have to trust me.

 

Done.

DAVE SMITH: There you go.

RENATO MANGENI: Awesome.

DAVE SMITH: All right.

That’s good.

RENATO MANGENI: OK, now, right?

DAVE SMITH: That’s
good-looking code.

I think we’re following
a pattern here.

RENATO MANGENI: Bye, bye.

DAVE SMITH: Well,
we work for Google.

I feel like we
should be able to do

a little bit better than
just blink a light on stage.

You said something about sensors
and displays on this board.

RENATO MANGENI: OK.

DAVE SMITH: Can we get something
a little more interesting

going?

RENATO MANGENI:
OK, we can do that.

We can do that.

So as I mentioned, there is
one sensor– one main censor

in this board, which is
this tiny thing here.

Can you see right here?

It is the BNP 280.

It’s a temperature and
ambient pressure sensor.

And we can easily connect to
that using the Rainbow HAT

driver.

So let’s do it.

 

And what I’m going to do is
not only read from the sensor–

I also show the results like
the temperature on the segment

display here, right?

DAVE SMITH: I think
that’s a better demo.

RENATO MANGENI: See?

And how I’m going to do that?

That’s really easy.

 

DAVE SMITH: So what you
may have noticed already

is earlier in the
presentation, I

talked about interacting
with a lot of these things

using low-level buses like
GPIO, I squared C, et cetera.

And none of that is
actually in this code.

The reason for that
is that the driver

that we’ve written for this
specific peripheral or rainbow

hat abstracts all of that away.

So there’s two reasons that
we’re showing you this.

First of all,
using these drivers

is a much faster way
of getting started

without necessarily
having to deal with all

those low-level details.

But these drivers are also
open source on GitHub.

And we’ll mention a link to
that a little bit later on.

So you can also use
them as a reference

to see how to build something
with these abstractions using

those interfaces directly,
and you can read the code.

Are you done yet?

RENATO MANGENI: Yeah.

Of course.

You take too long talking.

So on in the On Create,
I initiate the driver,

and I initiate the display
with a few default values.

And here in the loop,
I will keep the LED

so you see that it’s processing.

  Android Studio: Using and Creating Them

And I will display on the
display the sensor value–

Read Temperature.

And that’s it.

 

[APPLAUSE]

 

If you were wondering,
this is in Celsius, OK?

You have to do some conversion.

DAVE SMITH: All
right, very good.

I can see.

Can we switch back to
the slides real quick?

 

There we go.

Awesome.

So you’ve just
seen how easy it is

to get started very quickly
using the new Android

Studio to create a new
project, add some code to it,

and communicate
with peripherals.

And you’ve seen how easy
that is to do in Java.

But, again, if you were
at the keynote yesterday,

you heard that Kotlin is
now a first-class citizen

in Android Studio as
part of the new preview.

So can we use Kotlin to
develop for Android Things?

Well, I don’t see why not.

So this is not from the demo.

But just a simple
example of Java–

I’ve thrown up here to
give you an idea of what

some of this code might
look like if you’re

interacting with other
peripherals on the hat.

This is actually communicating
with that Rainbow LED

strip that was on the
top there, as well as

interacting with the button.

And it’s just doing
some basic setup

to change the color based
on a button in Java.

This code snippet is
fairly straightforward.

It’s about 25 lines of code.

But what if we were
to take that sample

and write it in Kotlin instead?

The same code looks a
little bit like this.

We’ve dropped out almost–

well, actually, a little
more than 10 lines of code.

And you can see some
of the things that

are gone– the Try Catch blocks
are no longer there anymore.

The callback is much
cleaner using a lambda.

And even the array initializers
are also much more expressive

and easy to read.

So this code is a lot more
concise and a lot more

of a joy to even write.

So what do you think, Renato?

Can we show them Kotlin running
on the Android Things board?

RENATO MANGENI: It’s
getting too complicated.

Yeah, sure.

Why not?

DAVE SMITH: All right, let’s
switch back to the demos

and see if you can do it.

RENATO MANGENI: So, again,
this was just launched.

I will just prepare you guys
if something goes wrong.

What we are going
to do here is we’re

going to start a new project.

The Kotlin support
on Android Studio–

you can add Kotlin and
Java at the same time

in the same project.

Just to simplify
things, and so you guys

know that I’m not cheating here,
I will start a new project.

OK?

 

And I would add Kotlin’s
support obviously.

Again, let’s not
do a Fauna tablet.

Let’s do Android Things–

 

same anti activity–
same no UI layout.

 

DAVE SMITH: So one of
the things you can see,

if you haven’t
seen this demo yet,

is that when you include
Kotlin support, by default,

the main activity is
created in Kotlin.

There’s a little bit of a Java
conversion that happens there.

But we’re automatically
in Kotlin already.

RENATO MANGENI: Yeah,
this is the main activity.

And it’s in Kotlin.

And it does nothing.

So now it’s my job to
do something with that.

First, I will add the dependency
on, if you guys remember well–

 

the same dependency.

See, our driver is
written in Java.

It’s there.

It’s open source.

You can look at Java.

It’s published as
a Java library.

It functions as a Java AR.

And the fact that in Kotlin
you can just add the dependency

and use it for free–

there’s no extra
steps to use that.

That’s really amazing.

 

You know what?

I just remembered something–

I’m not that Kotlin developer.

 

DAVE SMITH: Is there
something in the IDE

maybe we could use
to help you around?

RENATO MANGENI: Yeah, in fact,
luckily I kept this class here.

I will just add a little bit
of more correctness to it.

And I will copy my class–

Control C, Alt Tab–

select everything here
on the Kotlin side.

This is the Kotlin file.

OK and I will Control
V. And boom, here it is.

DAVE SMITH: Automatically
converted into Kotlin.

So you are a Kotlin developer.

You just didn’t know it.

RENATO MANGENI: I just
became a Kotlin developer.

Yeah, si, my code is great.

I have a lot of
exclamation points.

DAVE SMITH: That’s awesome.

RENATO MANGENI: So you
don’t trust me, right?

Of course you don’t.

I wouldn’t trust myself.

Let’s confirm that it’s working.

Currently, I have the
blue LED here, right?

So let me change that to the–

what color do you want?

Red?

OK, that’s a red.

It show bad on the TV.

 

See?

I’m running the Kotlin app.

I’m not running the Java app.

 

Launching.

 

DAVE SMITH: Boom!

There we go.

[APPLAUSE]

RENATO MANGENI: Awesome!

DAVE SMITH: Nicely
done, sir, nicely done.

All right, let’s switch
back the sides please.

 

OK.

So we’ve given you
just a very quick tour

today of the new features
in Android Studio

that are specific to Android
Things, as well as just a very

simple walkthrough of
how you can get started

and how quick and easy it
is to really just get up

and running from
zero to a working

project using all the tools
that are available here.

A couple of things while
you’re here at the conference

that I think you
should check out–

first of all, we’re going
to be in office hours

immediately after this.

So if we don’t
have an opportunity

to answer your
questions during Q&A,

you can always
follow us over there,

and we’ll continue
the discussion.

Also, we have a bunch of
Android Things code labs

in the Codelab section.

We have four of
them, ranging from

some basic peripheral stuff
like this to using TensorFlow.

So check those out,
and you can play around

with Android Things in
the Codelab area as well.

Once you get home, and you want
to start doing some of this

development for yourself,
check out the Android Things

documentation, which is
on the Android dev site

at developer.android.com/things.

Download the new Android Studio
Canary at Studio Preview.

And we also like
to encourage you,

if you’re working
with Android Things,

to join the IoT developers
community that we have on G+.

Us and other folks
from the IoT team

are constantly on there
answering questions,

and helping folks
out, and watching

you share your cool
projects and other things

that you’re doing
with the community.

So please check that
out as you go as well.

It looks like we’ve got
some time for questions.

So if you do have
questions, please

feel free to come
up to the mics.

Otherwise, thank you very
much for your time today.

[MUSIC PLAYING]

 

 

You might like

About the Author: admin

You cannot copy content of this page