Android自动测试之Monkey工具

什么是Monkey


Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中。它向系统发送伪随机的用户事件流(如按键输入、触摸屏输入、手势输入等),实现对正在开发的应用程序进行压力测试。Monkey测试是一种为了测试软件的稳定性、健壮性的快速有效的方法。

Monkey的基本用法


基本语法如下:

如果不指定options,Monkey将以无反馈模式启动,并把事件任意发送到安装在目标环境中的全部包。下面是一个更为典型的命令行示例,它启动指定的应用程序,并向其发送500个伪随机事件:

更加详细的命令参数,参考下表:

标题 选项 描述
General --help Prints a simple usage guide.
-v Each -v on the command line will increment the verbosity level. Level 0 (the default) provides little information beyond startup notification, test completion, and final results. Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Level 2 provides more detailed setup information such as activities selected or not selected for testing.
Events -s <seed> Seed value for pseudo-random number generator. If you re-run the Monkey with the same seed value, it will generate the same sequence of events.
--throttle <milliseconds> Inserts a fixed delay between events. You can use this option to slow down the Monkey. If not specified, there is no delay and the events are generated as rapidly as possible.
--pct-touch <percent> Adjust percentage of touch events. (Touch events are a down-up event in a single place on the screen.)
--pct-motion <percent> Adjust percentage of motion events. (Motion events consist of a down event somewhere on the screen, a series of pseudo-random movements, and an up event.)
--pct-trackball <percent> Adjust percentage of trackball events. (Trackball events consist of one or more random movements, sometimes followed by a click.)
--pct-nav <percent> Adjust percentage of "basic" navigation events. (Navigation events consist of up/down/left/right, as input from a directional input device.)
--pct-majornav <percent> Adjust percentage of "major" navigation events. (These are navigation events that will typically cause actions within your UI, such as the center button in a 5-way pad, the back key, or the menu key.)
--pct-syskeys <percent> Adjust percentage of "system" key events. (These are keys that are generally reserved for use by the system, such as Home, Back, Start Call, End Call, or Volume controls.)
--pct-appswitch <percent> Adjust percentage of activity launches. At random intervals, the Monkey will issue a startActivity() call, as a way of maximizing coverage of all activities within your package.
--pct-anyevent <percent> Adjust percentage of other types of events. This is a catch-all for all other types of events such as keypresses, other less-used buttons on the device, and so forth.
Constraints -p  <allowed-package-name> If you specify one or more packages this way, the Monkey will only allow the system to visit activities within those packages. If your application requires access to activities in other packages (e.g. to select a contact) you'll need to specify those packages as well. If you don't specify any packages, the Monkey will allow the system to launch activities in all packages. To specify multiple packages, use the -p option multiple times — one -p option per package.
-c <main-category> If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category.
Debugging --dbg-no-events When specified, the Monkey will perform the initial launch into a test activity, but will not generate any further events. For best results, combine with -v, one or more package constraints, and a non-zero throttle to keep the Monkey running for 30 seconds or more. This provides an environment in which you can monitor package transitions invoked by your application.
--hprof If set, this option will generate profiling reports immediately before and after the Monkey event sequence. This will generate large (~5Mb) files in data/misc, so use with care. See Traceview for more information on trace files.
--ignore-crashes Normally, the Monkey will stop when the application crashes or experiences any type of unhandled exception. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--ignore-timeouts Normally, the Monkey will stop when the application experiences any type of timeout error such as a "Application Not Responding" dialog. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--ignore-security-exceptions Normally, the Monkey will stop when the application experiences any type of permissions error, for example if it attempts to launch an activity that requires certain permissions. If you specify this option, the Monkey will continue to send events to the system, until the count is completed.
--kill-process-after-error Normally, when the Monkey stops due to an error, the application that failed will be left running. When this option is set, it will signal the system to stop the process in which the error occurred. Note, under a normal (successful) completion, the launched process(es) are not stopped, and the device is simply left in the last state after the final event.
--monitor-native-crashes Watches for and reports crashes occurring in the Android system native code. If --kill-process-after-error is set, the system will stop.
--wait-dbg Stops the Monkey from executing until a debugger is attached to it.

参考


UI/Application Exerciser Monkey
Android自动测试之Monkey工具

TranslateAnimation动画结束后的位置问题

今天在使用TranslateAnimation位移一个LinearLayout时,发现动画完成后又会自动回到初始的状态,设置了fillAfter也不太管用。

仔细研究了一下,发现这种现象很正常,因为TranslateAnimation只负责实现位移动画效果,并不会真正的改变LinearLayout的位置,所以需要加一个AnimationListener ,在动画结束(onAnimationEnd() )后,通过设置LayoutParam里相关属性来改变LinearLayout位置才行。

具体的Margin的数值,根据实际需要进行调整即可。也可以直接设置控件的起始X,Y坐标即可。

参考链接:Android: 解决动画完成后位置恢复到初始位置的问题

Android系统证书platform.x509.pem,platform.pk8转换为.keystore文件

概述


我们在进行Android开发的时候,如果开发的是系统应用,都需要系统的签名才能正常运行。而系统应用一般是把代码统一放到系统代码库中跟随系统一起编译。而如果我们想用Android Studio单独开发某个应用而使用系统签名的时候,有两种开发的方式,一种是编译完成后,使用SignApk.jar来进行签名,另外一种是把系统的platform.x509.pem platform.pk8 转换成为Android Studio常用的.keystore文件,编译时候自动签名。

Android系统签名证书的目录是“build/target/product/security”。

SignApk.jar签名


命令行中使用SignApk.jar签名的方式如下:

转换为.keystore文件


使用keytool-importkeypair本站下载)进行转换,转换命令如下:

Android Studio中使用证书:

Android Studio中使用SystemProperties

概述


Android的系统属性相当于Windows的注册表,由key和value组成,且都是核心系统的一个基本机制。相对于Windows的注册表,Android的系统属性要简单一些,它没有Windows注册表的树状结构,而只是一个列表,也就是说没有父子关系。value有string,int,long,boolean,但是设置只能通过字符串方式。

读取系统属性,是通过SystemProperties类来实现的。SystemProperties在android.os下,但这个类是隐藏的,上层程序开发无法直接使用。要使用这个类,有两种方法。一个是导入layoutlib.jar,另外一种是通过反射的方式调用。

导入layoutlib.jar


我们只介绍一下如何在Android Studio中导入的方式
在gradle配置文件中,写一个函数,动态获取layoutlib.jar路径,然后加到dependencies中即可,代码如下:

导入之后,直接

就可以正常使用了。
注意:引入的layoutlib.jar并不会编译到APK包里面,因此不需要担心增加最终的APK的大小的情况。

反射调用


对于不想引入layoutlib.jar的情况,可以直接使用下面的反射类来实现调用。

参考链接


Android Studio 中如何引入 layoutlib.jar?

GSON序列化时排除字段的几种方式


使用transient


这个方法最简单,给字段加上 transient 修饰符就可以了,如下所示:

单元测试用例:

使用Modifier指定


这个方法需要用GsonBuilder定制一个GSON实例,如下所示:

单元测试用例:

使用@Expose注解


注意,没有被 @Expose 标注的字段会被排除,如下所示:

单元测试用例:

使用ExclusionStrategy定制排除策略


这种方式最灵活,下面的例子把所有以下划线开头的字段全部都排除掉:

单元测试用例:

参考链接


GSON序列化时排除字段的几种方式

Android 4.4 前端调试

前端开发的同学通常会在Chrome的开发者调试工具中来对CSS和js进行即时调试和查看效果,如下图一样,对比调试的好处谁用谁知道。nuiqye.png!web

但是,当我们调试混合模式下通过Webview来调用页面的App时就不能使用这种方法啦。在 Android4.4下,原来PC上的调试体验可以完整继承下来,在PC打开调试工具,手机屏作为视图承载者能即时地显示出样式调整的变化。

让我们来一起看看怎么使用这种调试方法。打开手机=》设置=》开发者选项,进入到如下界面:FnUvqy.png!web

在这个界面上,我们可以看到“USB调试”和“ADB网络调试”,这两种调试模式在使用之前必须勾选打开。

在使用调试工具之前,我们还要确认以下几点:Chrome升级到最新版本、我本机Chrome的版本是 35.0.1862.2 dev-m ,可以在Chrome浏览器地址栏中输入“chrome://chrome/”查看当前Chrome的版本号;PC上安装ADB工具,在命令行中键入 “adb version”以查看adb版本。

这里我们以调试手Q应用中心线上首页为例

1、打开手机QQ=》动态=》应用宝

2、打开Chrome浏览器,在浏览器地址栏输入:chrome://inspect/#devices ,如下图,选中红框处的复选框AJBJ7n.png!web

3、在对应设备的下方会出现正在显示的页面,如下图所示,点击inspect链接,进入Chrome的调试模式

FFrQF3z.png!web

剽窃链接: Android 4.4 让前端调试也能这么酷

Android下WebView中Java与JavaScript通信

背景介绍


Android下面WebView开发,有时候需要调用底层的一部分接口,而这部分接口只有Android的SDK才提供相关的功能,这个时候就需要进行Java与JavaScript通信。

例子


  1. 生成JavaScript调用Java函数的接口类
  2. 开启 JavaScript支持,并向WebView注册接口
  3. Html中调用接口例子
  4. 销毁的时候,反注册接口,避免内存泄漏
  5. 销毁的时候,WebView从父容器中移除,避免内存泄漏

完整的例子代码如下(包含WebView的XML布局文件,请自行生成):

Android动画 Interpolator

目录


  1. 简介
  2. 简单插值器
    1. AccelerateInterpolator 加速插值器
    2. DecelerateInterpolator 减速插值器
    3. AccelerateDecelerateInterpolator 加速减速插值器
    4. LinearInterpolator 线性插值器
    5. BounceInterpolator 弹跳插值器
    6. AnticipateInterpolator 回荡秋千插值器
    7. AnticipateOvershootInterpolator
    8. CycleInterpolator 正弦周期变化插值器
    9. OvershootInterpolator
  3. 参考链接

简介


interpolator可以翻译成插值器。

Android中interpolator最底层的接口如下:

TimeInterpolator是在Android API11时加入的,之前类就叫Interpolator

现在Interpolatro继承了它。


简单插值器


注意下面的图,对应你脑海中的插值的大小应该是斜率。

1.AccelerateInterpolator 加速插值器

源代码如下:

加速的快慢度由参数fractor决定。

当fractor值为1.0f时,动画加速轨迹相当于一条y=x^2的抛物线。如下图:
AccelerateInterpolator

fractor不为1时,轨迹曲线是y=x^(2*fractor)(0<x<=1)的曲线。

示例:当fractor为4时,插值器的加速轨迹曲线如下图:
AccelerateInterpolator_Fractor_4

如果你在使用AccelerateInterpolator时,想要那种一开始很慢,然后突然就很快的加速的动画效果的话。

就将fractor设置大点。

你可以到这里调试下你想要的抛物线效果:http://www.wolframalpha.com/input/?i=x%5E%282*3%29%280%3Cx%3C%3D1%29

Android提供的一个不同factor的加速插值器:

(1)accelerate_cubic, factor为1.5

2. DecelerateInterpolator 减速插值器

源代码如下:

根据getInterpolationa(float input);方法可以知道。

fractor为1.0f。它减速的轨迹曲线为1-(1-x)^2。如下图:

DecelerateInterpolator

fractor增大到4时,曲线轨迹如下图:
DecelerateInterpolator_Fractor_4

3. AccelerateDecelerateInterpolator 加速减速插值器

源代码如下:

根据getInterpolation()方法可以得出其变化曲线如下:
AccelerateDecelerateInterpolator

4. LinearInterpolator 线性插值器

这可是最简单的插值器:

5. BounceInterpolator 弹跳插值器

源代码如下:

根据getInterpolation()得到以下插值曲线图:
BounceInterpolator

6.AnticipateInterpolator 回荡秋千插值器

这个插值器的值变化过程,可以想像成荡秋千时的一个段过程。(此时秋千已经在比较上面的位置了,一放手就可以荡下来)。你开始用力推向更上面,然后秋千终将荡回下面。

tension值就好比推力的大小。

源代码如下:

根据getInterpolation()方法。
tension为默认值2.0f时,曲线图如下:
AnticipateInterpolator_Tension_2

tension值为4.0f时,曲线图如下:
AnticipateInterpolator_Tension_4

7. AnticipateOvershootInterpolator

源代码如下:

根据getInterpolation()方法,

可以得到当tension为默认值时,曲线图为:

AnticipateOvershootInterpolator

8. CycleInterpolator 正弦周期变化插值器

源代码:

当cycle时为1时,即变化一周时,曲线图如下:
CycleInterpolator

9. OvershootInterpolator

源代码:

tension为默认值2时,曲线图如下:
OvershootInterpolator_Tension_2

tension的值为4时,曲线图如下:
OvershootInterpolator_Tension_4

通过学习了解Android自带的这些Interpolator,我们可以很好的根据自己的使用场景使用这些Interpolator了。也可以很容易的写出我们自己的Interpolator


参考链接


android动画(一)Interpolator

查看Keystore文件的签名信息/检查APK文件中的签名信息

  • 查看 keystore文件的签名信息

  • 检查APK文件中的签名信息

解压出apk中META-INF目录下的CERT.RSA文件,然后用keytool即可查看签名信息:

  • 比对签名是否一致

验证APK的签名是不是我们给定的签名文件的签名。

1.解压出apk中META-INF目录下的CERT.RSA文件.

2.执行如下命令:

Windows:

Linux:

比较两者输出的MD5字符串,是否一致即可。

com.android.dex.DexIndexOverflowException

现象描述


注意,本文描述的方案,仅在Android Studio 1.5,Gradle插件版本1.3.1中测试成立!

同时在工程中引入了多个第三方jar包,导致调用的方法数超过了android设定的65536个(DEX 64K problem),进而导致dex无法生成,也就无法生成APK文件。

编译时候产生如下的异常信息:

 


解决方案


Google给出的解决方案就是使用MultiDexApplication

    • Multidex Apps

      1.在项目的build.gradle文件的dependencies 节中添加分包设置:

      2.通过在defaultConfig节中设置multiDexEnabled标签为true,开启multi-dexing支持.

      3.如果没有创建自己的Application.class,直接在AndroidManifest.xml文件的Application声明中添加:

      如果实现了自己的Application.class,则修改继承的父类为
      android.support.multidex.MultiDex.MultiDexApplication

 

  • Multidex Apps单元测试

    1.在项目的build.gradle文件的dependencies 节中添加分包设置:

    2.通过在defaultConfig节中设置multiDexEnabled标签为true,开启multi-dexing支持.

 


参考链接


  1. Building Apps with Over 65K Methods
  2. Which package for MultiDexTestRunner? android.support.multidex or com.android.test.runner
  3. Android学习笔记----解决“com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题