使用非生产活动进行Android Studio测试 [英] Using Non-Production Activity for Testing with Android Studio

查看:88
本文介绍了使用非生产活动进行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

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中创建 AndroidManifest.xml 文件

此清单与 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.

这篇关于使用非生产活动进行Android Studio测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆