vstirbu / InstagramPlugin

Instagram plugin for PhoneGap/Cordova
MIT License
120 stars 78 forks source link

Plugin not working on Android 8.0 #97

Closed rodrigograca31 closed 6 years ago

rodrigograca31 commented 6 years ago

Hi!

This plugin doesn't work on Android 8.0, logcat outputs this:

03-16 18:25:01.899  4884  4884 W zygote  : Attempt to remove non-JNI local reference, dumping thread
03-16 18:25:01.966  4884  4970 E PluginManager: Uncaught exception from plugin
03-16 18:25:01.966  4884  4970 E PluginManager: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/[MY_APP_ID]/files/instagram8756136426115648363.png exposed beyond app through ClipData.Item.getUri()
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.net.Uri.checkFileUriExposed(Uri.java:2348)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.content.ClipData.prepareToLeaveProcess(ClipData.java:941)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.content.Intent.prepareToLeaveProcess(Intent.java:9735)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.content.Intent.prepareToLeaveProcess(Intent.java:9741)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.content.Intent.prepareToLeaveProcess(Intent.java:9720)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.app.Instrumentation.execStartActivity(Instrumentation.java:1609)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.app.Activity.startActivityForResult(Activity.java:4472)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.apache.cordova.CordovaActivity.startActivityForResult(CordovaActivity.java:342)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.app.Activity.startActivityForResult(Activity.java:4430)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.apache.cordova.CordovaInterfaceImpl.startActivityForResult(CordovaInterfaceImpl.java:66)
03-16 18:25:01.966  4884  4970 E PluginManager:         at com.vladstirbu.cordova.CDVInstagramPlugin.share(CDVInstagramPlugin.java:127)
03-16 18:25:01.966  4884  4970 E PluginManager:         at com.vladstirbu.cordova.CDVInstagramPlugin.execute(CDVInstagramPlugin.java:73)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:57)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
03-16 18:25:01.966  4884  4970 E PluginManager:         at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:41)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.os.Handler.dispatchMessage(Handler.java:105)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.os.Looper.loop(Looper.java:164)
03-16 18:25:01.966  4884  4970 E PluginManager:         at android.os.HandlerThread.run(HandlerThread.java:65)
03-16 18:25:01.968  4884  4884 W cr_Ime  : updateState: type [0->0], flags [0], show [false], 

The problem seems to be easy to solve but Java is not my forte..... https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed

Thanks

rodrigograca31 commented 6 years ago

It was fixed in the past for Android 7.... https://github.com/vstirbu/InstagramPlugin/commit/ebba2d1fd1a590a967066fa27747fa6952b24232

But the problem is still reproducible in Android 8 (API 26)

rodrigograca31 commented 6 years ago

Bounty of $50 created for this issue: https://www.bountysource.com/issues/56192821-plugin-not-working-on-android-8-0

albertleao commented 6 years ago

+1

Unsigno commented 6 years ago

I think there is solved on https://github.com/vstirbu/InstagramPlugin/pull/98 , can i claim now ?? :)

albertleao commented 6 years ago

Does this fix also maintain backwards compatibility with android 7 and below?

rodrigograca31 commented 6 years ago

(replying from my phone.....) I haven't tested but it should keep backwards compatibility.... I will test tomorrow or so and give the reward if it works.....

On Sun, Mar 25, 2018, 19:00 Albert Leao notifications@github.com wrote:

Does this fix also maintain backwards compatibility with android 7 and below?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vstirbu/InstagramPlugin/issues/97#issuecomment-375990116, or mute the thread https://github.com/notifications/unsubscribe-auth/ABFO5kEUDc7Owb2KoXkPygfk7-vfCTKVks5th9tNgaJpZM4SuQEw .

Unsigno commented 6 years ago

To be honest , I have not built it because I do not have the Cordova environment ready, nor instagram, but I know the error and this should solve the problem in Android 8 and maintain the previous compativility .

albertleao commented 6 years ago

@Unsigno , I have tested your branch (https://github.com/Unsigno/InstagramPlugin/tree/patch-1) and it is not working.

Following error:

03-26 03:17:03.461 17976-17976/com.app.test E/ViewRootImpl: sendUserActionEvent() returned.
03-26 03:17:04.026 17976-18798/com.app.test W/ContextImpl: Failed to ensure /dev/null/Android/data/com.app.test/files: -1
03-26 03:17:04.040 17976-18798/com.app.test E/PluginManager: Uncaught exception from plugin
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.app.test/files/instagram7979292317488596937.png exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
at android.net.Uri.checkFileUriExposed(Uri.java:2348)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:944)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10480)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10486)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10465)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1616)
at android.app.Activity.startActivityForResult(Activity.java:4564)
at org.apache.cordova.CordovaActivity.startActivityForResult(CordovaActivity.java:342)
at android.app.Activity.startActivityForResult(Activity.java:4522)
at org.apache.cordova.CordovaInterfaceImpl.startActivityForResult(CordovaInterfaceImpl.java:66)
at com.vladstirbu.cordova.CDVInstagramPlugin.share(CDVInstagramPlugin.java:128)
at com.vladstirbu.cordova.CDVInstagramPlugin.execute(CDVInstagramPlugin.java:73)
at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:57)
at org.crosswalk.engine.XWalkExposedJsApi.exec(XWalkExposedJsApi.java:40)
at org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
at org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:39)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
albertleao commented 6 years ago

I've never coded in Java, but from the research I've done it looks like there will have to be a change made to the android.manifest + provider_paths.xml for the new FileProvider in Android 7 to work.

Eventually, something like this should work:

FileProvider FileProvider = new FileProvider();

Uri photoURI = FileProvider.getUriForFile(
this.cordova.getActivity().getApplicationContext(),
this.cordova.getActivity().getApplicationContext() + this.cordova.getActivity().getPackageName() + ".provider",
file);

But again, that's about the limit of my Java knowledge.

rodrigograca31 commented 6 years ago

@Unsigno, @albertleao is right. it's not working.

I moved the plugin to a folder outside the project, added your change and compiled and the bug still exists.... I will trying again just to make sure I'm not missing something but @albertleao also confirmed it doesnt solve it....

I'm in the same camp as him.... not comfortable with Java.... maybe I could eventually solve it but I'm not sure how to go about those changes in the XML files.... They seem necessary to solve this......

albertleao commented 6 years ago

@rodrigograca31 Can you tell me if you have the following in your installation?

In your AndroidManifest.xml:

<provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
            <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>

and in your res/xml/provider_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

I have them in my install but I'm worried they are being created by another plugin and my current fix which I have working now won't apply to everyone.

Edit: My fix has only been tested on Android 7 and my app isn't compatible with my only other android device which is running android 5. Will need someone to test that.

rodrigograca31 commented 6 years ago

@albertleao searching for android:authorities in AndroidManifest.xml gives me this 2 results:

<provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" />
</provider>
<provider android:authorities="com.MY-PACKAGE.sharing.provider" android:exported="false" android:grantUriPermissions="true" android:name="nl.xservices.plugins.FileProvider">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/sharing_paths" />
</provider>
albertleao commented 6 years ago

Thanks @rodrigograca31 . I think I got it figured out, do you have an android 6 or below device we can test on?

rodrigograca31 commented 6 years ago

and res/xml/provider_paths.xml has this:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>
rodrigograca31 commented 6 years ago

I have Android 4.4, 5.1 and 8 Then I can set up emulators for 6 and 7 with a few clicks and some waiting..... :joy:

albertleao commented 6 years ago

Give it a shot for me https://github.com/albertleao/InstagramPlugin/tree/android_nougat_patch

rodrigograca31 commented 6 years ago

OK, testing.

rodrigograca31 commented 6 years ago

It's not compiling (ionic cordova run android) but I implemented the changes by hand.... let me download your code and do it the correct way............ but here is the error:

                FileProvider FileProvider = new FileProvider();
                ^
  symbol:   class FileProvider
  location: class CDVInstagramPlugin
/home/unknown/git/APP/platforms/android/src/com/vladstirbu/cordova/CDVInstagramPlugin.java:132: error: cannot find symbol
                FileProvider FileProvider = new FileProvider();
                                                ^
  symbol:   class FileProvider
  location: class CDVInstagramPlugin
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
2 errors

FAILURE: Build failed with an exception.
albertleao commented 6 years ago

Are you using Android Studio? I had that sem error at first then I changed one more line in the file and it 'synced' and started working. I think i added a random space. I'm kinda shooting from the hip with this java stuff.

rodrigograca31 commented 6 years ago

I just downloaded your repo and added directly and compiled! Seems not to work yet......... (not share to Instagram) let me try another way...........

rodrigograca31 commented 6 years ago

Wait................. Actually compiled because I downloaded the master branch.... fail... Downloading the correct branch produces that error above....

albertleao commented 6 years ago

Yea. I'm trying to figure out how to import the FileProvider library correctly. If you add it after compile, everything works.

Unsigno commented 6 years ago

Sorry guys , you are right with the FileProvider , i just missinterpreted a comment , thinking we just need set the flag . Looks you are solving that , if you want me to try again let me know ;)

albertleao commented 6 years ago

I just pushed an update to https://github.com/albertleao/InstagramPlugin/tree/android_nougat_patch

It's compiling on my end on android 7. @rodrigograca31 can you confirm with your tests?

rodrigograca31 commented 6 years ago

@albertleao 1st: not sure why you were complaining about nougat (android 7) because for me worked in 7 when I tested 1-2 weeks ago in a emulator... 2nd: used your first commit and tryed on my own to make the import (reading the docs) and just added import android.support.v4.content.FileProvider; to your first commit and works for me..... (Android 8 Oreo)

albertleao commented 6 years ago

Haha, my fault. I've been referring to Nougat as if it were Oreo.

I'll redo my commits with the right messages.

My 2nd commit ensures that when you add the plugin to cordova it references the correct libraries. I was having issues with that in my first commit

rodrigograca31 commented 6 years ago

And you repeat code in your first commit inside the if/else.... it would be better if that was cleaned and updated the package version, etc for the plugin creator to accept a PR.....

but first I will try that commit with my changes in 4.4, 5.1, 6, 7 and 8.......

rodrigograca31 commented 6 years ago

Oh........ ok.........

albertleao commented 6 years ago

Doing that now, give me a few.

rodrigograca31 commented 6 years ago

Ok, I'm testing in 4.4 and 5 atm. Then 6 and 7 will take a while do spin up the emulator install Instagram and test....

albertleao commented 6 years ago

Alright @rodrigograca31 . Sorry about my brainfart.

here's a new branch with cleaned up code, a version bump to 0.5.6. Everything compiled fine and worked with my Galaxy s8 on 8.0

https://github.com/albertleao/InstagramPlugin/tree/android_oreo_patch

rodrigograca31 commented 6 years ago

Ok, I will test that on all the things and report back maybe in 1-2 hours or tomorrow......

albertleao commented 6 years ago

I just tested on an emulator running Nougat and on my phone running Oreo and both are working.

rodrigograca31 commented 6 years ago

Tested on Android 4.4 and 8 real devices and all of this emulators: screenshot-window-2018-03-27-021947 and it's working! YAY! :smile: @albertleao would you mind registering in bountysource and claiming the reward? (3AM here, I'm heading out)

rodrigograca31 commented 6 years ago

Also, send a PR for the main plugin......

albertleao commented 6 years ago

Great! I'll do that and I'll make the pull request back to the main repo. I'll keep this up until then.

Have a great night!

albertleao commented 6 years ago

@vstirbu please merge #99

vstirbu commented 6 years ago

also published in npm. great job!

rodrigograca31 commented 6 years ago

Yay! I just spend 3 hours trying to make my ionic project work......... :cry: I updated Android Studio and it updated gradle and everything went bananas...... :cry:

Even almost solved one of my Rubik's cubes by accident while waiting for downloads/compiles :joy:

@albertleao bountysource says: "The bounty can be claimed once the issue gets closed on the original issue tracker. When a bounty claim is submitted, all Backers will be notified." you should be able to claim now and I will accept :)

albertleao commented 6 years ago

@rodrigograca31 glad you got it working. I have claimed the bounty.

rodrigograca31 commented 6 years ago

Accepted. Enjoy! :)

rodrigograca31 commented 6 years ago

hummmmmmm. Not sure what I've done but everything was working and I even deployed a new version of my app to the Play Store already.........

Then I basically removed and added cordova back: ionic cordova platform rm android && ionic cordova platform add android@6.2.3 and did ionic cordova run android and the app crashes on launch....

adb logcat:

03-27 18:37:31.533  2488  2488 D AndroidRuntime: Shutting down VM
03-27 18:37:31.538  2488  2488 E AndroidRuntime: FATAL EXCEPTION: main
03-27 18:37:31.538  2488  2488 E AndroidRuntime: Process: MY_APP_ID, PID: 2488
03-27 18:37:31.538  2488  2488 E AndroidRuntime: java.lang.RuntimeException: Unable to get provider com.vladstirbu.cordova.plugin.utils.AssetProvider: java.lang.ClassNotFoundException: Didn't find class "com.vladstirbu.cordova.plugin.utils.AssetProvider" on path: DexPathList[[zip file "/data/app/MY_APP_ID-zBBIXHndes8kSG-p2dOqZw==/base.apk"],nativeLibraryDirectories=[/data/app/MY_APP_ID-zBBIXHndes8kSG-p2dOqZw==/lib/arm64, /system/lib64, /system/vendor/lib64]]
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread.installProvider(ActivityThread.java:6548)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread.installContentProviders(ActivityThread.java:6094)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6015)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread.-wrap1(Unknown Source:0)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1756)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:105)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:164)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6809)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
03-27 18:37:31.538  2488  2488 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "com.vladstirbu.cordova.plugin.utils.AssetProvider" on path: DexPathList[[zip file "/data/app/MY_APP_ID-zBBIXHndes8kSG-p2dOqZw==/base.apk"],nativeLibraryDirectories=[/data/app/MY_APP_ID-zBBIXHndes8kSG-p2dOqZw==/lib/arm64, /system/lib64, /system/vendor/lib64]]
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        at android.app.ActivityThread.installProvider(ActivityThread.java:6533)
03-27 18:37:31.538  2488  2488 E AndroidRuntime:        ... 10 more

removing this from the plugin makes it work:

<config-file target="AndroidManifest.xml" parent="/manifest/application">
        <provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="com.vladstirbu.cordova.plugin.utils.AssetProvider">
          <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/instagramplugin_provider_paths" />
        </provider>
    </config-file>

but I think that's only because another plugin cordova-plugin-camera does basically the same.........

<config-file target="AndroidManifest.xml" parent="application">
          <provider
              android:name="android.support.v4.content.FileProvider"
              android:authorities="${applicationId}.provider"
              android:exported="false"
              android:grantUriPermissions="true" >
              <meta-data
                  android:name="android.support.FILE_PROVIDER_PATHS"
                  android:resource="@xml/provider_paths"/>
          </provider>
        </config-file>
rodrigograca31 commented 6 years ago

After typing that I think we need to add another <source-file.... for the AssetProvider.java file to be copied because its probably not copied and is missing....? Let me try to add that.....

rodrigograca31 commented 6 years ago

Yep, adding

<source-file src="src/android/utils/AssetProvider.java" target-dir="src/com/vladstirbu/cordova" />

after:

<source-file src="src/android/CDVInstagramPlugin.java" target-dir="src/com/vladstirbu/cordova" />

makes it work... and makes sense.... not sure how it was working before........? @albertleao opinion? should you add this line ...? thanks.

rodrigograca31 commented 6 years ago

@albertleao ?

robertovanderlinden commented 6 years ago

It is still not working for me.. I have added the source-file line as @rodrigograca31 suggested in the plugin.xml.

05-29 16:59:34.278 23282-23282/? E/UncaughtException: java.lang.RuntimeException: Unable to get provider com.vladstirbu.cordova.plugin.utils.AssetProvider: java.lang.ClassNotFoundException: Didn't find class "com.vladstirbu.cordova.plugin.utils.AssetProvider" on path: DexPathList[[zip file "/data/app/com.obi4wan.instagram-E6QuXbWUe_WYgZzEBXFz3A==/base.apk"],nativeLibraryDirectories=[/data/app/com.obi4wan.instagram-E6QuXbWUe_WYgZzEBXFz3A==/lib/arm64, /system/lib64, /vendor/lib64]]

Any ideas?

rodrigograca31 commented 6 years ago

Humm, did you remove and add the android platform so that it does the entire process again?

robertovanderlinden commented 6 years ago

... You are right! That worked.. hopefully will this fix be added soon. @albertleao

rodrigograca31 commented 6 years ago

Just made a PR to fix this..........