Incorrect Unicode property near index - android

I'm trying to use my method from java backend in android app (the problem might be that the backend is using JAVA 1.7, and the android app JAVA 1.6). The method is:
public static boolean isAlphabetCharacter(String letter) {
String pattern = "\\A\\pL+\\z";
return letter.matches(pattern);
}
It crashes with: Incorrect Unicode property near index ...

You should try \p{L} instead of \pL :)

It is Weird. When developing Android app, i encounter this problem.
Unit test is passed when i use Regex("[\\pP\\pZ]"), but Android app encounter java.util.regex.PatternSyntaxException: Incorrect Unicode property
then i change to Regex("[\\p{P}\\p{Z}]"), both unit test and android app is ok.
I dont know why....

Related

Cannot access includeAndroidResource It is private in UnitTestOptions

I was trying Kotlin Gradle Script and Android Testing.
When I try to configure android resource for Testing I get the following error :
Cannot access includeAndroidResource It is private in UnitTestOptions.
What is the proper way to enable using android Resource in Android Unit testing?
After posting this question, I went back to Android Studio and tried to see what's there for autocomplete. I pressed Control+Space just after testOptions.unitTests. and boom, there was the option called isIncludeAndroidResources which was kotlin getter in TestOptions class for
private includeAndroidResource
which I then set to true.
Solution :
testOptions.unitTests.isIncludeAndroidResources = true
Lesson: Try to write a question to explain the problem, sometimes answer is inside your own head.

Emoji symbol 👍 in string.xml crashes app

I would like to integrate the emoji symbol 👍 in my android app. Therefore I looked up the hex code for the utf-8 symbol and added the following to my string.xml file:
<string name="thumbsup">Perfect <node>👍👍</node></string>
This should result into Perfect 👍👍. However, instead my app crashes when the call activity tries to display this:
JNI DETECTED ERROR IN APPLICATION: input is not valid Modified UTF-8: illegal start byte 0xf0
Not particularly perfect ;)
The fix for that is:
Add "--utf16" to aapt by adding
android {
aaptOptions {
additionalParameters '--utf16'
}
}
to your build.gradle file, and make sure you are not using aapt2.
See https://issuetracker.google.com/issues/37140916
It seems that newer versions of Android don't cause the crash (API 24 worked in my tests), but that doesn't help if you are supporting older versions. The best I have been able to figure out is to use Java coded strings.
public class AppEmojiStrings {
// This is only a workaround for emoji causing crashes in XML strings.
// Use the standard strings.xml for all other strings.
public static final String thumbsUp = "Thumbs up 👍";
public static final String iLoveNY = "I \uD83D\uDC99 NY";
}
There are a number of disadvantages with this method, the main one being that it can't be accessed in the layout XML files. But it may be a viable workaround for some situations.

Using arc4random_uniform with cocos2d-x

I was happy using arc4random_uniform since iOS, and also for iOS targets of cocos2d-x.
Turns out it doesn't work for Android. The error:
error: 'arc4random_uniform' was not declared in this scope
How can I get around this?
Worst case, at compile time I'd check if arc4random_uniform() exists, and if not, use some other method (like the old arc4random()...).
I really would like to avoid using different code bases for different targets here.
Any other suggestions?
Note: As cocos2d-x is a "one code"→"many platforms", delegating this issue to Java code for Android would work against that.
Some of the C++ libs you can use in ios are not available in Android.
Unfortunately arc4ramndom is just one of them.
So the only way is to use an alternative of the stdlib like std::rand() or the default random engine if you want something more.
This is an example about how to use std::default_random_engine to get a random value in a given interval.
int randomValue(int from, int to) {
std::random_device rd;
std::default_random_engine e1(rd());
std::uniform_int_distribution<int> uniform_dist(from, to);
int mean = uniform_dist(e1);
return mean;
}
You can use Cocos2d native methods for generating random numbers. CCRANDOM_0_1() for example generates a random CGFloat between 0 and 1.

Android Studio: Regex not working

I migrated my project from Eclipse to Android Studio and everything worked fine. In this project I have a Regex which should find all image urls in a json-object I get from an API.
This is the Regex I have:
Pattern pattern = Pattern.compile("https?:\\/\\/[^\"]*?\\.(png|jpe?g|img)");
Matcher matcher = pattern.matcher(obj.toString());
while(matcher.find()) {
ImageBackgroundFetcher.getInstance(mActivity).addImageToLoadingQueue(matcher.group());
}
obj is the JSONObject I have with the image urls. It looks like the following and I would like to extract the url of the image (the bold marked part)
{"image":"http:\/\/www.test.de\/media\/2015\/01\/16\/bildtitel.jpg"}
After I migrate the project from Eclipse to Android Studio this Regex isn't working any longer. The matcher.find()-Method did not return true and Android Studio gives me a warning in the code at the regex part "\\/\\/" where it says "redundant character escape"
I already googled but didn't find a solution. Any ideas how to solve the problem would be great, thanks in advance ;)
You can use AS function to check Regex.
Press Shortcut: Alt+Enter → check regexp
Okay, I am able to solve this issue. The correct regex is:
https?:\\\\/\\\\/[^\"]*?\\.(png|jpe?g|img)
I usually use this site to check:
http://regexr.com/
using the pattern:
(https?:\\/\\/.+(png|jpe?g|img))(?:")
and capturing the first group it should work.
Just check in java, escaping \ and / is not the same as the site i linked
Using your expression, the pattern:
https?:\/\/[^\\"]*?\\.(png|jpe?g|img)
should work too. This last string should not be escaped again. Check also your string in the original eclipse project!
It has to do with the way Java treats Regex, it needs String literals. So \\ for a single \

MVEL2 on android throws exception

Did anyone has experience with MVEL2 on android?
i've tried out the same code with a simple java program and later on android:
The following exception is thrown when executed on android:
E/AndroidRuntime(30793): java.lang.ExceptionInInitializerError
I tried the example from the mvel website:
String template = "Hello, my name is #{name.toUpperCase()}";
Map vars = new HashMap();
vars.put("name", "Michael");
System.out.println(TemplateRuntime.eval(template, vars));
If theres no solution could anyone suggest a template engine which works with android
and supports iteration?
MVEL2 tries to substring the first 3 characters of the system java.version property when initializing the parser, and under Android the version is 0. That causes a bunch of exceptions which eventually cause the ExceptionInInitializerError.
If you want to force the java.version property, you can simply set it yourself:
System.setProperty("java.version", "1.6");
I have no idea what kind of odd side effects this may cause for Android, but at least it gets the MVEL parser up and running without throwing exceptions!
System.setProperty with "java.version" key seems to be read only propery in android, so it won't work.
i've tried to integrate MVEL 2 into android with no success, try using EVAL lib

Categories

Resources