AndroidX
Android Studio project to AndroidX. What's changed, and more importantly how to migrate your existing
project to that technology, to AndroidX. So let's make a start. 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.
Android Studio project to androidx rationalizes 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 Android Studio project to AndroidX package libraries
is 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 article, 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 the Android Studio project to AndroidX 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 based on 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 based 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 the Android Studio project to 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 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 there.
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 the full screen so we can see it
a bit easier. So in your imports, you can see there now, that basically Android Studio project to AndroidX.appcompact.app instead of android.support.v7.app. Android Studio project to 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 gone 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.
converts old support
library references to the Android Studio project 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. it to use the corresponding Android Studio project to 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 the time of recording, it's
saying my beta01 can be replaced with version 2.1.0-alpha02 and obviously
depending on when 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 the 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 Android Studio project to 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.


No comments:
Thanks for visiting!