Mono for Android: Unicode Assets file names can

2019-05-26 06:50发布

When an asset contains unicode characters in the file name, for example Chinese or Arabic, the file can not be deployed to a package, it errors out.

Renaming the file to ANSI characters fixes it.

Is there a way to get MonoDevelop + MonoDroid deploy unicode Assets?

2条回答
Evening l夕情丶
2楼-- · 2019-05-26 07:05

I got this from the MonoDroid team (thanks jonp) and it works:

Since Android doesn't support Unicode asset filenames, you can instead set the file's Build action to EmbeddedResource and use .NET resources to access the resource:

  using (var s = new StreamReader (typeof (Activity1).Assembly
       .GetManifestResourceStream ("Úñîćödę.txt")))
   button.Text = s.ReadToEnd ();

(You may need to change the Resource ID property of the file to match the value passed to Assembly.GetManifestResourceStream().)

查看更多
叛逆
3楼-- · 2019-05-26 07:13

I can't find this documented anywhere, but asset filenames must be ASCII, because that's what the aapt tool requires:

/*
 * Names of asset files must meet the following criteria:
 *
 *  - the filename length must be less than kMaxAssetFileName bytes long
 *    (and can't be empty)
 *  - all characters must be 7-bit printable ASCII
 *  - none of { '/' '\\' ':' }
 *
 * Pass in just the filename, not the full path.
 */
static bool validateFileName(const char* fileName)
{
    const char* cp = fileName;
    size_t len = 0;

    while (*cp != '\0') {
        if ((*cp & 0x80) != 0)
            return false;           // reject high ASCII
        if (*cp < 0x20 || *cp >= 0x7f)
            return false;           // reject control chars and 0x7f
        if (strchr(kInvalidChars, *cp) != NULL)
            return false;           // reject path sep chars
        cp++;
        len++;
    }

    if (len < 1 || len > kMaxAssetFileName)
        return false;               // reject empty or too long

    return true;
}

I have no idea why Android/aapt has this requirement.

查看更多
登录 后发表回答