问题
After updating junit version to 4.13 in gradle dependencies, classes and annotations like Assert
, @Test
, etc. under the junit package are displayed as red when used in my code. Lint check says:
Unresolved reference: <any junit class>
However, when I build and run my tests, it will build and run just fine.
I have tried:
- restarting Android Studio
- Invalidate caches and restart
- Clean and rebuild project
- added
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
What works is downgrading junit to 4.12. How to get rid of this lint error, without downgrading version?
Update:
- When I check
ALT + ENTER
options and selectInspection 'Unresolved reference, in wrong test scope' options
>Suppress 'IncorrectScope' for file <name of file>
, it gets rid of these lint errors for that particular file. I still like to solve this problem without using Suppress though. - According to this issue, it looks like this bug is not fixed yet. For now I'm downgrading to 4.12.
- Removing both espresso and runner from dependencies also fixes the problem.
回答1:
Just add the following to your build.gradle (App) file:
configurations.all {
resolutionStrategy.force "junit:junit:$junit_version"
}
dependencies {
...
}
回答2:
The first of all check your external dependencies
In my case I found double jUnit
versions which is
Junit 4.12
Junit 4.13
And the older version is come with androidx.test
The solution is
1. exclude dependencies
or
2. remove `androidx.test`
or
3. add conflict startegy
回答3:
Borrowing from @VaheGharibyan's idea about duplicate junit versions, I discovered that the following code also fixes the problem.
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module('junit:junit:4.12') with module('junit:junit:4.13')
}
}
来源:https://stackoverflow.com/questions/59598124/android-unresolved-reference-when-using-junit-4-13