Can Activity classes be made launchable only during development/debug?

629 views Asked by At

A common situation I face is that I wish to test a custom View visibly on screen while it is being developed as part of a large application. I realise that unit testing exists, particularly using ActivityUnitTestCase, but as far as I understand these frameworks don't actually attach the components visibly on screen.

At present, the way I tend to do this is to just place the component wherever it will actually be used within the application. This often means needing to wait some time for the application to start or to navigate through parts of the application before it is visible, which can become cumbersome.

The better way I've found is to create an Activity within the application that exists purely for test purposes (e.g. to simply display a custom View I'm developing), which is launched to display the custom View (or whatever) that I am working on. To do this, the only intent filter I've assigned to that Activity in the manifest is android.intent.action.MAIN, and not android.intent.category.LAUNCHER. Then, I can simply create a Run/Debug Configuration in Android Studio to launch that Activity directly. This, as far as I believe, effectively allows me to have Activity classes which may only be launched by me from the IDE.

My questions are:

  1. Does omitting android.intent.category.LAUNCHER guarantee that users of the application won't be able to launch that Activity by any means nor be aware of its existence? Is this a safe to have Activity classes for development only?

  2. Is there a workflow or test framework of any kind I could use to improve on how I am doing this?

3

There are 3 answers

0
CommonsWare On BEST ANSWER

I realise that unit testing exists, particularly using ActivityUnitTestCase, but as far as I understand these frameworks don't actually attach the components visibly on screen.

Well, ActivityInstrumentationTestCase2 does, as does the new ActivityTestRule. I don't recall playing with ActivityUnitTestCase.

Does omitting android.intent.category.LAUNCHER guarantee that users of the application won't be able to launch that Activity by any means nor be aware of its existence?

No. It won't be easy for them to start it, but it is exported and has an <intent-filter>, so somebody could find a way.

Is this a safe to have Activity classes for development only?

I would put them in your debug sourceset, assuming that you are using Android Studio. Here is a sample project that has a production launcher activity in main and a diagnostic activity in debug. In a release build, the diagnostic activity does not even ship.

0
Sruit A.Suk On

Why don't declared some value like

public static boolean IS_DEBUG = true;

then in any part you want to show it, just put in condition like

if (IS_DEBUG) {
    your_develop_view.setVisibility(View.VISIBLE);
    //...
}
else {
    your_develop_view.setVisibility(View.GONE);
}

after you finish develop. before you publish, just change

IS_DEBUG = false;
0
user1712200 On

Instead of defining your own IS_DEBUG, just use BuildConfig.DEBUG