AndroidTest 不使用测试应用程序

bus*_*lee 4 android android-testing android-gradle-plugin

嗨,我遇到了当我在 androidTest 清单文件中重写应用程序时,它不起作用。这是我AndroidManifest.xmlandroidTest文件夹中的文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="ru.app"
    android:installLocation="auto">

  <application
      tools:replace="android:name"
      android:name=".app.ApplicationAndroidTest" />

</manifest>
Run Code Online (Sandbox Code Playgroud)

这是原来的一部分AndroidManifest.xml,从main文件夹中:

<application
        android:name=".app.Application"
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:theme="@style/Theme">
...
</application>
Run Code Online (Sandbox Code Playgroud)

事实上,我调试了它,并从被测活动的断点调用 getApplication(),它返回.app.Application而不是ApplicationAndroidTest实例。

你知道为什么 androidTest 中的 android manifest 文件会被忽略吗?

bus*_*lee 5

作为一种解决方法,我使用了自定义测试运行程序类:

public class UiTestsRunner extends AndroidJUnitRunner {

  @Override
  @NonNull
  public Application newApplication(@NonNull ClassLoader cl, @NonNull String className, @NonNull Context context)
          throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    return Instrumentation.newApplication(ApplicationAndroidTest.class, context);
  }
}
Run Code Online (Sandbox Code Playgroud)

对我来说没问题。希望它可以帮助某人。