【问题标题】:MultiDexTestRunner does not work with MultiDexApplicationMultiDexTestRunner 不适用于 MultiDexApplication
【发布时间】:2015-03-13 04:00:09
【问题描述】:

我正在使用:

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

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

运行测试 测试运行开始测试运行失败:找不到仪器信息: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+'
}

【问题讨论】:

  • 几个问题问你:你达到dex限制了吗?为什么要使用 MultiDex?发布一个示例测试,您发布的测试是空的,这就是您收到错误的原因。
  • 我们正在使用第三方组件,这要求我们制作我们的应用程序 multidex。只需考虑 assertTrue(true);作为测试中的语句,使其变得非常简单!
  • 发布您的整个build.gradle。是的,但是测试是空的,你会得到你发布的错误。
  • 将所有内容发布到您的build.gradle.
  • 更新:我添加了完整的 build.gradle。另外,当我只使用 plan InstrumentationRunner 而不是 multi dex runner 时,测试似乎有效。

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


【解决方案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

【讨论】:

    【解决方案2】:

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

    为了使用InstrumentationTestRunner,您需要androidTest 文件夹中的 Instrumentation Tests。

    我强烈建议您将“基于 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')
        }
    }
    

    【讨论】:

    • 根据文档,我们可以在 gradle 中配置自己的结构。我认为没有任何理由立即这样做。请参阅tools.android.com/tech-docs/new-build-system/user-guide的项目结构@
    • @Adil 试试我发布的build.gradle。显然,您可以更改源集。我只是提出一个长期的建议。
    • 正如我在 cmets 中提到的,它可以与简单的 InstrumentationTestRunner 一起使用,但不能与文档 developer.android.com/tools/building/multidex.html 中建议的 multidex 一起运行
    • 那你不需要Multidex吗?你为什么要使用它?
    • 我想知道文档要求使用多 dex 仪表运行程序进行多 dex 应用程序!!
    猜你喜欢
    • 2019-05-07
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多