Refactor to Android X

At the Google I/O 2018 AndroidX is announced. To prevent any miscommunication about what parts are from Android os and which parts are from extra supporting libraries.

As app developer I'm always looking to improve how to build app and use the best tool / libraries to get maximal results. Migrate Android projects, using support libraries, to AndroidX to be ready for further updates.

AndroidX packages

With the implementation of AndroidX all API package naming is updated. Below we see an example of pre AndroidX packages com.android.support:appcompat-v7 and com.android.support.test.espresso.
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

After the migration to AndroidX the packages name has no longer the com.android.support but it's updated to androidx.

So the appcompat package name is androidx.appcompat:appcompat and for espresso androidx.test.espresso:espresso-core.
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
More information about the package name mapping can be found on the AndroidX refactoring.

Prerequisites

  • Android Studio 3.2+ (current only available in the preview channel)

  • Android SDK 28 (Android P Preview during preview)
    • Go to File >Settings... > Android SDK > SDK platforms > Select and download API level 28

  • Android SDK build tools 28
    • Go toFile > Settings... > Android SDK > SDK tools > Select and download Android SDK Build-tools 28

  • Android Phone device or emulator running Android P

Let's go

When Android Studio is launched for the first time it will ask to update the gradle (to version 4.6+) and Android Gradle Plugin (to version 3.2+).We advise to accept this update by pressing Update, to migrate to the latest gradle version available for Android Studio.

As first step we are going to update the update the compile SDK version inside the Gradle file.

Changes the file Gradle.build

  • Update compileSdkVersion to 28
  • Update targetSdkVersion to 28
  • Clean up: The element buildToolsVersionis no longer needed and can be removed.

Verification

To make sure all required tools are installed correctly, run the app, launch all unit and ui tests and validate that everything works as expected.

Refactor to AndroidX...

Now it's time to refactor the code base from support libraries to AndroidX libs The actual refactor step is made easy by Google, they added a menu item within Android Studio. Android studio made this step quite easy, just go to Refactor >Refactor to AndroidX...

A indicator will be shown that Android Studio is scanning the project and is preparing the refactor to AndroidX. When the preparations are complete a Refactoring preview is shown with in detail all proposed changes. Feel free to check them out, click on Do Refactor when you are done. If you don’t want to execute the migration to Android X, click Cancel.

After Android studio did his part it up to you to validate and possible fix any missed items. First run the Sync project with gradle files, execute a clean and rebuild the project.

The following error/warning can be shown:
  • Probably a wrong import
  • Probably deprecated API calls are used
Solve all issues and validate the funcionallity of the app manual or running unit tests / ui tests. This way all open issues can be detected and solved.

Q/A

  • When trying to "Refactor to AndroidX..." this message was shown
    • You need to have at least have compileSdk 28 set in your module build.gradle to refactor to androidx
    • Validate that within build.gradle the compileSdkVersionis set to 28

  • When trying to run the application on a device or emulator
    • Application Installation Failed
      • Installation failed with message Faild to finalize session: INSTALL_FAILED_OLDER_SDK: Failed parse during installPackage .apk (At Binary XML file line #7): Requires development platform P but this is a release platform..
    • Validate that you are running an API 28 / API P device or emulator

  • When you get the message Failed to find target with hash string 'Android-P' in: C:\Android\sdk
    • Validate that the compileSdkVersion has the following value android-P(only the P is uppercase)

  • When Butter Knife is used within the Android project, it can generate the following

Links

Reacties

Populaire posts van deze blog

Android-x86 virtual machine configuration tips

Android and SonarQube with code coverage

Setup SonarQube with Docker