【问题标题】:Build UiAutomator 2.0 from command line从命令行构建 UiAutomator 2.0
【发布时间】:2015-03-27 13:21:01
【问题描述】:

我想通过命令行构建、安装和运行 UiAutomator 项目的测试。

在我之前的版本中:

android create uitest-project -n <project_name> -p <project_path> -t <target_id>
ant build

建造 然后

adb push <jar_path> /data/local/tmp

安装,最后

adb shell uiautomator runtest <jar_name> -c <main_class>

但是,现在我被困在构建部分

结果是

-check-env:
  [checkenv] Android SDK Tools Revision 24.1.2                                    
  [checkenv] Installed at C:\Android                                                                                                                             
-build-setup:                                                                   
[getbuildtools] Using latest Build Tools: 22.0.0                                     
    [echo] Resolving Build Target for AndroidExplorerTester...                 
[getuitarget] Project Target:   Android 5.0.1                                   
[getuitarget] API level:        21                                                   
    [echo] ----------                                                               
    [echo] Creating output directories if needed...                                                                                                            
-pre-compile:                                                                                                                                                   
compile:                                                                                                                                                        
-post-compile:                                                                                                                                                  
-dex:                                                                                 
    [dex] input: <test_path>\bin\classes                                                                          
    [dex] Converting compiled files and external libraries into <test_path>\bin\classes.dex...
     [dx] no classfiles specified                                                                                                                             
BUILD FAILED                                                                    
C:\Android\tools\ant\uibuild.xml:198: null returned: 1                                                                                                          
Total time: 1 second             

不知道自从新版UiAutomator之后有没有更好的办法。

注意:我不知道这是否重要,但我之前使用的是 Eclipse,现在我使用的是 IntelliJ(如果你喜欢 Android Studio,哈哈)

【问题讨论】:

    标签: android shell testing android-uiautomator


    【解决方案1】:

    对于那些不想搬到 gradle 并希望与 ant 呆在一起的人来说,这是另一种方式。顺便说一句,主要原因,为什么旧​​方法不起作用,是 uiautomator 从其 2.0 版本开始从 jar 的独立测试运行器转移到应用程序的标准 android 'am instrument' 测试运行器。这一招只有一个“对立”。现在测试项目应该绑定到一个明确的目标应用程序(参见第一步中的解决方法)。所以这是一个计划。

    首先,您应该有一个目标项目,您的测试就是针对该项目设计的。实际上它可以是一个空的应用程序,它根本不会显示,无论是在应用程序菜单中,还是在测试期间。我已经设法在 Eclipse 中创建了一个,而没有在向导中创建任何活动。让 ant 的 build.xml 运行:

    android update project --target 0 --path %path_to_empty_app%
    

    有关 android 工具的更多信息,请参见 http://developer.安卓。 com/tools/projects/projects-cmdline.html

    注意:您可能希望为目标应用授予必要的权限,这些权限将传播到测试应用。现在测试不是以 shell 用户权限运行的。

    第二步是创建一个测试项目。正如我所提到的,uiautomator 现在集成在标准的 android 测试方案中。因此,它使用标准命令来创建测试应用程序:

    android create test-project -m %path_to_target_app% -n %test_app_name% -p %path_to_test_app%
    

    会在 %path_to_test_app% 中创建一个常用的应用结构,包括 ant 的 build.xml 欲了解更多信息,请参阅http://developer.android.com/tools/testing/testing_otheride.html

    第三:复制 uiautomator classes jar 来测试应用程序库。 jar 可以从 SDK 中的 *.aar 存档中提取,位于 \extras\android\m2repository\com\android\support\test\uiautomator\uiautomator-v18\2.1.0 或类似文件中。

    第四:把你的测试类 *.java 放到 test app src 文件夹。请注意此处 uiautomator 中的以下更改:

    1. 包从 com.android.uiautomator 重命名为 android.support.test.uiautomator
    2. UiAutomatorTestCase 类是 为兼容性而保留,但已弃用;扩展你的测试类 从 InstrumentationTestCase 获取 UiDevice 实例,使用 UiDevice.getInstance(getInstrumentation())

    第五:安装并运行您的测试。简单的方法如下:

    cd %path_to_test_app%
    :: Here 'ant instrument install' builds and installs both target and test apps.
    ant instrument install
    ant test
    

    或者最后一行可以修改为

    adb shell am instrument -w %target_app_full_name%.tests/android.test.InstrumentationTestRunner
    

    更多信息见http://developer.android.com/reference/android/test/InstrumentationTestRunner.html

    【讨论】:

      【解决方案2】:

      我终于明白了。

      从命令行,在项目的主文件夹(您可以在其中找到 gradlew.bat 文件)中运行以下命令

      构建

      .\gradlew.bat assembleDebug
      

      在设备上安装

      .\gradlew.bat installDebug
      

      (如果您想要 Release 版本,只需将 Debug 替换为 Release,我没有尝试过,但选项存在,所以我想它们可以工作)

      运行

      .\gradlew.bat connectedCheck
      

      如果你想知道你可能已经运行的其他选项

      .\gradlew.bat tasks
      

      额外信息

      要以编程方式 (Java) 使用 Runtime.getRuntime().exec(String command, String[] envp, File dir)。例如,

      Runtime.getRuntime().exec(shell_command + " <path_to_test_folder>\gradlew.bat assembleDebug", null, new File(<path_to_test_project>));
      

      其中shell_command取决于操作系统(命令行的命令):

      - Windows: cmd /C
      - Unix-based: /bin/sh -c
      

      【讨论】:

      • 请注意,connectedCheck 无论如何都会构建和安装,因此手动运行命令会浪费一些时间。
      猜你喜欢
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 2018-09-08
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多