Android 运行单元测试时,对findViewById()的引用不明确

Android 运行单元测试时,对findViewById()的引用不明确,android,findviewbyid,Android,Findviewbyid,今天早上,我将Android Studio更新为最新的稳定3.x版本,以及支持库、构建工具版本和新的gradle插件 我设法让一切都正常工作(在使用新的gradle插件时非常痛苦),但现在我遇到了一个我无法真正理解的问题: 项目编译完毕,PROJECT1和PROJECT2的单元测试运行良好, 但是当我尝试运行主项目的单元测试时,我得到了以下错误: 错误:(40,30)错误:对findViewById的引用不明确 活动中的方法findViewById(int)和方法findViewById(int

今天早上,我将Android Studio更新为最新的稳定3.x版本,以及支持库、构建工具版本和新的gradle插件

我设法让一切都正常工作(在使用新的gradle插件时非常痛苦),但现在我遇到了一个我无法真正理解的问题:

项目编译完毕,PROJECT1和PROJECT2的单元测试运行良好, 但是当我尝试运行主项目的单元测试时,我得到了以下错误:

错误:(40,30)错误:对findViewById的引用不明确 活动中的方法findViewById(int)和方法findViewById(int) 在AppCompatActivity match中,T是类型变量:T扩展视图 在方法findViewById(int)中声明

只有在我尝试运行测试时才会发生这种情况,应用程序(在其中我使用了大量对findviewbyd()的调用)工作正常。

我应该在哪里找到解决问题的方法

buildToolsVersion: 27.0.1
support repository version: 27.0.2
targetSdkVersion: 27
compileSdkVersion: 27
minSdkVersion: 19
这是一段发生问题的代码

@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);

    }
我可以构建和运行项目,但是当我运行测试时,我在所有测试类中都会遇到这种错误

这里是我的gradle文件,我有一个依赖于库项目的主项目

主项目的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'
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"
}
// 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"
}
PROJECT1的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'
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"
}
// 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"
}
项目2建在格拉德

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'
}
根构建.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'
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"
}
// 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"
}
我担心这个问题与空气有关,但我不确定这只是一种气味。
希望gradle文件能帮助您帮助我:)

您能将完整的java代码发布到问题所在吗?@Pavneet_Singh donei认为这
Robolectric.setupActivity(AppCompatActivity.class)
应该是
roblectric.setupActivity(YourActivityName.class)
我还进行了其他测试,使用MyActivityName,但也存在错误。Android O和Robolectric中更改的
findViewById
方法的签名需要兼容。如果您的CompileSDK版本为27,请使用Android O MR1的Robolectric(8.1,API 27)。也许是这个