Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin doesn't install if android resource file colors.xml already exists #132

Closed
ir2pid opened this issue Sep 5, 2019 · 6 comments
Closed
Labels
android Relates to Android platform bug Something isn't working properly build issue An issue related to build process legacy Related to inherited code/functionality

Comments

@ir2pid
Copy link

ir2pid commented Sep 5, 2019

I get this error as another plugin has already created the colors.xml file and the plugin doesn't append but tries to copy it first

Failed to install 'cordova-plugin-firebasex': CordovaError: Uh oh!
"/Users/xx/Workspace/xx/xx-xx-app/platforms/android/app/src/main/res/values/colors.xml" already exists!

Bug report

Current behavior:

use any plugin which adds colors to the res/values/colors.xml
simplest way is to create this file already.
Then try to install the plugin

Expected behavior:
line 59 of plugin.xml tries to copy the colors file and installation fails if it's already there.
<source-file src="src/android/colors.xml" target-dir="res/values" />

Steps to reproduce:

use any plugin which adds colors to the res/values/colors.xml
the simplest way is to create this file already.
Then try to install the plugin

Environment information

  • Cordova CLI version
    • cordova -v 9.0.0 (cordova-lib@9.0.1)
  • Cordova platform version
    • cordova platform ls android 8.0.0 ios 4.5.5
  • Plugins & versions installed in project (including this plugin)
    • cordova plugin ls
plugin list com.adjust.sdk 4.18.0 "Adjust" cordova-clipboard 1.3.0 "Clipboard" cordova-plugin-add-swift-support 2.0.2 "AddSwiftSupport" cordova-plugin-advanced-http 2.1.1 "Advanced HTTP plugin" cordova-plugin-androidx 1.0.2 "cordova-plugin-androidx" cordova-plugin-androidx-adapter 1.1.0 "cordova-plugin-androidx-adapter" cordova-plugin-app-version 0.1.9 "AppVersion" cordova-plugin-camera 4.1.0 "Camera" cordova-plugin-device 2.0.3 "Device" cordova-plugin-file 6.0.2 "File" cordova-plugin-firebasex 6.1.0 "Google Firebase Plugin" cordova-plugin-inappbrowser 3.1.0 "InAppBrowser" cordova-plugin-ionic-keyboard 2.1.3 "cordova-plugin-ionic-keyboard" cordova-plugin-ionic-webview 4.1.1 "cordova-plugin-ionic-webview" cordova-plugin-is-debug 1.0.0 "IsDebug" cordova-plugin-media 5.0.3 "Media" cordova-plugin-media-capture 3.0.3 "Capture" cordova-plugin-network-information 2.0.2 "Network Information" cordova-plugin-secure-storage 3.0.2 "SecureStorage" cordova-plugin-splashscreen 5.0.3 "Splashscreen" cordova-plugin-statusbar 2.4.3 "StatusBar" cordova-plugin-whitelist 1.3.4 "Whitelist" cordova-plugin-x-socialsharing 5.4.7 "SocialSharing" es6-promise-plugin 4.2.2 "Promise" phonegap-plugin-barcodescanner 8.1.0 "BarcodeScanner"


- Dev machine OS and version, e.g. - OSX ProductName: Mac OS X ProductVersion: 10.14.5 BuildVersion: 18F132

Android build issue:

  • Node JS version
    • node -v v10.14.0

Console output

console output


Failed to install 'cordova-plugin-firebasex': CordovaError: Uh oh!
"/Users/xx/Workspace/xx/xx-xx-app/platforms/android/app/src/main/res/values/colors.xml" already exists!



@dpa99c
Copy link
Owner

dpa99c commented Sep 5, 2019

The Cordova <source-file> element doesn't handle the case if the file already exists at the target location - there's no option to merge or overwrite it.

I guess the ideal behaviour would be to check if the file exists already and if so, add the keys to it from this plugin's colors.xml (if they don't already exist).

Since Cordova doesn't provide a mechanism to do that, it would have to be done with a custom hook script. This something which could be done, but it's not high on my priority list.

@dpa99c dpa99c added android Relates to Android platform build issue An issue related to build process legacy Related to inherited code/functionality TODO Something needs doing labels Sep 5, 2019
@ir2pid
Copy link
Author

ir2pid commented Sep 5, 2019

This is what I use, checks if colors.xml file exists else make one before the plugin is installed and appends values.

removed below line from plugin's config.xml
<!--<source-file src="src/android/colors.xml" target-dir="res/values" />-->

add before_plugin_install hook
before_plugin_install.js

var fs = require("fs");
var path = require("path");
var rootdir = process.env.PWD;
var valuesPath = "/platforms/android/app/src/main/res/values/";
var colorsFile = "colors.xml";
var destFile = path.join(rootdir, valuesPath, colorsFile);
var destDir = path.dirname(destFile);

module.exports = {

    createAndroidColorXml: function (context) {
        console.log("*************************************");
        console.log(`before_plugin_install running`);
        console.log("*************************************");

        if (fs.existsSync(destDir)) {
            if (!fs.existsSync(destFile)) {
                console.log("*************************************");
                console.log(`creating new  ${destFile}`);
                console.log("*************************************");
                fs.writeFile(
                    destFile,
                    '<?xml version="1.0" encoding="utf-8"?>\n<resources>\n <color name="colorPrimary">#3F51B5</color>\n <color name="colorPrimaryDark">#303F9F</color>\n <color name="colorAccent">#FF4081</color>\n</resources>',
                    function (err) {
                        if (err) {
                            console.error("writeFile error" + err);
                        }
                        console.log(`${colorsFile} created!`);
                    }
                );
            } else {
                console.log(`skipping, file ${colorsFile}  exists`);
            }
        } else {
            console.log("skipping, android platform not found");
        }
    }

};

@tom-hartz
Copy link

Changing it from <source-file target-dir= to <resource-file target= is a much simpler fix for this. I've had this change for a while in my fork of the arnesson plugin:
tom-hartz@d5b7a43

@tom-hartz
Copy link

tom-hartz commented Nov 6, 2019

Oops. My suggestion only works if you already have a colors.xml with "accent" defined in it, and with my changes the plugin variable ANDROID_ICON_ACCENT is just ignored. If your project colors.xml does not define an "accent" color, you then end up with this build error:

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:processDebugResources'.
> Android resource linking failed
  ../platforms/android/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml:73: AAPT: error: resource color/accent (aka <package>:color/accent) not found.

Ideally there should be a build hook or something that can correctly merge the colors.xml, probably like what @ir2pid suggested.

dpa99c added a commit that referenced this issue Nov 7, 2019
…tual boolean type rather than a binary integer.

Fixes #132.
@dpa99c dpa99c added the bug Something isn't working properly label Nov 7, 2019
@dpa99c dpa99c added ready for release Something has been implemented and is awaiting release to npm and removed TODO Something needs doing labels Nov 8, 2019
@dpa99c dpa99c closed this as completed in bda54cb Nov 9, 2019
@dpa99c dpa99c removed the ready for release Something has been implemented and is awaiting release to npm label Nov 9, 2019
@captainerd
Copy link

Script at: /plugins/cordova-plugin-firebasex/scripts/after_prepare.js it keeps replacing my own defined colors.xml in config.xml so i was getting errors and couln't compile.

Cause At line 193:

Utilities.writeJsonToXmlFile($colorsXml, PLATFORM.ANDROID.colorsXml.target);
Utilities.log('Updated colors.xml with accent color');

It overwrites the original colors, i worked around this with including #FF00FFFF to my colors.xml and commenting out // that line of script

@kapilSoni101
Copy link

@captainerd :Are u fixed above issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
android Relates to Android platform bug Something isn't working properly build issue An issue related to build process legacy Related to inherited code/functionality
Projects
None yet
Development

No branches or pull requests

5 participants