cordova-plugin-file-transfer: Cordova 11 > Android incompatible

Does not work with Cordova 11 Android; cordova build android fails.

Errors due to cordova-plugin-whitelist missing.

Include cordova-plugin-whitelist and cordova build android fails due to an incompatibility with cordova-plugin-whitelist.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 15

Commits related to this issue

Most upvoted comments

Here is a hook I did:

config.xml: <hook type="after_prepare" src="hooks/hook-android-fix-file-transfer.js" info="Comment out whitelist code" />

hook-android-fix-file-transfer.js:

const fs = require('fs-extra');
const path = require('path');

const DIR = path.join(__dirname, '../');

/**
 * This hook removes whitelist from FileTransfer.java
 * see : https://github.com/apache/cordova-plugin-file-transfer/issues/345
 */
module.exports = function main(ctx) {
    if (!ctx.opts.platforms.includes('android')) return;

    console.log('[hook] Fix file transfer on android');

    const file = `${DIR}/platforms/android/app/src/main/java/org/apache/cordova/filetransfer/FileTransfer.java`;
    let code = `import org.apache.cordova.Whitelist;`
    let commentedOut = `//  Commented out by hook-android-fix-file-transfer.js
//  import org.apache.cordova.Whitelist;`

    let str = fs.readFileSync(file, 'utf8');
    str = str.replace(code, commentedOut);

    code = `        if (shouldAllowRequest == null) {
            try {
                Method gwl = webView.getClass().getMethod("getWhitelist");
                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
            } catch (NoSuchMethodException e) {
            } catch (IllegalAccessException e) {
            } catch (InvocationTargetException e) {
            }
        }`
    commentedOut = `//  Commented out by hook-android-fix-file-transfer.js
//          if (shouldAllowRequest == null) {
//            try {
//                Method gwl = webView.getClass().getMethod("getWhitelist");
//                Whitelist whitelist = (Whitelist)gwl.invoke(webView);
//                shouldAllowRequest = whitelist.isUrlWhiteListed(source);
//            } catch (NoSuchMethodException e) {
//            } catch (IllegalAccessException e) {
//            } catch (InvocationTargetException e) {
//            }
//        }`

    str = str.replace(code, commentedOut);

    fs.writeFileSync(file, str, 'utf8');
};