Emoji symbol 👍 in string.xml crashes app - android

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.

Related

String encoding in class library project

I am facing a strange problem.
I have a solution that contains an Android project and a class library project.
If in that library project I execute this line:
String s = "Código inválido";
This is actually shown when watching the variable: Código inválido.
However, when I do that assignment inside the Activity code, the text is shown correctly as Código inválido.
What may be wrong with this?
The origin of the problem was when I tried to show, in the Activity layout, a text that was assigned in a class library method. After a lot of digging, I found what I expose in this question.
Just in case, I tell you that all files encodings are UTF-8, and in the project settings all encodings are also UTF-8.
This is Android Studio 4.0.1
Thanks
Jaime

A string resource from an Android lib overrides a resource from the app with the same name

I created a simple Android project out of my curiosity. It contains two modules: app and lib. Each module has its own package name. I created one string resource in the lib module and one string resource in the app module. Like this:
<string name="my_str">my_str_from_lib</string>
<string name="my_str">my_str_from_app</string>
Then I set the texts to two TextViews in the app module:
fromLib.text = resources.getString(ru.maksim.sample.lib1.R.string.my_str) // here I expected to see the string from the lib.
fromApp.text = resources.getString(R.string.my_str)
In both cases it was my_str_from_app
I ran Lint and thought it would detect the fact of resource overriding. But it didn't.
Is there a chance to detect this situation? Not necessarily with Lint. Other tools suit me too.
Well you can do a global search in your root folder. Find all instances of my_str and make sure its only the string.xml in your app that uses it as name.
The answer here is a script that parses a resulting XML to which all the string from the main app module and all its libraries are merged and finds all the duplicates.

How can I ignore all hard coded strings inside Log in Android Studio?

I have been reading about ignoring hardcoded string from Log:
Log.d(TAG, "onBindViewHolder: ");
Even inside a Toast:
Toast.makeText(context,"Hardcoded text",LENGTH_SHORT).show();
Sources:
How can I find all RELEVANT hard coded strings in Android Studio?
Android find all hardcoded strings in code using Android Studio
In the first source there is the best approach but somthing is not working and that is:
"adding #SuppressLint("HardcodedNonLog") to the top of the class definition"
Is there a way to ignoring these kind of hardcoded strings?
UPDATE
Create a Gradle task to add "//NON-NLS" to every Log, could be a good way? If the answer is yes, how could I do that?
Yes, you can!
At least with Android Studio 3.0 canary 9 (not checked with previous version).
There is option like "Annotate class 'Log' as #Non_Nls"
After that will be generated file at path "$modulename/android/util/annotations.xml" with this content:
<root>
<item name='android.util.Log'>
<annotation name='org.jetbrains.annotations.NonNls'/>
</item>
</root>
With this file all strings mentioned in Log will be ignored for this module.
Hardcoded text is not an error it is a warning which can be dealt with by pressing left alt+ Enter. When you press alt+Enter you are just defining that hardcoded string to string.xml in values folder.

Android Release error: Expected a color resource id (R.color.) but received an RGB integer

I have an application in the market for a year. Last week, I have changed the source code of my app. When I wanted to build the release version Android Studio throws an Error:
"Error: Expected a color resource id (R.color.) but received an RGB integer [ResourceType]"
Color is only used in this part of code and I haven't made ​​any changes in this part:
if (android.os.Build.VERSION.SDK_INT >= 16) {
rlFlash.setBackground(new ColorDrawable
(Color.parseColor(("#86cc55"))));
}
else{
rlFlash.setBackgroundDrawable(new ColorDrawable
(Color.parseColor(("#86cc55"))));
}
It is so strange that in the Debug version Android studio didn't throw any error and I can build the apk.
Do you know whats happen??
Thanks.
Though it's too late, but this answer is for those who may still get this error while taking release build like me. Don't know if it's a solution or just workaround.
#SuppressLint("ResourceType")
Use this at your method signature like below where you get the error.
#SuppressLint("ResourceType")
public void aMethodWhereYouMayGetTheError(){
}
By using this you will be able to take release build without that error.
Do it like this:
rlFlash.setBackgroundColor(Color.parseColor("#86cc55"));
If you have used the anotation #ColorRes in this method then remove that. The apk will be generated successfully.
Refer to SO Answer to clarify further about the lint checks. The answer is quoted below:
There are Java annotations to support these checks in your own code.
They can all be found in the android.support.annotations package:
IdRes DrawableRes LayoutRes StringRes ColorRes &c In this case,
for example, I could use:
private void mySetContentView(#LayoutRes int resourceId) {
setContentView(resourceId);
}
and Android Studio will check that the provided resource id is indeed
for a layout. Moreover, these annotations are exported, so they can be
especially useful when designing a library.

Incorrect Unicode property near index

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

Categories

Resources