Android Studio: Tips and Tricks (Google I/O’19)

Android Studio: Tips and Tricks (Google I/O’19)

[MUSIC PLAYING] KAREN NG: We’re pretty excited
to demo our favorite tips that you can use now with
Android Studio 3.5 Beta. And I’ve collected some
of the best suggestions from across our whole team. This talk is all demo. So this is the last
real slide you’ll see. You’ll see 39 minutes
of code in IDE. I’ll start with a
short tip around one of my favorite features,
the Profiler tools. Justin will then show you
some tips around core editing. Diego will take you
through design tools. And then Ivan will end with
building and deploying code. Now, with that,
let’s get started. [APPLAUSE] So what I have here is I have a
really simple image filter app. You can do something like
select a stock image. What it does is it’ll
pull up a stock image. And then I can
choose some filters and go ahead and run that jump. To start the Profilers,
you hit this button. When you hit this button,
it builds the app, and then it starts
a profiling session. I already have a
profiling session running, so it’s already running. This is super small. And from the back, I’m sure
you can’t see anything. So my first tip is
resize the window. Command+Shift+Up Arrow with
Mac does this with any window. Down arrow drags it down. And now you can easily see
our CPU, memory, network, and energy profiler. I’m going to go into
the memory profiler. So this tool is a great
way to detect memory leaks. You can do the normal
thing and grab a heap dump and then follow
the GC route down. But I’m going to show
you another tip that’s an easier way to find if
something’s wrong with memory. So here I have my
allocation tracking to full. What I’m going to do is try to
see if any objects are hanging around after the GC runs. So let me go back to my app. I’m going to go back to
the Select Image Activity. You can see the
activities up here. I’ll select a stock image. That switches me to a
filter image activity. And then I’m going to switch
back, which kills that filter image activity. And then I can hit Command+G or
force a GC event hitting this button. Now I’m going to pause. You can use another
tip in Profilers, which is Command+Minus to zoom
the view out or zoom it in. Zero gives me the default view. And I’m going to zoom
in a little bit here. I want to grab this
entire activity here from filter activity to
after the GC event runs. And then I’m going to
filter to Filter Activity. Here I can see– I can now see the allocations
and de-allocations. So what I wanted to
see was one allocation and one de-allocation. So this might be indicative
of something going on. The next thing I want
to show you– sorry, I going to switch back. I’m going to change
this back to none. The next thing I want to show
you is our energy profiler. So working on mobile tools, you
know that battery and Wi-Fi is pretty much like air and water. And it’s really hard to see
how much battery your app uses. So the Energy Profiler, if I go
ahead and select a stock image here, it does a
couple of things– so if I’m going to
run these filters. It takes your CPU
network and GPS sensor, and it digitalizes the amount
of energy your app is using. The other thing it
does is in this demo, it’s using the Work Manager– Jetpacks Work Manager library. So when I change those
filters or RAM them, they’re being run
as async tasks. Another little tip in
the Energy Profiler is it also shows
me system events. So if I go ahead
and grab this, you can now see if you were
using Work Manager, I can see all the jobs
that are running as well. OK, with that, I’m going
to give it to Justin. AUDIENCE: Thanks Karen. [APPLAUSE] JUSTIN CLAREBURT: Thanks Karen. That’s fantastic. Now we’re going to
switch over, and I’m going to give you some
tips about core IDE. It’s worth pointing out that
Android Studio is powered by the IntelliJ platform. So a lot of the tips
I’ll be showing you today work across many of
the JetBrains products. First, we’re going
to look into how to become a master
of the environment, how to move around Tool
Windows, Editor Windows, and find whatever you need. So I’ll switch over
to Android Studio now. At the heart of
Android Studio is Tool Windows and Editor Tabs. You can open and
close Tool Windows by clicking on the
little buttons that are around the edges. But, of course, it’s much
easier to use the keyboard. If you look carefully at
the ones down at the bottom, you’ll see this
underlined number nine. If I press Alt+9, it
opens that window. And Alt+1 will Open
Project Window for me. Press Alt+1 again
to toggle it closed. So that’s really handy. But if that was easy for
you, and you’re already doing that, how about we wake
you up with a super pro tip? What if you want to get
to this gradle window, for which there’s no shortcut. I can use the switcher. Control+Tab switches
between most recent files. But if you hold the Control
key and don’t let go, you can now navigate
with the arrow keys. Notice that every
item on the left here has an underlined letter. And the gradle window has
a G. So I can just press G to open the gradle window. Now, of course, you want to
be able to close windows. Shift+Escape closes
the window you’re in. But it’s also super useful
if you’re inside your Editor and you don’t want to
have to leave your focus to get that extra space. So without leaving the Editor,
just press Shift+Escape, and it closes the most
recently opened window. It’s great. You can easily get back
to it with F12 any time. And Escape puts you back
in the document well. And now at trick that
I use all the time is to hide all windows. Just give me every
space that I’ve got. Control+Shift+F12
hides all the windows. And it’s neat because
you can press it again to bring them all back. Let’s see what else
I’ve got to say today. Well, I’ve got
some windows here. It’s worth noting that whenever
you’re in a tree view in a Tool window, in many
different places, you can’t just start typing. And immediately,
results get filtered. And you can navigate
with the arrow keys. Particularly handy
in the Find results if you need to filter
through thousands of results, it’ll instantly take
you to what you need. But we’re done with our
Tool windows for now. Let’s take a look
at our Editor tabs. So these are our Editor
tabs up the top here. And as I mentioned, Control+Tab
switches between the most recent tabs. But what if you want
to move to the one immediately to your right, this
tab here, or this one here? For that, we use the Alt key. Alt+Right moves to the right. And Alt+Left moves to the left. If you happen to be in a Layout
Editor and you want to switch between the tabs
at the bottom here, Alt+Shift+Right and
Alt+Shift+Left help toggle through that. This also works if you happen to
be in some of the Tool windows, like Version Control. You can use the Alt key
to tab between those. So now that we’ve seen
that, if you happen to be on a wide screen,
like many developers are, it’s sometimes very handy
to have more code available. So here I can take a tab
and I can say, move right. Now I’ve split it into
two different editors. And you can easily
drag other editors over to join the other side. Now at this point, I would
like to unsplit that row. I’m going to show you
the Find Anything. Double Shift opens a box that
lets me type anything I want. If I type “unsplit,” I get an
action, I could press Enter, and all my windows are
back together again. This double Shift also lets me
find files, classes, symbols, and actions. But pro developers
tend to filter directly to files, or classes,
or actions if they know that’s what they want. And it’s easy enough to
remember the shortcuts. They all start with N on a
Windows and Linux versions of Control+N, Control+Shift+N.
And so you can easily find a file with Control+Shift+N.
And it filters directly to files only. So that’s just getting in
and around our environment. Now let’s look at getting
in and around code. So, one thing worth noting
is my favorite setting, which I always turn on
every installation, is Camel Hump navigation. I’ll show you the beauty of it. When we use the Control
key, we can skip words with the left and right key. And when we use the Shift
key, we can select text. Put them together, and we
can select entire words. But what do we do if we want to
change this part in the middle here? It can be very painful to get
there with many keystrokes. And I don’t want to have
to pick up my mouse. So now, opening up
my Find Anything, I search for Camel Hump. And there’s a setting
here, Use Camel Hump Words. And I can turn the
setting on right here just by pressing Enter. Now with one keystroke,
I can go straight to the next capital letter. And with another, I can
select that middle part, easily replace it
with a new name. Neat, huh? Now to get to the method
below, you press Alt+Down. You repeatedly press Alt+Down
to go a method below and press Alt+Up to go to
the method above. This is especially useful
if you’re at the bottom of a method, and you’re
trying to get to the top, just press Alt+Up and it takes
you straight to the method signature. But now, what if you find
you are navigating around, you’ve been moving
everywhere, and now you finally want
to go back to where you were editing code last? At this point, we use Go
To Last Edit Location. Control+Shift+Backspace
takes me right there. So, let’s look about
some of the language features that make it a lot
easier to code in your editor. We’ll start off by talking about
the method info and parameter info. I’m trying to fix a bug here. There’s something wrong
with the image dimensions. So I’m going to say,
what does this method do? By pressing Control+Q, it opens
up a little tool tip with all the Java doc. Now I can read the method
signature, what it’s returning, and any comments about it. You can even click to drill
further to find out more about the objects in it. Now, if I happened to be
inside the parameter set, I can press Control+P
on a windows box. And now I can Tab and Shift+Tab
between the parameters. Oh, hang on, I see
what’s going on here. It’s looking for a width,
but I’ve provided a height. So now I’m going to use
basic code completion. Control+Space opens up
basic code completion. And cleverly, it’s
automatically put cart width at the top of the list
because it matches part of the parameter name. I like that. Now, to take that,
there’s two different ways of taking these things. You can either press
Tab or press Enter. If I press Tab, it
will replace the text that’s there, which is
what I usually want. But in this instance, I want
to keep that so I can use it as my next parameter. So I’m going to press Enter. And now the text stays, and
I can simply add a comma, and I’ve got another
parameter that I can use. But let’s look at
smart code completion. I suppose most people here are
familiar with code completion. But did you know
how smart it was? If I need to fill out
the parameters here, I don’t even want to
look and see what I need. That’s too much thinking. If I press Control+Space, I get
a long list of too many things available to me. But if I use Smart
Type Completion, I can press Control+Shift+Space
and just get a filtered list of only those variables
that will fit in that type. That means I don’t have
to think half as hard. And when I press Tab, it’ll
automatically completes it with a comma and moves
me to the next field. So I make it a reflex. I always use Smart
Type Completion. And in this case, it’s going
to give me the one item I need. Even better still, it’s
so smart, if I do it from the beginning, if you look
down, it’s already suggested the two parameters that will
complete this [? line. ?] And with one tab, it
fills them all out for me. How’s that? Now, the next section is
going to be a little bit mad. So hold onto your
seats because I’m going to blind you through
a whole lot of editing tips and tricks, including some
advanced and some basic ones. I expect you’re probably
using a lot of these today. But see if you can pick
which ones you’re not using and maybe try to work out how
they can be helpful to you. Don’t worry about trying
to learn all these things. If there’s anything
you need to find, you can always use Find
Action, Control+Shift+A. And then you can look
for what you need, and you find a shortcut
that applies on your system. Let’s get started. So here’s my task. I need to add Look Up and
Query has extra parameters. I’m going to copy this. I’m going to copy that. Hang on, did I just
copy two things? That’s going to
come in handy later. First, scroll the page
with Control+Down. Down here I want to
join these lines. And Control+Shift+J
brings them together. I can repeatedly press it
to bring up the bottom line. Now at this point,
Expand Selection increases their
selection through scope. It nicely stops at blank
lines and then goes through to Parameters. You can bring it back with
Control+Shift+W so you get just what you need. Duplicate code, I
can take this block and make a copy, many
copies, and it doesn’t map with my Copy/Paste buffer. Comment that code with
Control+Slash and comment it again with Control+Slash. The neat thing is if you’re on
a single line, you can press it, it moves down one. That’s especially
useful if you want to turn one lot off
and turn one lot on. Let’s look at moving lines. Take these two
lines– you don’t even need to select the whole line. You can move them up or
down using Control+Shift+Up and Down. At this point, if I’m going
to move it into a block, it automatically
formats it for me. But notice, it won’t go
any higher than the scope. For that, there’s
a special shortcut. Alt+Shift+Up will push those
lines wherever you need them to be. But I don’t even
want those lines. So I’m going to lose
Control+Y to delete them. Now, I want to get
to this section here where it says “Search.” But I don’t want
to use my mouse, so I’ll use Control+F
and just start typing. That’s easy enough. Now, to grab this item
and the one below it, I’ll these multi-carets. I’ll grab one, I’ll
grab another, hell, I’ll grab several. Actually, I went too far. I’ll use the Shift key to
grab back a little bit. Now I can paste the
item I copied earlier. Keeping my two carets,
I’ll push them down again, use Expand Selection
to select both. And now Control+Shift+V lets me
copy and paste from my previous items. How’s that? Now, let’s see what
else we can do. We’ll have a look at
extracting variables, methods, and parameters. I see a lot of this
code is duplicated. So why don’t I take this
and make a method out of it, or a function in Kotlin. Control+Alt+Shift+T offers me an
entire refactoring menu where I can do all sorts of things. It’s worth noting that the
shortcuts for Extract Variable, Extract Parameter, and Extract
Method are all start with the same Control+Alt and
end with V for variable, P for parameter, M for method. I’m going to take this one
now, give it a method name, and note that it creates
the method for me and automatically
calls it in that place. Sure, that’s great. But what about all
these other things? I want those to be
in the method, too. So I’m going to undo that. Now, here’s a pro tip. If you’re going to be
extracting methods, and there’s part of it that’s
similar but just different a little bit, extract that
into a variable, Control+Alt+V. I’ll take both of these. I’ll call it Prefix. Now if I try to extract this
part with Control+Alt+M– we’ll call it Build Path– the tool detects that
there are already two other sections
that are very similar and will replace them if I want. Yes, I’ll take that. And yes, I’ll take that. Thank you! And now I can easily
put this back. And I’m back to where I was. That was using the
inline variable. So now I’m going to show
you some more things. Before I show you
Extract Parameter, I’m going to show
you Surrounds With. I want to stop this from having
errors, so Control+Alt+T. And now I can now around
with anything I like. In this case, I’m
going to type Try. And I can surround
it with a Try Catch. Control+Shift+Enter does a
complete statement and puts my caret where I need it to be. Now I’m going to use a live
template like Log D, which will print out a log statement. Or like the classic
Java System Out, I can type [INAUDIBLE] to
get a Kotlin Print statement. At this point, I want this to
be different in every caller. So I’m going to extract
that as a parameter. Note that I’ll give
it a name, Error Text. And now that text is going to
be put in every method that was called before. Here you can see the
text is up here above. Oops, wrong caret. Now to get to the method,
Control+B goes to definition. But if you’re already
at the definition, it’ll go to the callers. Now I can change this text
and say, error 2 and error 3. Now let’s look at
some intentions. You can just tell the tool what
you want and have it do it. Let’s say I want this to
start returning a Boolean. I’ll get a red squiggly
and a light bulb because it’s not a
Boolean function. But when I press Alt+Enter,
it will change the return type to Boolean for me. That’s neat. So now I can come up here. If I want to wrap this
in an If statement, I can use Control+Alt+T
Surround With again, which is handy if I want to
get an Else block as well. But check out
post-fixed completion. If you have a Boolean,
you can just type “.if.” And now when I press Tab, it
wraps that in an if statement. I like that. You can also use post-fixed
completion for an iterator. Let’s say I have a list already. If I type “.for” and press
Tab, it gives me a variable that’s nicely named as the
singular version of my plural and puts me right
back where I need. Now, this is kind of cool. At this point, I realized I
could have just always used this one loop to get me around. So I’m going to fix this up
with a smart code completion, put my URI prefix in here. And now delete line
becomes my friend. Control+Y repeatedly
to delete all that. Now, there’s more intentions. If I hold my caret on
anything and see a light bulb, I can see what it offers me. And there’s all sorts
of things like Invert If condition, which nicely flips
and indents everything for me. And there’s another
one over here. It’s offering me to
replace a negated– and negate the Is Empty
and flips that around. Of course, there’s
dozens of intentions. And I encourage you to go into
the Search and look for them yourself. We have provided lots of
intentions specifically built for Kotlin. So you’ll see all
of those in here, including the ones that
I just demonstrated now, and many other great ones
like Merging If Statements. So that’s the end
of my fly-by demo. And so with that, I’d like
to hand over to Diego. He’ll take a through
design tools. Thank you. Thank you. [APPLAUSE] DIEGO PEREZ: Thanks Justin. So in Android Studio, we have
a number of design tools. Let me login. There we go. So we have a number
of design tools. Today I’m going to show you
some tips around three of them. So first we have the
Navigation Editor, which will help you to
edit your navigation graph. Then I’m going to show you
how to use the Layout Editor and a few ideas around that. Last, I’m going to show you
the newest design tool, which is the Resource Manager. So let’s get started with
the Navigation Editor. So let me open my
navigation graph. So this is the navigation
graph in my application. What this has is
all the destinations in my app on how to go
from one to the other. So as you can see, I
have a title screen, which is the entry point. So what you see here is a
very simple application. But you can already
see how he’s getting a little bit complicated. So you can zoom in and out as
with any of the other tools that we have. So you can press plus and minus. You can navigate through
the different destinations, click in on the
left side so you can see how the different
destinations look. But I wanted to show you
that in this situation– so basically, I have
changed some things. You can manually
move them around. But I want to get
it in a state that helps me to– in
a consistent state that basically I can start from. So if I click this button
here, which says Outdoor Range, I click it, and it will
get it nicely sorted. On the left side, I
have my entry point. And then you have
all the columns. You can still move them
around and to put them in the better position. But this will give you
a nice starting point. The second thing that
I wanted to show you is how to create nested graphs. So, nested graphs is our way to
organize your navigation graph, and to reuse some parts of
it, and to simplify this one that we are seeing here. So the way to do that is I
just select the destinations that I want to put
in the nested graph. So I’m going to select
this one and this one. And then I simply right click,
move to the nested graph, and click New Graph. I can also press Command+G.
So as you can see, those two destinations have gone
into an nested graph that I can easily reuse. So let me navigate to it. So I’ll double click
in the nested graph so you can see that
this is much cleaner. So this way, you can
organize your application in different ways. So those were a couple of ways
to organize your navigation graph a little bit better. Let me now go to
the Layout Editor. So to do that from
here, I just double click in any destination,
and here we go. This is the Layout Editor. What you are seeing here is a
layout with our RecyclerView. So our RecyclerView is just
one item that is repeated. And the data can come
from any dynamic source. So it can come from the network. It can come from our database. When you are designing
your app, you don’t have access
to that information. So what we did in
the Layout Editor is you can get that information
from using sample data. So it’s a way to basically have
some information that you can use to populate your layout. So let’s see where this
information is coming from. So if I click here, and I
open the Attributes Panel, let’s see what item I’m using. So this is the item that I’m
repeating in my RecyclerView. If I come and click, it
will take me to the– to that layout. If I select one of
the items, let’s see how this is populated. So the text for this, as you
can see, it’s not hardcoded. It doesn’t come from me
typing in every single item. So let’s see where– what this is. So let me press Command
again and click. So this is the JSON file
that defines the information that I have in my RecyclerView. So it is just a
regular JSON file. You can put anything
that you want. And you can give it any
format that you want. This will be available
for you to use. So if I type this
and I put Diego, now, let’s go, and that will update. So this is a very easy way
to populate your layouts. So let me show you something
else in the Layout Editor. Let’s go to this user profiles. So there are a couple
of things that I want to do in this layout. One, I want to change the
color of the user name. I don’t like it. I want something different. And I want to move that winner
badge which is the [INAUDIBLE] that you see in
the bottom right. I want to put it next
to the profile picture. So let’s just start with that. If I try to move these
badge, if you see, it has two constraints, one
to the parent on the right, and one to the top of the name. So first in order to be
able to move it freely, I need to remove them. So in 3.5, we have changed this
a little bit to make it easier. So you can do it in two ways. One is press Command
and then click. So that is one constraint on. The other way is to select the
constraint and press Remove. So that’s the other one. Now I can freely move it around. Now let me put it here. So that doesn’t look right. I cannot see the badge. The reason for that is
with Constraint Layout as with many other view
groups, the order where we like– that we
used to paint things is the order index amount. So, obviously, this picture here
is below than the other picture that I showed you before. So let’s fix that. You can do it in many ways. I can go to the XML
and move the item. That’s one way to do it. I want to show you a
different way to do it today. So if I open the Componentry,
it shows the hierarchy of my layout. So as you can see here, this
is the badge that I moved, this is the profile picture. So you can just simply
drag, put it below. OK, so now I see it. So let me constrain it back. So I’ll put this constraint
back in the bottom. And that’s it. Now, the badge is next to it. You can do many things
with the Componentry. You can add constraints. You can remove constraints. The other thing
that you can do is you can navigate
to other layouts. So, for example, I
have an include here. If I double click, it
will take me to it. If I want to go back, I
click back, and that’s it. So let’s go with the
color of the name. So if we click the name,
we see that the color is set to Color Accent. If we click the
background, we also see that the color is
set to Color Accent. So let’s change the– we have a couple of
ways to do this as well. So one is let me click the
big Resource button here. And that will take me
to the Resource Speaker. So in here, I can change
the color to something else. Let me change it to
something very obvious. If I click OK, what
this will do is it will change the color accent
all through my application. So anywhere that I am
using Color Accent, this will be that color. So let’s assume that I
don’t want to do that. I just want to change the
color of this particular view. Well, in that case, I can
click in this preview, and this will open
the new color picker. So if I’m here, I select
a different color. So now you can see that
only this view actually changed color, nothing else. So, yeah, that’s
pretty much everything that I wanted to show you
around the Layout Editor. So let me now go to
the Resource Manager. So the Resource Manager– let me actually open
and empty layout. So the Resource Manager is
a new tool that we have– well, the newest tool we
have in the Layout Editor and in the [INAUDIBLE]. And it basically allows you
to see every single resource that you have in
your application. So you see drawables, you
see colors, you see layout, and you see others. So I can quickly see the
previews of all my drawables. I can click Layout,
and I can also see the preview of my layout. I can switch to a different
mode when if I want to see it– I want to see all the previous. And yeah, I can navigate
through everything. So one thing that I can
do is also, of course, yeah, if you click this
filter bottom at the top and click Show Libraries, you
can also see all the resources in your application not
only coming from your code but coming from every
library in your project. So in here, you can
see that this is also coming from our [INAUDIBLE]
and every other resource. So it’s a very nice way to
navigate through all of them very quickly. So let’s do something with this. So if you basically
click any of the images– so let me actually get
this one and put it here. So that will create
a new image field. If this one– this not
only works with images. This also works with layouts. So if I get an hour layout
instead, and then drag it and drop it, this will create
and include into my surface. So again, it’s very simple. It’s a very convenient way to
actually deal with these things Not only works with the Layout
Editor, so if you go to Text, on you are in XML– I’m going to actually
close this a little bit– so you can get a different
image, so this one. If you drag it into
an empty space, that will create an image. If I select a different
one, this one, and I drag it on top
of an existing image view, what this will do is it
will change the existing image. So again, very
simple, very quick. So yeah, so that was– oh yeah, there was– I forgot about something else
that I wanted to show you in the Layout Editor. Let me go back to
the main graph. So this is a main graph. So let’s go here. So yeah, there are a
couple of cool things that I wanted to show you here. So I have this layout. I can stretch the
orientation by pressing O so you see how it looks in
different– like landscape and portrait. So this doesn’t look great. You can see that the
image is scrubbed. You can solve this in
many different ways. One of them is you
can go and change the scaling of your Image View. But I want to show
you a different way that you can do it using
just Constraint Layout. So if I go to
Attributes, and you see this constraint
we get here, you can change many parameters
of the constraints. But I want to draw your
attention to this corner here. If you click it, it will
set the aspect ratio. So now the aspect ratio of
this constraint is 1 to 1. What this means, I
change it to something else like to 1 or
anything that I want. My default is 1:1. So what this means
is Constraint Layout will keep the aspect ratio
of this image all the time, no matter the size of your
layout or your screen. So with that, if I press O
now, that looks much better. The other thing that I can do
is if you drag this corner here in the Layout
Editor, you can also see how it looks in any
device or resolution. Very quickly, you can– anything that you want. So for now, I’m going to
leave it in Pixel 2 here. So yeah, so that’s it for me. That’s everything that
I wanted to show you around our design tools. So now, Ivan is going to
show you some cool trips– yeah, tip and tricks around
build, debug, and deploy. [APPLAUSE] IVAN GAVRILOVIC: Hi everyone. Today I’d like to share
a few tips and tricks around building, and running,
and deploying your application. So I’m going to start
with the basics, and that is how you
organize your project. So let me close
all of these tabs and bring up the
project for you. So, I know many of you might
not be comfortable editing Gradle files. I wasn’t either. But let me explain
you a few things. So basically in settings on
Gradle, if you open it up, you can see this app and core. And those are two
projects that are actually included in our build. And by default, Gradle is
going to look in the location– in the Directory app and in the
Directory core in order to find these projects. So Gradle is, in
general, using convention over configuration
paradigm, which means that it assumes
a reasonable default values for many of the options
that you’re able to configure. However, you might
want to change it. Say I want to put the core
under the Library’s directory. So I can just pull it in here. And now I need to specify the
new location of the project. I can do that by specifying
the project directory here and passing the new location,
which is Libraries core. So you can see that
the name is still core. So if I’d like to change
that, I can do that by simply renaming it here. And let’s call it “core-utils.” And if I hit the
Sync button here, the Studio’s going to
pick up the new model. And it’s going to show
them on the left-hand side. So let’s switch to
the end review here. And you can see that now
we have core-utils here. So there are other
things you can configure as well, for
instance, the Build directory. So I open up the core-utils
build.gradle file. In here– I’m not going
to demo this right now. But I just want to show
you that you can set the build’s directory values. So if you want to have
a global place where all of your products
are creating artifacts, the build artifacts,
you can do that here. For instance, you might
have a faster disk or some common location
so it makes it easier for you to clean up. Cool. So, the next thing that
I would like to mention is managing dependencies. So in projects that
have multiple modules, it can be a bit difficult
to actually update all of the dependencies
if there are hardcoded, such is the case here line 36. So this line, what it does, it
adds the AppCompat dependency to the project. And when there is a new version
of the AppCompat library, you basically need to find
all of the occurrences of the hardcoded
Maven coordinates and replace them
with the new version. Why you want to do
this is you typically want to have the same library
version all across all of your projects
because otherwise you might run into some
issues where you’re compiling against one
version and running against a different one. And they can run– you can cause some
runtime issues that are pretty hard to debug. Let me show you how
you can do this. So, I’m going to go back
to the Project View here. And I created a
library_versions.gradle file. So here I want to specify
the Maven coordinates. So I have AndroidX. And then I’m just going
to create an empty map. This is groovy. You can write any groovy
here in this file. So that’s write
“AndroidX.appcompat equals.” And then I’m just going
to go back to this file, copy this value,
and drop it here. Now, I would like to access this
value as libs.AndroidX.appcompt in my build files. So for that, I’m using this
extension object that exists. And an extension object is
a Gradle-provided artifact. And you can add additional
properties here. So I can create a property libs. And initially, I am going to
set it to be an empty map. And right now, I can actually
set the value for AndroidX– oops, sorry typo– for on the value of
the map that I had just created above on line 6. So now how I’m going to
use this in my project is I’m going to open up the
build.gradle file in the root project. And if I uncomment
this line and sync, what this does it runs the
script that I have just created, so
library_versions.gradle, against the current
build script. So it adds all the properties
that I have created to the extension object. So, if I switch back to
the core-utils product, I can actually access
those properties with libs.AndroidX.appcompat. And it’s not only in
the core-utils project that I can access them. I can also access them
in the App project. So let’s open up
app, build.gradle. And here, I have conveniently
left another occurrence of this library. So we can add it here as
well, access it has AppCompat. If I hit Sync now,
it should pick it up. Hopefully I made no typos. Let’s see. It works, cool. Now when there is a new
version of AppCompat, you can just change the one
in library_versions.gradle. And you don’t have to go
throughout your products and hand down all the
currencies of that library. Now– sorry– here you
can see that we have just added a single line
in order to add the dependency of the product. But Gradle is going to
download all of the libraries that this library
transitively depends on. So it’s not only
AppCompat that we’re going to have in our application. We’re going to have all of
the runtime dependencies of AppCompat. So you might want
to actually explore what are those
libraries in order to understand why something
part of your final APK. For that, we have built
products start to dialogue. So if you go File and
Project Structure, and here on the left-hand
side, you select Dependencies. And I’m going to select
the core-utils module. So here in the core-utils–
core-utils module, you can see that their
AppCompat is specified. And that’s the explicitly
specified library in your build.gradle file. However, if you click
Resolved Dependencies on the right-hand
side, you can actually see all of the variants
of this library. And on top of that,
you can actually explore the dependency tree. So for AppCompat, we can see
that it depends on the Fragment library. And further on, Fragment
depends on the loader and so on. So here you can get
a pretty good view of all of the libraries
that you actually depend on. So let’s close this. Next thing that I wanted to
mention is the build speed. So I know it’s a pretty endless
topic where we are constantly trying to make you
more productive. And we’re putting a lot
of effort into that. However, there are some things
that you can do yourself. And that is to make sure that
your build is set up correctly. So one of the things that I like
to do is to run the build once, and then make no changes, and
then run the build second time. And what I would like to
see is that the second time build is fully up to date. So if you change
nothing, the final output should be the same. No tasks should actually run. So let’s try it out
with this project. So, I selected core-utils here. I select Build and Make
Module, which is basically building the whole project. If you pull up the Build
window here, at the bottom, you can see that there
were 21 tasks, 19 of which were executed and
two were up to date. And that’s fine. Now if I hit Built again, I
expect it to see all of them up to date. However, you can see that
five of them were executed. So if you are able to try
this out on your projects, and you see that
something is wrong, you can debug it further. Let me tell you how. So here are all the
tasks that were executed. And so you can see that some
of them were up to date. And that is expected. However, there is this task,
generateDebugBuildConfig, that actually ran. There was something that changed
that caused this task to run. So, this could be due to
your build.gradle file. Or it could be
due to some plugin that you’re applying
in your project. So let’s quickly skim
through this file. And here, you can see that I
am trying to add information when the application
was built. For instance, you might use that in the
About page or something else. And I’m setting the
BUILD_TIME field of the buildConfig as new Date. Well, this new Date
here, that is going to change whenever we run. Because whenever
there is a new run, we are going to pick
up the new value. So this task will have to rerun. So let me fix that by
setting it to a constant. And now if we select
the core-utils module, and if we build,
the first time, it is expected that there were four
executed tasks because we have actually changed the value. However, if I run
for the second time, you can actually see that all
of the tasks were up to date. And this is exactly
what you should be seeing in your own builds. That was the bit about Gradle. Now I would like to also show
you the new deployment flow that we have developed
in Android Studio 3.5. And that is Apply Changes. So previously, Instant
Run was our attempt– better– to try to make the
incremental builds faster and to provide a
hotspot for Android. Apply Changes is fundamentally
different than Instant Run. It relies on the support in the
platform on the device itself in order to provide
incremental updates to the APK. So when you run this
on the emulator– I’m just going to
minimize this in order for you to see the emulator. So right now, we are
building the APK. Here you can see that
there are three buttons. So we have the Run button
that is building the full APK. And it restarts the application. There is this button, which
applies the resource– and with resource changes. And it restarts your
activities so you’re able to see those resource
changes in on the screen. And there is the third button,
which applies code changes. So if you change
the method body, for instance, you
can hit this button, and a new version
of code is going to be deployed to the device. So here, let’s go to
the Leaderboards page– screen. So here we can see players. We can see their score. And we can see their rank. So let me show you how you
can change a resource really quickly. So, let’s open up
the strings on XML. And say, I would like to
rename the application. So this is really obvious. And if I hit this
button, what happens is that the APK
on the device, we get all the information
necessary from it. we know its structure. We build the APK on the host. So we invoke the full build. Then we create a diff
between those two APKs. And only the diff is actually
deployed to the device. So the runtime is
able to pick it up. So here you can see that
we picked up a new name– so it’s Quiz Sample– without actually
restarting the application. And if what it allows you
is not to lose the contact. So if you’re somewhere deep
down in the application, then we make your life easier. Another thing that I’d like to
show you is the code change. So, let’s quickly
open up the– sorry– User Profile page. So this is a screen that
shows the user profile. And I have conveniently
created a bug here in order to demonstrate you something. Let’s put a breakpoint here. So now if I open
a profile here, I have hit this breakpoint here. And you can see that
the user rank is two, although I have clicked
on the second user. So this is a screen
that’s supposed to paint the next user. And it’s supposed to pick up
the value from this bundle. So let me change
this here quickly to get an integer because
I know how to fix this. I wish every bug was like this. And let me redeploy the code. So now what we are
doing, we’re redeploying the new version of the
application to the device. Now here’s the interesting bit. You see this in the bottom? You can see this
exclamation mark that actually says that the
current code is obsolete. So what this means is
that the current version that we are running in
has been overwritten with a new definition
of the method. So I can click this button
here, which says “drop frame.” What it does, it puts
me back in the context of the method that invoked
the onCreate View method. So now if I continue
here, what it did, it re-executed this new method. And it picked up
the new version– new version of the code. And you can see that the
user rank is correctly now set to two. It’s not one anymore. So now if I click
Continue, we can see that the rank two is loaded. [APPLAUSE] Thank you. So I hope you will
find this useful. And I hope you’ll be able to
leverage this Apply Changes deployment flow in order to
make your debugging easier. As you can see, it’s very
useful when you’re somewhere deep in the application and
your just fixing bugs as you go. With that, I would like
to wrap it up for today and bring everybody
back to the stage. [APPLAUSE] JUSTIN CLAREBURT: Thank you. [MUSIC PLAYING]

8 thoughts to “Android Studio: Tips and Tricks (Google I/O’19)”

  1. The green popups in Justin's presentation are from a plugin called Presentation Assistant.

  2. Nothing like using a bunch of cryptic keyboard shortcuts on a MacBook with a crappy keyboard where many of the keys don't work properly. No thanks, I'm fine with the mouse.

Leave a Reply

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