I have a custom charset which is already working on JavaSE.
The class of my CharsetProvider is specified in a file java.nio.charset.spi.CharsetProvider
which is located in META-INF/services
and everything get's loaded normally and works as expected.
However now I'm using the lib on android as well, but the charset isn't loaded in Android-App.
How can I integrate my charset, so that it can be used like expected in an Android-App?
Charset.forName("MyCS");
At the moment I'm doing a workaround like this:
public static String decode(String encoding, byte[] buffer, int offset, int length) {
String result = "";
try {
result = new String(buffer, offset, length, encoding);
} catch (UnsupportedEncodingException ex) {
MyCharsetProvider provider = new MyCharsetProvider();
Charset cs = provider.charsetForName(encoding);
if (cs == null) {
Logger.getLogger(Converters.class.getName()).log(
Level.SEVERE,null,ex);
result = new String(buffer, offset, length);
} else {
result = cs.decode(ByteBuffer.wrap(buffer, offset, length)).toString();
}
}
return result;
}
Which works, but seems ineffective to me, since everytime I try to decode like this with my own charset, an exception will be thrown and a CharsetProvider-Object will be created.
The creation of course could be reduced by singleton pattern. But the issue is to avoid the direct usage of MyCharsetProvider entirely.
EDIT :
Since META-INF/services/java.nio.charset.spi.CharsetProvider
is missing in the apk I though maybe proguard removes it. I then tried the following options in proguard.cfg:
-adaptresourcefilenames **.CharsetProvider
-adaptresourcefilecontents **.CharsetProvider
But the problem still persists. so how can I get these files from META-INF/services
into my apk automatically using ant (netbeans)?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…