1 00:00:04,990 --> 00:00:06,410 In the previous videos, 2 00:00:06,410 --> 00:00:09,340 we created our first Hello World Android application 3 00:00:09,340 --> 00:00:11,220 and then I showed you how to set up 4 00:00:11,220 --> 00:00:14,310 and configure a virtual device or emulator. 5 00:00:14,310 --> 00:00:17,800 So now it's time to run the application on the AVD. 6 00:00:17,800 --> 00:00:20,260 Now you'll find I talk about virtual devices, 7 00:00:20,260 --> 00:00:23,350 emulators and AVD's throughout the course 8 00:00:23,350 --> 00:00:25,690 and use the terms interchangeably. 9 00:00:25,690 --> 00:00:28,400 Strictly speaking, emulator refers to the software 10 00:00:28,400 --> 00:00:30,950 that the virtual device is running inside, 11 00:00:30,950 --> 00:00:33,920 but we're not really interested in that level of detail. 12 00:00:33,920 --> 00:00:35,990 Using an emulator to run a virtual device 13 00:00:35,990 --> 00:00:38,400 is a way to the get the Android operating system 14 00:00:38,400 --> 00:00:41,180 running on your computer so that you can run programmes 15 00:00:41,180 --> 00:00:43,870 on it as though it were a real device. 16 00:00:43,870 --> 00:00:46,630 And it's very handy if you haven't got a physical device, 17 00:00:46,630 --> 00:00:49,500 and also let's you test on different Android versions 18 00:00:49,500 --> 00:00:51,430 just by creating a new AVD. 19 00:00:51,430 --> 00:00:54,040 So we're going to run the Hello World application 20 00:00:54,040 --> 00:00:56,740 on an Android virtual device. 21 00:00:56,740 --> 00:00:59,310 Now you should have Android Studio running as I've got 22 00:00:59,310 --> 00:01:01,760 and the Hello World application open. 23 00:01:01,760 --> 00:01:04,340 Android Studio keeps a track of the last few projects 24 00:01:04,340 --> 00:01:06,680 that you've worked on so you can just click on Hello World 25 00:01:06,680 --> 00:01:08,320 on the left hand side of the Welcome screen 26 00:01:08,320 --> 00:01:10,880 to load the project if it's not already opened. 27 00:01:10,880 --> 00:01:12,840 Now, we saw one way to start an emulator 28 00:01:12,840 --> 00:01:14,400 in the previous video. 29 00:01:14,400 --> 00:01:19,130 From the Tools menu, I chose Android and then AVD manager, 30 00:01:19,130 --> 00:01:20,910 and now from the AVD manager, 31 00:01:20,910 --> 00:01:23,320 click the green triangle next to the virtual device 32 00:01:23,320 --> 00:01:24,580 that you want to start. 33 00:01:24,580 --> 00:01:25,907 So I'm gonna click on that now. 34 00:01:25,907 --> 00:01:27,300 And if you've got a slow computer, 35 00:01:27,300 --> 00:01:29,990 it's probably a good idea to start the virtual device 36 00:01:29,990 --> 00:01:31,310 before you actually need it, 37 00:01:31,310 --> 00:01:33,550 you can leave it loading to get on with your work. 38 00:01:33,550 --> 00:01:35,960 And you can also close down the Android virtual device 39 00:01:35,960 --> 00:01:39,670 manager before the emulator has fully loaded, 40 00:01:39,670 --> 00:01:41,870 you don't need to wait for it to fully load. 41 00:01:43,340 --> 00:01:45,340 Now when the emulator has fully started, 42 00:01:46,430 --> 00:01:47,800 you're ready to use it to run your app. 43 00:01:47,800 --> 00:01:49,560 And you can see that it's started now. 44 00:01:49,560 --> 00:01:51,840 So I'm gonna switch back to Android Studio, 45 00:01:51,840 --> 00:01:54,110 now to run our app, there's several ways. 46 00:01:54,110 --> 00:01:56,470 The obvious way to run is to go to the Run menu 47 00:01:56,470 --> 00:01:57,880 and select Run app there. 48 00:01:57,880 --> 00:01:58,720 Before I click that though, 49 00:01:58,720 --> 00:02:00,880 notice the green arrow up here, 50 00:02:00,880 --> 00:02:03,770 and there's also another green arrow up on the toolbar, 51 00:02:03,770 --> 00:02:04,830 so we can choose that as well 52 00:02:04,830 --> 00:02:07,860 and get that to run that way instead. 53 00:02:07,860 --> 00:02:10,910 So I'm gonna click on this one, either one would work. 54 00:02:10,910 --> 00:02:13,180 So the next thing it needs to know is 55 00:02:13,180 --> 00:02:15,080 what do we want to run this on? 56 00:02:15,080 --> 00:02:17,430 Now because I've already started an emulator, 57 00:02:17,430 --> 00:02:20,260 it appears at the top here under Connected Devices. 58 00:02:20,260 --> 00:02:21,590 Any emulators that are running, 59 00:02:21,590 --> 00:02:23,190 as well as any physical devices that are 60 00:02:23,190 --> 00:02:25,970 plugged into the computer should appear here 61 00:02:25,970 --> 00:02:29,128 and you can just choose the one you want to use. 62 00:02:29,128 --> 00:02:30,570 Now, this also integrates quite nicely 63 00:02:30,570 --> 00:02:32,610 with the AVD managers, so that system images 64 00:02:32,610 --> 00:02:34,590 that you've set up from there will appear 65 00:02:34,590 --> 00:02:36,230 in the available emulator section, 66 00:02:36,230 --> 00:02:38,370 which will show up if there are extra ones. 67 00:02:38,370 --> 00:02:40,960 And you can create a new emulator here if you like, 68 00:02:40,960 --> 00:02:41,960 using the button at the bottom, 69 00:02:41,960 --> 00:02:44,120 and that just launches the AVD manager. 70 00:02:44,120 --> 00:02:46,720 So it's probably not often that you want to use this button, 71 00:02:46,720 --> 00:02:48,450 more useful is that you can choose an emulator 72 00:02:48,450 --> 00:02:50,310 that isn't running and start it from here, 73 00:02:50,310 --> 00:02:51,950 and I'm gonna be doing that next. 74 00:02:51,950 --> 00:02:55,410 For now though, we've already got an emulator started 75 00:02:55,410 --> 00:02:57,350 so I'm just going to select that one or leave them. 76 00:02:57,350 --> 00:03:00,223 So I currently selected at the top and click on OK. 77 00:03:01,162 --> 00:03:02,307 And I'm going to Tab over to my emulator 78 00:03:02,307 --> 00:03:03,440 and you can see down the bottom, 79 00:03:03,440 --> 00:03:05,020 we've got some things happening there, 80 00:03:05,020 --> 00:03:08,020 Gradle Build Running, eventually we should see the app 81 00:03:08,020 --> 00:03:09,503 start in the emulator. 82 00:03:10,380 --> 00:03:12,240 Now in previous versions of Android Studio, 83 00:03:12,240 --> 00:03:15,620 before version 2.2, you used to open up the run pane 84 00:03:15,620 --> 00:03:18,610 automatically then switched to the Android monitor pane 85 00:03:18,610 --> 00:03:20,390 once the app started running. 86 00:03:20,390 --> 00:03:21,633 But the version of Android Studio 87 00:03:21,633 --> 00:03:24,420 that I'm using to record this video doesn't do that, 88 00:03:24,420 --> 00:03:26,970 but they may have put that feature back in again 89 00:03:26,970 --> 00:03:28,160 in the future. 90 00:03:28,160 --> 00:03:32,270 The Android monitor pane has also been renamed to Logcat, 91 00:03:32,270 --> 00:03:33,600 I suppose that's not surprising 92 00:03:33,600 --> 00:03:36,283 because everyone referred to it as the Logcat anyway. 93 00:03:37,250 --> 00:03:39,850 Now, you can see that the app has now started. 94 00:03:39,850 --> 00:03:41,490 And it is worth having a look at these panes 95 00:03:41,490 --> 00:03:42,330 when you run an app. 96 00:03:42,330 --> 00:03:45,257 So I'm gonna open up the Run pane, over here, click on that. 97 00:03:45,257 --> 00:03:48,310 And you can see the commands that it used to deploy the app 98 00:03:48,310 --> 00:03:51,030 to the phone and start it running. 99 00:03:51,030 --> 00:03:52,720 And if you've played around with ADB 100 00:03:52,720 --> 00:03:55,010 to root your device and instal apps on it, 101 00:03:55,010 --> 00:03:56,990 then this may all look very familiar, 102 00:03:56,990 --> 00:03:59,480 but if it doesn't make much sense then don't worry about it, 103 00:03:59,480 --> 00:04:01,050 it's not something you need to know how to do 104 00:04:01,050 --> 00:04:04,750 because Android Studio runs the ADB commands for you. 105 00:04:04,750 --> 00:04:08,160 Now ADB, by the way, stands for Android Debug Bridge 106 00:04:08,160 --> 00:04:10,210 and that's the method used to communicate 107 00:04:10,210 --> 00:04:12,310 with an Android device when developing, 108 00:04:12,310 --> 00:04:13,900 and we're gonna be looking at these commands 109 00:04:13,900 --> 00:04:16,810 in a later section and we'll set up ADB 110 00:04:16,810 --> 00:04:18,060 and have a look at some of the things 111 00:04:18,060 --> 00:04:19,250 that you can do with it. 112 00:04:19,250 --> 00:04:21,130 The only reason you'd be interested in this pane 113 00:04:21,130 --> 00:04:23,770 at the moment though is if something goes wrong. 114 00:04:23,770 --> 00:04:25,910 You can then either google the error message 115 00:04:25,910 --> 00:04:28,160 or ask a question in the Q&A section of this course, 116 00:04:28,160 --> 00:04:30,160 and then paste the contents of the screen 117 00:04:30,160 --> 00:04:31,654 into your question. 118 00:04:31,654 --> 00:04:34,180 Now the Logcat screen is a little bit more interesting, 119 00:04:34,180 --> 00:04:35,920 so I'm going to click on that, 120 00:04:35,920 --> 00:04:38,690 but it's also scary because a lot seems to be happening. 121 00:04:38,690 --> 00:04:40,590 Now I'm not gonna talk about it right at this moment 122 00:04:40,590 --> 00:04:42,590 because we've missed most of the excitement, 123 00:04:42,590 --> 00:04:44,600 so I'm gonna come back to the Logcat shortly. 124 00:04:44,600 --> 00:04:47,450 So firstly, if we go back to our emulator, 125 00:04:47,450 --> 00:04:50,440 Tab over, we can see the Hello World programme app 126 00:04:50,440 --> 00:04:51,857 running on this emulator, 127 00:04:51,857 --> 00:04:53,980 and it looks just like a physical Android device 128 00:04:53,980 --> 00:04:55,410 with the home button, etc. 129 00:04:55,410 --> 00:04:58,280 at the bottom and our app running in the window. 130 00:04:58,280 --> 00:05:00,020 Our simple app doesn't do a great deal 131 00:05:00,020 --> 00:05:03,100 but that's just how you can test you app using an emulator. 132 00:05:03,100 --> 00:05:05,710 So I can close the app using the back button 133 00:05:05,710 --> 00:05:07,170 and just like on a physical device 134 00:05:07,170 --> 00:05:09,140 when you instal apps on the Play Store, 135 00:05:09,140 --> 00:05:10,710 there's an icon to launch Hello World 136 00:05:10,710 --> 00:05:12,240 from the device's Apps menu. 137 00:05:12,240 --> 00:05:14,113 So if I click on that, 138 00:05:14,113 --> 00:05:15,440 you can see we've got our Hello Word app there, 139 00:05:15,440 --> 00:05:17,736 I can click on that to start it. 140 00:05:17,736 --> 00:05:19,530 Now you may have noticed some activity down here 141 00:05:19,530 --> 00:05:21,970 in this Logcat pane while all that was going on. 142 00:05:21,970 --> 00:05:23,900 So let's actually have a look at that 143 00:05:23,900 --> 00:05:25,630 and so that we can see that all from the start, 144 00:05:25,630 --> 00:05:27,420 I'm gonna shut down this emulator 145 00:05:27,420 --> 00:05:29,450 and we'll start it back up using the other method 146 00:05:29,450 --> 00:05:30,810 I mentioned. 147 00:05:30,810 --> 00:05:32,600 You can stop the emulator by either 148 00:05:32,600 --> 00:05:33,980 holding down this power button here, 149 00:05:33,980 --> 00:05:36,500 holding down the button and powering off, 150 00:05:36,500 --> 00:05:37,420 but you can also close it 151 00:05:37,420 --> 00:05:39,540 by clicking on this little X button down here. 152 00:05:39,540 --> 00:05:42,740 Either way will actually close down the emulator. 153 00:05:42,740 --> 00:05:45,230 Alright, so now instead of starting an emulator first, 154 00:05:45,230 --> 00:05:47,540 I'm just going to run the programme. 155 00:05:47,540 --> 00:05:50,090 Now if you've got a fast computer with loads of RAM, 156 00:05:50,090 --> 00:05:52,670 there's no need to start an emulator first. 157 00:05:52,670 --> 00:05:54,540 In the button bar at the top of the screen, 158 00:05:54,540 --> 00:05:56,430 there's a green arrow that you can use to run your app 159 00:05:56,430 --> 00:05:59,810 like I clicked earlier, and do that again. 160 00:05:59,810 --> 00:06:00,643 And when I've clicked that, 161 00:06:00,643 --> 00:06:03,510 it asks for an emulator or device to run on 162 00:06:03,510 --> 00:06:05,730 and that's the same screen we saw earlier 163 00:06:05,730 --> 00:06:07,330 but now there's no running devices, 164 00:06:07,330 --> 00:06:09,110 connected devices is none, 165 00:06:09,110 --> 00:06:10,740 we've got available virtual devices 166 00:06:10,740 --> 00:06:15,290 showing the Nexus 5X API 26 virtual device. 167 00:06:15,290 --> 00:06:17,680 So I can choose one from there and click on OK. 168 00:06:17,680 --> 00:06:19,470 And we should find the emulator starts up for us 169 00:06:19,470 --> 00:06:21,760 automatically and eventually once that's loaded, 170 00:06:21,760 --> 00:06:23,890 the application will automatically start. 171 00:06:23,890 --> 00:06:26,860 And I've got the Logcat showing on the screen here 172 00:06:26,860 --> 00:06:28,940 as you can see, so we should be able to see things 173 00:06:28,940 --> 00:06:32,170 happening here as that does start. 174 00:06:32,170 --> 00:06:34,340 Now I say should be able to, 175 00:06:34,340 --> 00:06:36,500 the Logcat often hangs on the emulator 176 00:06:36,500 --> 00:06:38,950 that you've just closed so it might be showing 177 00:06:38,950 --> 00:06:41,950 as disconnected or just generally not updating. 178 00:06:41,950 --> 00:06:43,510 It doesn't seem to be doing much in the way of updating 179 00:06:43,510 --> 00:06:44,747 here now, as you can see. 180 00:06:44,747 --> 00:06:46,140 And this is the disconnected here 181 00:06:46,140 --> 00:06:48,257 that I'm referring to in the dropdown window. 182 00:06:48,257 --> 00:06:51,570 I'm gonna change that to select a new emulator, 183 00:06:51,570 --> 00:06:53,610 click on that, select the new emulator 184 00:06:53,610 --> 00:06:54,967 and then we'll be good to go. 185 00:06:54,967 --> 00:06:57,050 And as we get new message and as we do things 186 00:06:57,050 --> 00:06:59,030 on the computer, that will then be updated 187 00:06:59,030 --> 00:07:00,500 and show in the Logcat. 188 00:07:01,378 --> 00:07:02,550 And you can see that as I'm doing things 189 00:07:02,550 --> 00:07:04,160 we're getting entries in there. 190 00:07:04,160 --> 00:07:05,670 Start Hello World again, 191 00:07:05,670 --> 00:07:07,817 there's generally a lot going on in the Logcat, 192 00:07:07,817 --> 00:07:10,300 and what we might do is just to see that in full glory, 193 00:07:10,300 --> 00:07:12,610 we'll close this down, power it off, 194 00:07:12,610 --> 00:07:15,430 and then we'll start it again and you'll be able to see 195 00:07:15,430 --> 00:07:17,590 the emulator and all the various messages. 196 00:07:17,590 --> 00:07:19,280 Alright so at the moment you can see it says "disconnected", 197 00:07:19,280 --> 00:07:21,280 so I'm gonna start that again 198 00:07:21,280 --> 00:07:22,850 and I'm gonna come down here straight away, 199 00:07:22,850 --> 00:07:27,260 give it a second to start, and select no filters over here, 200 00:07:27,260 --> 00:07:29,270 and you can see we've got absolutely everything. 201 00:07:29,270 --> 00:07:31,170 Quite a bit of information as you can see there. 202 00:07:31,170 --> 00:07:32,700 And this was what I was talking about before, 203 00:07:32,700 --> 00:07:35,260 you can see some of the messages that have got 204 00:07:35,260 --> 00:07:38,170 an I and a slash at the beginning, information messages, 205 00:07:38,170 --> 00:07:42,110 and you can see others, we've got a W/ for warning 206 00:07:42,110 --> 00:07:45,663 and E/ for error, and it's also color-coded as well. 207 00:07:46,605 --> 00:07:49,210 Now the Logcat clears itself as our app launches each time, 208 00:07:49,210 --> 00:07:52,380 so if I go back and actually select our app, 209 00:07:52,380 --> 00:07:54,980 show only selected application, there's currently nothing, 210 00:07:54,980 --> 00:07:56,970 so there will be little to nothing showing, 211 00:07:56,970 --> 00:07:57,870 and this is because this happens to be 212 00:07:57,870 --> 00:08:00,470 a bit too simple to log much of interest. 213 00:08:00,470 --> 00:08:01,980 And in fact, as you can see in my case, 214 00:08:01,980 --> 00:08:04,340 it's absolutely blank at the moment. 215 00:08:04,340 --> 00:08:06,370 Now, we're gonna be looking at Logcat 216 00:08:06,370 --> 00:08:08,470 in more detail a bit later 217 00:08:08,470 --> 00:08:10,980 when we've actually got some more log entries to see. 218 00:08:10,980 --> 00:08:13,530 For now though, I just want you to be aware that it's there, 219 00:08:13,530 --> 00:08:15,950 if you get problems with your app, if they crash, 220 00:08:15,950 --> 00:08:18,410 you'll find a crash report in the Logcat 221 00:08:18,410 --> 00:08:20,760 and you can copy that in the same way as you copy text 222 00:08:20,760 --> 00:08:22,750 in the edit window and paste them into the Q&A section 223 00:08:22,750 --> 00:08:25,920 of this course along with your question. 224 00:08:25,920 --> 00:08:27,690 Now it may not mean much to you at first 225 00:08:27,690 --> 00:08:29,730 but it will be very useful to us 226 00:08:29,730 --> 00:08:31,890 when working out why your app crashes. 227 00:08:31,890 --> 00:08:33,909 So the emulator overall does a pretty good job 228 00:08:33,909 --> 00:08:36,330 of showing what your application will look like 229 00:08:36,330 --> 00:08:38,480 and you'll be seeing that in operation a lot more 230 00:08:38,480 --> 00:08:40,400 as we go though the course, and that's because 231 00:08:40,400 --> 00:08:42,890 it's one of the core things that I recommend you do 232 00:08:42,890 --> 00:08:45,550 to be testing your applications as much as possible 233 00:08:45,550 --> 00:08:48,110 and seeing what they look like on the screen. 234 00:08:48,110 --> 00:08:50,350 Now I certainly recommend that you test the final version 235 00:08:50,350 --> 00:08:53,080 of an application on a physical device 236 00:08:53,080 --> 00:08:55,050 before you upload it to the Play Store, 237 00:08:55,050 --> 00:08:58,120 but for testing purposes, the emulators are great. 238 00:08:58,120 --> 00:09:00,590 They also allow you to test on the different versions 239 00:09:00,590 --> 00:09:02,420 of Android that your app supports 240 00:09:02,420 --> 00:09:03,400 without you having to go out 241 00:09:03,400 --> 00:09:06,530 and buying loads of Android phones and tablets. 242 00:09:06,530 --> 00:09:08,800 So it's a great way to get a feel for what your application 243 00:09:08,800 --> 00:09:12,200 is going to look like before it goes into production 244 00:09:12,200 --> 00:09:14,400 and before you test it on a physical device. 245 00:09:14,400 --> 00:09:16,320 So that's testing on an emulator, 246 00:09:16,320 --> 00:09:18,900 if you can run them, they are a great help. 247 00:09:18,900 --> 00:09:20,590 If you do have problems running emulators 248 00:09:20,590 --> 00:09:22,630 because your machine doesn't have enough memory, 249 00:09:22,630 --> 00:09:24,060 then you can run all the apps in this course 250 00:09:24,060 --> 00:09:25,810 on a real Android device though. 251 00:09:25,810 --> 00:09:29,270 So don't be too put off if you can't run an emulator, 252 00:09:29,270 --> 00:09:31,560 Android devices are also fairly cheap these days 253 00:09:31,560 --> 00:09:33,920 so it can be a very inexpensive way 254 00:09:33,920 --> 00:09:35,490 to test on a real Android device 255 00:09:35,490 --> 00:09:37,340 if you have problems using emulators. 256 00:09:37,340 --> 00:09:39,060 Alright, so I'm gonna end the video now. 257 00:09:39,060 --> 00:09:40,670 In the next video, we're going to fire up 258 00:09:40,670 --> 00:09:43,590 one of my Android devices, my Nexus 5X, 259 00:09:43,590 --> 00:09:45,610 and run the app on the physical machine 260 00:09:45,610 --> 00:09:47,110 just to show you what it looks like on that 261 00:09:47,110 --> 00:09:49,140 and how to use it on a real device. 262 00:09:49,140 --> 00:09:50,640 So I'll see in the next video.