Developers Manual > Cocos2d-x > Cocos2d-console > How to exclude files in resources directory when building for android

How to exclude files in resources directory when building for android

The requirement

We know that resources files will be copied to assets directory when building project for android.
Most developers will have a question: How can I exclude some files in my resources directory? For example, the files or directories for Source Control. This document will give you the answer.

Configuration File

Here is a configuration file named build-cfg.json in the proj.android directory. The content of build-cfg.jsonshould be looks like this:

{
    "ndk_module_path" :[
        "../../cocos2d-x",
        "../../cocos2d-x/cocos/",
        "../../cocos2d-x/external",
        "../../cocos2d-x/cocos/scripting"
    ],
    "copy_resources": [
        {
            "from": "../../../src",
            "to": "src"
        },
        {
            "from": "../../../res",
            "to": "res"
        }
    ],
    "must_copy_resources": [
        {
            "from": "../../../config.json",
            "to": ""
        }
    ]
}

The value of copy_resources is used for copy resources into assets. The cocos command will parse it to generate the assets.
Every element can have these parameters:

param meaning comments necessary?
from The source directory A relative path from proj.android directory yes
to The destination directory A relative path from assets directory yes
include The include rules Look at the rules no
exclude The exclude rules Look at the rules no

How the rules be parsed

The rules is parsed as a regular expression. But it's a little different with regular expression. The parse steps:

  • Replace the . in the rules with \..
  • Replace the * in the rules with .*.
  • Then use the result string as a regular expression.

The reason: For most developers, they only want to exclude files with specified extensions. So it's not necessary for them to use regular expression.
For example, I want to only copy the .jpg files. I can write the rules like this:

{
    "from": "../../../res",
    "to": "res",
    "include" : [
        "*.jpg"
    ]
}

Attention

You can use both include and exclude rules in the configuration. But when both of them are configured, only the include rules are used.
Because I don't know a file should be include or exclude if it match both rules.
So if you want exclude a folder except some files in it. You can write your configuration like this:

{
    "from": "../../../res",
    "to": "res",
    "exclude": [
        "folder"
    ]
},
{
    "from": "../../../res",
    "to": "res",
    "include": [
        "folder/a.jpg"
    ]
}

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.