JaneaSystems / nodejs-mobile

Full-fledged Node.js on Android and iOS
https://code.janeasystems.com/nodejs-mobile
Other
2.57k stars 182 forks source link

Cordova Android Plugin Installation Issue #222

Open schingeck opened 5 years ago

schingeck commented 5 years ago

Trying to get nodejs-mobile working with Android (I've had no issues with iOS). Basically, I can get the plugin to install property when making sure my plugins/ directory is removed and then executing cordova platform add android and I can see the plugin was added to android.json and the android project.

However, once the plugins directory has already been fetched and I remove the android platform and re-add the android platform, the nodejs-mobile plugin will not be installed this time and the node-mobile plugin folder is not present in the android project.

I've tried on Cordova 7.1, 8.0 and 9.0, none fixed this issue. Also been using cordova-android 7.1.4 and 8.0.0.

jaimecbernardo commented 5 years ago

Hi @schingeck , Could you please share the list of commands you're using for this? Thank you.

schingeck commented 5 years ago

@jaimecbernardo thanks for getting back to me. Basically this is only occurring after your platform is already initialized and the plugin has been added. On the first install of the plugin, it works as expected.

cordova plugin add

Then after added, remove android

cordova platform remove android

And re-add cordova

cordova platform add android

For me, it is re-installing all Cordova plugins in the plugins directory except nodejs. It also appears to remove nodejs from the android.json file in plugins.

If I delete the entire plugins folder before I re-add the android platform, it works as expected. I haven't seen any errors in verbose mode

jaimecbernardo commented 5 years ago

Hi @schingeck ,

I've tried replicating this issue with the following commands:

npm install -g cordova@8
cordova create TestIssue
cd TestIssue
cordova platform add android
cordova plugin add nodejs-mobile-cordova
cordova platform remove android
cordova platform add android

nodejs-mobile-cordova is still in TestIssue/plugins in my case.

Is your project setup in a different way? Could you please give some more specific reproduction steps?

schingeck commented 5 years ago

@jaimecbernardo this has been fixed with some updates, however, there is a problem with the latest 0.3.4 release on my end in cordova-android 8.1.0, basically throws an error when installing nodejs-mobile-cordova at the edit-config for setting the uses-sdk in AndroidManifest.xml due to the tag not existing.

Not sure what has changed but with cordova-android 8.1.0 it looks like it is not adding the uses-sdk tag in the manifest file.

For the time being I just created a hook to make sure the tag exists in the AndroidManifest before cordova prepare installs the plugins.

jaimecbernardo commented 5 years ago

Hi @schingeck ,

Thank you for reporting it. It looks like cordova-android changed its sdk settings to the Gradle files for 8.1.0. The latest documentation still uses this field has a valid example, though: https://cordova.apache.org/docs/en/9.x/plugin_ref/spec.html#edit-config

A fix to this might break compatibility with previous cordova-android versions, so more information is needed at this time. In the meanwhile, could you please share your current hook for other users that might run into this issue?

schingeck commented 5 years ago

@jaimecbernardo

Unfortunately, it felt very 'hacky' so we decided against it and using older versions for now. But the hook I wrote, just added it as a after_platform_add hook

#!/usr/bin/env node
module.exports = function(context) {
  const fs = context.requireCordovaModule('fs'),
    path = context.requireCordovaModule('path');

  const platformRoot = path.join(context.opts.projectRoot, 'platforms/android');
  const manifestFile = path.join(platformRoot, 'app/src/main/AndroidManifest.xml');

  if (fs.existsSync(manifestFile)) {
    try {
      const data = fs.readFileSync(manifestFile, 'utf8');
      if (data.indexOf('<uses-sdk') === -1) {
        const result = data.replace(
          /<\/application>/g,
          '</application><uses-sdk android:minSdkVersion="21" />',
        );

        try {
          fs.writeFileSync(manifestFile, result, 'utf8');
          console.log('after_platform_add added uses-sdk to AndroidManifest.xml');
        } catch (err) {
          throw new Error('Unable to write into AndroidManifest.xml: ' + err);
        }
      }
    } catch (err) {
      throw new Error('Unable to find AndroidManifest.xml: ' + err);
    }
  }
};