Successfully Migrating to AndroidX

I have been putting off the switch to the AndroidX replacement for the old Android Support libraries for quite a while. Every time I tried, I ran into a conflicting class issue and never found the time to properly debug it. During some dedicated procrastination time today, and incentivised by the new DayNight theme (with a different background colour to the old library), I finally found the solution to a few problems that I've encountered - hopefully, the following post will help if you've come across a similar issue.

In this post I am going to go through the steps and issues that occurred when I updated the app I have been working on. Since I haven't touched the code for this app for almost half a year, it's using the latest release of the support libraries.

1: Update dependencies


The first thing I did was make sure that my dependencies were all up to date - yours might already be up to date, however, I hadn't done this for a while. Firstly I updated Gradle for Android Studio 3.4

Right click on the app module and go to the module settings. Head to dependencies, and click update for all the dependencies that have warnings.
At this point, I didn't bother re-running the app to make sure it still worked. I think it wouldn't compile since some of the updated libraries are for AndroidX only.

2. Migrate to AndroidX tool

The Refactor>Migrate to AndroidX tool does a good job of replacing AppCompat references with AndroidX. Unfortunately for me, I still encountered issues after this, so this is not the last step.

Take a backup of your project and click Migrate to AndroidX... and then 'Do Refactor'. Resync Gradle and try running your application. If the app runs perfectly and you don't come across any issues, then lucky you! You're done.

The first error I was shown was due to a change in Lifecycle:

error: name clash: observe(LifecycleOwner,Observer) in SingleLiveEvent and observe(LifecycleOwner,Observer) in LiveData have the same erasure, yet neither overrides the other


To resolve this I used the suggested action and tried running the application again. Another error further down the line was now showing.

3. Fix Duplicate class error

java.lang.RuntimeException: Duplicate class androidx.core.graphics.PathSegment found in modules classes.jar (androidx.core:core-ktx:0.3) and classes.jar (androidx.core:core:1.0.0)

... was my next hurdle. This is where I previously gave up, however, after a little research I found that the issue is due to conflicted Android ktx versions (well, it's quite obvious from the error, however I don't use Kotlin in this project!) It was fixed in Plaid (Article). Interestingly, the bug in Android Studio that caused this is supposedly fixed, however, I believe my issue was slightly different.

There is a problem with androidx.core:core-ktx:0.3, however this dependency is not in my build.gradle files. In fact, I haven't written any Kotlin code in this project, so it's a weird issue to see. Since I plan to build some future features in Kotlin, I figured what's the harm in using the Tools>Kotlin>Configure Kotlin In Project.. tool.


This caused more issues when trying to run the app! To solve these, I moved the plugins block to just after buildscript and then removed the 'task clean' block since it's a duplicate.


No more gradle issues. Finally, I found the latest version of ktx from https://developer.android.com/kotlin/ktx and added it to my app module level build.gradle. This resolved the conflicting class error and at last, the app would build and install onto my phone!

4. Fix crashes - Material Button

Since the Material Button requires the activity theme to be a descendant of Theme.MaterialComponents, the application was now crashing on launch. To solve this, I replaced all references to AppCompat in my styles.xml with Theme.MaterialComponents.DayNight.NoActionBar

And it worked! There don't seem to be any glaring issues with libraries that I use, including Retrofit, RxJava, Dagger and ButterKnife, and I should be ready to start integrating some Kotlin in the app soon! 

But at the end of the day, I did all this for a darker background.

Now, back to the A Level revision.

Comments