Mono for Android: Unicode Assets file names can't be packaged

蓝咒 提交于 2019-12-08 02:11:57

问题


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?


回答1:


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.




回答2:


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().)



来源:https://stackoverflow.com/questions/10543713/mono-for-android-unicode-assets-file-names-cant-be-packaged

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!