MultiDexTestRunner不适用于MultiDexApplication

时间:2015-03-13 04:00:09

标签: android android-studio android-gradle android-instrumentation

我正在使用:

  • Android Studio 1.1.0
  • Gradle 2.2.1
  • Gradle的Android插件:1.1.0

我们最近搬到了Android Studio,我们正在努力让我们的仪器测试工作。但是当我运行测试时,它会给我错误

运行测试 测试运行startedTest运行失败:无法找到以下内容的检测信息:ComponentInfo {com.xxx.xxx.test / android.test.InstrumentationTestRunner} 空测试套件。

我不确定我在这里做错了什么。请参阅下面的配置:

申请类:

public class XxxApp extends MultiDexApplication
{
...
}

App build.gradle

apply plugin: 'com.android.application'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.google.android.gms:play-services-base:6.5.87'
    compile 'com.google.android.gms:play-services-maps:6.5.87'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.android.support:multidex:1.0.0'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'joda-time:joda-time:2.7'
    compile 'org.roboguice:roboguice:2.0'
    compile project(':Common')
    compile project(':zxing-lib')
    androidTestCompile fileTree(dir: 'test-libs', include: '*.jar')
    androidTestCompile('com.android.support:multidex-instrumentation:1.0.1') {
        exclude group: 'com.android.support', module: 'multidex'
    }
    androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
    androidTestCompile 'junit:junit:4.11'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
}

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"
    enforceUniquePackageName false
    dexOptions {
        javaMaxHeapSize "4g"
    }
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 21
        applicationId "com.xxx.xxx"
        multiDexEnabled true
        testApplicationId "com.xxx.xxx.test"
        testInstrumentationRunner 'android.test.InstrumentationTestRunner'
        // Does not work testInstrumentationRunner 'com.android.test.runner.MultiDexTestRunner'
    }
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'LICENSE.txt'
    }
    lintOptions {
        abortOnError false
        checkReleaseBuilds false
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')
        androidTest.java.srcDir('tests/src')
        androidTest.res.srcDir('tests/res')
        androidTest.assets.srcDirs('tests/assets')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    productFlavors {
    }
}

测试类

public class XxxTests extends InstrumentationTestCase
{
   public void testXxx()
   {
    ...
   }
}

顶级build.gradle

dependencies 
{
        classpath 'com.android.tools.build:gradle:1.1.0+'
}

2 个答案:

答案 0 :(得分:1)

原始问题中描述的确切情况可能与较旧版本的工具有关。

我在这里添加了我的经验,因为这是使用... Unable to find ... ComponentInfo ...错误消息进行搜索时Google首次点击之一。

使用最新版本的工具和Android插件,您似乎除了build.gradle之后不需要其他任何操作才能让测试与multiDexEnabled true一起使用:

...
android {
    ...
    defaultConfig {
        ...
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }
}

(我有com.android.tools.build:gradle:1.5.0

答案 1 :(得分:0)

我不确定compile project(':Common')是什么,很可能是Android库;但是,除了该库之外,您没有理由使用MultiDex

要使用InstrumentationTestRunner,您需要在androidTest文件夹中进行检测测试。

我强烈建议您将“基于Eclipse”的项目结构移动到Android Studio的默认Gradle结构:“src/main/javasrc/test/javasrc/androidTest/java

热门build.gradle

dependencies {
    classpath 'com.android.tools.build:gradle:1.1.3' // <-- Updated
}

应用build.gradle

apply plugin: 'com.android.application'

dependencies {
    compile project(':Common') // <-- Not sure what this is
    compile 'com.google.android.gms:play-services-maps:6.5.87'
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'joda-time:joda-time:2.7'
    compile 'org.roboguice:roboguice:2.0' // <-- There is a newer versoin, but might break your current code
    compile 'com.google.zxing:core:3.2.0'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.+'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.+'
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
}

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.0"
    enforceUniquePackageName false

    dexOptions {
        javaMaxHeapSize "4g"
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 22
        applicationId "com.xxx.xxx"
        testApplicationId "com.xxx.xxx.test"
        testInstrumentationRunner 'android.test.InstrumentationTestRunner'
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'LICENSE.txt'
    }

    lintOptions {
        abortOnError false
        checkReleaseBuilds false
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')
        androidTest.java.srcDir('tests/src')
        androidTest.res.srcDir('tests/res')
        androidTest.assets.srcDirs('tests/assets')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}