- Alright, so in this video,
I want to show you how to migrate
an existing Android Studio project to AndroidX.
I'm gonna talk about the technology.
What's changed, and more importantly
how to migrate your existing project
to that technology, to AndroidX.
So let's make a start.
(upbeat music)
Alrighty, so let's get started.
In 2018 Google decided to migrate
the various support libraries into AndroidX.
The support libraries were becoming unmanageable,
and were also confusing for us developers.
I mean why did we have to include
the v4 and v7 support libraries
in an app whose minimum SDK was 17?
There seemed to be no rhyme or reason
to the support libraries,
and you couldn't easily predict
what would be bundled into your app.
AndroidX rationalises all of that,
and provides all the features of the support libraries,
in a collection of independently versioned libraries.
Google can now release a new version of one component,
without having to update an entire support library.
And they've also included a host of new features,
Including the new Lifecycle Aware Architecture Components.
The AndroidX package libraries are part of JetPack,
which includes these new components.
Now that many of the libraries
are out of beta,
with the rest expected to be released soon,
it's time to consider switching over
your android projects, to AndroidX.
There won't be any further development
of the old support libraries.
Version 28.0.0 will be the last version of them,
which is another reason for switching over to AndroidX.
So in this video, we are going to have a go at migrating
an existing Android project to AndroidX,
to see what's involved.
The process is fairly straightforward.
And that's because Android Studio 3.3
does a lot of the work for us.
So, see on the screen I've got an existing project open.
And we are going to be using this one for the demonstration.
Now this app is an app that downloads photo's
from Flickr, the website.
One requirement for using the new libraries
is that your app must be compiled against SDK 28,
or higher of course, but at the time I'm recording this
APR 28 is the latest version.
And, on screen I've opened
five of the apps classes in Android Studio
just to let us see what's going to be changed
by the migration.
So the first one I've got on screen is base activity.
This is an abstract class that's used as the basis
for the app's activities.
It saves having to duplicate the toolbar code
in each activity.
So what we're interested in are the imports
at the top of the screen there.
We're inputting from Android.support.v7
to get the Android bar and toolbar.
The class extends AppCompactActivity
so that also has to be important.
And what I've done is added those imports as comments,
just above the real imports section.
We'll use these comments to see what's changed
after the migration.
Alright so that's base activity.
The next one, is our RecyclerView Adapter.
Which is called Flickr RecyclerView Adapter.
And you can see that imports
Android.support.v7.widget.RecylerView
as well as the non-null annotation,
from the support library.
Moving on.
Main activity.
Where this imports the RecyclerView
from Android.support.v7
as well as the linear layout manager.
In photo.Java,
that class only imports an annotation as you can see there.
Now we're gonna open another class now,
Recycler Item List Listener.
This one handles touch events,
in the Recycler View and imports
from both Android.support.v7
and Android.support.v4.
So why v7 and v4?
It's pretty confusing isn't it?
The good news is that AndroidX
makes it much easier to work out
what's in the various packages.
This class as you can see also imports
the non-null annotation
from Android.support.annotation.
And it's not just the classes that are modified
by the migration, the layout files are also changed as well.
If I open activity_main.xml.
I go to the xml, by clicking on the text tab.
You can see that we are using the co-ordinator layout
from the support libraries.
And there's also the app bar layout as you can see,
and toolbar.
Well these come from the support libraries as well
so all of these should be updated,
when we migrate to AndroidX.
And there's one other file we are gonna
be interested in looking at.
And this is the module,
build.gradle file.
And in case you're wondering this down here
in the Gradle scripts section of the projects pane
if you're in android view as I am,
otherwise it's in the app folder.
Now, notice that the compilesdkVersion is set to 28.
You won't be able to migrate to Androidx
unless you're compiling against API 28 or higher.
Now these dependencies at the bottom,
which I'll scroll down to,
these are the libraries
where the various support objects come from.
Now for these entries I've put the comment below each one.
Alright, so we've got a note
of the things we expect to change.
It's now time to migrate the project.
So on the Refactor menu, click on that,
you can use this option to migrate, to AndroidX.
And this is added to Android Studio in version 3.3,
so it's not available on earlier versions.
So I'm gonna click on that.
Now, you can ignore this next dialogue,
because you've got your project backed up already.
Right?
Okay, well maybe you haven't.
If not, make sure you tick,
or make sure the checkbox is checked,
to back up project to zip file, which it is for me.
I mean, after all we are going to let something
change a load of files in our project, and if goes wrong,
we could potentially lose a lot of work.
So don't do this without a back-up of your project.
Pretty straightforward, click on migrate, with that checked.
It's going to save it.
I suggest you save it into another folder,
and not into the same folder, where your project resides.
Just to prevent any problems, with having that file there,
so I'm going to save that.
I'm just going to overwrite it,
because obviously I've done it previously,
I've now got my back up.
Now you may get a dialogue.
I'm gonna show it on the screen now,
that says or saying that changes were detected.
If you get something that looks like that,
click on the Rerun search button.
Now at the moment, as of the time I'm recording this video,
that can behave strangely, and you may get
a second refactoring preview pane
but without the do refactor button.
Just close that pane if that happens.
And click the do refactor button in the original pane.
But as you can see with me that didn't come up.
So, basically we got straight to the stage
of refactoring preview down the bottom of the screen there.
So what I'm gonna do, is put this pane
into floating mode.
And I'll make it full screen so everyone can see
it a little better in what we are doing here.
Right, so I'll just size that up so we can see that's
as good as possible.
Right so if we open up some of these things and have a look.
See that first one there?
We've got a reference to the RecyclerView in main activity.
That's going to be changed.
As RecyclerView comes from the Support Libraries,
that's hardly surprising.
Now I'm not gonna go through all these other sections,
but you might find it interesting to do that
with your own projects a couple of times.
What I will do though, is quickly expand
the users in imports section, this one down here.
And the last class down here,
this RecycleritemclickListener.
I'm gonna expand that as well.
And that's actually showing the imports
that we identified earlier.
And moving on down, the next section shows
the changes that are going to happen
to the xml resource files,
so I can expand that out and have a look.
And you can see that there's something
going to be changed in a lot of those files.
Right so let's follow down here
by the usage in Gradle build script.
I can expand that out,
and again you saw earlier in the video
that I already commented that so that we've got a note
of what's going to change there.
Now, if you want to record what's going to change,
you can expand all the entries,
there's a button to do that on the far left,
and then copy and paste all the entries into a text editor.
But I won't do that now.
What I'm gonna do is click on Do Refactor.
You can see what's happened Android Studio has helpfully
made those changes, and it's now configuring
and re-syncing the project and rebuilding it.
So that you should find that it will download,
in fact you can see it in the status bar at the bottom
of the screen, downloading a number of components.
And you can now see that in the sync pane there
that's showing on the screen.
So we'll just give that a moment to finish,
it shouldn't take too long.
But obviously that will be dependent
on the size of your project.
Once it's done that,
we'll take a look at some of the changes that have occurred.
Alright so you can see that's now finished,
and it's re-compiled, it's re-synced the project.
And we can now see that on screen because we've got
the build.Gradle file open.
There's a lot of suggestions in there.
I'm gonna come back to this file because it's the one
you're most likely to want to change.
We'll go back and start by having a look at base activity.
I'll just close down that window.
Let's just go to full screen so we can see it a bit easier.
So in your imports you can see there now,
that basically Androidx.appcompact.app
instead of Android.support.v7.app.
AndroidX will do away with the v4, v7, v14 and v whatever
support libraries, and put things into more rational places.
So moving on.
We've also go a change
in our Recycler Item Click Listener
we'll check that file out.
Notice that the GestureDetectorCompat class
is now coming from Android.core.view,
instead of Android.support.v4.view,
and the RecyclerView
comes from Androidx.recyclerview.widget.
Now incidentally, if you want to do this manually,
you'll need to add two entries
to the Gradle.properites file.
So what I'm going to do is just open that file up,
Gradle.properties,
and incidentally, there's two of those files,
you want the project properties one, and not the global one.
And you can see that I've got the project properties version
of the file open.
Now the migration tool has added those two entries for us.
The ones on lines 12 and 13.
Jetifier takes care of managing
dependencies in other modules,
and converts old support library references
to AndroidX references when the project is built.
Now as an example, we'll switch back
to our build.Gradle file.
Looking right down the bottom there,
there's a reference, or a dependency, I should say,
to the Picasso Library, that's currently using
the old Android support libraries.
Jetifier, will change it to use
the corresponding AndroidX Libraries,
when it builds the project.
So now that we are back here,
we can have a quick look at those, the version suggestions
that we're actually getting here.
At the moment, many of the AndroidX Libraries,
are still in Beta.
You probably won't want to release an app that contains
Beta versions of code, but it shouldn't be long
before Google provides release versions of these.
Now Android Studio's suggesting these .zero Beta versions
can be replaced, with .one Alpha versions.
I've highlighted some code here, you can see
as of time of recording, it's saying my beta01
can be replaced with version 2.1.0-alpha02
and obviously depending on when you're watching this video
you may have a later release than that.
Now Alpha versions are released
before Beta versions for testing.
And that means they probably contain more bugs.
Will a 2.1 Alpha version contain fewer bugs than a 2.0 Beta?
Well, perhaps, but I certainly wouldn't use these libraries
in production code at the moment.
Now the constraint library,
you can see that on line 33,
that's been stable for quite a while,
and there's new 1.1.3 available,
I can hover over that.
So I'm happy to update that one.
So I'll just change that one to a 1.1.3.
But all the other AndroidX packages are Beta versions.
And by the time you're watching this,
Google should have production versions
of all these libraries, or getting close to,
having those released.
Now I'm gonna put a link to a Google web page,
in the description for this video.
But just to show you the link.
And you can see down here at the moment,
it's talking about the various Beta versions and so forth.
And fairly recently, I'm recording this video
on February the 26th in Australia,
so you can see they are in the process
of updating on a regular basis.
Alright, so after you've tested the migration
there's one change that the migration tool doesn't make,
that you will want to think about.
We are using the material library,
we go back to our Android Studio Project.
Basically you're talking about the line 35 there.
Using com.google.android.material
we're using the 1.0.0-beta01.
Now if we have a look in our Res Values folder,
and look at our styles.xml.
We can change the apps theme here,
to inherit from on of the material themes.
So instead of Theme.AppCompat.Light.DarkActionBar,
you can change that to,
Theme.MaterialComponents.Light
Now with the new material components you can do things
like styling buttons without using drawables,
which is pretty cool.
Alright, so that was a quick look at how to migrate
an existing project to AndroidX.
So review all your files to make sure nothing's broken.
But for Java code there are few problems.
However, if you are programming in Kotlin,
there can be some minor problems to fix.
Google have made the new AndroidX Libraries
more Kotlin friendly and that involved changing
some of the function signatures, to use non-nullable types.
So as a result, some of your overrides may need modifying.
So, look out for the next video
in this series, where I'm going to show you
some of the problems you can get with Kotlin code,
and more importantly, how to fix them.
But with that said now, let's just start try running
our application, just to make sure that it still works.
Right, so there's our app working, and I can do a search
for something, say, sunset.
And you can see we've got the results back from Flickr.
And let's just open one of these images.
There you go.
All working.
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 on the link down 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.