Cannot access includeAndroidResource It is private in UnitTestOptions - android

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.

Related

Can't retrieve resources programmatically using Kotlin

I was trying to load a drawable programmatically in my kotlin app via
resources.getDrawable(R.drawable.XXX)
and all I got was:
Method threw 'android.content.res.Resources$NotFoundException' exception.
whatever the target drawable was.
After trying different things, I finally tried on a newly created Kotlin project, only to find out it didn't work either.
I then created a new Java project, and everything worked flawlessly in this one.
I found nothing about people having the same problem online, I can get the "Resources" object in both project, but it just can't find drawables in the Kotlin one.
Is there anything to do to make it work that I don't know about?
Edit:
I'm getting the resources like this in the newly created project:
override fun onResume() {
super.onResume()
val drawable = resources.getDrawable(R.drawable.ic_launcher_foreground)
}
This happens whatever the target resource is, drawables, mipmaps, colors...
I'm on Android Studio 3.5.3 with gradle 3.5.3 and Kotlin 1.3.61, API level 26
Project resources are the one added on project creation
I found a temporary fix, and mostly something very interesting.
Taking the example of "ic_launcher_background" as a random drawable, Java gives this result :
While the exact equivalent code in Kotlin gives us this :
I don't understand why, but when using a project generated the Kotlin way, the Ids retrievable via R just don't match the ones used by the Resources class.
So for now I'll use the "getIdentifier()" method to fix my problem, but it really isn"t something you'd want to do naturally.
If someone that has a better understanding of what is going on here could unfold this mystery, that would be greatly appreciated.
Try this:
override fun onResume() {
super.onResume()
val drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_launcher_foreground, null)
}

Various questions #NonNull, #NotNull and #ParametersAreNonnullByDefault

I'm an Android dev who is using AndroidStudio or IntelliJ IDEA.
I tend to trust my IDE and I'm annoyed with next facts:
In android project when IDE autogenerates a method in java that extends Kotlin class then both IDE uses #NotNull instead of #NonNull, is there setting for this? This
We have #ParametersAreNonnullByDefault but when I override the method from the point 1 and I don't put any annotation IDE warns me, but why?
Am I wrong in my assumptions?
Are there solutions?
Which annotations to use for null/not-null is set under Configure annotations... in the Compiler page of the Settings/Preferences dialog. Select the one you want and press the checkmark button. See https://www.jetbrains.com/help/idea/nullable-notnull-configuration-dialog.html for documentation.
I can't test right now whether IDEA/AS use the default annotations from there when overriding a method which already uses another, but if they don't you'll need to file a ticket.

Class name is marked as 'not covered' while running code coverage in Android Studio

I have tried AndroidStudio's code coverage feature and I have met a strange issue:
It is marks the tested class's name as 'not covered' code.
How is that possible? Is that a bug?
Picture here:
As you can see it has one method with 4 lines and each one of them is covered. So why is the red line at the class's name?
You are using a static method, so the class itself is never created as an object, therefore never testing that ability.
I tried the lombok #UtilityClass, it helped to ignore the class name and code coverage was improved to be 100%.
Since also a class with a static function has a default no-argument constructor, your code coverage tool will complain. A good way to solve this, is by adding a private constructor.
private EmailValidator() {
}

AndroidAnnotations cannot find symbol class

I'm using AndroidAnnotations in an Android Studio gradle project. I currently get error output from AA during compilation that says:
cannot find symbol class MyActivity_
The error output does not prevent building the application - its not really a compile error because the class is there, it seems that it is just an unfortunate timing issue with the compilation process.
Is there anything I can do to avoid these false-positive errors from AA? When there are "fake" errors shown every time I compile, its very easy to miss the real errors.
I had same error. To solve it I have revert my last changes and it has worked again.
I think it was either wrong optimized import(you must import generated classes eg. xxx_) or I injected layout by id what was not existed in the layout xml
Update
I figured it. My problem was what I had use private mofidier instead of proteced in
#ViewById(R.id.list)
private ListView list;
Try to see if you missed to fix some errors in the class MainActivity or in someone of his Bean member that you have annoted.
The problem doesn't have to be in MainActivty, but it is probably because of a private modifier which is used with Android Anotations (in injection, method declaration etc) somewhere in your code

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