3

I'm a total n00b at PhoneGap (version: 3.4). Currently I have a project structure like:

.cordova
+---config.json
hooks
+---README.md
merges
+---android
    +---js
        +---index.js
platforms
+---.gitkeep
plugins
+---org.apache.cordova.device (content: the plugin sources, doc, and data)
+---android.json (content: plugins config for android)
www
+---css (content: many assets here)
+---img (content: many assets here)
+---js (content: many assets here, siblings of index.js)
    +---index.js (a default file intended to be overriden by the merged index.js file)
+---res (content: resources for splashscreen and stuff configured in config.xml)
+---index.html
+---config.xml
+---icon.png

My intention is to have many files being overriden by platform specific files (in this case, android), so I created a merges/android directory for my custom android assets.

Contents of index.js (base):

var app = {
    selectDevice: function() {
        window.alert('using default');
    },
    initialize: function() {
        document.addEventListener("deviceready", this.selectDevice, false);
    }
};

Contents of index.js (to-merge):

var app = {
    selectDevice: function() {
        window.alert('using android');
    },
    initialize: function() {
        document.addEventListener("deviceready", this.selectDevice, false);
    }
};

Contents of index.html (the only-and-common html file for every platform):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <script type="text/javascript" src="phonegap.js"></script>
        <title>1001Carros</title>
    </head>
    <body>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            window.alert('initializing...');
            app.initialize();
            window.alert('initialization was run.');
        </script>
        <div id="log">
            <button id="clickme" value="Click Me"></button>
            pantalla principal
        </div>
    </body>
</html>

The filesystem structure I detailed here is the state of my current repository (notes: empty folders have dummy .gitkeep files so they exist in the repository - this is specially true for the platforms directory, which is currently empty).

However, when I push my contents to my repo and pull-and-rebuild in http://build.phonegap.com, I had two scenarios:

  1. In the first try, I did not create an index.js file in the base www directory, but just in the merges/android directory. Result: the file was missing (i.e. it's content, which was an alert being shown inside the deviceReady event -telling that the current platform is android- was never executed, and subsequent code neither -message: initialization was run-, which is the result of a ReferenceError when trying to call app.initialize()).
  2. In the second try, I create the index.js to be the default under www/js when no file was merged. Result: the message printed was 'using default'.

My conclusion: the file was never merged.

My question: Why? Should I create an explicit platforms/android directory (well: platform) in order to get my files merged? or should build.phonegap.com do it automatically when I tell to rebuild the android application?

What am I missing here?

1 Answer 1

3

The merges folder is meant for ressources like images or css, not that much for entire JS in my opinion. Instead, I advise you to add the Device plugin and test device.platform against "android" for a different behaviour on those platforms.

http://docs.phonegap.com/en/3.0.0/cordova_device_device.md.html#device.platform

You asked if you should create a platform/android directory. This is a weird question because if you are doing this right, you should be using the build command and that directory should be created by itself! Then in the platform/android/assets/www you should find your files merged, nowhere else.

1
  • I noticed the Phonegap Build service does not support MERGEs. I noticed that phonegap build android neither supports merges. Finally: noticed that cordova build android (appending --release or not) supports merges. Had to have BOTH command lines installed. Jul 3, 2014 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.