Search code examples
androidkotlinassets

Are there reasons why Android does not copy asset folders starting with _?


I have a assets folder started with '_'. And of course after compiling the application it cannot find some resources.

I have found answer how to fix it: https://github.com/ionic-team/capacitor/issues/1750, but I would like to know if this could somehow negatively affect the operation of the application?


Solution

  • On Android (at least by default) the file names encoded as UTF-8. From android.os.FileUtils

        private static boolean isValidFatFilenameChar(char c) {
            if ((0x00 <= c && c <= 0x1f)) {
                return false;
            }
            switch (c) {
                case '"':
                case '*':
                case '/':
                case ':':
                case '<':
                case '>':
                case '?':
                case '\\':
                case '|':
                case 0x7F:
                    return false;
                default:
                    return true;
            }
        }
    
    private static boolean isValidExtFilenameChar(char c) {
        switch (c) {
            case '\0':
            case '/':
                return false;
            default:
                return true;
        }
    }
    

    Note: FileUtils are hidden APIs (not for apps; for AOSP usage). Use as a reference (or by reflection at one's own risk)

    It is used as a sign to ignore the file.

    As jcesarmobile mentions,

    This is really an Android "feature". By default, it doesn't copy assets folders starting with _ to the resulting apk (see https://android.googlesource.com/platform/frameworks/base/+/b41af58f49d371cedf041443d20a1893f7f6c840/tools/aapt/AaptAssets.cpp#60).

    It could potentially creates platform specific problems. As rules for what passes for a valid filename are set by and dependent on the operating system, file system, compiler, linker and other parts of the compilation tool chain.

    For example,

    • In default ASP.NET MVC 3 projects, layout & partial .cshtml files start with an underscore. These files are not meant to be browsed. Files with a leading underscore are to be ignored.
    • Ruby on Rails does it with their Partials and same with Ember Partials
    • Partial Sass files
    • In Python, Modules starting with _ shouldn't be used directly; similarly, methods with _ are supposedly private and so on.
    • In LaTeX the _ is used to create a subscript and have to be included e.g. in $...$ to avoid error, but it will generate a text different from what you are expecting. The other contexts I found are included here.

    I believe modern systems allow almost anything that isn't directly forbidden by the file system. While I know some software will automatically replace underscores with spaces when displaying files to make them look nicer. However, Files can be named whatever you want if the underlying system supports the name.

    Some special characters can be employed: &, _, ^, but not %, ~, #, and \. Even { and } might be used, as long as they are accepted by the system.