3

After setting testInstrumentationRunner "com.example.theapp.utils.CustomAndroidJUnitRunner" in Gradle and in Run/Debug Configurations -> Android Tests -> MyInstrumentedTest -> General -> Specific instrumentation runner (optional) and extending 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);
    }
}

I set BP in newApplication and in callApplicationOnCreate and see that callApplicationOnCreate is called, but not newApplication. What could be the problem?

Tar
  • 8,529
  • 9
  • 56
  • 127

2 Answers2

0

I thought newApplication() wasn't being called as the breakpoint wasn't being hit, but it appears that the method was being called before the debugger had the chance to attach.

If you need to debug the newApplication() method, I recommend adding Debug.waitForDebugger(); either in the Runner constructor or anytime before your breakpoint. Otherwise use some other flag to figure out whether the method is being called or not (ie don't rely on the debugger and breakpoints).

TheIT
  • 11,919
  • 4
  • 64
  • 56
0

On line 5732 of ActivityThread in the sources of android 27 you will see the below which explains why you are seeing that behavior:

        // 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);
            }
        }
nbransby
  • 359
  • 4
  • 12