问题
this morning I updated Android Studio to the latest stable 3.x build, together with support libraries, buildTools version and the new gradle plugin
I managed to get everything work (with a lot of pain dure to the new gradle plugin), but now I have a problem which I can't really understand:
The project compiles, and the unit tests of PROJECT1 and PROJECT2 run fine, but when I try to run unit tests of main project, I get this error:
Error:(40, 30) error: reference to findViewById is ambiguous both method findViewById(int) in Activity and method findViewById(int) in AppCompatActivity match where T is a type-variable: T extends View declared in method findViewById(int)
This happens only when I try to run the tests, the app (where I use a lot of calls to findViewById() ) is working fine.
Where should I look for to fix the issue?
buildToolsVersion: 27.0.1
support repository version: 27.0.2
targetSdkVersion: 27
compileSdkVersion: 27
minSdkVersion: 19
This is a piece of code where the issue happens
@Before
public void setUp() {
super.setUp();
ChangePasswordActivity mActivity = Robolectric.setupActivity(ChangePasswordActivity.class);
mPasswordBox = mActivity.findViewById(R.id.general_password_vPasswordBox);
mBtnNext = mActivity.findViewById(R.id.general_password_btnNext);
}
I can build and run the project, but when I run tests I get this kind of error in all the test classes
Here my gradle files, I have a main project which depends on library projects
MAIN PROJECT's build.gradle
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'com.fernandocejas.frodo'
apply plugin: 'realm-android'
apply plugin: 'checkstyle'
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
maven { url 'https://maven.fabric.io/public' }
}
task checkstyle(type: Checkstyle) {
source 'src'
include '**/*.java'
exclude '**/gen/**'
// empty classpath
classpath = files()
}
tasks.withType(Checkstyle) {
exclude '**Messages.java'
}
android {
testOptions {
unitTests {
includeAndroidResources = true
}
}
dexOptions {
javaMaxHeapSize "4g"
}
ext.getVersionCode = {
int ret = rootProject.ext.appVersionCode
if (System.getenv("BUILD_NUMBER") != null && System.getenv("BUILD_NUMBER").isInteger()) {
ret = System.getenv("BUILD_NUMBER") as Integer
}
return ret
}
int versionCodeVar = ext.getVersionCode()
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
versionCode versionCodeVar
versionName rootProject.ext.appVersionName
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
flavorDimensions "default"
productFlavors {
local {
dimension "default"
applicationId 'com.myapp.package.test'
buildConfigField "boolean", "DEBUGGING_OPTIONS_ENABLED", 'true'
}
demo {
dimension "default"
applicationId 'com.myapp.package.test'
buildConfigField "boolean", "DEBUGGING_OPTIONS_ENABLED", 'true'
}
production {
dimension "default"
applicationId 'com.myapp.package'
buildConfigField "boolean", "DEBUGGING_OPTIONS_ENABLED", 'false'
}
}
buildTypes {
debug {
testCoverageEnabled = true
buildConfigField "String", "LOCAL_SERVER_ADDRESS", 'null'
ndk {
abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "mips", "mips64", "x86", "x86_64"
ldLibs "log"
}
}
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
debuggable false
ndk {
abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "mips", "mips64", "x86", "x86_64"
}
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
String sharedTestDir = '../data/src/sharedTestData/java'
test {
java.srcDir sharedTestDir
}
androidTest {
java.srcDir sharedTestDir
}
}
useLibrary 'org.apache.http.legacy'
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
// allow to see logs when tests are executed from command line
tasks.withType(Test) {
testLogging {
events "standardOut", "started", "passed", "skipped", "failed"
}
systemProperty 'useMock', System.getProperty('useMock')
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// PROJECTS
implementation project(':project1')
// LIBRARIES
// dagger
implementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
//butterknife
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor "com.jakewharton:butterknife-compiler:8.8.1"
// libgdx
implementation 'com.badlogicgames.gdx:gdx:1.9.6'
implementation 'com.badlogicgames.gdx:gdx-backend-android:1.9.6'
implementation 'com.badlogicgames.gdx:gdx-box2d:1.9.6'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.airbnb.android:lottie:2.2.5'
implementation 'com.artemzin.rxjava:proguard-rules:1.1.7.0'
implementation 'org.zakariya.stickyheaders:stickyheaders:0.7.6'
implementation 'jp.wasabeef:recyclerview-animators:2.2.7'
implementation 'org.jsoup:jsoup:1.10.3'
implementation 'com.github.ParkSangGwon:TedPermission:v1.0.9'
implementation 'com.mikhaellopez:circularprogressbar:1.1.1'
compile('de.psdev.licensesdialog:licensesdialog:1.8.3') {
exclude group: 'com.google.code.findbugs', module: 'jsr305'
}
// TEST DEPENDENCIES
testImplementation 'com.badlogicgames.gdx:gdx:1.9.6'
testImplementation 'com.badlogicgames.gdx:gdx-backend-android:1.9.6'
testImplementation 'com.badlogicgames.gdx:gdx-box2d:1.9.6'
testImplementation 'com.badlogicgames.gdx:gdx-platform:1.9.6:natives-desktop'
testImplementation('org.robolectric:shadows-multidex:3.5.1') {
exclude group: 'com.google.guava', module: 'guava'
exclude group: 'com.google.protobuf', module: 'protobuf-java'
}
testImplementation('org.robolectric:shadows-support-v4:3.1.4') {
exclude group: 'com.google.guava', module: 'guava'
}
testImplementation 'com.android.support:appcompat-v7:27.0.2'
testImplementation 'org.robolectric:android-all:7.1.0_r7-robolectric-0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.assertj:assertj-core:3.8.0'
testImplementation 'org.mockito:mockito-core:2.11.0'
testImplementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
testImplementation 'eu.codearte.catch-exception:catch-exception:2.0.0-beta-1'
// INSTRUMENTATION TESTS
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.3-alpha', {
exclude group: 'com.android.support', module: 'support-annotations'
exclude group: 'com.android.support', module: 'support-v4'
exclude group: 'com.android.support', module: 'design'
exclude group: 'com.android.support', module: 'recyclerview-v7'
}
androidTestCompile('com.android.support.test.espresso:espresso-core:2.3-alpha', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile('com.android.support.test:runner:0.6-alpha', {
exclude group: 'com.android.support', module: 'support-annotations'
})
}
apply plugin: 'com.google.gms.google-services'
PROJECT1's build.gradle
buildscript {
repositories {
mavenCentral()
}
}
repositories {
maven { url 'https://maven.fabric.io/public' }
}
apply plugin: 'com.android.library'
apply plugin: 'realm-android'
apply plugin: 'checkstyle'
task checkstyle(type: Checkstyle) {
source 'src'
configFile = rootProject.file('app/config/checkstyle/checkstyle.xml')
include '**/*.java'
exclude '**/gen/**', '**/Messages.java', '**/countries.json'
ignoreFailures = false
// empty classpath
classpath = files()
}
check.dependsOn 'checkstyle'
configurations {
localDebugCompile
localReleaseCompile
demoDebugCompile
demoReleaseCompile
productionDebugCompile
productionReleaseCompile
}
android {
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
multiDexEnabled true
versionCode rootProject.ext.appVersionCode
versionName rootProject.ext.appVersionName
}
buildTypes {
debug {
buildConfigField "String", "LOG_LEVEL", '"DEBUG"'
}
release {
buildConfigField "String", "LOG_LEVEL", '"INFO"'
}
}
flavorDimensions "default"
productFlavors {
local {
dimension "default"
}
demo {
dimension "default"
}
production {
dimension "default"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
String sharedTestDir = 'src/sharedTestData/java'
test {
java.srcDir sharedTestDir
}
androidTest {
java.srcDir sharedTestDir
}
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/maven/com.google.protobuf/protobuf-java/pom.properties'
exclude 'META-INF/maven/com.google.protobuf/protobuf-java/pom.xml'
}
lintOptions {
quiet true
abortOnError false
ignoreWarnings true
}
}
tasks.withType(Test) {
testLogging {
events "standardOut", "started", "passed", "skipped", "failed"
}
systemProperty 'isMock', System.getProperty('isMock')
}
dependencies {
compile project(':project2')
// LIBRARIES
compile "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
compile "javax.inject:javax.inject:1"
compile 'com.squareup.okhttp:okhttp:2.7.5'
compile 'com.google.code.gson:gson:2.8.2'
compile "com.android.support:support-annotations:$rootProject.ext.supportRepositoryVersion"
compile 'com.google.protobuf.nano:protobuf-javanano:3.2.0rc2'
compile 'org.jsoup:jsoup:1.10.3'
compile "com.android.support:appcompat-v7:$rootProject.ext.supportRepositoryVersion"
compile 'com.android.support:multidex:1.0.2'
compile 'com.google.guava:guava:20.0'
compile('io.fabric.sdk.android:fabric:1.3.10@aar') {
transitive = true
}
// TRUST KIT
compile 'com.datatheorem.android.trustkit:trustkit:1.0.0'
api "com.google.firebase:firebase-core:11.6.2"
api "com.google.firebase:firebase-messaging:11.6.2"
api "com.google.android.gms:play-services-vision:11.6.2"
implementation "com.google.android.gms:play-services-places:11.6.2"
// TEST DEPENDENCIES
testImplementation "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
testImplementation "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
testImplementation "junit:junit:4.12"
testImplementation 'org.assertj:assertj-core:3.8.0'
testImplementation "org.robolectric:robolectric:3.5.1"
testCompile 'eu.codearte.catch-exception:catch-exception:2.0.0-beta-1'
testCompile "org.robolectric:shadows-multidex:3.5.1"
testCompile 'org.mockito:mockito-core:2.11.0'
androidTestCompile "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
androidTestCompile "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
androidTestCompile "com.android.support:support-annotations:$rootProject.ext.supportRepositoryVersion"
androidTestCompile 'com.android.support.test:runner:1.0.1'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.3-alpha', {
exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile('com.android.support.test:runner:0.5', {
exclude group: 'com.android.support', module: 'support-annotations'
})
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
testAnnotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"
provided "javax.annotation:jsr250-api:1.0"
}
PROJECT2's buid.gradle
apply plugin: 'com.android.library'
buildscript {
repositories {
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'io.fabric.tools:gradle:1.+'
}
}
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
maven { url 'https://maven.fabric.io/public' }
}
configurations {
provided
}
sourceSets {
main {
compileClasspath += configurations.provided
}
}
android {
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
multiDexEnabled true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
flavorDimensions "default"
productFlavors {
local{
dimension "default"
}
demo {
dimension "default"
}
production {
dimension "default"
}
}
}
dependencies {
provided "javax.annotation:jsr250-api:1.0"
annotationProcessor "com.google.dagger:dagger-compiler:2.0.2"
compile('com.crashlytics.sdk.android:crashlytics:2.6.7@aar') {
transitive = true;
}
compile "com.google.dagger:dagger:2.0.2"
compile "javax.inject:javax.inject:1"
compile 'com.google.guava:guava:20.0'
compile "com.google.code.gson:gson:2.7"
compile "io.reactivex:rxjava:1.3.0"
compile "io.reactivex:rxandroid:1.2.1"
testCompile "junit:junit:4.12"
testCompile 'org.mockito:mockito-core:2.11.0'
}
ROOT build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath ('com.google.firebase:firebase-plugins:1.1.5') {
exclude group: 'com.google.guava', module: 'guava-jdk5'
}
classpath 'com.jakewharton:butterknife-gradle-plugin:8.8.1'
classpath "com.fernandocejas.frodo:frodo-plugin:0.8.3"
classpath "io.realm:realm-gradle-plugin:3.1.1"
classpath 'com.google.gms:google-services:3.1.1'
}
}
allprojects {
tasks.withType(JavaForkOptions){
jvmArgs "-Xmx2048m"
}
repositories {
maven {
url "https://jitpack.io"
}
maven { url 'https://dl.bintray.com/kennyc1012/maven' }
maven { url "http://dl.bintray.com/jjhesk/maven" }
google()
jcenter()
}
}
ext {
appVersionCode = 49
appVersionName = "1.3.3"
protocolVersion = "0.9.5"
compileSdkVersion = 27
buildToolsVersion = "27.0.1"
targetSdkVersion = 27
minSdkVersion = 19
supportRepositoryVersion = "27.0.2"
playServicesVersion = "11.6.2"
daggerVersion = "2.0.2"
}
I am afraid that the problem is somehow related to the new dependency configurations , but I'm not sure about that, is just a smell. Hope that the gradle file can help you to help me :)
来源:https://stackoverflow.com/questions/47556249/reference-to-findviewbyid-is-ambiguous-when-running-unit-test