11

When compiling a cordova application every single file in my /www folder gets copied to the assets/www folder(android) but I'd like to customize what files are copied. I use several pseudo languages like CoffeeScript, Jade or Stylus which are auto-compiled by my IDE and they shouldn't be shipped into the final application.

6 Answers 6

7

With the help of this article, I found that you can create/edit the platform/android/ant.properties, and add the following line to it:

aapt.ignore.assets=!*.map:!thumbs.db:!.git:.*:*~

With this line, any file or directory that matches one of these patterns will not be included in the .apk file:

  • *.map
  • thumbs.db
  • .git
  • .*
  • *~

I found that editing the platforms/android/build.xml won't work because it's overwritten each time the build is invoked; also, creating a build.xml file in the root of the project didn't work.

The rules for this property are the following, taken from $ANDROID_HOME/tools/ant/build.xml:

<!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
         Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

         Overall patterns syntax is:
           [!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...

         - The first character flag ! avoids printing a warning.
         - Pattern can have the flag "<dir>" to match only directories
           or "<file>" to match only files. Default is to match both.
         - Match is not case-sensitive.
    -->
    <property name="aapt.ignore.assets" value="" />
4
  • Is there any possibility to ignore eg. text.txt only in one certain subdirectory of /res or /assets?
    – Bowi
    Oct 25, 2016 at 11:32
  • This is Android specific, and I am downvoting this answer because of that. Mar 31, 2019 at 14:57
  • 1
    @amn ..and the question mentions/is tagged with Android. Apr 1, 2019 at 0:49
  • Oh. Didn't see that. Checked the question for "android" but didn't look at the tags. And now I can't remove my downvote :( Sorry about that! Apr 1, 2019 at 9:33
3

Hidden (.*) folder & files will be ignored by default

All hidden files and folders will be ignored while build for example .git/ & .gitignore

To hide : Rename the folder by a . (dot) prepended to the folder/file name.

Use Terminal in Linux/Mac to rename the folder.

mv dev .dev

Use cmd in Windows

ren dev .dev
2

I do not know any way how to filter files for cordova, but I can describe you approach that we use in our projects.

We are using gruntjs with phonegap plugin. We configure it to use prebuilt folder (which contains only necessary files and folders) and it:

  • creates cordova/phonegap project
  • adds plugins,platforms
  • builds native applicaiton
  • launches native application on emulator

Main thing in this approach is that cordova/phonegap project (directory with .cordova, platforms and www folders) is just a build artefact.

Here is relevant part of our Gruntfile.js as an example:

  phonegap : {
     config : {
        root : './out/dist',
        config : {
           template : './config.tpl.xml',
           data: {
              id: pkg.id,
              version: pkg.version,
              name: pkg.name,
              author : pkg.author
           }
        },
        path : 'out/phonegap',
        plugins : [
           // PHONEGAP OFFICIAL PLUGINS
           'org.apache.cordova.globalization',
           'org.apache.cordova.network-information',
           'org.apache.cordova.splashscreen',

           //THIRD-PARTY PLUGINS
           'de.appplant.cordova.plugin.local-notification'
        ],
        platforms : [
           'android'
        ],
        maxBuffer : 200, // You may need to raise this for iOS.
        verbose : false,
        releases : 'out/releases',
        releaseName : function() {
           return pkg.name + '-v' + pkg.version;
        },

        // Android-only integer version to increase with each release.
        // See http://developer.android.com/tools/publishing/versioning.html
        versionCode : function() {
           return 1;
        }
     }
  }

Note, out/dist is generated by previous build step and contains concatenated and minified version of code.

2

I highly recommend cordova-plugin-exclude-files. You simply specify files to exclude as pattern attributes of exclude-files tags in your config.xml file.

For example, to exclude all .scss files:

<exclude-files pattern="**/*.scss" />

And to exclude all files with the string "ios-only" on the Android platform only:

<platform name="android"> <exclude-files pattern="ios-only" /> </platform>

2
  • How would you exclude the .git folder? I've tried with several patterns without luck... I've tried **/.git, .git, **/git and others
    – Albert
    Apr 20, 2021 at 16:57
  • @Albert the build should not be picking up hidden files and folders. But what's more is that (if www is your build output) then you shouldn't have to version control it at all. But if you absolutely must - perhaps this link is helpful? github.com/markmarijnissen/cordova-app-loader/issues/28 Jun 11, 2021 at 0:17
1

I've created a custom script to ignore files/folders which works not just for android:

Add this to your config.xml:

<ignore entries="lang,foo/bar,ignore/asdf.html" />

And you need two more files located in hooks/before_build and hooks/after_build folders.

0

This is what I'm doing. I've moved the src files for the app that was in www folder to another folder created in root - web-app. Next, the following script was added in package.json :

enter image description here

This surely needs to be refined, but am working with it as makeshift arrangement.

Good Luck...

Your Answer

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

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