Before Android Studio, testing and Android app involved a separate Android project that would be ignored when building for production. With Android Studio, production code and test code exist within the same project, which itself only has one set of anything else (manifest, assets, resources).
This being the case, how would I define a custom Activity to be used only for testing? For Android to allow any Activity to be started, it must be declared in the manifest. Is there a way around this restriction? How can Android be instructed to load test-only Activities without polluting the production facets of the project?
Go to "Edit Configurations..." in the "Run" menu. In the left pane, select your application. In the right pane, in the "General" tab, in the "Launch Options" section, there is a "Launch:" dropdown. Select "Specified Activity", and enter the name of your activity as it appears in your Manifest.
AndroidX Test is a collection of Jetpack libraries that lets you run tests against Android apps. It also provides a series of tools to help you write these tests. For example, AndroidX Test provides JUnit4 rules to start activities and interact with them in JUnit4 tests.
ActivityScenarioRule launches a given activity before the test starts and closes after the test.
Here's how to do it.
1. Define a new build type in your build.gradle:
buildTypes { extraActivity { signingConfig signingConfigs.debug debuggable true } }
In mine I've given it the debug signing configuration and set it to debuggable; configure as you see fit.
2. Click the Sync Project with Gradle Files button.
3. Choose your new build type from the Build Variants window.
4. Set up source directories for your new build type
In my example, my files are going in the com.example.myapplication3.app
Java package.
src/extraActivity/java/com/example/myapplication3/app src/extraActivity/res
5. Create your new activity in the folders for your build type
Be aware that if you right-click on the package and choose New > Activity, there's a bug and it will not put the files for the activity into your new build type's folder, but it will put them in src/main instead. If you do that, you'll have to move the filers over to the correct folder by hand.
6. Create an AndroidManifest.xml file in src/extraActivity
This manifest gets merged with the version in src/main, so only add the bits that you need to overlay on top of the original:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication3.app" > <application> <activity android:name=".ExtraActivity" android:label="Extra Activity" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
In my example, I've set up my new activity as a launcher activity so I can see it in the Apps screen and confirm it's working; you may not need to do that. Since I'm giving my app two launcher icons, I also need to follow the advice at Two launcher activities and add this to my main actvity's intent-filter
(in src/main/AndroidManifest.xml); you may not need to do this either:
<category android:name="android.intent.category.DEFAULT"/>
Here's a screenshot of my project layout after all this is done:
This works for me. I can switch build types back and forth with the Build Variants window (you can see the tab for it on the left-hand side of the screenshot above); building the debug variant only gives me one activity, and building the extraActivity variant gives me two.
I had the same problem. By following the answer by Scott Barta, I simply created a folder named "debug" in the "src" folder and created an AndroidManifest.xml with the activity used only for testing. This way the activity is added in the debug variant without creating a new variant like in the Scott Barta's answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With