AndroidX Support in React Native Apps
React Native lets you build mobile apps using only JavaScript. It uses the same design as React, letting you compose a rich mobile UI using declarative components.
What Is React Native?
React Native is a JavaScript framework for writing real, natively rendering mobile applications for iOS and Android. It’s based on React, Facebook’s JavaScript library for building user interfaces, but instead of targeting the browser, it targets mobile platforms. In other words: web developers can now write mobile applications that look and feel truly “native,” all from the comfort of a JavaScript library that we already know and love. Plus, because most of the code you write can be shared between platforms, React Native makes it easy to simultaneously develop for both Android and iOS.
Similar to React for the Web, React Native applications are written using a mixture of JavaScript and XML markup, known as JSX. Then, under the hood, the React Native “bridge” invokes the native rendering APIs in Objective-C (for iOS) or Java (for Android). Thus, your application will render using real mobile UI components, not webviews, and will look and feel like any other mobile application. React Native also exposes JavaScript interfaces for platform APIs, so your React Native apps can access platform features like the phone camera, or the user’s location.
React Native currently supports both iOS and Android and has the potential to expand to future platforms as well. In this blog, we’ll cover both iOS and Android. The vast majority of the code we write will be cross-platform. And yes: you can really use React Native to build production-ready mobile applications! Some example: Facebook, Palantir, and TaskRabbit are already using it in production for user-facing applications.
AndroidX Overview
If you haven’t heard the term yet, AndroidX is the new open-source project being rolled out by Google to package librairies with Jetpack. Essentially, the old way of managing librairies was becoming overcomplicated and so Google decided to start cleaning up their act and use a new system, along with new library names.
AndroidX is the new open-source project that the Android team uses to develop, test, package, version and release libraries within Jetpack.
AndroidX is a major improvement to the original Android Support Library. Like the Support Library, AndroidX ships separately from the Android OS and provides backwards-compatibility across Android releases. AndroidX fully replaces the Support Library by providing feature parity and new libraries. In addition AndroidX includes the following features:
- All packages in AndroidX live in a consistent namespace starting with the string
androidx
. The Support Library packages have been mapped into correspondingandroidx.*
packages. For a full mapping of all the old classes and build artifacts to the new ones, see the Package Refactoring page. - Unlike the Support Library, AndroidX packages are separately maintained and updated. The
androidx
packages use strict Semantic Versioning starting with version 1.0.0. You can update AndroidX libraries in your project independently. - All new Support Library development will occur in the AndroidX library. This includes maintenance of the original Support Library artifacts and introduction of new Jetpack components.
Using AndroidX
If you want to use AndroidX in a new project, you need to set the compile SDK to Android 9.0 (API level 28) or higher and set both of the following Android Gradle plugin flags to true
in your gradle.properties
file.
android.useAndroidX
: When set totrue
, the Android plugin uses the appropriate AndroidX library instead of a Support Library. The flag isfalse
by default if it is not specified.android.enableJetifier
: When set totrue
, the Android plugin automatically migrates existing third-party libraries to use AndroidX by rewriting their binaries. The flag isfalse
by default if it is not specified.
android.useAndroidX=true
android.enableJetifier=true
How to Migrate Android Support Packages into AndroidX in React Native 0.59
Step 1
First of all, I opened MyProject/Android/build.gradle and verified I am targeting Android Pie or later. It’s because AndroidX support is available for apps targeting API 28 or later. My build.gradle file was as given below:
Step 2
Then I opened MyProject/Android/gradle.properties and added the following lines
android.useAndroidX=true
android.enableJetifier=true
Step 3
If you use React Native modules with native Java code that isn’t converted to AndroidX, and your app is AndroidX, you probably need jetifier.
The jetifier AndroidX transition tool in npm format, with a react-native compatible style
Why do you need Jetifier?
The standard AndroidX migration rewrites your current installed source code, and at build time dynamically re-writes any linked jar/aar/zip files. This is all a “normal” Android app needs.
React Native apps are not standard Android apps. React Native modules with native Java code usually distribute that code as source, and link the source code directly.
When you update your modules (or install them again after following the standard AndroidX migration), the freshly installed Java code from your react-native dependencies will not be translated to AndroidX anymore, and your build will fail.
So you have to perform an AndroidX migration on your linked source every time you update react-native modules that ship native Java code. That is what this tool does.it can rewrite the source in node_modules every time you call it.
Usage for source files
To jetify / convert node_modules dependencies to AndroidX
Imagine you have a react-native project. One of your library dependencies converts to AndroidX., and you need to use the new version.
So now you need to convert your app to AndroidX, but many of your react-native libraries ship native Java code and have not updated. How is this done?
- First, use Android Studio’s refactoring tool to convert your app re: the Android developer docs
npm install --save-dev jetifier
npx jetify
npx react-native run-android
(your app should correctly compile and work)- Call
npx jetify
run in the postinstall target of your package.json (Any time your dependencies update you have to jetify again)
Proof it works / how this is tested: https://github.com/mikehardy/rn-androidx-demo. You can clone that repo, run the script, and see it works. Please feel to make PRs to that repo, especially in App.js or in the dependencies included, if you would like to demonstrate success or failure for a specific module.
Inspiration: this jetify command was based on an idea from @janicduplessis — thank you Janic!
To reverse-jetify / convert node_modules dependencies to Support Libraries
Maybe you are in the position where you must not migrate to AndroidX yet. But your libraries have started to migrate and they ship AndroidX native Java code.
You can convert them back with reverse-jetify mode
Follow the instructions from above to convert to AndroidX, but add the -r
flag to the npx jetify
call.
If a library ships only as a jar/aar/zip file, you will have to use jetifier-standalone to convert that as well, but you can delay the AndroidX migration indefinitely with this style.
Usage for jar/zip/aar files
You may be a library maintainer, wanting to ship an AAR of your support code converted to AndroidX, or maybe you ship an AAR normally and you want to continue to support your non-AndroidX users even after you convert your library to AndroidX?
As part of your build process you can use this tool like so:
npm install jetifier
(or maybenpm install -g jetifier
to make it globally available)npx jetifier-standalone <your arguments here>
(usenpx jetifier-standalone -h
for help)
Conclusion
In this post, we learnt about How to Migrate Android Support Packages into AndroidX in React Native 0.59. we also learn about what in androidX and jetifier.