Using Non-Production Activity for Testing with Android Studio
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.
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.