agamemnus / cordova-plugin-xapkreader

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

Gradle patch (work in progress) #22

Closed agamemnus closed 8 years ago

agamemnus commented 8 years ago

Cordova 5 ignores the ant build system. It uses gradle, instead. That means that the links to the Google Play downloader and licensing libraries are broken. To fix that, we need to use a build-extras.gradle file, and do some other things...

Due to some various Cordova 5 issues/bugs, a fully working Cordova 5 version isn't currently available. (there is a test Cordova-5 branch, however) Here are steps to make this plugin work for Cordova 5 (PLEASE NOTE THESE ARE STILL DRAFT INSTRUCTIONS): 1) Create a /platforms/android/com.flyingsoftgames.xapkreader/ directory. 2) Copy your /plugins/com.flyingsoftgames.xapkreader/android-sdk/extras/google/play_apk_expansion/downloader_library directory contents and your /plugins/com.flyingsoftgames.xapkreader/android-sdk/extras/google/play_licensing/library directory contents to /platforms/android/com.flyingsoftgames.xapkreader/downloader_library and /platforms/android/com.flyingsoftgames.xapkreader/library. 3) Instead of the single reference we added before, add this instead to project.properties:

android.library.reference.2=com.flyingsoftgames.xapkreader/downloader_library
android.library.reference.3=com.flyingsoftgames.xapkreader/library

4) Inside downloader_library, add a file named build-extras.gradle, with the following contents:

dependencies {
debugCompile project(path: ':com.flyingsoftgames.xapkreader:library',configuration: "debug")
releaseCompile project(path: ':com.flyingsoftgames.xapkreader:library',configuration: "release")
}

5) The automatic downloader DOES NOT SEEM TO WORK. We must disable it. In platforms/android/src/com/flyingsoftgames/xapkreader/XAPKDownloaderActivity.java, change return isDebug; to return true;.

6a) Cordova 5 has a whitelist plugin requirement. New builds have it by default. You must add this: cordova plugin add cordova-plugin-whitelist. In your index.html file, you also need to add a Content-Security-Policy meta tag. Note that the instructions on the whitelist plugin on github do not show the most permissive meta tag and forget about content:// or cdvfile://. The most permissive meta tag seems to be: <meta http-equiv="Content-Security-Policy" content="* * 'self' default-src 'unsafe-inline' 'unsafe-eval' http://* https://* data: cdvfile://* content://*;">. (content:// and cdvfile:// should really be included by default. If you want to omit those, I made a patch here: https://github.com/agamemnus/cordova-plugin-whitelist)

6b) Make sure to add allow-navigation, and allow-intent, and extra access origin tags to config.xml. E.G.:

    <allow-navigation href="*://*/*"/>
    <allow-intent href="*" />
    <access origin="*" />
    <access origin="content:///*" />
    <access origin="cdvfile:///*" />

NOTE 1: Please do not post any logs here (unless absolutely necessary)! NOTE 2: If you are on Crosswalk, note that you must use the plugin method for Cordova 5+ builds. The migrate method no longer works.

agamemnus commented 8 years ago

I am wondering then if you are actually using a new platforms/android build? That has its own version separate from the Cordova version and they should be updated in parallel.

marcoturi commented 8 years ago

I'm using Cordova 5.0 + Ionic 1.0.0 (1.5.0 CLI), building with command 'ionic build android'

agamemnus commented 8 years ago

After that, do you do ionic plugin add https://github.com/igormas/cordova-plugin-xapkreader? (note this is igormas's fork)

marcoturi commented 8 years ago

@agamemnus thank you! For same reason that folder was not generated. I tried a new fresh install and now all works :D i will test the APK now.

mattrayner commented 8 years ago

I'm back on this again today - @Shinji89 did you get the whole plugin working?

agamemnus commented 8 years ago

Ok, Matt, I have a few questions: With Cordova 4, were you able to use the non-Google Play build with the current version normally? No stalled download issues? If yes, were you using a debug build or a release build, and in either case, were you using a debug certificate?

Same questions for 5.

marcoturi commented 8 years ago

@mattrayner i will test it in the next week.

mattrayner commented 8 years ago

@agamemnus I've not tried it in Cordova 4 - I'm not too worried about it not downloading outside of Google play for my app at the moment - as long as I can read the files and it will download when published as Alpha, Beta or Production (which I'm currently testing) that's ok with me

My app deadline has been moved back a little so I have some more time to work on this.

I've also discovered that you can have multiple origin tags such as:

    <!-- A wildcard can be used to whitelist the entire network,
     over HTTP and HTTPS.
     *NOT RECOMMENDED* -->
    <allow-navigation href="*" />
    <!-- Allow all unrecognized URLs to open installed apps
      *NOT RECOMMENDED* -->
    <allow-intent href="*" />
    <!-- Don't block any requests -->
    <access origin="*" />

    <access origin="content:///*" />
    <access origin="cdvfile:///*" />

they do not appear to overwrite each other - the above allows for content:///* even though cdvfile:///* follows

@Shinji89 Awesome!

mattrayner commented 8 years ago

I am still getting these errors though:

06-09 17:31:01.435: E/AndroidProtocolHandler(19946): Unable to open content URL: content://com.bdnetworkXXX/img/pages/XXX.jpg
agamemnus commented 8 years ago

Have you been able to get any of them open at all?

Do you have spaces or caps in the filename?

You are using the expansion content provider thingo instead of the project name in content://, right?

igormas commented 8 years ago

Woohoo i finally managed to retrieve the content from the expansion file. I'm using cordova 5, i haven't tested the downloader yet but when the file is downloaded(i copied it manually) then everything seems to work. Important points:

agamemnus commented 8 years ago

Igor, that shouldn't happen. I am reading xapk_expansion_authority without issues, although I do get ERR_UNKNOWN_URL_SCHEME for content://, but that might be related to a too-new Chromium or Crosswalk version. Are you using Crosswalk? If not, what Chrome version are you using?

igormas commented 8 years ago

agamemnus, I'm using crosswalk (engine c0.7.1), currently the xapk_expansion_authority value in my project is com.test.expansion but I changed my package name to $project_I'd and everything works ok. I changed it because I saw that the plugin is looking for the expansion files in 'Android/obb/$project_id' folder. If everything works correct in your project, then probably I missed something. I'll double check it again today.

agamemnus commented 8 years ago

c0.7.1? What is that? ... can you link me?

igormas commented 8 years ago

Its just the crosswalk version, nothing special.

agamemnus commented 8 years ago

So you are using cordova plugin add cordova-plugin-crosswalk-webview?

igormas commented 8 years ago

No, i'm using ionic so in order to add crosswalk I am running ionic browser add crosswalk. http://blog.ionic.io/crosswalk-comes-to-ionic/

agamemnus commented 8 years ago

I am not sure how ionic adds crosswalk. This may be an issue with the exact Chromium and crosswalk version ionic uses, or maybe not. You are definitely not getting net::ERR_UNKNOWN_URL_SCHEME on the content:// requests?

igormas commented 8 years ago

Where do you see this error ? Anyway I'm receiving the wanted image for the content://com.test.expansion/wantedImg.png request.

agamemnus commented 8 years ago

I get the error in Developer Tools in Chrome when I create a debug build. (You will need to hard-code isDebug to true in XAPKDownloaderActivity.java to use a debug build with the same cert as release.)

igormas commented 8 years ago

@agamemnus i have no errors in console when running with idDebug=true. this is how i retrieved my image < img src="content://$package_name/www/assets/img/1x.png" > . The image was retrieved and rendered successfully in the view. Do you have crosswalk in your project ?

agamemnus commented 8 years ago

Yes, I am using cordova plugin add cordova-plugin-crosswalk-webview.

Regarding your content URL, check your plugins/android.json file and your platforms/android/res/values/xapkreader.xml file for any duplicates or different values of xapk_expansion_authority.

igormas commented 8 years ago

Does the plugin works well in your project ?

agamemnus commented 8 years ago

No, it doesn't work. At first I thought it did but it was just loading the backup files stored on my server. It just gives me net::ERR_UNKNOWN_URL_SCHEME errors.

igormas commented 8 years ago

What crosswalk version do you use ? I guess you using cordova 5 right ?

agamemnus commented 8 years ago

Right, Cordova 5. I don't know what the version is but I guess it is the stable version for 5.

igormas commented 8 years ago

you can check the version in package.json of the crosswalk plugin (plugins/cordova-plugin-crosswalk-webview). I checked yesterday with 1.2.0 and its works.

agamemnus commented 8 years ago

Odd, well, it is 1.2.0.

igormas commented 8 years ago

How do you sending the requests? Have you tried using the img tag (< img src="content://..." >) ? this is the only way i tested the plugin....

agamemnus commented 8 years ago

Basically, yes. new Image ()...

igormas commented 8 years ago

Try to put img tag within html (when i tested i put it as the first child of the body element). Just making sure it doesn't work under the conditions i tested it.

mattrayner commented 8 years ago

I've followed your above and can't get it to work - This is my most recent error:

06-11 11:54:37.957: E/AndroidProtocolHandler(5641): Unable to open content URL: content://com.test.expansion/main_extension/img/icons/menu-test-drive.png

I have the file in Android/obb/com.test.expansion/mail.***.***.***.***.obb

Does the file need to be named in some specific way that I'm missing?

mattrayner commented 8 years ago

I'm not using crosswalk or ionic - just the base android chrome browser

igormas commented 8 years ago

@mattrayner you need correct the name of the main file, it should be in the following structure Android/obb/$package_name/main.$version.$package_name.obb

mattrayner commented 8 years ago

@igormas - is version the version that shows up in the play developer console or the version that is set in config.xml?

mattrayner commented 8 years ago

the developer console appends 8 to my version so config.xml shows android-version='20' and play shows 208

mattrayner commented 8 years ago

@igormas - I now get:

06-11 12:06:29.820: E/AndroidProtocolHandler(7907): Unable to open content URL: content://com.bdnetwork.peugeot208/main_extension/img/black.jpg
06-11 12:06:29.823: E/ActivityThread(7907): Failed to find provider info for com.bdnetwork.peugeot208
igormas commented 8 years ago

It should be the value of android:versionCode of manifest tag in AndroidManifest.xml

mattrayner commented 8 years ago

hmm, that's still not working for me. If I set my content urls to com.test.expansion as set in android.json it gets rid of the ActivityThread issue but moving the files still doesnt help

igormas commented 8 years ago

What is the value of xapk_expansion_authority variable ?

mattrayner commented 8 years ago

In android.json: com.test.expansion

in xapkreader.xml: com.test.expansion

igormas commented 8 years ago

So try to change the expansion file name to main.$version.com.test.expansion.obb, and put in the following path Android/obb/com.test.expansion. So in your device you have the following structure: Android/obb/com.test.expansion/main.$version.com.test.expansion.obb

mattrayner commented 8 years ago

trying that now... This is what my Android File Explorer looks like: image

mattrayner commented 8 years ago

Same error:

06-11 12:32:33.190: E/AndroidProtocolHandler(12029): Unable to open content URL: content://com.test.expansion/main_extension/img/icons/menu-test-drive.png
mattrayner commented 8 years ago

maybe it's a problem with my zip file?

Do you have a zip command that you know works?

igormas commented 8 years ago

Change the name to main.20.com.test.expansion.obb. I edited my previous comment, you probably read it before the edit :)

mattrayner commented 8 years ago

Updated tree: image

mattrayner commented 8 years ago

Same error

mattrayner commented 8 years ago

Is there a way of checking if the app can see the file on the device's file system from cordova?

igormas commented 8 years ago

I had the same issue, try to change the package name in all relevant places to app id (app id in config.xml (id attribute of the widget tag) ). By the way your version number is the same as android:versionCode ?