R.id not found when writing Espresso test - android

I tried writing a small test to try Espresso.
package com.mycompany.myapp.somemodule
import com.mycompany.myapp.R
import other.uselful.imports
#RunWith(AndroidJUnit4::class)
#LargeTest
class DemoTest {
#get:Rule
var startActivity: ActivityTestRule<StartActivity> = ActivityTestRule(StartActivity::class.java)
#Test
fun aDemoTest() {
onView(withId(R.id.theElementId))
.check(matches(isClickable()))
}
}
Android Studio doesn't show any error. if I ctrl+click on R.id.theElementID, it finds it in the appropriate layout file. However when I try to run, I get a compile error:
Unresolved reference: id
How can I solve this?

I bumped into this issue. Spent 4 hours investigating, and no luck finding anything useful on the internet.
So, I'm hoping that the information below may help someone.
Short answer:
clean the project
execute gradlew :app:build from the command line / Terminal, it may show a build error which leads to the root cause
The longer answer:
I did the following:
I hit "Invalidate caches and restart" from Android Studio
I've executed gradlew :app:build from the command line
The second step printed an error message that the BuildConfig was duplicated.
The reason for this issue was, that I had 2 modules (android-app and android-library) with slightly different package names (I made a typo when creating the second one). This led to the failed build from the command line. After modifying the 2nd module's package name to be the same as the 1st one, the issue was solved.
I didn't receive the "R.id not found" error. So, I could continue to write the UI tests, which worked :-)
Happy coding!

Short answer: Try remove any incorrect import android.R statement.
Long answer
I also have faced this issue.
In my case, in test class, I was importing android.R package due to referring some incorrect resource id previously (R.id.progress) which belonged to android.R package.
I removed that reference as well ans the import statement i.e. import android.R from the test class and the issue resolved. Then source recognized the R.id.etPassword resource id of my layout file.

guess the actual problem is, that the activity had not been started.
R.id.theElementId might be an invalid resource descriptor.
try R.id.the_element_id instead.
also, add #UiThreadTest annotation ...

Related

"Expected resource of type interpolator [ResourceType]" and "Cannot resolve symbol" error when generate a signed APK

I have tested my app on many devices and everything goes well but when I'm trying to generate a signed apk this message appears: Expected resource of type interpolator [ResourceType]
This line seems the problem:
scaleDown.setInterpolator(AnimationUtils.loadInterpolator(activity, android.R.anim.decelerate_interpolator));
Behind this line there is a class called AnimationUtils, this class belongs to android SDK so I don't understand why when I inspect its the source code the following imports show me Cannot resolve symbol
import android.annotation.AnimRes;
import android.annotation.InterpolatorRes;
I already tried clean and rebuild.
I finally overcome this issue replacing
AnimationUtils.loadInterpolator(activity, android.R.anim.decelerate_interpolator)
To this
new DecelerateInterpolator()
1.try to change android.R.anim.a to R.interpolater.b

Android: Gradle Error in Android Studio

I recently added some .gifs to my /drawable so that I can use them with buttons. This worked fine (no errors). Now, when I go to rebuild/run my application, I get the following error:
Error: Gradle: Execution failed for task ':MyProject:mergeDebugResources'. > Index: 0
I'm not sure how to fix this (and there appears to be no other similar issues that I could find online.
Edit: Now it's the same error, but it also says (at the end of the first error)
Running /Applications/Android Studio.app/sdk/build-tools/android-4.2.2/aapt failed. See output
Okay, I fixed the issue. It was quite weird.
A) I had some capital letters in my res/drawables folder. Not too weird -- I can see how that would remove the first error.
B) Then, however, a lot of values in my main activity file became undefined. I had to manually import 'com.myapp.R'. Now that was weird.
Fortunately, everything is now working.
I hope that if anyone gets the same errors as me, that this response can help them.

Code Coverage on Android

I know that there are many questions that have been asked about Android code coverage (I have done some researches before asking here), yet I still get stuck at some points so if you can help, I will appreciate it very much.
What I am doing right now is not really Unit Test but Activity Test to verify the views (check whether they're null or not, check the buttons if they're clickable, counts the number of view in one activity), then I implement some code to test public methods of each activity too and I want to know the code coverage of those tests. First I try EclEmma but it seems like they don't support android so I move to Emma. Then I have a common error as many many people on the Internet:
[exec] android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests:INSTRUMENTATION_RESULT: shortMsg=java.lang.IllegalAccessError
[exec] INSTRUMENTATION_RESULT: longMsg=java.lang.IllegalAccessError: Classref in pre-verified class resolved to unexpected implementation
[exec] INSTRUMENTATION_CODE: 0
[echo] Setting permission to download the coverage file...
[exec] Unable to chmod /data/coverage.ec: No such file or directory
[echo] Downloading coverage file into project directory...
[exec] remote object '/data/coverage.ec' does not exist
I tried many things that other people do but they just can't fix my issues. I changed from data/data/coverage.ec to sdcard/coverage.ec and now is data/coverage.ec, none of them works. I tried to mount -o remount rw /sdcard, that doesn't work too.
Is there any other ways to fix this? Are Activity Tests even test-able? or I have to change to something other than Emma?
My bad, at first I followed a site (I don't remember) to copy emma.jar into my test project /libs/. I think it somehow calls the jar file twice so it throws IllegalAccessError . I solve this problem by deleting all emma, emma_ant jar files and copy them into /Java/jdk/jre/lib/ext. Now all of my tests run fine and I can get the code coverage now.

Adding a breakpoint in Eclipse, causes Dalvik to crash

This is an interesting bug of Eclipse. I am using:
Version: Indigo Release
Build id: 20110615-0604
Out of nowhere today, I set a breakpoint in my android app on Eclipse. When it reaches the code, debug (dalvik JVM) aborts and crashes:
Tried to execute code in unprepared class (followed by the class name)
If I don't set the break point, it will run through with no problem. I have searched through Google but found nothing so far.
Another note, where I set this breakpoint does not matter, it could simply be the 1st line of onClick() function, or anywhere else. The moment the app "pauses" due to the breakpoint, it crashes.
I run into the same issue; for me, the problem was solved by removing all of the watched expressions I had set up in the debugger (one of them was causing the problem).
I run into the same issue when used watch expression with static method of this (unprepared) class.
I solved this issue by removing this expression from expressions list.
I solved this issue by removing an unused import package.
actually the error message is saying "tried to execute code in unprepared class" (followed by the class name)
I am not using that class but it is imported.
Simply remove the import, and it works fine ....

Error NullPointerException when running an altered version of an Android Project

I have this Android program running well, called TestePHP2 (name of main class), package com.testedeacesso.www. It is stable and runs perfectly.
I've cloned this project's folder to start developing an upgrade to it. I called it TesteTempo01, after importing it to Eclipse - option 'from existing project' (yes, I used the refractor to change the class name and I checked 'update references'). I modified this program's main class as I mentioned before and added some code to it, but I kept the package name the same, as well as other classes from the program.
Whe I try to run this program, though, I got a 'can't find main activity' error. After searching through my code, I found out that on the manifest there was a reference to the TestePHP2 that I hadn't changed before. for reference, this was the line on the Android Manifest XML:
> <activity android:name="com.testedeacesso2.www.TesteTempo01"
> android:label="#string/app_name">
After this, I keep getting a NullPointerException because the class can't find the main class! I've searched through the whole code but I found no other references to the old class. Have I forgotten something? Do I need to alter something else to change this class' name? Or the import will only work with the original class names?
You said the package was 'com.testedeacesso.www', but when you reference the class, its 'com.testedeacesso2.www'. I'm guessing you have a problem with class/package references.

Categories

Resources