callApplicationOnCreate被调用,但不是newApplication(扩展AndroidJUnitR



在Gradle和Run/Debug Configurations -> Android Tests -> MyInstrumentedTest -> General -> Specific instrumentation runner (optional)中设置testInstrumentationRunner "com.example.theapp.utils.CustomAndroidJUnitRunner"并扩展AndroidJUnitRunner后:

import android.app.Application;
import android.content.Context;
import android.support.test.runner.AndroidJUnitRunner;
public class CustomAndroidJUnitRunner extends AndroidJUnitRunner {
    @Override
    public Application newApplication(ClassLoader cl, String className, Context context) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return super.newApplication(cl, className, context);
    }
    @Override
    public void callApplicationOnCreate(Application app) {
        super.callApplicationOnCreate(app);
    }
}

我在newApplicationcallApplicationOnCreate中设置BP,看到callApplicationOnCreate被调用,而不是newApplication。有什么问题吗?

我认为newApplication()没有被调用,因为断点没有被击中,但似乎在调试器有机会附加之前调用了该方法。

如果您需要调试newApplication()方法,我建议添加Debug.waitForDebugger();在Runner构造函数中或在断点之前的任何时间。否则,使用其他标志来确定是否正在调用该方法(即不依赖于调试器和断点)。

在android 27源代码中的ActivityThread的第5732行,你会看到下面解释为什么你会看到这种行为:

        // Do this after providers, since instrumentation tests generally start their
        // test thread at this point, and we don't want that racing.
        try {
            mInstrumentation.onCreate(data.instrumentationArgs);
        }
        catch (Exception e) {
            throw new RuntimeException(
                "Exception thrown in onCreate() of "
                + data.instrumentationName + ": " + e.toString(), e);
        }
        try {
            mInstrumentation.callApplicationOnCreate(app);
        } catch (Exception e) {
            if (!mInstrumentation.onException(app, e)) {
                throw new RuntimeException(
                  "Unable to create application " + app.getClass().getName()
                  + ": " + e.toString(), e);
            }
        }

相关内容

  • 没有找到相关文章

最新更新