1
votes

I am using ionic to build mobile app. When I using the command: ionic cordova build android / ionic cordova platform add android it's appear following error. Please help me to fix it.

'AppMyCity> ionic cordova platform add android

cordova platform add android --save Using cordova-fetch for cordova-android@~7.0.0

Adding android project...

Creating Cordova project for the Android platform:

    Path: platforms\android
    Package: io.ionic.starter
    Name: MyCity
    Activity: MainActivity
    Android target: android-26

Subproject Path: CordovaLib Subproject Path: app Android project created with [email protected]

Android Studio project detected

Android Studio project detected

(node:70480) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token < in JSON at position 3 at JSON.parse () at getJson (C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\src\plugman\util\metadata.js:29:31) at Object.exports.get_fetch_metadata (C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\src\plugman\util\metadata.js:41:24) at C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\src\cordova\platform\addHelper.js:343:48 at _fulfilled (C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\node_modules\q\q.js:787:54) at self.promiseDispatch.done (C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\node_modules\q\q.js:816:30) at Promise.promise.promiseDispatch (C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\node_modules\q\q.js:749:13) at C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\node_modules\q\q.js:810:14 at flush (C:\Users\anhde\AppData\Roaming\npm\node_modules\cordova-lib\node_modules\q\q.js:108:17) at _combinedTickCallback (internal/process/next_tick.js:131:7) (node:70480) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:70480) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code'

1

1 Answers

1
votes

I had some problems when already had an existing platform and removed it. Sometimes u do need to delete the plugins folder in the project and than add the platform again. Do you have some scripts to make sure everything is working with [email protected]? I had to modify some files so all paths are correct.

patch-android-studio-check.js

module.exports = function(context) {
  if (context.opts.cordova.platforms.indexOf('android') < 0) {
    return;
  }

  const path = context.requireCordovaModule('path');
  const androidStudioPath = path.join(context.opts.projectRoot, 'platforms/android/cordova/lib/AndroidStudio');
  const androidStudio = context.requireCordovaModule(androidStudioPath);
  androidStudio.isAndroidStudioProject = function() {  return true; };
};

build-extras.gradle.js

var fs = require('fs');
var path = require('path');

if(fs.existsSync(path.resolve(__dirname, '../platforms/android'))) {
  fs.createReadStream(path.resolve(__dirname, 'build-extras.gradle')).pipe(fs.createWriteStream(path.resolve(__dirname, '../platforms/android/build-extras.gradle')));
}

build-extras.gradle

android { 
    flavorDimensions "default"
} 

config.xml

    <platform name="android">
        <hook src="package-hooks/build-extras.gradle.js" type="after_platform_add" />
        <hook src="package-hooks/patch-android-studio-check.js" type="before_plugin_install" />
        <hook src="package-hooks/patch-android-studio-check.js" type="before_plugin_add" />
        <hook src="package-hooks/patch-android-studio-check.js" type="before_build" />
        <hook src="package-hooks/patch-android-studio-check.js" type="before_run" />
        <hook src="package-hooks/patch-android-studio-check.js" type="before_plugin_rm" />
...