agamemnus / cordova-plugin-xapkreader

Easily access Google Play APK expansion file data.
32 stars 55 forks source link

Not allowed to load local resource: content://com.example/assets/new.png #140

Open devtechvalens opened 4 years ago

devtechvalens commented 4 years ago

Hello,

I am using this plugin to generate expansion apk of my ionic project for Android.

I am using this plugin - https://github.com/erobertson42/cordova-plugin-xapkreader/tree/cordova-9

I successfully able to make the build of project but the problem is the images from .obb file cannot loaded while running the app in device.

This way I am testing my application- 1)Paste the .obb file in location - /Internal storage/Android/obb/{your.apps.package.name}/ 2)Run the application in Android device.

The name format of .obb file is - main.{version}.{your.apps.package.name}.obb

I followed each and every step present in the readme doc of this repo - https://github.com/erobertson42/cordova-plugin-xapkreader/tree/cordova-9

Still getting error

Not allowed to load local resource: content://com.example/assets/new.png

Here is my system environment details -

cordova version : 9.0.0
android platform - 7.0.0

I am using android platform 7.0.0 because the build failed with android version - 7.1.4/8.0.0 and the XApk reader plugin throws error that was solved with android 7.0.0 version

If anyone know what I am doing wrong. please give suggestion I think I am missing some small things.

Thanks

erobertson42 commented 4 years ago

Please take no offense if any of the following suggestions sound "simple" or I'm repeating things you've already mentioned. I'm just covering the bases for clarity, and some of these issues I ran into myself while trying to get this working for the first time.

  1. For the .obb file name, you are replacing "{version}" with the android-versionCode value from your config.xml, and not version, correct?
  2. Be sure the .obb file is placed in the correct folder on the device. When I first attempted this, my device had 2 paths that were very similar and I was putting it in the wrong one. I didn't realize until I started poking around the filesystem with adb. I'm not sure how you're currently going about getting the .obb file onto the device, but I just use this command:
    adb push filename.obb /sdcard/Android/obb/[package]/filename.obb
  3. Did the following code get successfully added to the file platforms/android/app/src/main/res/xml/config.xml?
    <access origin="content:///*" />
    <access origin="cdvfile:///*" />
  4. Are you also using the plugin cordova-plugin-ionic-webview, and if so, is it up to date? It blocked loading local urls for security reasons, but they added a work-around in version 3.0.0. Here's more info: https://github.com/ionic-team/cordova-plugin-ionic-webview/pull/242
  5. You can also try adding something like the following to the <head> of your HTML (although I didn't need to, Ionic might be different):
    <meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; media-src *; img-src * filesystem: data:">

I hope something in there is of some help. Also, I'm curious, what was the build error when using cordova-android 7.1.4/8.0.0?

agamemnus commented 4 years ago

Thanks for helping this guy!

devtechvalens commented 4 years ago

Thanks for replying.

I am replying on each of your suggestion points and the error that I got -

1)In Ionic config.xml there is no keyword - android-versionCode, only we have version on widget tag. So I am using the version value while naming the .obb file name - 
main.{version}.{your.apps.package.name}.obb

What I tried - 

I added android-versionCode in config.xml file the value of android-versionCode is like this - 0030 and the {version} in .obb file name is also - 0030 that means on Google Playstore my app version shows 0.0.30

But When I used this plugin for the same project previously, I was successfully got the images from version value of placed in config.xml that was - 0.0.29 and in .obb file naming format for {version} I put 0.0.29

2)As you mentioned the path to placed .obb file is inside Sdcard but when I download my application from Google Play store it will placed the .obb file inside device Internal Storage.
 Here is the path - /Internal Storage/Android/obb/[package]/filename.obb 
Is there any problem if I place .obb file inside mobile Internal Storage?

What I tried- I placed .obb file inside sdcard but the images aren't loaded.

3)Yes the code is successfully added to this path - platforms/android/app/src/main/res/xml/config.xml

4)Yes, I am using cordova-plugin-ionic-webview and getting error in latest version so I changed the plugin version to 1.2.1. 
But this was not work and shows this error Not allowed to load local resource: What I tried- 
I update cordova-plugin-ionic-webview plugin version to 3.0.0 but getting same error.

5)I added this code

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'; media-src *; img-src * filesystem: data:">

in my project index.html

Along with this I also have this code-

  <meta http-equiv="Content-Security-Policy" content="* * 'self' default-src 'unsafe-inline' 'unsafe-eval' http://* https://* data: cdvfile://* content://*;">

in my index.html While debugging I getting this error also -

Errors - Unrecognized Content-Security-Policy directive '*'

Here is the code I am using in my html file for showing images, please have a look-

<img src="content://com.example/assets/new.png">

this is correct, right? Is there anything else that causes the error? And I will share soon the build error with android - 7.1.4/8.0.0.

erobertson42 commented 4 years ago

1. I gave you slightly bad advice here. The android-versionCode attribute in the config.xml file is optional, however, if you do not specify one, it will be generated for you. Instead, remove it from config.xml, rebuild your app, and look for the android:versionCode attribute of the <manifest> tag in the file platforms/android/app/src/main/AndroidManifest.xml. Use this value as the replacement for {version} in your .obb file name. (more info)

2. You may have the path correct, I suppose it can be different on various devices. On every device I tested, including the Android Studio emulator, it was always /sdcard (which technically was the internal storage, so it's confusing why it was always mounted it to "/sdcard").

5. Unfortunately, I'm not familiar enough with the syntax of the Content-Security-Policy meta tag to give much advice, I found the example I gave you in a forum post. However, you should probably only have one Content-Security-Policy tag.

Here is the code I am using in my html file for showing images, please have a look- <img src="content://com.example/assets/new.png">

In your .obb file, the file structure should match what you have after content://com.example/. So in the root, there should be an assets folder, and inside that should be a file called new.png. Also, the .obb zip file should not be compressed, just "stored".

devtechvalens commented 4 years ago

Hello I am still getting this issue.

agamemnus commented 4 years ago

Is there a way for you to test without Ionic?

devtechvalens commented 4 years ago

Unfortunately no, as I am using this plugin only for ionic project.

agamemnus commented 4 years ago

But can you? Just to find out what the issue is.

devtechvalens commented 4 years ago

I am trying to debug the exact issue but not find it yet. What I got on debugging is just the error - Not allowed to load local resources. I am checking if any other plugin is causing this issue.

Can you please let me know that this plugin is perfectly working with android platform version 7.0.0 ? or I have to use this plugin with only specific android platform version?

agamemnus commented 4 years ago

6.5... Check some of the issues...

devtechvalens commented 4 years ago

Are you trying to say that I have to use Xapk reader plugin version - 6.5.0 ? If yes, then I am facing issues in building my project that's why I am using the new version - https://github.com/erobertson42/cordova-plugin-xapkreader/tree/cordova-9

I am getting exact same error - https://github.com/agamemnus/cordova-plugin-xapkreader/issues/129

And it was solved with this version - https://github.com/erobertson42/cordova-plugin-xapkreader/tree/cordova-9.

agamemnus commented 4 years ago

Only that the 6.5 (should) work with 6.5. I dunno about newer versions. Everything keeps changing.

devtechvalens commented 4 years ago

Hello I debug the issue everything is correct, but the problem is with Webview plugin. I uninstall the plugin and the images are visible from .obb file in android app. But this create a problem if I removed the Webview plugin, in the IOS build I am not able to play videos from assets folder, the IOS app crashes.

I tried to downgrade the Webview plugin to - 2.1.3,3.0.0,2.0.0 etc but none helped me.

Can you please tell me which Webview version is stable with the Xapk reader plugin?

devtechvalens commented 4 years ago

I solved my issue by doing following steps-

1) uninstall webview ionic cordova plugins rm cordova-plugin-ionic-webview

2) install old one: ionic cordova plugins add cordova-plugin-ionic-webview@1.2.1

3) clean cordova cordova clean android

The images are visible in Android application. I tested this only in one case- by running the app in android device and placed .obb file on specific folder.

But with signed .apk and placing the .obb file on specific folder I am getting - Error : Download failed dialog

I don't know why this error is displaying even I placed the .obb file on the specific folder. The path of .obb file is - /Internal Storage/Android/obb/[package]/filename.obb

Can you please tell me why I getting this error download failed error?

agamemnus commented 4 years ago

This isn't supposed to be used for iOS at all...

How did you "place" the .obb file?

devtechvalens commented 4 years ago

I download the .obb file from e-mail and then copy the .obb file on this location /Internal Storage/Android/obb/[package]/filename.obb

agamemnus commented 4 years ago

A number of changes have been made to the logic there.

What it should do is check if the file exists, and if it doesn't, it tries to download the file. Perhaps the file is not named as it should be, or is not in the location it should be in.

Maybe search for where that error is outputed in the Java files and add the location it tries to the output.

devtechvalens commented 4 years ago

Ok Let me check it

devtechvalens commented 4 years ago

I am using this plugin

ionic cordova plugin add https://github.com/erobertson42/cordova-plugin-xapkreader.git#cordova-9 --variable XAPK_PUBLIC_KEY="YOUR_GOOGLE_PLAY_LICENSE_KEY"

as you can see here it is working with Cordova 9 and when I am using this plugin and my app get stuck on every page.

Previously I used this plugin https://github.com/agamemnus/cordova-plugin-xapkreader and my app is working fine there is no stuck.

ionic cordova plugin add https://github.com/agamemnus/cordova-plugin-xapkreader.git#cordova-6.5.0

I tested my app with new Xapk plugin version cordova 9 by uploading the .apk and .obb file on Android beta testing.

Can anyone know why I am facing this issue in new version of Xapk reader plugin?

erobertson42 commented 4 years ago

It seems as though you were getting close. Based on what you've shared, my guess is that it is either related to the ionic webview plugin, or you have the .obb file named incorrectly.

Perhaps try creating a new internal test release in the Google Play Console, including the .obb file. Delete the app entirely from your device, and download it from Google Play instead to see what happens. Google automatically renames whatever apk expansion zip file you upload to the correct name.

At the very least, this will let you look to see what Google named the .obb file and that should remove a possible point of failure.

devtechvalens commented 4 years ago

I am using this version of webview-

ionic cordova plugins add cordova-plugin-ionic-webview@1.2.1

Do you think this version is the reason for stuck issue? As I used the same webview version 1.2.1 previously and upload the .apk and .obb of my project with the Xapk reader version cordova 6.5.0 on Google Play store and it was working fine.

And I also checked the naming of .obb file, it is correct. I completely uninstalled the app before installing the new one from Google playstore.

erobertson42 commented 4 years ago

You already mentioned that everything works if you remove the ionic webview plugin, so that's definitely what's causing the issue.

According to the notes in the ionic webview pull request I linked previously, you need to be using at least version 3.0.0 to be able to load local content. Upon further research, the official ionic docs on the webview plugin say you also need to be using at least version 3.2.0 of Ionic, so I would suggest upgrading both. Also, it seems that when using the ionic webview plugin, you don't want to be using the content:// URL - here's more info. According to that link, with webview version 3.x, you should be using http://localhost, as it's running a local webserver to serve the files instead of reading them directly.

I don't use Ionic or any of their plugins, so I probably cannot be too much more help. If you still have issues after trying the above, you might want to try asking Ionic.

FaakhirIqbal commented 4 years ago

content:// is not working for me in any case I have removed - cordova plugin rm cordova-plugin-ionic-webview and even tried by adding - cordova plugin add cordova-plugin-ionic-webview@4.1.1 (3.1.2, 1.2.1) 404 (Not Found) resource not found

xapk_

xapk_3

xapk_file_reader

erobertson42 commented 4 years ago

@FaakhirIqbal

  1. Does your .obb file actually contain the folder main_expansion? After the package name, the content:// path should match the folder structure in the .obb file. So for example, if your .obb file only contained a single folder name folder1 and inside that, a single file named file1.jpg, your path to that file would be content://my.package.name/folder1/file1.jpg.
  2. Your .obb file may be named incorrectly. Check your AndroidManifest.xml file for the android:versionCode attribute in the manifest tag. The version number in the .obb file name should match this.

Also, please try to get it working without the cordova-plugin-ionic-webview plugin first. I believe this is causing a lot of these issues.

FaakhirIqbal commented 4 years ago

Thank you very much for your reply! I was creating obb incorrect.

C:\Users\admin\AppData\Local\Android\sdk\tools\bin\jobb -d C:\Users\f.iqbal\Desktop\testing_xapk_reader__test\img\ -o ImageTargets.obb -k globe -pn com.xxxxx.xxxx-pv 100 -v

I used this this to create obb file and also working perfectly with cordova-plugin-ionic-webview@1.2.1, but content://my.package.name/folder1/file1.jpg helped me thanks!