(music)
(typing)
In the previous video,
you saw how to run the app,
the Hello World app,
on an emulator.
In this video,
I'm gonna connect a real Android device,
my own Android Nexus 5x phone
and run our app on that.
Now, if you're using Windows,
it is important that you instal
the Google USB driver,
when we configured Android Studio using the SDK manager
in the earlier video in section two.
In addition, depending on the model of phone,
you may need to instal
download and instal a specific driver for your phone.
That's only if you're running on Windows.
Now, on a Mac or Linux machine,
however, you can just plug the device in and it all works.
So, that's a bit easy.
We're now gonna be doing this on my Mac.
So, I'm going to assume that in the case of Windows,
you've got the necessary drivers installed
and you can connect your phone to your computer
using a USB cable to transfer files.
Now if you have any problems doing that,
then you do need to resolve them before
continuing with this video.
Samsung phones for Windows, for example,
need KIES, K-I-E-S,
installed on Windows,
or certainly did at the time I'm recording this video.
So do check if you're running on Windows,
if there's specific software drivers needed
for your Android device to communicate with Windows.
All right, so that's set.
The first thing to check before using your phone
to run your apps on,
is to make sure that you can transfer files
from the phone or tablet.
Now, if you can't do that,
then anything else we try to do isn't going to work.
Now, some phones come with USB cables
that are for charging only
that don't allow data transfer.
And if you got problems copying photos,
for example, from your phone to your computer,
then do try a different cable.
We have seen problems in the past that
can be resolved with a new cable,
so certainly if you're having issues,
do try using a separate cable.
Now, if you've got phone transfer working,
let's get started running apps on a real device.
Now, I've got some software that I'm about to
start on my phone that lets me record the phone screen
so that I can show you what's happening on my phone
as well as what's happening in Android Studio.
So, I'm going to switch over to the phone now,
start the recording,
and allow you to see what's actually going on on my phone.
All right, so now you can see that my screen,
my Nexus 5x screen is showing
and what we're going to do is enable
the developer options on this phone.
Now, developer options aren't in any of
the normal settings menus.
Instead, they're actually hidden until you enable them.
And in my case, we go into settings to actually do that.
So we're gonna come down here,
go into my settings,
and this may be slightly different
depending on the phone that you're running,
but in the case of the Google Nexus 5x,
I scroll down to the bottom, okay?
So once I'm on this settings menu
I'm gonna click on System in my case.
So it'll be something along the lines of
Settings, About phone.
You need to get to the menu that has the About phone option.
So I'm gonna click on System in my case.
You can see down on the bottom here,
I've got About phone.
And what I can do there now,
is I can actually click on that
and that brings us to this About phone option.
Now just before I scroll down to the bottom
I'm gonna hit this build number.
Do note that this option here hasn't
got a developer's menu option at the moment.
So what this means is that it's not enabled.
So in the case of my phone,
this is where that menu will appear.
So I go into About phone,
and you'll need to find that for your device.
You get down here and scroll down to the bottom
we got this build number as you can see here.
So, what we need to do is actually tap that seven times.
When you do that,
you'll get a message that developer options
have been enabled.
And it's not very intuitive, but that's deliberate.
So it prevents people from turning developer options on
without knowing what they are or what they're for.
And in the case of using an Android device for testing,
we need to have that developer's option.
So, I'm gonna tap this build number seven times.
And notice that we've got a menu (mumbles)
popping up there now.
And on the phone we've got this message here,
and because I'm using the Google Nexus 5x,
I've got a pattern that I can specify
to unlock this phone,
but if you'll use,
you might have a pin or some other mechanism
to unlock the phone at this point.
So I'm just gonna unlock this and continue.
And now you can see that it says down there
at the bottom that you are now a developer.
So basically, tapping the build number seven times
turns on the developer options
and lets you use the phone for development purposes,
which we want to do in this course.
So you now should have a new menu on your phone
it should either be in this menu here
or if we go back one,
you can see now that we've got this menu
Developer options showing on the screen now,
which wasn't there before.
So your first challenge on this course,
is if you're not running a Google Nexus 5x,
is to find that menu on your phone.
So in my case you can see it's there.
So I'm gonna click on Developer options.
Now there's a host of options in the
Developer options menu.
But we're only really interested
in two of them at this stage.
And only one is really important.
Now the first one, is Stay Awake,
you can see that on the screen there.
And this prevents the phone from locking
while it's charging.
So you won't have to keep unlocking it
while testing your app.
It saves a bit of time and annoyance,
but if you don't have that option then don't worry,
but if you do have it,
then it might be a good idea to tick it
and that's if you have something like a password
or pattern, as in my case,
to unlock your phone.
So I'm gonna select that anyway
to leave it on,
so that it will never sleep while charging
so that it just saves us a bit of time during our testing.
But the important setting is USB debugging.
So I need to scroll down a little to see that.
So there's USB debugging,
the debugging menu there.
Now without this one setting,
you won't be able to test apps on your device
from Android Studio.
So you need to make sure you turn on USB debugging.
So I'm going to do that.
And you can see we've got this pop-up,
and you'll probably get a similar message on your phone.
And it's asking us to confirm that
we want to allow USB debugging.
In this case that's fine,
because we are using it for development purposes.
I'm gonna click on OK.
So once that's done,
you want to plug one end of the USB cable into the phone
and the other into the computer.
So I'm gonna stay on my phone display,
insert that now and plug my device in.
And just before that,
what I'm gonna do is switch back to Android Studio
and I wanna bring up the screen
where we normally start our app.
So I'm gonna come back to there now
I'm gonna click on the Run menu
and you can see here on the list of connected devices
at the moment there's nothing.
There's only the virtual device that
we previously configured that we run our app in
in the last video,
there's nothing under connected devices.
We're gonna come back to that shortly,
but for now, we're going back to
the screen on my phone again.
I'm gonna plug in the cable,
and I've now got this message on my phone that says
"Allow USB debugging?"
And it's giving me the message about the computer's
RSA key fingerprint,
which I have to a bit obscure for security reasons,
but it's just basically a large number.
It's asking us whether we want to allow that.
When I switch back to Android Studio quickly,
you can see at the moment under connected devices
is a code there that says offline at the moment.
So what I'm gonna do now,
I'll switch back to my phone's display,
is I'm going to click on Always allow from this computer.
'Cause this is gonna be my main test computer.
And I'm gonna click on OK,
and once we do that we'll switch back over
to Android Studio.
Clicked on OK there,
and now if we switch back to Android Studio,
you can see under connected devices
this LGE Nexus 5x,
and that'll be your phone's model
and it tells you also the API that is running
and in my case I'm running the Android 8
on this phone, API 26.
I've now got a device that we can actually test on.
So basically what we've done is
we've set up the phone and allowed it,
or given it access,
so that we can use it to run applications to test.
And again, with the deployment target on
you can see that we've got this Nexus 5X
available as an option now,
which wasn't there before.
And we've still got our available virtual device,
which we're running the emulator,
but we've now got a real device as well,
which we're going to be using to run now.
So you can use the same mechanism as I've used before.
You can just come up here and select Run.
And choose either the connected device
and just if I unplug the USB cable now,
that's what I'm doing now,
you can see that then disappears.
Connected devices now.
If I plug it back in again,
give it a moment,
you can see that it actually comes back again.
So once you have done that one time configuration,
you'll be able to access this
without going into any menus
or setting anything else up.
Keep in mind that if you're running on Windows,
you should probably also disconnect the device properly
from the task bar,
as you would like a USB memory stick
or something along those lines.
But obviously in my case, on a Mac,
I haven't done anything to the phone's storage.
I haven't written any data to the phone.
So it was okay just to unplug it.
Now if your phone doesn't appear
or you can't see its full name or the serial number,
that probably means that you haven't ticked
the USB debugging in the developer's options
that I showed you earlier in this video,
or you haven't allowed access from your computer.
In my case let's actually try running this.
I'm gonna click on OK to start it running
and then I'm gonna switch back to
my Google Nexus 5x phone
and check that it works.
Clicking on OK,
I go back to the phone now,
and you can see that on my phone
the app was launched automatically
and we've got the Hello World application
now running on my Google Nexus 5x.
Now by the way, you don't need to be running Oreo,
API 26, on your phone,
as long as your phone is running API 17 or higher,
then the app should work.
And if your phone isn't running
at at least the minimum SDK that we set,
you'll get a message when you select the device
and click OK.
And in fact looking at the slide here,
you'll see that there was actually an indication of this
in the phone's description before clicking OK.
So again, your phone needs to be running the
minimum SDK that we set, which was API 17,
when we configured this project.
Now if that's the only physical device you've got,
you can change the minimum SDK.
You can go into File,
Project Structure,
Hit File,
Project Structure,
(clicks)
and then click on App down here.
And you can see we've got, on the Flavours tab,
come over here to Flavours,
we've got minimum SDK version
and you can change that to match the version of
Android running on your phone.
We can select that.
And actually it doesn't really matter
what is available in the drop-down
because what you can actually do is
you can just type the API number
that you want to set into the box.
So, for example,
if you're using an API 16 device,
you can change the 17 to a 16 just by typing it.
Click OK and the project updates.
You can see here on the bottom (mumbles) was working away.
And from that point,
when you go to run it again after you've finished updating,
you'll be able to run that on your phone,
assuming in this particular case,
it was an API 16 device.
In the case of what we did,
I'm just gonna go back and make that change again.
Go back into Project Structure,
App, click on Flavours,
I'm gonna set it back to what it was,
which was in my case,
17 being the minimum version that we defined
at the start of this project.
So this example,
as long as your phone is running API 17 or higher,
it should work fine.
If not, you'll just need to change the minimum SDK level,
as you saw me do there.
But with 96% of devices now running API 17 or higher,
it should be fine in most cases.
So that's it,
that's how to run an app on your phone
rather than using an emulator.
So it's running fine as you saw
and I'll go back now briefly to the phone now
and if I actually were to take this into landscape,
so I've moved the phone,
you can see the app successfully changes
to landscape automatically.
So that's it.
How to run an app on your physical device.
So as long as you've enabled Developer options
by tapping seven times as you saw me do,
you can turn USB debugging on whenever
you want to use your phone to test your apps.
And you may want to switch off
USB debugging when you're done,
just remember to turn it on again
when you come back to developing on it.
But there's nothing wrong with leaving USB debugging on
if that's something you want to do
and you're testing on a regular basis.
All right, so that's the end of this section.
In the next section,
we're going to start creating proper applications
and I'm gonna be explaining some of the concepts
like main activity and layouts
and so forth in more detail.
We haven't done any programming yet,
but we've now got the development environment set up
and we can now run apps
using emulators and physical devices.
So, we're ready to start.