pwlin / cordova-plugin-file-opener2

A File Opener Plugin for Cordova
MIT License
314 stars 584 forks source link

AndroidX support #256

Closed timbru31 closed 1 year ago

timbru31 commented 5 years ago

Expected Behaviour

The plugin is compatible with AndroidX (as discussed here: https://github.com/pwlin/cordova-plugin-file-opener2/issues/251#issuecomment-467488576)

Actual Behaviour

Plugin is not compatible with AndroidX.

Reproduce Scenario (including but not limited to)

Steps to Reproduce

  1. Install the plugin and build it for Android.
  2. As cordova-android does not yet support AndroidX (see https://github.com/apache/cordova-android/issues/565), open the project manually with Android Studio.
  3. Manually migrate to AndroidX (see https://developer.android.com/jetpack/androidx/migrate#migrate)
  4. The refactoring has to be done manually:

image

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

n/a

Cordova CLI info

cordova info

Here is the output:

n/a

Plugin version

cordova plugin version | grep cordova-plugin-file-opener2

Here is the output:

2.2.0

Sample Code that illustrates the problem

The current code [...] extends android.support.v4.content.FileProvider { needs to be updated to androidx.core.content.FileProvider.

This is a breaking change and will require a major version bump.

Logs taken while reproducing problem

n/a

Background info

Sadly, since Cordova plugins ship the .java instead of the .class files the automatic migration (jettifier, see: https://developer.android.com/jetpack/androidx#using_androidx) does not work and the plugin authors need to update their plugins.

timbru31 commented 5 years ago

Good news! dpa99c just released two plugins, one to enable AndroidX support, one to fill in the compatibility gap: https://github.com/dpa99c/cordova-diagnostic-plugin/issues/350#issuecomment-485551917

pwlin commented 5 years ago

hi @timbru31 thanks that's great news! I will try to look into that comment and merge the changes back in here.

mg commented 4 years ago

Hi is there any news on this issue?

timbru31 commented 4 years ago

You can use the cordova-plugin-androidx-adapter and cordova-plugin-androidx plugins in the meantime to solve this issue

mg commented 4 years ago

that solved it for me, thank you

pwlin commented 4 years ago

@timbru31, @mg How do you go solving this issue? I have looked into the plugin mentioned but I am not sure what do you. Is this just a matter of changing the FileProvider class to extend androidx.core.content.FileProvider, instead of android.support.v4.content.FileProvider? I appreciate your suggestions.

Update: Is #292 something that would completely fix this issue?

mg commented 4 years ago

No, I just installed the 2 plugins, that took care of it for me

timbru31 commented 4 years ago

The plugins do two things:

As a plugin author, I'd do the following:

  1. Wait until cordova-android@9 is released with AndroidX support out of the box (https://github.com/apache/cordova-android/pull/901)
  2. Upgrade my plugin code to use only AndroidX
  3. Release a new major version which requires cordova-android@9

Users should be able to either just use cordova-android@9 or cordova-android@8 with cordova-plugin-androidx.

timbru31 commented 4 years ago

Update: Is #292 something that would completely fix this issue?

This forces all your users to install two plugins from another author or wait until cordova-android@9 is released - or breaks the plugin for them in case they can't migrate to AndroidX (yet).
But yes, technically it would close this issue.

pwlin commented 4 years ago

Thank you for the explanation. I think at the moment the best way to handle this is to add cordova-plugin-androidx and cordova-plugin-androidx-adapter to one's project. I will add a note to readme.md about how to support AndroidX, linking to those 2 plugins.

StefanVanStaden commented 4 years ago

Hi, I am having this issue as well but I am using Capacitor not Cordova. So I installed cordova-plugin-androidx and cordova-plugin-androidx-adapter using npm and when I synced with the Android project it still came up with the issue. Every time I make a build I need to manually change

public class FileProvider extends android.support.v4.content.FileProvider { }

to the following

public class FileProvider extends androidx.core.content.FileProvider { }

I would imagine this change will be required for anyone who keeps there projects up to date with the latest technology. If not then they simply need to use the lock file and lock this package to where they need it. I will not be able to compile my application using CI/CD since I will need to manually come and change this code after every Ionic build.

davidmarquis commented 3 years ago

@StefanVanStaden Looks like using Jetifier with Capacitor has cut it for me. I just followed the instructions here: https://capacitorjs.com/docs/android/troubleshooting#error-package-android-support-does-not-exist

In short, I ran:

npm install jetifier
npx jetify
npx cap sync android

And the subsequent automated builds ran correctly.

vikshv commented 3 years ago

The plugins do two things:

As a plugin author, I'd do the following:

  1. Wait until cordova-android@9 is released with AndroidX support out of the box (apache/cordova-android#901)
  2. Upgrade my plugin code to use only AndroidX
  3. Release a new major version which requires cordova-android@9

Users should be able to either just use cordova-android@9 or cordova-android@8 with cordova-plugin-androidx.

Cordova 10.0.0 released with cordova-android@9.0.0: https://cordova.apache.org/announcements/2020/07/28/cordova-lib-release-10.0.0.html

Now my build failed with cordova@10.0.0 and cordova-android@9.0.0. Required android.support.v4.content

AndroidXEnabled = true

richardkshergold commented 3 years ago

When I build my Ionic 5 Capacitor app locally the app builds ok but when I build up on AppFlow the build fails with the error

android.support.v4.content does not exist against the FileOpener2 plugin.

The ionic docs suggest adding jetifier and running npx jetify so I have installed it and added the following to the package.json scripts section:

"postinstall": "npx jetify"

Unfortunately the builds still fail on AppFlow with an error of:

npm WARN lifecycle myApp@2.9.9~postinstall: cannot run in wd myApp@2.9.9 npx jetify (wd=/builds/myName/myApp)

I then tried with these two plugins instead (as per the docs):

cordova-plugin-androidx
cordova-plugin-androidx-adapter

However when I add these and try to build on AppFlow I get a failure with the original error again:

android.support.v4.content does not exist

Does this mean I am not going to be able to use this plugin with AppFlow?

almothafar commented 3 years ago

It is a blocked issue for me now!

> Task :app:compileReleaseJavaWithJavac FAILED
/app/platforms/android/app/src/main/java/io/github/pwlin/cordova/plugins/fileopener2/FileProvider.java:28: error: package android.support.v4.content does not exist
public class FileProvider extends android.support.v4.content.FileProvider {
                                                            ^
/app/platforms/android/app/src/main/java/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java:119: error: cannot find symbol
                                                path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);
                                                                   ^
  symbol:   method getUriForFile(Context,String,File)
  location: class FileProvider
/app/platforms/android/app/src/main/java/io/github/pwlin/cordova/plugins/fileopener2/FileOpener2.java:127: error: cannot find symbol
                                        Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);
                                                               ^
  symbol:   method getUriForFile(Context,String,File)
  location: class FileProvider
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors

FAILURE: Build failed with an exception.
almothafar commented 3 years ago

Any update or better fork for this?

patriciopage commented 3 years ago

@StefanVanStaden Looks like using Jetifier with Capacitor has cut it for me. I just followed the instructions here: https://capacitorjs.com/docs/android/troubleshooting#error-package-android-support-does-not-exist

In short, I ran:

npm install jetifier
npx jetify
npx cap sync android

And the subsequent automated builds ran correctly.

This worked for me. Without installing the androidx plugins (cordova-plugin-androidx and cordova-plugin-androidx-adapter) I ran those three commands and now my FileProvider class from cordova FileOpener2 extends androidx.core.content.FileProvider.

Thanks!

tshesko1 commented 3 years ago

@StefanVanStaden Looks like using Jetifier with Capacitor has cut it for me. I just followed the instructions here: https://capacitorjs.com/docs/android/troubleshooting#error-package-android-support-does-not-exist In short, I ran:

npm install jetifier
npx jetify
npx cap sync android

And the subsequent automated builds ran correctly.

This worked for me. Without installing the androidx plugins (cordova-plugin-androidx and cordova-plugin-androidx-adapter) I ran those three commands and now my FileProvider class from cordova FileOpener2 extends androidx.core.content.FileProvider.

These 3 lines of codes from https://capacitorjs.com/docs/android/troubleshooting#error-package-android-support-does-not-exist just saved my projects. Thank You !

almothafar commented 3 years ago

Running npx jetify in ionic cordova project solved my issue, just before I build the app.

@richardkshergold you seems found your solution here: https://forum.ionicframework.com/t/android-support-v4-content-does-not-exist-appflow-build-issue/194978 just need to link this thread :)

KirstenStake commented 2 years ago

@richardkshergold did you ever solve this for AppFlow? Am experiencing the same issue. Build manually works with the jetifier plugin etc but AppFlow fails

richardshergold commented 2 years ago

Hi Kirsten. Yes I did with the help of an Ionic engineer. The solution was to place a .npmrc file in the root of the project with the following in it:

unsafe-perm=true

I hope that works for you too.

KirstenStake commented 2 years ago

@richardshergold thank you for your speedy reply.

for anyone else, to summarise entire steps:

As a workaround, you can patch the plugin using jetifier:

npm install jetifier
npx jetify
npx cap sync android

The jetifier should be in package.json file.

You need to include a postinstall script in package.json for jetifier to run. This can be done as shown below:

"scripts": {
...
"postinstall": "jetifier"
}

In the file named .npmrc (create the file if not already present) in the root of your project, add the following contents. This will give npm running the jetifier script permissions. unsafe-perm=true

shnist commented 1 year ago

androidX is now supported in the latest release