I have been working on sample UIAutomator project. I have created new Testcases using AndroidX Testing libraries.
I am able to build, run the test cases from the command line. But when I tried to open the app from Android Studio I can see most of the classes are not imported properly. For eg.,
import androidx.test.uiautomator.UiObject2;
import org.junit.Before;
The above two imports are showing as not imported. Likewise lots of classes are showing the same error except android.content.Context, android.content.Intent, etc.,
Can someone through some light on this. I am able to execute the testcases properly from the command line but not able to execute them properly from Android Studio.
I have Restarted PC/Studio, cleared caches, still the problem exists.
below is the dependency I have added in app/build.gradle.
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation 'androidx.test:core:1.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'junit:junit:4.12'
Added the below code in gradle.properties
android.useAndroidX=true
android.enableJetifier=true
I have pretty much followed the same what I have got from android-testing-master/ui/uiautomator sample code, not sure what I am missing.
I faced same problem too (Android Studio 3.4.2), uiautomator was red:
import androidx.test.uiautomator.UiDevice
"Clean Project" and "Rebuild Project" didn't help, even invalidating caches. But choosing another Build Variant helped me.
Related
Following is my code for custom plugin
import com.android.annotations.NonNull;
import com.android.build.gradle.LibraryExtension;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
public class CustomPlugin implements Plugin<Project> {
#Override
public void apply(#NonNull Project project) {
project.getPluginManager().withPlugin("com.android.library", plugin -> {
//code here
});
}
}
After upgrading android gradle plugin from 4.2.1 to 7.0 above custom plugin code showing error: package com.android.annotations does not exist
How to fix this error?
As I work in the same project as alphanso and took over the AGP upgrade from him, here is what I finally found out:
The code sits in the .buildSrc directory of our project and is compiled there using the Gradle plugins java-library and java-gradle-plugin.
Therefore android.useAndroidX=true has no effect as this works only if you use the Android plugins, either com.android.library or com.android.application.
The root cause of the suddenly missing classes (we had a few other ones missing from sub packages of com.android.build as well) was somewhere else:
With AGP 7.0 most library dependencies of com.android.tools.build:gradle changed from "compile time" to "run time" as you can see here:
AGP 4.2.2:
https://mvnrepository.com/artifact/com.android.tools.build/gradle/4.2.2
AGP 7.0:
https://mvnrepository.com/artifact/com.android.tools.build/gradle/7.0.0
This means that when switching to AGP 7.x you must add the missing dependencies manually.
In our case adding
implementation "com.android.tools:common:30.0.3"
solved the problem.
In android gradle plugin 7.0, the use of AndroidX Libraries is recommended. First, in your gradle.properties file, enable AndroidX by adding
android.useAndroidX=true
Add these dependencies also (To set up the other androidx libraries),
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Sync your gradle files and then change the import com.android.annotations.NonNull to androidx.annotations.NonNull
Pretty weird problem: Mockito deps look broken even though they actually work.
Why does this happen and how to fix it?
The tooltip over those errors says "unresolved reference".
Some details:
These are unit tests (under app/src/test), not Android instrumentation tests.
This only happens when the test is Kotlin. In equivalent Java test, Mockito deps show up just fine in Android Studio.
To reiterate, even though Mockito stuff is shown in red, it still works: the test compiles and passes, both on the command line and in Android Studio.
In build.gradle, under dependencies:
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.19.0'
// ...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50"
(Also I have a file named org.mockito.plugins.MockMaker containing mock-maker-inline, so that Mockito works with final Kotlin classes.)
Edit: This shouldn't be relevant, but for Android instrumentation tests, there's also this. (I had some problems updating to Mockito 2 earlier, so sticking with 1.10.19 there.)
// Here keeping older Mockito for now
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
// dexmaker needed for Mockito to work in androidTest
androidTestImplementation 'com.google.dexmaker:dexmaker:1.2'
androidTestImplementation 'com.google.dexmaker:dexmaker-mockito:1.2'
Using Android Studio 3.5.3
What I've tried
Re-sync the project (build.gradle)
Run clean Gradle task inside AS
Close the project and open it again
Quit Android Studio completely and open it again
Update
I think this is related to several versions of Mockito being present in the project.
When I Cmd-click on the names shown in red, it takes me to sources in mockito-core-1.9.5-sources.jar; but when I Cmd-click on ArgumentMatchers or MockitoJUnitRunner, it takes me to mockito-core-2.19.0-sources.jar, the correct one.
I investigated with app:dependencies, and the only reference to Mockito 1.9.5 is through dexmaker-mockito.
+--- com.google.dexmaker:dexmaker-mockito:1.2
| +--- com.google.dexmaker:dexmaker:1.2
| \--- org.mockito:mockito-core:1.9.5 -> 1.10.19 (*)
But as dexmaker dependency is only for androidTest it shouldn't affect anything under test, right...? 🤔
Edit: indeed, the androidTest deps somehow confused Android Studio; commenting out all mockito and dexmaker deps in androidTestImplementation removed the faulty error highlights (but as mentioned, the different version was used for a reason).
In the end, Android Studio update fixed this.
Oh well, this was a bug in Android Studio: updating to latest version (3.6.2) fixed it.
The androidTestImplementation dependencies were conflicting with testImplementation ones; see updated question for details.
I am unable to run the unit test when I import org.hamcrest.Matchers as I need lessThan(). My instrumentation tests compile properly while using the greaterThan matcher but not the unit tests
Code:
import org.hamcrest.CoreMatchers.*
import org.hamcrest.Matchers.lessThan
import org.junit.Assert.assertThat
import org.junit.Test
import java.util.*
Gradle Logs: https://pastebin.com/ibgzzrg1
Removing the 2nd line makes the project compile and runs the test.
In my case, I received a compiler error "Unresolved reference: Matchers" while running the Task :compileTestKotlin. It turned out that I had declared the hamcrest dependency as testRuntimeOnly. Changing it to testImplementation fixed the issue.
testImplementation("org.hamcrest:hamcrest:2.2")
It is interesting to check out the documentation here.
I had this same issue. What I was finding was that I was only experiencing this issue with instrumented tests. Non-instrumented tests were able to import and run fine.
What fixed this for me was adding androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' to my dependencies.
I had removed this thinking it wasn't necessary, since none of my tests explicitly use Espresso. Turns out it is, for reasons beyond my understanding.
I had been running my unit test so far without any problems until we decide to update the gradle version from 3.1 to 4.6 and the gradle plugging from 2.3.3 to 3.5. After this update the project run without any problem and even the unit tests runs fine but not the ones that need to import AndroidJUnit4 because it can't resolve the dependence any more.
On the build.gradle we are importing:
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
Any idea?
Thanks in advance
androidTestImplementation and the runner are used for UI tests. You seem to not be explicitly importing JUnit:
testImplementation "junit:junit:4.12"
I feel dumb for even having to ask this, but I can't find an answer anywhere. I'm trying to write simple unit tests that test static methods from my Android app and I've already added
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
To my app build.gradle file, but org.junit.test annotations or anything related to Junit4 will NOT resolve. There is no "Unit Test" build variant either.
Can anybody assist me? Thanks.
looks like you have not download dependencies:
try run "gradle build"
or sync gradle project if your IDE has this option