All right, so this is a follow up video
to my earlier video where I showed you how to
migrate an Android Studio project to Android X.
This one's specific to Kotlins.
I'm going to talk about some of the pitfalls
and problems you may come across when converting
a Kotlin Android Studio project to Android X.
So make sure you check out that first video first,
where I've outlined some of the things
you'll need to do to migrate,
but then after you've watched that,
come back here and watch this one
and we'll go through the remaining steps
to hopefully get you up and going with your Kotlin Project
converting it and later migrating it over to Android X.
Let's make a start.
(upbeat house music)
If you haven't watched
the previous video of this series
you may want to do that before watching this one
and that's because in this video
I'm going to show you a couple of problems
you might get when migrating code to use Android X.
However, I won't be discussing
the migration process itself in detail,
that's in the previous video.
So there's a link to that video on screen now
and also in the description for this video.
And moving on we've got a Kotlin project to migrate.
So let's see what happens when
I migrate it to use Android X.
So I'm gonna come up to the refactor menu,
I'm going to select migrate to Android X.
Bearing in mind this is only going
to be showing on your screen
for Android Studio if you're running
3.3, of Android Studio.
Now, what I'll do is I'll let the
tool create a backup zip file and then
I'm gonna click on double factor.
And I'm gonna put that in the folder above...
the current project so there's no way
it gets added to this project.
I'm gonna replace the one that I had there now.
And you can see it's actually scanning for uses
and working out what needs to be updated
to do the refactor process.
I also mentioned in the last video,
you may get a dialogue that looks something like this
showing or saying that the changes were detected.
If you do get this dialogue,
click on the rerun search button.
At the moment though, that behaves strangely
and you'll probably get a second
refactoring review pane, preview pane I should
say, but without the do refactor button.
If that's the case just close the
pane and click on the
do refactor button, in the original pane,
but in my case none of that came up.
Alright, so I'm gonna click on do refactor now.
And that'll take a while to finish
because it's going to go through and figure out
what needs to be updated, and it's
actually going to download the appropriate components.
Once that's finished we're
going to check out what has actually changed.
Alright, so let's finish now, so first thing
we'll do is, we're going to
check out the module's build dot gradle file.
So I'm going to come down to Gradle scripts
and look at our module app.
Our build gradle file.
And if you scroll down and have a look
there's quite a few old versions here
and we can see that all these versions
have now got newer versions available.
Basically Android X is changing rapidly
at the moment, as we get close to the
released versions of each library
as of the time I'm shooting this.
Now the light bulb, if I click into some of these.
It'll actually change everything but this first entry.
And the reason I won't do this first one
is because this one defines the version number separately,
and Android Studio won't offer the option to change it.
But I can go through all do the other ones,
and I'm gonna do that now,
so I'm gonna select each of these.
And change.
And obviously by doing this
I'll need to re-sync the project.
I'll just go through and do all of these.
Alright, so I've got all of those changed
other than that first entry, as I mentioned.
And I'll need to re-sync now
or click on sync now to enable that to go through.
The project will then re-sync.
And once that's finished you can see
its downloading various files now,
in the status bar in the bottom of the screen.
Once that's finished we'll go through
and have a look and see what problems that has caused.
And in general when you're doing this it's a good
idea to review all the files in the project,
to check for problems.
But the two main things I'll tell you now,
whilst this is still working, that you'll find
currently are unused imports
as well as incorrect overrides.
We'll just let this finish and
we'll come back and go through that.
Alright you can see now the gradle
project build, the build has finished.
We've just got this one warning now
which we'll leave, we won't bother changing that,
we can type that in manually if we wanted to.
In fact, why don't I do that, I'll just
go through and change the life cycle version.
So 2.1.0 dash alpha two.
2.1 dash zero dash it's alpha two.
Let's just do that and re-sync it.
Just so we've got no other warnings
in our build gradle file.
Alright so I wanna show you now some
of the errors that'll be introduced.
So the first one is gonna, I'm gonna open
some of our Kotlin code and, in this project
the first one is the date picker fragment
class, we'll have a look at that.
And if we have a look at the error over here,
we can see if we hover over it,
on attach overrides nothing.
And that's because the function
signatures changed in the Android X code.
So in line 52 here, the context parameters
declare this a nullable context,
Java doesn't have non-nullable types,
but Google have been adding annotations
to the Android framework code to let Kotlin know
that some functions won't be
called with null arguments.
And that's actually what's happened here.
And you can see that by control or command
clicking on the super attach function, on line 54.
So I'm gonna do that.
We're viewing the source code now.
And you can see on line 310,
the context parameter is marked
as non null using that annotation,
so therefore to fix it, that's quite easy.
What we can do is just come back
and remove the question mark from the parameter type.
The context arguments being used
inside the function and deleting the
nullable type marker, as I've done,
has now given us a warning, and that's an unnecessary
safe call operator, down here, on line 57.
The Android X library's a lot nicer to
use from Kotlin because Google have been
adding these non-null annotations.
We don't have to use so many safe call operators,
when overriding functions now.
Right so what I'm gonna do is delete that safe call,
and we'll see what the next problem is, so let's do that.
And you can see we've now got a redundant
call to to string now.
I needed an example to demonstrate
how Android X results in changes to our code.
But that line's done its job now and therefore
the to string function call can be deleted,
so I'm going to do that, I'm just gonna
leave in dollar context, and delete the rest of this code.
Right.
Alright so that's fixed up that class,
the next thing I wanna look at is
the cursor recycler view adapter class.
Now you may find that in your import sections
you'll actually see an unused import,
in my case that hasn't come up but
that may come up for you, but the problem
comes about because of the fully qualified
names that the migration process has actually adopted.
You can see on line 17 for example look at
Android X dot recycler view dot widget.
So that's a fully qualified name for the recycler view,
so I'm going to remove that.
And I'm going to accept the import.
Alright, so that's one that I've actually fixed
and I'll just go back and put that code back as it was.
Now again this wasn't a particularly bad problem
but if you do see an unused import it's just
a minor annoyance that perhaps you might wanna fix,
in my case though I'd prefer to actually not
use those fully qualified path names as you saw there.
So what we've done is we've
removed that fully qualified name.
I'm gonna do a search for Android X.
And I'm going to remove the other ones as well.
There's one more here on line 41,
as you can see I'm gonna remove that as well.
I'll do a search, and basically
that's all there is now.
Alright so I've corrected both of those.
Now if we have a look at another example,
we'll go back to our project and
look at our durations report.
Now you probably saw there briefly
there was an unused import,
and the reason it's not showing up actually
is because I've got in my preferences...
Under general, auto import.
I've got optimise imports on the fly.
So it's actually hiding those unused imports.
But basically it's still using the same,
or it's the same issue of that fully qualified name,
and I think it's better not to use
those fully qualified names.
And what we can just do is
just do a search for Android X.
You can see there's one there again,
we can just replace that with linear manager.
I won't go through and do all of these now.
Okay, and there's the other one.
Basically it's more of the same
so I won't fix up any more of those.
But there are some other problems
that'll prevent the app from running currently.
First one I wanna draw your attention to
is the main activity class.
I've got an error down here.
Basically we need to do is change our bundle
question mark to a non nullable type bundle
so we'll just remove the question mark.
And that will fix that error.
And main activity fragment...
has got the same problem.
Basically with context that we've seen previously,
we're going to remove that non nullable type again.
And I'm just gonna fix up the redundant string call.
Okay, and that's good to go.
So as you can see these issues
are pretty easy to find and fix.
And before I run the app to check that it still works
I'm gonna finish with a warning.
We've been changing full qualified
class names to simplify them.
And there are quite a few
in some of the other classes.
Fragment activity extensions for argument's sake,
has got a load of them,
and that could certainly do with being tidied up.
When you're going through the project it's easy to
get into the habit of automatically simplifying these names,
but one place you shouldn't do that is
in the layout resource files.
So we'll open one of my task layout files,
it's in the resource layout folder.
And the name is our task underscore durations,
now open that up.
And make sure that I'm in text view
which I am and we'll just go to fullscreen.
So first you notice that the constraint layout
has been fully qualified, it's
coming from Android X dot constraint layout dot widget.
Don't try and simplify that to just constraint layout,
or you'll break the app.
And the last widget in this file if
we scroll down to the bottom...
The recycler view is also fully qualified
as you can see, once again leave that alone.
If you change those references to remove
the package name, your app will crash
when it tries to inflate the layouts.
Alright so with that said let's close it down.
And let's try running our app, to make sure that it works.
And you can see in fact we've got one
other thing that I need to fix,
an on attach override that does nothing.
So I'm just gonna double click into that.
Same deal there as we saw previously,
get rid of the non-nullable type,
and that fixes the problem.
And we'll try running it again.
Okay, well we'll ignore those other warnings
in the bottom of the screen for now,
but you can see that at this point
the main screen's loaded up,
if I tap on the eye icon, on the toolbar or in the toolbar,
we can see the report screen.
Now there's no data in the app so the report's blank,
but we know that at least it's loading,
I can go back to the main screen.
And I can also click on the about dialogue
from the extended menu.
So that's working fine.
Now obviously I should put this app
through its complete test cycle,
changing the libraries that your app uses
is a major change, and you'll need to test
your app thoroughly afterwards, and
we obviously haven't done that in this video.
Alright, so that's the project
migrated and ready for thorough testing
and future development.
Alright, so thanks for watching,
if you've got questions leave a comment,
if you're ready for the next
programming-specific video, click on
the link up here, in the top left hand corner
of your screen, to look at non-coding
programming related questions,
click the link on the bottom left hand corner
of your screen, feel free to subscribe
by clicking on the link up here,
and I'll see you in another video.