(energetic music)
(typing)
In this video, we're going to download the
YouTube player library and add it to a project
that we're also going to create.
So, with Android Studio loaded,
we're going to create this new project by clicking on,
Start a New Android Studio Project,
and we'll call this one YouTube player.
Now, notice that I've used a lower case T
in the word YouTube.
For the library that we're going to be using,
that has a capital T throughout.
And what I suggest you do for the company domain
is do something a little bit different.
Now, in my case I'm going to use
learnprogramming.academy for the company domain.
But the reason we want to do something different
is because we need to get an API key from Google
and it's tied to the package name for the app.
Now, I'm going to show you how to get the API key
in another video.
And that key's used by Google to track usage of your app.
And that allows them to charge you,
but don't worry you get plenty of free use.
So that won't be a problem for this app.
But if every student used the same key,
then without the 40,000 students that signed up
to my previous version of this course,
it may well be a problem.
So don't worry about being charged though,
you have to enable billing in the developer console
before Google will charge you.
And you'd have to use the app continuously
on loads of devices all day to exceed
the generous daily limit that they actually give you anyway.
And again, if you haven't enabled billing,
you won't get charged.
And it'll actually stop working until the next day.
So, no matter what if you don't enable billing
you won't get charged anything,
if you do happen to manage to go over the usage allowance.
So for this company domain,
I'm going to stick to using learnprogramming.academy.
But if you've got your own domain name,
I suggest you use that.
Otherwise use something like yourname.example.org,
something that will be unique to you.
And probably a good idea to run your first and last name
together, like Tim Buchalka.example.org for example,
or the other way around.
Just try to make sure you enter something unique there.
Alright, so you know the drill
with the Kotlin support pass by now,
so tick it if you want Kotlin code or un-tick it for Java.
And I'm going to click on Next now,
and in fact you know the whole process pretty much now,
so in future videos we're going to be starting
with the project already created and open and I'll just give
you the project name and template to choose.
Alright, so I'm going to choose API 17 here as before,
go to Next.
I'm going to select Empty Project, Empty Activity,
click on Next.
I'm going to leave the default,
for the activity name and the layout name,
so that Android Studio creates the activity for us,
and also creates a layout and
I'm going to click on the Generate Layout File check box
as well as the Backwards Compatibility one
and click on Finish.
Alright now while that's getting setup,
what we need to do is go ahead and download the library
that we're going to be using for this project.
So let's go ahead and do that.
So I'm going to open a browser.
Going to paste in the URL.
And again what we're doing here, is what we need to do,
is integrate the YouTube API into our app.
And we have to download the library file from the
Google YouTube site which I've gone to now.
And in case you're wondering, API stands for,
Application Programme Interface.
An API provides functions or classes for performing
a certain task or one task or another,
in this case playing YouTube videos.
Alright so pages you can see here contains
a lot of information on using the Android YouTube API.
There's also this "Reference" tab up here which you can
check with all the documentation for the various classes.
And as an exercise, perhaps once you've
finished this app in this section of the course,
you might want to come back and
experiment with various other things.
One example might be checking out this
YouTube.ThumbnailLoader.
So, have a play once you finish
the app in this section of the course.
And maybe modify it to add some more functionality.
By the time we finish this app,
you will understand how to do things like,
launch a new activity when the user clicks a button,
so that you can add more screens to the app if you want.
So there's all this information available,
which makes using the API a lot easier.
But we need to download the library,
so I'm going to click on "Guides" up here,
the "Guides" tab to go back.
Now just down the page here,
is a link to the download page, this one here.
There's also a link over here in the left-hand side.
But I'm showing you this one for a reason.
And it's saying you need to have version 4.2.16 or higher
of the YouTube app installed on your device,
to be able to use this API.
Now previously, I had to suggest that you use a physical
device to test this app on, however,
when the Android Oreo was released,
Google also released emulators that can instal apps
from the Google Play Store,
so that limitations been removed.
There's actually now two different types of emulators,
but we'll be discussing the differences
when we come to running this app.
So for now, I'm going to click on the download link.
That takes us to the download page.
Now the version number while I'm recording this video,
as you can see on the screen, is 1.2.2.
And it doesn't seem to change very often.
But if there happens to be a new version
when you come to watch this, then that's fine.
Just grab that current version and download it.
So I'm going to click on download.
And I'm going to save it to my downloads folder.
Now we're going to find that downloaded file,
and I'm going to double click it in my case to extract it.
But if you're on Linux,
you can right click and choose extract,
and if you're on Windows you can do a similar thing,
you can click on extract all.
So basically,
unzip the file and then go into the unzipped folder,
there's a libs sub-folder,
you want to click on that,
then there's this YouTubeAndroidPlayerAPI.jar,
so we're going to select that.
In my case I'm going to select copy, so you'll do
the equivalent if you're on Windows or on Linux.
And then we want to swing back to our project
in Android Studio.
Now, generally speaking, keeping the project pane in
Android view is more convenient but there are a
few things that you can't do in Android View.
One of these is copying external libraries into the project.
That's because the libs folder, that we need,
isn't available in Android view.
So you can see up here there's no mechanism
to get to the libs folder.
So what I'm going to do,
is switch into project view briefly
using this drop-down up here.
Select Project.
Once I do that I can then click to open the project folders,
click on app.
We've now got this libs folder,
and you can see at the moment that's empty.
So I can right click the libs folder and I can select Paste.
Now you can see that we've got a dialogue
asking for confirmation of the copy.
So make sure the directory it's copying the file into
does say libs at the end.
And you can see clearly here on my screen
that it does say libs so we're in the right place,
click Okay.
Now if we expand by clicking on this little triangle,
we can see that the YouTubeAndroidPlayerAPI.jar
is now in the libs folder.
With that done I'm going to go back now
and switch it back to Android view.
So now that we have copied the file into the libs folder,
we need to tell Android Studio to use it in our project.
Now one way to do that, is to add a reference to it
in the build.gradle file.
The things are changing at the moment,
and you might still be working on the older version
of Android Studio as well as this one.
So I'm going to show you another way.
So you can go into the File menu
and click on Project Structure.
Once you do that, in the left-hand side list here,
select app from the Modules section down here.
Then you want to come over here to the right,
and click on the "Dependencies" tab.
That lists all the dependencies for this project.
Now, as you can see, we're using the app compat library
and we're also using the constraint layout et cetera.
Now if you're producing Kotlin code,
you'll also see the standard Kotlin library in the list.
You can see down here of got that showing as well.
Now right at the top, usually it's at the top,
there's a reference to *.jar files in the libs directory.
Now that would automatically include any jar files
that we place in the libs directory.
And of course, we've actually done that at the moment.
So there's really no need to do what we're about to do,
and that's because our YouTube library will be
automatically included in the project,
because we put it into the libs folder.
But I think it's useful to know how to add libraries,
especially if you decide to keep the
jar files in a different directory,
so I'm going to go through that process anyway.
So to add a jar file explicitly rather than relying
on that catch-all include, you click on the green +.
In my case it's down here, but it may be at the top right
depending on what operating system you're running on.
But click on the add anyway in any event.
Once you do that, you get this little pop-up menu.
And I want to click on and choose Jar dependency.
Now once you do that, that pops up a file browser
which lets us browse the directories in the project.
Now the jar file has to be in the project somewhere,
so on Linux though unfortunately,
there's no ability to move back up to
browse the entire hard disc.
On my Mac though, I have to actually browse into
the project directory and find libs.
And you'll need to do a similar thing for Windows.
So in my case I'm actually already there,
I've already got the YouTube Player app libs folder,
and you can see the YouTubePlayerAPI.jar there,
so with YouTubeAndroidPlayerAPI.jar is actually there.
I can just expand that out we can see the full file name.
So do whatever you need to do to get that open,
select the file then and click on Okay or Open,
depending on what you're operating system shows.
And you can see that that entries
been added now to this screen.
Now if you want you can remove this
catch-all include entry up here at this point.
You just click on it and use the red minus button,
or in my case it's not red but it will be depending on
your operating system it may be a different colour,
and it may also be over here to the right.
But in my case on the Mac it's down at the bottom.
So if I clicked on that now,
that would actually remove that entry.
But there's no need to remove it because the library
won't be included twice if you leave it in.
Now if you're experimenting with different libraries or
trying different versions,
then you don't want everything included automatically.
And this is how you'd prevent
everything in libs from being added.
So any of your libraries you add explicitly
will appear in the project if you actually remove this line.
But I'm going to leave it,
so you can see it in the build.gradle file.
So for that reason I'm just going to click Okay,
because we're actually finished here now.
You can see it's got a gradle project sequence in process,
so Android Studio quickly realised there was a change
and synced the project for us.
Now in earlier version of this course,
we added the library by modifying the build.gradle file.
But the way we've just done it is safer because
there's no chance of typing the file name incorrectly.
It's useful though to be familiar with build.gradle though,
so let's have a look in there to see what we've just done.
Now confusingly with Android Studio projects,
there's two build gradle files,
one you should edit and one that you'll very rarely change.
Now the one we want,
I'm going to click on and expand this down here,
the one we want is in the apps directory.
This one appears lower in the tree,
so you can see below the project YouTubePlayer.
It's this one here, build.gradle module app.
Because we know to be looking at the android view though,
I'm not going to change the file name here.
And if you go back now, have a look in project view again,
you can see we've got this build.gradle and it's actually
in the app path and there's also another build.gradle there.
But if we go back now again,
just to have a look in Android view,
we've got all the gradle scripts are in the same place,
but it still shows us the module app here.
And this is the one we ultimately want to have a look at.
I'm going to double click on that to open it.
And just out of interest,
if you accidentally open the wrong one,
have a look at this other one.
It actually makes a point of saying,
"Do not place your application dependencies here;
they belong in the individual module build.gradle files".
That's really just a warning,
to let you know or to remind you that perhaps you're looking
at the wrong build.gradle file.
So I'm going to close that one down
and leave our app one open.
Now eventually you will get used to which ones which,
but until then just open one and if you see that warning
then close it and then open the other one.
So in this case we've now got the right one open.
So, this module build.gradle file has all sorts
of information telling Android Studio,
or really the gradle build system,
how to build the file that's deployed
to our devices or emulators.
So down at the bottom here inside the dependencies section,
we can see the jar file that we added
right on the last line sitting there now.
Now we could have just typed that line in instead
of going through the menu that we did earlier in the video.
If you do type these entries in,
it's really important that you get the case right.
Now I mentioned at the start of the videos about the
upper and the lower case T.
You can see here that on the libs/YouTube,
we've got a capital T there.
If you use a lower case T in YouTube, for example,
it won't work.
Now if you're used to Windows,
remember to put the slash rather than
the backslash between libs in the jar file names.
So don't use back backslashes in other words.
So at the top of this dependencies section,
is that catch-all entry I talked about to include
all jar files from the libs directory.
Now I didn't delete it there so you can see it in here,
but I'm going to delete that entire line now.
And move that up.
Now it's usually a good idea to be explicit about
what you want to do in programming rather
than relying on default behaviour.
And this is why I've specifically listed
the YouTube Android Player API jar file.
So I've deleted that implementation file tree line.
Now it wouldn't hurt to leave it in,
the library wouldn't be included twice.
But we're being explicit here and we're listing the jar file
in it's own implementation statement anyway on line 34.
Now with that said, there's no guarantee that Google won't
remove that default inclusion of all jars in the
lib folder so it's really useful to know
what's going on here.
Now whenever you make a change to the build.gradle file
you'll get a message at the top as you can see now,
telling you that the file's changed.
And you need to click the Sync Now link,
but if you're making several changes,
you can actually do all the changes first just make sure
that you do the Sync Now or click the Sync Now
by clicking the link once you've finished
the changes or you'll find things won't work properly.
So I'm going to click on Sync Now.
Now there are a few more things to look at in this file.
The first are that these dependencies over here
start with the word implementation.
Now in previous versions of Android Studio,
that used to say compile instead.
And if you're working on an old version as well or more
accurately if you're using an older version of gradle,
then that will be compile instead of implementation.
So watch out for that and use compile
if you're working with the older versions.
Now if you go through Android Studios menu
like we did first, it will use the correct key word.
Now the other thing that you may want to change
in here are the minSdk up here,
and the targetSdkVersion here at the top.
Now these are the values we provided in the wizard when
we originally created the project earlier in this video.
Now if you change your mind,
then this is the place to change them.
So more importantly if you open an old project,
and you want to convert it to target the latest version
of Android or maybe drop support for an old version,
then you can change these settings in here.
Remember that the minimum version now supported
is API 14, Ice Cream Sandwich.
Now if you're converting an old Marshmallow or Nougat
project to Oreo then you'll also want to change
the compile SDK version to 26 and the build tools version
to the latest version of Oreo.
So as I record this it's 26.0.2.
But have a look at any new project that you've created
to get the correct version numbers.
Assuming that you've installed
all the latest updates of course.
Now the last thing here is that it's possible that this
appcompat line is actually highlighted.
Now what I'm going to do,
is change this back just to show you what I mean here.
I'm going to change this back to an older version,
26.0.2.
Now I'm going to show you what happens
when there's a newer version available.
Leave that a moment to sync.
You'll notice now that we've got an older version here,
that this is actually highlighted.
And if I hover over that it actually says
a newer version of the appcompat library is available.
If I actually click on that,
we get the light bulb over here,
I can come down here and I can click on
change to 26.1.0 and that number will
be whatever the latest version is and that will update it.
Then I'll need to come over here and click on Sync Now.
So if you see any entries highlighted like that in yellow,
that's the way to actually update them to make sure
that you're running the latest version.
And keep in mind that when you click on the Sync Now button,
that will actually do any necessary downloads to get
that latest version for you, pretty cool.
Now I've already went ahead and actually clicked on that
and actually updated it,
but generally before you update there are implications
and that's why Android Studio
didn't just go ahead and upgrade it anyway.
Now you'll find it does that for certain things,
like the gradle.build system.
It will prompt you but can update to gradle automatically.
The reason for that is that only effects the build process
it doesn't change what's compiled into your final app.
But changing those library versions does change your app.
So if your apps gone through final testing, for example,
and it's ready for release,
you can't suddenly go changing the library versions.
If you do, the app will need full testing again.
And the other thing to keep in mind is that sometimes
new libraries do introduce bugs.
They can also change the way your app works in subtle ways.
So bear that in mind if you change any
of these library versions your app will need testing again
before you release it.
Now while you're working through the examples in this course
I recommend that you do use the latest version of
all the libraries though.
And that's why I went ahead and clicked
on the light bulb and I changed
the appcompat version to 26.1.0.
Which is the latest version currently existing.
And then obviously,
I went through and clicked on the Sync Now.
Alright, we're going to be coming back to this file later
to look at some of the other things in here.
But for now though, that's the project set up
so that we can use for YouTube API.
So I'm going to go ahead and close the API app,
the build.gradle file here.
And I will finish off the video.
In the next one we're going to start with
creating a new activity to show those YouTube videos.
So see you in the next video.