Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Android X dependencies #1937

Closed
jcesarmobile opened this issue Sep 9, 2019 · 19 comments · Fixed by #2454
Closed

Use Android X dependencies #1937

jcesarmobile opened this issue Sep 9, 2019 · 19 comments · Fixed by #2454

Comments

@jcesarmobile
Copy link
Member

jcesarmobile commented Sep 9, 2019

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.

@nikmartin
Copy link

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

@unclebumpy
Copy link

@nikmartin How did you do that?

@kyleabens
Copy link

@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.

@jcesarmobile
Copy link
Member Author

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

@jcesarmobile
Copy link
Member Author

jcesarmobile commented Oct 29, 2019

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.

@Nasicus
Copy link

Nasicus commented Nov 15, 2019

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...

@Raerten
Copy link

Raerten commented Nov 15, 2019

@Nasicus use jetifier without -r

@Nasicus
Copy link

Nasicus commented Nov 15, 2019

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).
Nasicus/capsal@4ac736d

@Raerten
Copy link

Raerten commented Nov 15, 2019

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

@Nasicus
Copy link

Nasicus commented Nov 15, 2019

@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...

@Karrlllis
Copy link

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.

@mike-roberts
Copy link
Contributor

mike-roberts commented Dec 5, 2019

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!

@donjon1234
Copy link

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!

@andirsun
Copy link

@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

@jeanlambert17
Copy link

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

@jcesarmobile
Copy link
Member Author

jcesarmobile commented Jun 10, 2020

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

@thesheikhwasim
Copy link

thesheikhwasim commented Sep 6, 2020

Add below lines to gradle.properties to overcome this error

android.useAndroidX=true
android.enableJetifier=true

@karthickbadrick
Copy link

image
i tried many methods but cannot resolve this issue plz give any solution

@nikmartin
Copy link

nikmartin commented Sep 21, 2022

@karthickbadrick you should open a new issue, not reply to this three year old, closed, unrelated issue

@ionic-team ionic-team locked as resolved and limited conversation to collaborators Sep 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.