Capacitor: Use Android X dependencies

Created on 9 Sep 2019  路  17Comments  路  Source: ionic-team/capacitor

Next major should be updated to use Android X (dependencies and code usage on @capacitor/android should be updated).

See if jetifier works on cordova/capacitor plugins, if not, the CLI should remap cordova plugins to use Android X on the update process, remapping dependencies and patching the code usages, and we might want to create a tool that works like jetifier that for Capacitor plugins.

enhancement android high

Most helpful comment

according to a message in Capacitor slack, jetifier is able to patch plugins and even @capacitor/android

Mike Roberts
While we wait for 2.0 to add Android X support, just add this to gradle.properties
android.enableJetifier=true
android.useAndroidX=true
And if you have issues with stuff in node_modules not using android x then install jetifier npm i -D jetifier and add a postinstall hook in package.json under scripts and run npm install again
"postinstall": "jetifier -r"

Going to unlock the thread so people can comment if they find issues with this approach on Cordova or Capacitor issues, but please, keep it on topic or I'll lock it again.

All 17 comments

just FYI i updated a 1.1.1 project to use Android X and updated all dependencies including Firebase and all looks good so far

@nikmartin How did you do that?

@nikmartin I would also like to know how you did that because I tried incorporating the Firebase Analytics sdk (17.2.0) which uses Android X and was unable to build it without some kind of error.

please people, don't hijack the thread for your own questions

according to a message in Capacitor slack, jetifier is able to patch plugins and even @capacitor/android

Mike Roberts
While we wait for 2.0 to add Android X support, just add this to gradle.properties
android.enableJetifier=true
android.useAndroidX=true
And if you have issues with stuff in node_modules not using android x then install jetifier npm i -D jetifier and add a postinstall hook in package.json under scripts and run npm install again
"postinstall": "jetifier -r"

Going to unlock the thread so people can comment if they find issues with this approach on Cordova or Capacitor issues, but please, keep it on topic or I'll lock it again.

Hmm I just tried the approach, didn't work at all for me:

I did the following, after doing npm i the jetifier did some stuff:

> jetifier -r

Jetifier found 170 file(s) to reverse-jetify. Using 8 workers...

However once I opened Android Studio and tried to build I still got this:
image
=> the Plugin.java is also in the node_modules folder...but it looks like jetfier didn't modify / touch it...

@Nasicus use jetifier without -r

I see... what is correct now? With -r and without is a big difference:

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.

However without the -r I seem to have more success, but it still doesn't work. I can now build it again in Android Studio and then even deploy/install it on the phone, however there it opens and closes immediately and from the log cat I see the following:

    java.lang.RuntimeException: Unable to get provider android.support.v4.content.FileProvider: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/ch.cycl.capsal-trk9mT-ootskpmX4v0v56g==/base.apk"],nativeLibraryDirectories=[/data/app/ch.cycl.capsal-trk9mT-ootskpmX4v0v56g==/lib/arm64, /system/lib64]]
        at android.app.ActivityThread.installProvider(ActivityThread.java:6625)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6150)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6056)
        at android.app.ActivityThread.access$1300(ActivityThread.java:207)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1758)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

Here is the sample project in which I tried to make it work (see the commit log for the commands I executed).
https://github.com/Nasicus/capsal/commit/4ac736de0a766eee459f26510716bf6e3893e44e

without -r it patchs node_modules for AndroidX
-r for reverse - patch for support lib.
@Nasicus now you have error in project native code
find string android.support.v4.content.FileProvider in android/app/src/main/AndroidManifest.xml and change it to androidx.core.content.FileProvider

@Raerten Thanks again. I actually did it via Android Studio: Refactor => Migrate to Android X....
Here are the changed files, for anyone who cares: https://github.com/Nasicus/capsal/commit/ddf75a43ee84314645253e17cd7c2e9cf32bad12

Anyway after this I could indeed build & install the app again.

Now I just have to do it with my real application, pray to god (for safety maybe sacrifice a baby goat) and hope that it works too...

Hi
Jetifier without "-r" works for me
"postinstall": "jetifier"

Migrate to androidX will work on one system. After committing files nex env will not work and another Migrate will fail.

Ah yeah, without the -r is the proper way. Was looking at my react-native app when I typed that which needs the reverse, my bad!

Well, I struggled with this 2 days now thanks to this thread I got it working, with androidX set to true.
Jetifier works for sure but its a bit sad we needs to do all the extra work and debugging all the time to make it work Capacitor is not supporting androidx per out of the box but ah well thanks guys for the help!

@Raerten Thanks again. I actually did it via Android Studio: Refactor => Migrate to Android X....
Here are the changed files, for anyone who cares: Nasicus/capsal@ddf75a4

Anyway after this I could indeed build & install the app again.

Now I just have to do it with my real application, pray to god (for safety maybe sacrifice a baby goat) and hope that it works too...

Work For Me thankssss

This isn't working for me. When I run npx jetifier it prints Jetifier found 885 file(s) to forward-jetify. Using 8 workers...

But I don't see any change and I'm still having the same issue

Run npx cap update after jetifier
jetifier patches node_modules, but cordova plugin files get copied to the android folder, you need to run update to copy the patched files

Add below lines to gradle.properties to overcome this error

android.useAndroidX=true
android.enableJetifier=true

Was this page helpful?
0 / 5 - 0 ratings