代码之家  ›  专栏  ›  技术社区  ›  penguin359

如何使现代Multidex与旧API的仪器化测试一起工作

  •  0
  • penguin359  · 技术社区  · 4 年前

    我用Multidex设置了一个应用程序,似乎可以加载并使用较旧的Android API,但是,我有时会从仪器化的测试运行器中收到错误,因为它找不到任何测试。我可以找到如何将multidex添加到Android应用程序的详细信息,但根据使用的支持库,有几种解决方案,似乎没有一种解决方案可以直接解决使用当前推荐的AndroidX库运行multidex的测试问题。在安卓API 19级上,我目前得到这个错误:

    $ ./gradlew connectedCheck
    > Task :app:connectedDebugAndroidTest
    Starting 0 tests on API-19(AVD) - 4.4.2
    
    com.android.build.gradle.internal.testing.ConnectedDevice > No tests found.[API-19(AVD) - 4.4.2] FAILED 
    No tests found. This usually means that your test classes are not in the form that your test runner
    expects (e.g. don't inherit from TestCase or lack @Test annotations).
    

    在API级别为21的模拟器上运行此程序可成功运行所有测试。我没有启用Proguard或minify。为了启用Multidex,我进行了以下更改。我在应用程序的build.gradle中添加了以下内容:

    android {
        defaultConfig {
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
    }
    
    dependencies {
        implementation "androidx.multidex:multidex:2.0.1"
        androidTestImplementation 'org.mockito:mockito-core:3.4.6'
        androidTestImplementation 'com.linkedin.dexmaker:dexmaker:2.28.0'
        androidTestImplementation 'com.linkedin.dexmaker:dexmaker-mockito:2.28.0'
    }
    

    我的清单定义了一个继承自 androidx.multidex.MultiDexApplication 在android:name属性中。

    我看到有人提到要为test应用程序的AndroidManifest.xml添加一个多线程测试运行器,但我目前还没有明确的清单,而且它似乎只在旧的Android支持库的上下文中,而不是AndroidX。我需要做些什么才能在API 16-19级上使用Multidex和AndroidX进行仪器测试?

    0 回复  |  直到 4 年前
        1
  •  2
  •   vitalyster    3 年前

    我的清单定义了一个继承自androidx.multidex的类。MultiDex应用程序

    1. 这仅在不重写代码中的Application类时有效。否则,您的Application类应该扩展 MultiDexApplication 如所述 documentation

    2. 现在可以找到测试,但会崩溃,我通过将测试运行器类添加到第一个索引中来解决这个问题 build.gradle :

    android {
      ...
      buildTypes {
        ...
        debug {
          multiDexKeepProguard file('multidex-config.pro')
        }
      }
    }
    

    multidex-config.pro 包含:

    -keep class androidx.test.** { *; }