1 00:00:04,689 --> 00:00:09,610 Google have made Kotlin a top-level 2 00:00:07,390 --> 00:00:11,110 language for Android development, which 3 00:00:09,610 --> 00:00:13,090 means that it's fully supported in 4 00:00:11,110 --> 00:00:15,519 Android Studio. And they'll also be 5 00:00:13,090 --> 00:00:17,920 publishing example code using Kotlin, as 6 00:00:15,519 --> 00:00:20,080 well as Java. At the moment though, most 7 00:00:17,920 --> 00:00:22,689 of their code samples are still in Java. 8 00:00:20,080 --> 00:00:24,579 Now, it turns out that's not a huge problem, 9 00:00:22,689 --> 00:00:27,009 because we're gonna look, in this section, 10 00:00:24,579 --> 00:00:29,950 at how Android Studio can convert Java 11 00:00:27,009 --> 00:00:31,930 code to Kotlin for us. So in this section, 12 00:00:29,950 --> 00:00:33,910 we're going to look at the Kotlin 13 00:00:31,930 --> 00:00:36,040 programming language, just to give you an 14 00:00:33,910 --> 00:00:38,110 overview and introduction to it. So let's 15 00:00:36,040 --> 00:00:40,270 get started. Now Android Studio's 16 00:00:38,110 --> 00:00:42,550 designed for Android development, which 17 00:00:40,270 --> 00:00:44,590 means that the projects it creates are 18 00:00:42,550 --> 00:00:46,810 intended to be run on an Android device, 19 00:00:44,590 --> 00:00:48,520 and not on your computer. Now it is 20 00:00:46,810 --> 00:00:51,850 possible, though, to get Android Studio to 21 00:00:48,520 --> 00:00:53,920 compile and run pure Kotlin code. It is 22 00:00:51,850 --> 00:00:56,800 very fiddly, so we've created a project 23 00:00:53,920 --> 00:00:59,079 for you. Now this that Android Studio 24 00:00:56,800 --> 00:01:01,630 project's been specifically configured, to 25 00:00:59,079 --> 00:01:03,970 run pure Kotlin code that'll run on 26 00:01:01,630 --> 00:01:05,319 your computer. Now before I show you how 27 00:01:03,970 --> 00:01:07,360 to get that working, there's another 28 00:01:05,319 --> 00:01:10,179 alternative. Now if you're already using 29 00:01:07,360 --> 00:01:12,490 IntelliJ as your IDE, then I recommend 30 00:01:10,179 --> 00:01:15,369 using that for these tutorials instead. 31 00:01:12,490 --> 00:01:18,130 Now Android Studio is based on IntelliJ 32 00:01:15,369 --> 00:01:21,069 IDEA, so everything will work, just as is, 33 00:01:18,130 --> 00:01:22,990 in the videos. Now you may also be taking 34 00:01:21,069 --> 00:01:25,119 Sarah Etrich's Kotlin for Java 35 00:01:22,990 --> 00:01:27,700 Developers' course, in which case you'll 36 00:01:25,119 --> 00:01:29,859 see how to install IntelliJ IDEA in that 37 00:01:27,700 --> 00:01:31,240 course. For everybody else, though, I'm going 38 00:01:29,859 --> 00:01:34,899 to go through the steps to get Android 39 00:01:31,240 --> 00:01:37,209 Studio to run pure Kotlin code. Now the 40 00:01:34,899 --> 00:01:39,579 project we've created is in a zip file 41 00:01:37,209 --> 00:01:41,499 in the resources section of this video. 42 00:01:39,579 --> 00:01:43,869 So let's just move over now, and I'm 43 00:01:41,499 --> 00:01:45,850 going to show you how to get that zip 44 00:01:43,869 --> 00:01:48,150 file how to unzip it, and get it ready 45 00:01:45,850 --> 00:01:50,520 for use on your computer. 46 00:01:48,150 --> 00:01:53,130 Alright, so the project we've created 47 00:01:50,520 --> 00:01:55,200 is in a zip file in the resources 48 00:01:53,130 --> 00:01:56,670 section of this video, and this is the 49 00:01:55,200 --> 00:01:59,070 file that we're going to be using for 50 00:01:56,670 --> 00:02:00,420 our Kotlin tutorial. And if you move the 51 00:01:59,070 --> 00:02:02,070 mouse over the video, 52 00:02:00,420 --> 00:02:04,170 a pop up balloon will appear at the top 53 00:02:02,070 --> 00:02:06,420 left, and just above that you'll see a 54 00:02:04,170 --> 00:02:08,190 menu icon. And if you click on that and 55 00:02:06,420 --> 00:02:10,889 scroll down to this video in the list, 56 00:02:08,190 --> 00:02:12,420 you'll actually see an area where you 57 00:02:10,889 --> 00:02:14,190 can download the file - and the file that 58 00:02:12,420 --> 00:02:15,990 you want is called KotlinTutorial.zip. 59 00:02:14,190 --> 00:02:17,340 So you can check that out and 60 00:02:15,990 --> 00:02:19,620 download that file because we'll need 61 00:02:17,340 --> 00:02:21,540 that file to actually work on these 62 00:02:19,620 --> 00:02:23,220 tutorials - the Kotlin tutorial. And if 63 00:02:21,540 --> 00:02:25,530 you've got any problems finding the 64 00:02:23,220 --> 00:02:26,820 Kotlin tutorial file, just look in this 65 00:02:25,530 --> 00:02:28,680 lecture - the lecture that you're 66 00:02:26,820 --> 00:02:29,940 looking at now. Basically, just go into 67 00:02:28,680 --> 00:02:32,640 the top left of the screen and you 68 00:02:29,940 --> 00:02:34,410 should see a resources section, but also, 69 00:02:32,640 --> 00:02:35,670 the last lecture of the course has got a 70 00:02:34,410 --> 00:02:37,470 list of all the downloads, so you can 71 00:02:35,670 --> 00:02:39,090 grab it from there as well. And what you 72 00:02:37,470 --> 00:02:41,430 need to do is download that file - 73 00:02:39,090 --> 00:02:43,530 KotlinTutorial.zip - and put it 74 00:02:41,430 --> 00:02:45,090 somewhere on your computer. And you can 75 00:02:43,530 --> 00:02:47,370 see, in my case, I'm on a Mac and I've 76 00:02:45,090 --> 00:02:49,770 downloaded into a folder. Once you've 77 00:02:47,370 --> 00:02:51,870 done that you need to unzip it. Now in my 78 00:02:49,770 --> 00:02:54,209 case, on a Mac, I can just double-click 79 00:02:51,870 --> 00:02:56,690 the file to unzip it, and you can see it 80 00:02:54,209 --> 00:02:58,860 creates a sub-folder. Now if you're on a 81 00:02:56,690 --> 00:03:00,180 Windows machine or a Linux machine, you'll 82 00:02:58,860 --> 00:03:04,170 most likely have to right-click that 83 00:03:00,180 --> 00:03:06,720 file and select Expand all or Uncompress 84 00:03:04,170 --> 00:03:09,030 or words to that effect, and expand that 85 00:03:06,720 --> 00:03:10,590 directory out. And make a note of where 86 00:03:09,030 --> 00:03:12,350 that directory is, because we'll need 87 00:03:10,590 --> 00:03:14,280 that when we go back to Android Studio. 88 00:03:12,350 --> 00:03:17,820 Alright, so let's go back to Android 89 00:03:14,280 --> 00:03:20,970 studio now. Alright, so now that we've 90 00:03:17,820 --> 00:03:23,430 downloaded and unzipped the Kotlin 91 00:03:20,970 --> 00:03:25,860 tutorial files, next step is to open it 92 00:03:23,430 --> 00:03:32,850 in Android Studio. So click on Open an 93 00:03:25,860 --> 00:03:34,709 existing Android Studio project. You want 94 00:03:32,850 --> 00:03:37,320 to navigate to the folder where the file 95 00:03:34,709 --> 00:03:38,670 has been unzipped. In my case, it's Kotlin 96 00:03:37,320 --> 00:03:42,299 tutorial so I'm going to double click on 97 00:03:38,670 --> 00:03:44,400 that to open it, and click on Open. Now 98 00:03:42,299 --> 00:03:46,049 Android Studio will load the project and 99 00:03:44,400 --> 00:03:48,600 it may take quite a while the first time, 100 00:03:46,049 --> 00:03:50,280 while it builds the project. On Windows 101 00:03:48,600 --> 00:03:52,670 in particular, be prepared to wait about 102 00:03:50,280 --> 00:03:55,170 a minute before the project fully loads. 103 00:03:52,670 --> 00:03:56,850 And while it's loading, you may get an 104 00:03:55,170 --> 00:03:59,310 error like this, or a warning like this, 105 00:03:56,850 --> 00:04:02,370 basically saying that the path, and 106 00:03:59,310 --> 00:04:04,200 whatever the path is that we've 107 00:04:02,370 --> 00:04:06,390 actually used to create the zip file, 108 00:04:04,200 --> 00:04:08,909 doesn't belong to a directory. Just click 109 00:04:06,390 --> 00:04:10,799 OK to that, and Android Studio's then 110 00:04:08,909 --> 00:04:13,590 going to use the setup or configuration 111 00:04:10,799 --> 00:04:16,129 that you've done in your version of 112 00:04:13,590 --> 00:04:16,129 Android Studio. 113 00:04:23,139 --> 00:04:28,400 Alright, so we've now got the project open 114 00:04:25,880 --> 00:04:29,870 in Android Studio. Now you may actually 115 00:04:28,400 --> 00:04:32,150 be asked if you want to upgrade the 116 00:04:29,870 --> 00:04:34,880 version of the Kotlin plugin or gradle, 117 00:04:32,150 --> 00:04:37,130 used by this project. So if you do get 118 00:04:34,880 --> 00:04:38,870 asked that, don't do it. Now we normally 119 00:04:37,130 --> 00:04:41,570 suggest that you accept all the updates 120 00:04:38,870 --> 00:04:43,880 Android Studio offers, but don't update 121 00:04:41,570 --> 00:04:45,290 anything in this particular project. The 122 00:04:43,880 --> 00:04:47,690 reason is that we spent a lot of time 123 00:04:45,290 --> 00:04:50,510 hacking the gradle files around to make 124 00:04:47,690 --> 00:04:52,850 this work. Updating Google or the Kotlin 125 00:04:50,510 --> 00:04:54,380 plugin will break those changes. Now if 126 00:04:52,850 --> 00:04:55,820 that happens, the program won't compile. 127 00:04:54,380 --> 00:04:57,680 You'll need to delete the project 128 00:04:55,820 --> 00:05:00,290 directory and extract it from the zip 129 00:04:57,680 --> 00:05:02,350 file again. Alright, so we've now 130 00:05:00,290 --> 00:05:04,550 got the project open in Android Studio. 131 00:05:02,350 --> 00:05:06,500 Now the main part of the window is the 132 00:05:04,550 --> 00:05:09,229 edit window, which is currently empty - 133 00:05:06,500 --> 00:05:11,120 that's this part here. And it shows some 134 00:05:09,229 --> 00:05:13,220 useful shortcuts but there's no 135 00:05:11,120 --> 00:05:15,260 file displayed, because we're not editing 136 00:05:13,220 --> 00:05:18,830 anything yet. And over to the left here is 137 00:05:15,260 --> 00:05:20,720 the project panel, or project pane, and this 138 00:05:18,830 --> 00:05:22,160 shows the files making up our project 139 00:05:20,720 --> 00:05:25,520 and at the moment, we're only interested 140 00:05:22,160 --> 00:05:26,660 in what Main.kt is over here. So 141 00:05:25,520 --> 00:05:28,010 just before we go into it, I'm going 142 00:05:26,660 --> 00:05:31,010 to explain briefly what are these other 143 00:05:28,010 --> 00:05:32,330 things. So the Gradle Scripts folder - that 144 00:05:31,010 --> 00:05:33,860 heading there contains various 145 00:05:32,330 --> 00:05:35,270 configuration options for the gradle 146 00:05:33,860 --> 00:05:37,190 build system, so I can open that up and 147 00:05:35,270 --> 00:05:39,080 have a look there. Now gradle's 148 00:05:37,190 --> 00:05:41,570 responsible for things like compiling 149 00:05:39,080 --> 00:05:44,360 our Kotlin code, and producing the 150 00:05:41,570 --> 00:05:45,860 executable that we can run. It's gradle 151 00:05:44,360 --> 00:05:48,560 that we had to change to get Android 152 00:05:45,860 --> 00:05:50,479 Studio to cope with plain Kotlin, rather 153 00:05:48,560 --> 00:05:52,490 than the Android project structure that it 154 00:05:50,479 --> 00:05:53,690 was designed for. Now we're going to be 155 00:05:52,490 --> 00:05:55,700 looking at a couple of the files in 156 00:05:53,690 --> 00:05:57,889 there later, but for now you can safely 157 00:05:55,700 --> 00:05:59,389 ignore everything in this section. Now 158 00:05:57,889 --> 00:06:01,669 the top of the project pane is our 159 00:05:59,389 --> 00:06:03,710 project, KotlinTutorial, which you can see 160 00:06:01,669 --> 00:06:05,389 highlighted. And expand that if you 161 00:06:03,710 --> 00:06:07,789 haven't already, and again, it should be - 162 00:06:05,389 --> 00:06:09,740 it shows a Java sub-folder here, as you 163 00:06:07,789 --> 00:06:11,960 can see. Now I know this was a Kotlin 164 00:06:09,740 --> 00:06:14,090 course, but historically, everything was 165 00:06:11,960 --> 00:06:16,220 written in Java. So consequently, 166 00:06:14,090 --> 00:06:18,919 Android Studio wants that directory to be 167 00:06:16,220 --> 00:06:20,030 called Java. Now below that is the Kotlin 168 00:06:18,919 --> 00:06:21,770 source code file. We're going to be 169 00:06:20,030 --> 00:06:24,050 creating more of these, but at the moment, 170 00:06:21,770 --> 00:06:26,390 there's only the one Kotlin file in the 171 00:06:24,050 --> 00:06:29,060 project. And by the way, it's possible to 172 00:06:26,390 --> 00:06:31,610 mix Java and Kotlin source code in the 173 00:06:29,060 --> 00:06:33,319 same project. Now if you do that, 174 00:06:31,610 --> 00:06:35,719 the Java files will have a dot Java 175 00:06:33,319 --> 00:06:38,240 extension, and the Kotlin files will have 176 00:06:35,719 --> 00:06:40,789 a dot kt extension, as you can see in 177 00:06:38,240 --> 00:06:42,590 this example here. Now to edit a file, you 178 00:06:40,789 --> 00:06:44,330 just need to double-click it. So we can 179 00:06:42,590 --> 00:06:47,449 open main by double-clicking it now, 180 00:06:44,330 --> 00:06:49,219 which I'm going to do - Main.kt - and that 181 00:06:47,449 --> 00:06:52,189 shows some Lotlin code on the screen 182 00:06:49,219 --> 00:06:53,719 there. Now this is a very simple Kotlin 183 00:06:52,189 --> 00:06:55,879 program, and I'm going to be going 184 00:06:53,719 --> 00:06:58,250 through what it all means, shortly. For 185 00:06:55,879 --> 00:07:00,710 now though, we have to get it working. Now 186 00:06:58,250 --> 00:07:02,449 let's try running the program. Now it 187 00:07:00,710 --> 00:07:04,460 turns out there's four different ways to 188 00:07:02,449 --> 00:07:07,930 do that and they all do the same thing. 189 00:07:04,460 --> 00:07:10,909 Now we could go to the run menu, up here, 190 00:07:07,930 --> 00:07:13,789 and click on and select run kt, 191 00:07:10,909 --> 00:07:16,939 Run Mainkt. We can also right-click 192 00:07:13,789 --> 00:07:19,219 on the editor here, and choose Run Main 193 00:07:16,939 --> 00:07:21,889 kt there, from the context menu. 194 00:07:19,219 --> 00:07:24,289 Now by the way, sometimes the menu 195 00:07:21,889 --> 00:07:26,360 doesn't appear there, but it does when 196 00:07:24,289 --> 00:07:27,860 you right-click on Main.kt in the 197 00:07:26,360 --> 00:07:30,139 project pane. So you might need to click on 198 00:07:27,860 --> 00:07:32,960 that - right-click - and Run 199 00:07:30,139 --> 00:07:35,029 Mainkt there, to get it to show. The 200 00:07:32,960 --> 00:07:38,089 fourth way to run is to use this green 201 00:07:35,029 --> 00:07:40,219 triangle in the toolbar. Now what that 202 00:07:38,089 --> 00:07:41,539 does can vary and the drop-down list on 203 00:07:40,219 --> 00:07:42,830 its left will tell you what it's going 204 00:07:41,539 --> 00:07:44,719 to run. So you can see over here, it's 205 00:07:42,830 --> 00:07:47,960 telling you it's gonna be running Main 206 00:07:44,719 --> 00:07:49,759 kt. So in other words, it's set to Main 207 00:07:47,960 --> 00:07:52,610 kt so it's going to run our main, or 208 00:07:49,759 --> 00:07:53,870 our current, Kotlin file. But watch out 209 00:07:52,610 --> 00:07:55,490 for that, because we're gonna be using 210 00:07:53,870 --> 00:07:57,589 different files as we work through these 211 00:07:55,490 --> 00:07:59,870 videos. So keep an eye on what it says 212 00:07:57,589 --> 00:08:01,360 over here in that Dropbox, to make sure 213 00:07:59,870 --> 00:08:03,409 that you're running the correct file. 214 00:08:01,360 --> 00:08:04,969 Alright, so we've got four different ways 215 00:08:03,409 --> 00:08:07,189 to run this program but none of them 216 00:08:04,969 --> 00:08:09,349 will work. As I said, we had to hack 217 00:08:07,189 --> 00:08:11,599 around with the project files to get 218 00:08:09,349 --> 00:08:13,520 this to work with Android Studio, but 219 00:08:11,599 --> 00:08:16,310 there's one final step we couldn't get 220 00:08:13,520 --> 00:08:18,289 past. So use any one of these four 221 00:08:16,310 --> 00:08:19,969 methods and run the program. In my case, 222 00:08:18,289 --> 00:08:23,000 I'm going to right-click for the Main. 223 00:08:19,969 --> 00:08:26,439 kt, over here in the project pane, and 224 00:08:23,000 --> 00:08:26,439 choose Run Mainlt from there. 225 00:08:26,700 --> 00:08:29,910 Now the project will be built, as it 226 00:08:28,710 --> 00:08:31,560 was there, but you'll actually get an 227 00:08:29,910 --> 00:08:33,990 error, and you can see this error: Could 228 00:08:31,560 --> 00:08:36,450 not find or load main class Main 229 00:08:33,990 --> 00:08:38,280 kt. Now this will only happen the 230 00:08:36,450 --> 00:08:39,690 first time we try this, so just follow 231 00:08:38,280 --> 00:08:41,880 along and run the program and you'll get 232 00:08:39,690 --> 00:08:43,890 the error like I've got here now. Once 233 00:08:41,880 --> 00:08:45,120 that's happened, and you see the error, as 234 00:08:43,890 --> 00:08:48,480 you can see in the screen there: Could 235 00:08:45,120 --> 00:08:51,570 not find or load class Mainkt, go to 236 00:08:48,480 --> 00:08:56,250 the Build menu up here, and click on 237 00:08:51,570 --> 00:08:58,620 Rebuild Project. Then when you do that, 238 00:08:56,250 --> 00:09:01,050 nothing really seems to happen, but now 239 00:08:58,620 --> 00:09:03,600 the project will run using any of the 240 00:09:01,050 --> 00:09:05,190 four methods. Now it's a tradition that 241 00:09:03,600 --> 00:09:06,870 the first program you write in a new 242 00:09:05,190 --> 00:09:09,330 language should print Hello World, and 243 00:09:06,870 --> 00:09:10,680 that's what this program does. So let's 244 00:09:09,330 --> 00:09:12,960 try running it - I'm don't go do it the same 245 00:09:10,680 --> 00:09:16,230 way, try running it the same way - select 246 00:09:12,960 --> 00:09:17,820 Run Mainkt. And you can see, in this 247 00:09:16,230 --> 00:09:20,010 case, now that we've done that, we're 248 00:09:17,820 --> 00:09:22,560 successfully getting the text to output 249 00:09:20,010 --> 00:09:24,840 on the screen; Hello World. Alright, so 250 00:09:22,560 --> 00:09:26,670 that's our project set up and working. 251 00:09:24,840 --> 00:09:28,890 Now we're going to use this same 252 00:09:26,670 --> 00:09:31,950 project throughout these tutorial videos, 253 00:09:28,890 --> 00:09:33,390 and add new files as we go along. As you 254 00:09:31,950 --> 00:09:35,520 start each new video, make sure you've 255 00:09:33,390 --> 00:09:38,520 got this project open in Android Studio. 256 00:09:35,520 --> 00:09:40,410 And again, as a recap, the reason we've 257 00:09:38,520 --> 00:09:42,930 done all this, is because it's much 258 00:09:40,410 --> 00:09:45,300 easier to explain Kotlin when we can 259 00:09:42,930 --> 00:09:46,770 just run our programs directly, rather 260 00:09:45,300 --> 00:09:49,200 than having to run them as an Android 261 00:09:46,770 --> 00:09:51,240 app in an emulator. Now there are two 262 00:09:49,200 --> 00:09:53,700 ways to close the project; if you just 263 00:09:51,240 --> 00:09:56,760 close Android Studio using the close 264 00:09:53,700 --> 00:09:58,590 icon at top left, up here, or top 265 00:09:56,760 --> 00:10:01,020 right on Windows, then the project will 266 00:09:58,590 --> 00:10:03,180 open again automatically, the next time 267 00:10:01,020 --> 00:10:06,060 you start Android Studio. Personally, I 268 00:10:03,180 --> 00:10:09,060 prefer to go to the File menu, and 269 00:10:06,060 --> 00:10:11,610 choose Close Project first. Now when 270 00:10:09,060 --> 00:10:13,050 you do that, Android Studio starts up on 271 00:10:11,610 --> 00:10:15,480 the welcome screen, as you can see here. 272 00:10:13,050 --> 00:10:17,430 But it also remembers the last projects 273 00:10:15,480 --> 00:10:20,550 you've opened, so you can come over here and 274 00:10:17,430 --> 00:10:21,800 click on Kotlin tutorial in the list, to 275 00:10:20,550 --> 00:10:24,030 open the project again. 276 00:10:21,800 --> 00:10:26,070 Alright, so we're all set up now. So I'm 277 00:10:24,030 --> 00:10:27,780 going to stop the video here, and in the 278 00:10:26,070 --> 00:10:29,910 next video, we'll start looking at the 279 00:10:27,780 --> 00:10:33,500 Kotlin language and what we can do with 280 00:10:29,910 --> 00:10:33,500 it. See you in the next video.