Google have made Kotlin a top-level
language for Android development, which
means that it's fully supported in
Android Studio. And they'll also be
publishing example code using Kotlin, as
well as Java. At the moment though, most
of their code samples are still in Java.
Now, it turns out that's not a huge problem,
because we're gonna look, in this section,
at how Android Studio can convert Java
code to Kotlin for us. So in this section,
we're going to look at the Kotlin
programming language, just to give you an
overview and introduction to it. So let's
get started. Now Android Studio's
designed for Android development, which
means that the projects it creates are
intended to be run on an Android device,
and not on your computer. Now it is
possible, though, to get Android Studio to
compile and run pure Kotlin code. It is
very fiddly, so we've created a project
for you. Now this that Android Studio
project's been specifically configured, to
run pure Kotlin code that'll run on
your computer. Now before I show you how
to get that working, there's another
alternative. Now if you're already using
IntelliJ as your IDE, then I recommend
using that for these tutorials instead.
Now Android Studio is based on IntelliJ
IDEA, so everything will work, just as is,
in the videos. Now you may also be taking
Sarah Etrich's Kotlin for Java
Developers' course, in which case you'll
see how to install IntelliJ IDEA in that
course. For everybody else, though, I'm going
to go through the steps to get Android
Studio to run pure Kotlin code. Now the
project we've created is in a zip file
in the resources section of this video.
So let's just move over now, and I'm
going to show you how to get that zip
file how to unzip it, and get it ready
for use on your computer.
Alright, so the project we've created
is in a zip file in the resources
section of this video, and this is the
file that we're going to be using for
our Kotlin tutorial. And if you move the
mouse over the video,
a pop up balloon will appear at the top
left, and just above that you'll see a
menu icon. And if you click on that and
scroll down to this video in the list,
you'll actually see an area where you
can download the file - and the file that
you want is called KotlinTutorial.zip.
So you can check that out and
download that file because we'll need
that file to actually work on these
tutorials - the Kotlin tutorial. And if
you've got any problems finding the
Kotlin tutorial file, just look in this
lecture - the lecture that you're
looking at now. Basically, just go into
the top left of the screen and you
should see a resources section, but also,
the last lecture of the course has got a
list of all the downloads, so you can
grab it from there as well. And what you
need to do is download that file -
KotlinTutorial.zip - and put it
somewhere on your computer. And you can
see, in my case, I'm on a Mac and I've
downloaded into a folder. Once you've
done that you need to unzip it. Now in my
case, on a Mac, I can just double-click
the file to unzip it, and you can see it
creates a sub-folder. Now if you're on a
Windows machine or a Linux machine, you'll
most likely have to right-click that
file and select Expand all or Uncompress
or words to that effect, and expand that
directory out. And make a note of where
that directory is, because we'll need
that when we go back to Android Studio.
Alright, so let's go back to Android
studio now. Alright, so now that we've
downloaded and unzipped the Kotlin
tutorial files, next step is to open it
in Android Studio. So click on Open an
existing Android Studio project. You want
to navigate to the folder where the file
has been unzipped. In my case, it's Kotlin
tutorial so I'm going to double click on
that to open it, and click on Open. Now
Android Studio will load the project and
it may take quite a while the first time,
while it builds the project. On Windows
in particular, be prepared to wait about
a minute before the project fully loads.
And while it's loading, you may get an
error like this, or a warning like this,
basically saying that the path, and
whatever the path is that we've
actually used to create the zip file,
doesn't belong to a directory. Just click
OK to that, and Android Studio's then
going to use the setup or configuration
that you've done in your version of
Android Studio.
Alright, so we've now got the project open
in Android Studio. Now you may actually
be asked if you want to upgrade the
version of the Kotlin plugin or gradle,
used by this project. So if you do get
asked that, don't do it. Now we normally
suggest that you accept all the updates
Android Studio offers, but don't update
anything in this particular project. The
reason is that we spent a lot of time
hacking the gradle files around to make
this work. Updating Google or the Kotlin
plugin will break those changes. Now if
that happens, the program won't compile.
You'll need to delete the project
directory and extract it from the zip
file again. Alright, so we've now
got the project open in Android Studio.
Now the main part of the window is the
edit window, which is currently empty -
that's this part here. And it shows some
useful shortcuts but there's no
file displayed, because we're not editing
anything yet. And over to the left here is
the project panel, or project pane, and this
shows the files making up our project
and at the moment, we're only interested
in what Main.kt is over here. So
just before we go into it, I'm going
to explain briefly what are these other
things. So the Gradle Scripts folder - that
heading there contains various
configuration options for the gradle
build system, so I can open that up and
have a look there. Now gradle's
responsible for things like compiling
our Kotlin code, and producing the
executable that we can run. It's gradle
that we had to change to get Android
Studio to cope with plain Kotlin, rather
than the Android project structure that it
was designed for. Now we're going to be
looking at a couple of the files in
there later, but for now you can safely
ignore everything in this section. Now
the top of the project pane is our
project, KotlinTutorial, which you can see
highlighted. And expand that if you
haven't already, and again, it should be -
it shows a Java sub-folder here, as you
can see. Now I know this was a Kotlin
course, but historically, everything was
written in Java. So consequently,
Android Studio wants that directory to be
called Java. Now below that is the Kotlin
source code file. We're going to be
creating more of these, but at the moment,
there's only the one Kotlin file in the
project. And by the way, it's possible to
mix Java and Kotlin source code in the
same project. Now if you do that,
the Java files will have a dot Java
extension, and the Kotlin files will have
a dot kt extension, as you can see in
this example here. Now to edit a file, you
just need to double-click it. So we can
open main by double-clicking it now,
which I'm going to do - Main.kt - and that
shows some Lotlin code on the screen
there. Now this is a very simple Kotlin
program, and I'm going to be going
through what it all means, shortly. For
now though, we have to get it working. Now
let's try running the program. Now it
turns out there's four different ways to
do that and they all do the same thing.
Now we could go to the run menu, up here,
and click on and select run kt,
Run Mainkt. We can also right-click
on the editor here, and choose Run Main
kt there, from the context menu.
Now by the way, sometimes the menu
doesn't appear there, but it does when
you right-click on Main.kt in the
project pane. So you might need to click on
that - right-click - and Run
Mainkt there, to get it to show. The
fourth way to run is to use this green
triangle in the toolbar. Now what that
does can vary and the drop-down list on
its left will tell you what it's going
to run. So you can see over here, it's
telling you it's gonna be running Main
kt. So in other words, it's set to Main
kt so it's going to run our main, or
our current, Kotlin file. But watch out
for that, because we're gonna be using
different files as we work through these
videos. So keep an eye on what it says
over here in that Dropbox, to make sure
that you're running the correct file.
Alright, so we've got four different ways
to run this program but none of them
will work. As I said, we had to hack
around with the project files to get
this to work with Android Studio, but
there's one final step we couldn't get
past. So use any one of these four
methods and run the program. In my case,
I'm going to right-click for the Main.
kt, over here in the project pane, and
choose Run Mainlt from there.
Now the project will be built, as it
was there, but you'll actually get an
error, and you can see this error: Could
not find or load main class Main
kt. Now this will only happen the
first time we try this, so just follow
along and run the program and you'll get
the error like I've got here now. Once
that's happened, and you see the error, as
you can see in the screen there: Could
not find or load class Mainkt, go to
the Build menu up here, and click on
Rebuild Project. Then when you do that,
nothing really seems to happen, but now
the project will run using any of the
four methods. Now it's a tradition that
the first program you write in a new
language should print Hello World, and
that's what this program does. So let's
try running it - I'm don't go do it the same
way, try running it the same way - select
Run Mainkt. And you can see, in this
case, now that we've done that, we're
successfully getting the text to output
on the screen; Hello World. Alright, so
that's our project set up and working.
Now we're going to use this same
project throughout these tutorial videos,
and add new files as we go along. As you
start each new video, make sure you've
got this project open in Android Studio.
And again, as a recap, the reason we've
done all this, is because it's much
easier to explain Kotlin when we can
just run our programs directly, rather
than having to run them as an Android
app in an emulator. Now there are two
ways to close the project; if you just
close Android Studio using the close
icon at top left, up here, or top
right on Windows, then the project will
open again automatically, the next time
you start Android Studio. Personally, I
prefer to go to the File menu, and
choose Close Project first. Now when
you do that, Android Studio starts up on
the welcome screen, as you can see here.
But it also remembers the last projects
you've opened, so you can come over here and
click on Kotlin tutorial in the list, to
open the project again.
Alright, so we're all set up now. So I'm
going to stop the video here, and in the
next video, we'll start looking at the
Kotlin language and what we can do with
it. See you in the next video.