在 Android Studio 中使用非生产活动进行测试
在 Android Studio 之前,测试和 Android 应用程序涉及一个单独的 Android 项目,在为生产构建时会被忽略.使用 Android Studio,生产代码和测试代码存在于同一个项目中,而该项目本身只有一组其他内容(清单、资产、资源).
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).
既然如此,我将如何定义仅用于测试的自定义活动?为了让 Android 允许启动任何 Activity,它必须在清单中声明.有没有办法绕过这个限制?如何指示 Android 在不污染项目的生产方面的情况下加载仅用于测试的活动?
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?
这是怎么做的.
1.在 build.gradle 中定义一个新的构建类型:
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.点击将项目与 Gradle 文件同步按钮.
3.从 Build Variants 窗口中选择您的新构建类型.
3. Choose your new build type from the Build Variants window.
4.为您的新构建类型设置源目录
在我的示例中,我的文件位于 com.example.myapplication3.app
Java 包中.
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.在文件夹中为您的构建类型创建新活动
请注意,如果您右键单击包并选择新建 > 活动,则会出现错误,它不会将活动的文件放入您的新文件中构建类型的文件夹,但它会将它们放在 src/main 中.如果这样做,则必须手动将文件管理器移至正确的文件夹.
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.在 src/extraActivity
此清单与 src/main 中的版本合并,因此只需添加您需要覆盖在原始清单之上的位:
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>
在我的示例中,我已将我的新活动设置为启动器活动,以便我可以在应用"屏幕中看到它并确认它正在运行;你可能不需要这样做.由于我给我的应用程序提供了两个启动器图标,因此我还需要遵循 两个启动器活动上的建议并将其添加到我的主要活动的 intent-filter
(在 src/main/AndroidManifest.xml 中);您可能也不需要这样做:
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:
这对我有用.我可以使用 Build Variants 窗口来回切换构建类型(您可以在上面屏幕截图的左侧看到它的选项卡);构建 debug 变体只给我一个活动,构建 extraActivity 变体给我两个.
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.