operation not permitted, unlink while adding cordova-plugin-firebase plugin in ionic

Viewed 421

Installing "cordova-plugin-firebase" for android Error during processing of action! Attempting to revert... Failed to install 'cordova-plugin-firebase': Error: Uh oh! EPERM: operation not permitted, unlink 'F:\ionic\PkjshopMerchant\platforms\android\app\src\main' at Object.unlinkSync (fs.js:1129:3) at mayCopyFile (F:\ionic\PkjshopMerchant\platforms\android\cordova\node_modules\fs-extra\lib\copy-sync\copy-sync.js:59:8)

1 Answers

As commented in this link : https://github.com/arnesson/cordova-plugin-firebase/issues/1154 there is a posible solution for it that is not downgrading to cordova@8.0.0

Studying the error message that occurs during the installation of the firebase plugin, we made a debug session and found that things were going wrong while the plugin was trying to copy google-services.json file from the plugin folder to the project folder.

The error message was: Error during processing of action! Attempting to revert... Failed to install 'cordova-plugin-firebase-lib': Error: Uh oh! EPERM: operation not permitted, unlink '[My project folder]\apk\platforms\android\app\src\main'

It's something to the with file write permissions but we couldn't solve this by changing folder permissions and indexing.

From the stack trace, we saw that the copy operation was handled by the mayCopyFile function in the copy-sync.js javascript file that was located in the ..\apk\node_modules\fs-extra\lib\copy-sync folder.

Here's the function body:

function mayCopyFile (srcStat, src, dest, opts) {
  if (opts.overwrite) {
    fs.unlinkSync(dest)
    return copyFile(srcStat, src, dest, opts)
  } else if (opts.errorOnExist) {
     throw new Error('${dest}' already exists)
  }
}

fsunlinkSync and copyFile calls here crashes the installation. We couldn't proceed with the debug further, perhaps it would be better to proceed but we stopped here and found this solution:

Since these lines of code is trying to copy google-services.json file from the ..\apk\plugins\cordova-plugin-firebase\src\android location to the ..\apk\platforms\android\app\src\main destination, we decided to do this copy manually.

So here are the steps we followed:

Make sure you have a clean cordova project folder with no firebase plugin.

Have the android platform 9.0.0, if already not: cordova platform rm android cordova platform add android@9.0.0

Comment out the lines fsunlinkSync and copyFile in the mayCopyFile function in the ..\apk\node_modules\fs-extra\lib\copy-sync copy-sync.js file.

install firebase plugin: cordova plugin add cordova-plugin-firebase-lib@3.0.0 (We've always been using this lib. Unfortunately cordova-plugin-firebase gives other build errors)

See that the plugin is installed and the platform is android 9.0.0 cordova plugin ls cordova platform ls

Copy google-services.json file from ..\apk\plugins\cordova-plugin-firebase\src\android location to the ..\apk\platforms\android\app\src\main destination, manually.

Before the cordova build, uncomment the code lines you commented out in the 3rd step, because cordova build is going to copy lots of files from some source location to some destination location that isn't smart to copy manually.

Edit build.gradle file in the apk\platforms\android folder (change the gradle version appropriately): classpath 'io.fabric.tools:gradle:1.28.0'

       cordova build android

       cordova run android

This worked. Of course, we added the cordova-device-plugin, edited the config.xml, added google-services.json to the project root folder etc as usual prerequisites, to make the plugin work.

Related