1 00:00:04,720 --> 00:00:08,360 G'day everyone. Welcome back and welcome to section 13. 2 00:00:08,360 --> 00:00:13,500 I'm going to start this section by showing you how to convert the app to AndroidX. 3 00:00:13,500 --> 00:00:17,780 We've talked about the support libraries, and we've seen how useful they are, 4 00:00:17,780 --> 00:00:21,420 but they've grown huge, and a bit confusing over the years. 5 00:00:21,420 --> 00:00:29,720 Google are in the process of moving things like AppCompat into a new set of libraries, which they're calling AndroidX. 6 00:00:29,720 --> 00:00:35,940 If you started this course recently, you'll have ticked the Android X Box when creating your new projects. 7 00:00:35,940 --> 00:00:41,240 In that case, there's no conversion necessary and you can skip this video if you want. 8 00:00:41,240 --> 00:00:46,400 It's useful to see the conversion though, because you may have to convert older projects. 9 00:00:46,400 --> 00:00:55,140 If you're not using AndroidX already, you'll have noticed some of your classes are importing from v4 and v7 libraries. 10 00:00:55,140 --> 00:01:00,680 One example is MainActivity. Have a look there 11 00:01:00,680 --> 00:01:07,800 at the imports, and we've got imports of android.support.v4.app.Fragment 12 00:01:07,800 --> 00:01:13,180 and android.support.v7.app.AppCompatActivity 13 00:01:13,180 --> 00:01:20,240 That's confusing, especially as our minimum SDK is set to API 17, not 4 or 7. 14 00:01:20,240 --> 00:01:23,366 It's not just confusing, it's also a bit unwieldy. 15 00:01:23,366 --> 00:01:30,480 Google have to release new versions of very large libraries, even when they only want to update a single component. 16 00:01:30,480 --> 00:01:35,340 So they're rationalising everything into the new Android X libraries. 17 00:01:35,340 --> 00:01:41,160 They've also made it very easy to convert your apps, and you should get very few problems. 18 00:01:41,160 --> 00:01:43,620 Most of the time, you'll have no problems at all. 19 00:01:43,620 --> 00:01:48,160 Alright, I've got the TaskTimer app loaded in Android Studio. 20 00:01:48,160 --> 00:01:51,920 Let's see how to switch the libraries to AndroidX. 21 00:01:51,920 --> 00:01:59,160 In the module app build.gradle file, 22 00:01:59,160 --> 00:02:05,500 at the bottom, we can see all the dependencies, on com.android.support libraries. 23 00:02:05,500 --> 00:02:11,000 It's these dependencies, and the imports, that the conversion modifies. 24 00:02:11,000 --> 00:02:20,260 Go to the Refactor menu, and choose the option Migrate to AndroidX. 25 00:02:20,260 --> 00:02:23,720 It's a very good idea to have a backup of your project first. 26 00:02:23,720 --> 00:02:27,400 Of course, you've already done that, but just in case you haven't, 27 00:02:27,400 --> 00:02:33,000 the migration process will zip up your project files to create a backup for you. 28 00:02:33,000 --> 00:02:42,840 If you want to backup, make sure the 'Backup project as zip file' box is ticked, before clicking the migrate button. 29 00:02:42,840 --> 00:02:47,560 I'll leave it ticked, because there's something to watch out for if you use that option. 30 00:02:47,560 --> 00:02:51,300 We have to specify a location for the backup zip file, 31 00:02:51,300 --> 00:02:52,900 but be careful. 32 00:02:52,900 --> 00:02:56,580 It doesn't do it on my Mac, but on Windows and Linux, 33 00:02:56,580 --> 00:03:01,500 notice the default to putting the zip file into your project directory. 34 00:03:01,500 --> 00:03:06,800 So be careful there, and make sure you save it somewhere else out of the way. 35 00:03:06,800 --> 00:03:18,360 I'll put it in my documents folder. 36 00:03:18,360 --> 00:03:21,620 Click OK after choosing a suitable location, 37 00:03:21,620 --> 00:03:28,780 and Android Studio will convert the project to use the Android X libraries, instead of the old support libraries. 38 00:03:28,780 --> 00:03:33,680 It shows you what's going to change in the refactoring preview at the bottom of the screen. 39 00:03:33,680 --> 00:03:38,220 You might want to expand all the sections there to get a feel for what's going to change. 40 00:03:38,220 --> 00:03:47,540 It's fairly straightforward, and I'm just going to click the Do refactor button. 41 00:03:47,540 --> 00:03:54,280 It takes a while to update everything, and you may see a dialog asking if you want to update Gradle. 42 00:03:54,280 --> 00:03:59,200 Android Studio seems to perform a check for updates, as part of the migration. 43 00:03:59,200 --> 00:04:03,540 So you'll see that dialog if any new updates are available. 44 00:04:03,540 --> 00:04:05,760 Click Update if you do see that. 45 00:04:05,760 --> 00:04:09,300 Android Studio will then resync the Gradle files. 46 00:04:09,300 --> 00:04:13,760 At the moment, some of the AndroidX libraries are still in beta. 47 00:04:13,760 --> 00:04:19,240 You won't want to use them in a production app, but they should all be released versions quite soon. 48 00:04:19,240 --> 00:04:25,160 I've got a load of suggestions about version numbers in my build.gradle dependencies, 49 00:04:25,160 --> 00:04:30,740 because new versions of the libraries came out after this version of the migration tool. 50 00:04:30,740 --> 00:04:37,780 It's always worth checking your build.gradle file after migrating, to fix up any version numbers, as necessary. 51 00:04:37,780 --> 00:04:41,740 We've done this before, in an earlier video. 52 00:04:41,740 --> 00:04:59,280 I'll change the version to match what Android Studio's suggesting. 53 00:04:59,280 --> 00:05:06,060 It can be tricky to get the light bulb to appear, but clicking on the ticks in the right margin seems to work well. 54 00:05:06,060 --> 00:05:11,020 Clicking on the word before the final colon in each line, also seems to be reliable. 55 00:05:11,020 --> 00:05:14,860 You can also just type the new numbers in without using the light bulb. 56 00:05:14,860 --> 00:05:19,880 Google have also changed the way the lifecycle version number is provided. 57 00:05:19,880 --> 00:05:29,260 They no longer use that lifecycle underscore version variable, and it can be deleted. 58 00:05:29,260 --> 00:05:36,020 Okay, sync now to use the new versions. 59 00:05:36,020 --> 00:05:42,040 Our dependencies are now on the Android X libraries, rather than the old support libraries. 60 00:05:42,040 --> 00:05:48,220 Switching back to MainActivity, 61 00:05:48,220 --> 00:05:52,980 the imports no longer have that odd mix of v4 and v7. 62 00:05:52,980 --> 00:05:57,180 They're all from one of the Android X dot something libraries. 63 00:05:57,180 --> 00:06:01,640 If you get any unused import, you can safely delete them. 64 00:06:01,640 --> 00:06:04,540 Make sure they really are unused, though. 65 00:06:04,540 --> 00:06:11,300 I've noticed that the conversions sometimes results in fully qualified class names, instead of using imports. 66 00:06:11,300 --> 00:06:17,560 For example, check the removeEditPane function, in MainActivity. 67 00:06:17,560 --> 00:06:22,840 It's using Androidx.fragment.app.Fragment, 68 00:06:22,840 --> 00:06:33,400 which is a bit unwieldy. I'll delete the package name. 69 00:06:33,400 --> 00:06:37,740 And now the import's no longer unused. 70 00:06:37,740 --> 00:06:42,860 Of course, if Android Studio removed the import, you'll now get an error. 71 00:06:42,860 --> 00:06:47,180 Use Alt + Enter to add the import back in again, if that happens. 72 00:06:47,180 --> 00:06:53,980 Now, I said you don't get many problems, but you may find errors with some of the functions you've overridden. 73 00:06:53,980 --> 00:06:57,880 Google have been adding annotations to the Android framework code, 74 00:06:57,880 --> 00:07:01,400 and some of the function signatures have changed slightly. 75 00:07:01,400 --> 00:07:07,680 I'll spend a moment explaining all this, because it's very easy to deal with when you understand what's happening. 76 00:07:07,680 --> 00:07:10,140 As I mentioned in an earlier section, 77 00:07:10,140 --> 00:07:16,260 it was safe to delete the nullable type marker on the onSaveInstanceState function. 78 00:07:16,260 --> 00:07:20,620 Let's have a look at that in MainActivity. 79 00:07:20,620 --> 00:07:30,600 Ctrl-click on the super dot onSaveInstanceState function, in line 146. 80 00:07:30,600 --> 00:07:33,960 That takes us to the Android source code. 81 00:07:33,960 --> 00:07:38,340 Notice that there's no annotation on the outState bundle parameter. 82 00:07:38,340 --> 00:07:41,660 As a result we don't have to change our code. 83 00:07:41,660 --> 00:07:44,360 I haven't got an error in MainActivity. 84 00:07:44,360 --> 00:07:51,280 As they introduced new versions of the AndroidX libraries, and other librariesin the Android framework, for that matter. 85 00:07:51,280 --> 00:07:55,120 Google are annotating parameters that can't be null. 86 00:07:55,120 --> 00:08:02,080 When you convert to AndroidX, you may find that later versions of the libraries are used, and you may get errors. 87 00:08:02,080 --> 00:08:07,960 I can simulate that behavior by swapping to pre-release versions of the AndroidX libraries. 88 00:08:07,960 --> 00:08:24,580 I'll paste in a new dependency section in the module build.gradle file, to replace the current versions. 89 00:08:24,580 --> 00:08:29,120 Sync now, 90 00:08:29,120 --> 00:08:32,679 and back in MainActivity, 91 00:08:32,679 --> 00:08:36,360 we've got an error on the onSaveInstanceState function. 92 00:08:36,360 --> 00:08:43,500 Control click on the super call again, 93 00:08:43,500 --> 00:08:49,520 and we can see that this new version does annotate outState with @NonNull. 94 00:08:49,520 --> 00:08:57,400 Back in MainActivity, the function signature no longer matches the function that it's overriding. 95 00:08:57,400 --> 00:09:04,240 We have to delete the question mark from the parameter to fix the problem. 96 00:09:04,240 --> 00:09:07,060 If you're not sure what the correct signature should be, 97 00:09:07,060 --> 00:09:12,760 comment out your function, and get Android Studio to generate the stub for you again. 98 00:09:12,760 --> 00:09:17,020 If Android Studio generated your function subs with the latest libraries, 99 00:09:17,020 --> 00:09:21,060 outState won't have been marked as nullable and you won't get an error. 100 00:09:21,060 --> 00:09:24,840 This only really applies when converting old projects. 101 00:09:24,840 --> 00:09:29,400 Okay, that one was easy because I had MainActivity open. 102 00:09:29,400 --> 00:09:33,080 I'm not going to open up every class file to check for errors. 103 00:09:33,080 --> 00:09:36,700 Instead, use Build, Rebuild project 104 00:09:36,700 --> 00:09:48,200 and check the build pane at the bottom of the screen to see if there are any more errors. 105 00:09:48,200 --> 00:09:55,520 It takes a little while to rebuild and I've got another error. 106 00:09:55,520 --> 00:10:00,540 The onAttach override in MainActivityFragment is incorrect. 107 00:10:00,540 --> 00:10:06,120 You can double click the error in the build pane, to go straight to the problem code. 108 00:10:06,120 --> 00:10:12,340 We can delete the question mark after context, 109 00:10:12,340 --> 00:10:19,480 and then rebuild the project again. 110 00:10:19,480 --> 00:10:24,240 The AndroidX libraries are more Kotlin friendly than the old support libraries. 111 00:10:24,240 --> 00:10:28,620 In fact, Google aren't developing the old support libraries anymore. 112 00:10:28,620 --> 00:10:33,780 Version 28.0.0 will be the last version of them. 113 00:10:33,780 --> 00:10:37,680 So that's two good reasons for switching to AndroidX. 114 00:10:37,680 --> 00:10:41,240 There may also be unused imports at the top of file. 115 00:10:41,240 --> 00:10:46,780 Search the file for AndroidX, 116 00:10:46,780 --> 00:10:50,700 to quickly find any fully qualified class names. 117 00:10:50,700 --> 00:10:52,440 They don't cause a problem 118 00:10:52,440 --> 00:11:07,680 It's just harder to read the code if they're fully qualified, rather than imported. 119 00:11:07,680 --> 00:11:12,720 Unused imports are a good indication that the migration's done something like that, 120 00:11:12,720 --> 00:11:15,140 but sometimes the import's missing. 121 00:11:15,140 --> 00:11:21,600 Searching the file for AndroidX is a more reliable way to detect those fully qualified class names. 122 00:11:21,600 --> 00:11:27,120 I've also got a suggestion that there's a redundant call to toString, 123 00:11:27,120 --> 00:11:29,060 in the onAttach function. 124 00:11:29,060 --> 00:11:34,500 I'll use a light bulb to fix that. 125 00:11:34,500 --> 00:11:39,200 I'll also space the function out better. 126 00:11:39,200 --> 00:11:52,960 There may be another fully qualified class name in onViewCreated, 127 00:11:52,960 --> 00:12:07,800 and I'll fix the fully qualified classes in AddEditfFagment, 128 00:12:07,800 --> 00:12:28,140 and our cursorRecyclerViewAdapter class. 129 00:12:28,140 --> 00:12:35,320 It's worth going through all your class files, looking out for changes that you may need to make after the conversion. 130 00:12:35,320 --> 00:12:40,400 Android Studio suggestions in the right margin are very useful, 131 00:12:40,400 --> 00:12:47,600 and the presence of unused imports is also a good clue, that some of your class references will be fully qualified. 132 00:12:47,600 --> 00:12:56,940 After these changes, the project builds successfully. 133 00:12:56,940 --> 00:13:09,300 I'll run it in the emulator just to be sure 134 00:13:09,300 --> 00:13:12,340 You should really test the app thoroughly, of course, 135 00:13:12,340 --> 00:13:14,900 but you don't want to watch me doing that. 136 00:13:14,900 --> 00:13:18,520 Make sure it works before moving on to the next video. 137 00:13:18,520 --> 00:13:22,460 There's one change I'm going to make before finishing this video. 138 00:13:22,460 --> 00:13:24,880 Back in the build.gradle file, 139 00:13:24,880 --> 00:13:29,640 I change the library versions to use the alpha versions. 140 00:13:29,640 --> 00:13:33,320 You shouldn't release an app with the alpha or beta libraries, 141 00:13:33,320 --> 00:13:36,520 and I don't want to forget that I'm currently using them. 142 00:13:36,520 --> 00:13:42,700 So I'll add a to do comment as a reminder to update things like androidx.appcompat. 143 00:13:42,700 --> 00:14:06,560 when Google makes the release versions available. 144 00:14:06,560 --> 00:14:10,640 Alright, that's our project converted to use AndroidX. 145 00:14:10,640 --> 00:14:15,340 We'll make a start with dialogues in the next video.