Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 696
Trevor Avatar asked Apr 01 '26 03:04

Trevor


1 Answers

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.

like image 199
CommonsWare Avatar answered Apr 03 '26 18:04

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!