标签:length runner acea 进程 dex plugins 说明 ntb android
MonkeyRunnerStarter是MonkeyRunner启动时的入口类,由于它里面包括了main方法.它的整个启动过程主要做了以下几件事情:
假设确实不知道不论什么參数的话它就会进入MonkeyRunner的交互模式,事实上就是Jythong的交互模式,让用户能够边写代码边执行
它维护了一个最新的设备列表
本小节我们会先去看下MonkeyRunner在启动的时候是怎样获得命令行參数并对其进行解析处理的。
整个过程事实上跟monkey在启动的时候的命令行參数分析相似。往下我们先看下牵涉到的关键类之间的关系:
图8-2-1 MonkeyRunnerStarter类图
从类图中我们看到MonkeyRunnerStarter持有了一个MonkeyRunnerOptions类型的成员变量options,这个实例保存的就是解析出来的命令行參数。同一时候该类会提供一个processOptions方法来专门解析命令行參数。
我们先进入到MonkeyRunnerStart这个类的main方法:
178 public static void main(String[] args) {
179 MonkeyRunnerOptions options =
MonkeyRunnerOptions.processOptions(args);
180
181 if (options == null) {
182 return;
183 }
184
185
186 replaceAllLogFormatters(MonkeyFormatter.DEFAULT_INSTANCE,
options.getLogLevel());
187
188 MonkeyRunnerStarter runner =
new MonkeyRunnerStarter(options);
189 int error = runner.run();
190
191
192 System.exit(error);
193 }
194 }代码3-2-1 MonkeyRunnerStart - main这里主要做了三件事情:
我们这一节会先去分析下monkeyrunner是怎样对參数进行处理的,我们跳转到MonkeyRunnerOptions这个类里面的processOptions这种方法:
93 public static MonkeyRunnerOptions processOptions(String[] args)
94 {
95 int index = 0;
96
97 String hostname = DEFAULT_MONKEY_SERVER_ADDRESS;
98 File scriptFile = null;
99 int port = DEFAULT_MONKEY_PORT;
100 String backend = "adb";
101 Level logLevel = Level.SEVERE;
102
103 ImmutableList.Builder<File> pluginListBuilder = ImmutableList.builder();
104 ImmutableList.Builder<String> argumentBuilder = ImmutableList.builder();
105 while (index < args.length) {
106 String argument = args[(index++)];
107
108 if ("-s".equals(argument)) {
109 if (index == args.length) {
110 printUsage("Missing Server after -s");
111 return null;
112 }
113 hostname = args[(index++)];
114 }
115 else if ("-p".equals(argument))
116 {
117 if (index == args.length) {
118 printUsage("Missing Server port after -p");
119 return null;
120 }
121 port = Integer.parseInt(args[(index++)]);
122 }
123 else if ("-v".equals(argument))
124 {
125 if (index == args.length) {
126 printUsage("Missing Log Level after -v");
127 return null;
128 }
129
130 logLevel = Level.parse(args[(index++)]);
131 } else if ("-be".equals(argument))
132 {
133 if (index == args.length) {
134 printUsage("Missing backend name after -be");
135 return null;
136 }
137 backend = args[(index++)];
138 } else if ("-plugin".equals(argument))
139 {
140 if (index == args.length) {
141 printUsage("Missing plugin path after -plugin");
142 return null;
143 }
144 File plugin = new File(args[(index++)]);
145 if (!plugin.exists()) {
146 printUsage("Plugin file doesn‘t exist");
147 return null;
148 }
149
150 if (!plugin.canRead()) {
151 printUsage("Can‘t read plugin file");
152 return null;
153 }
154
155 pluginListBuilder.add(plugin);
156 } else if (!"-u".equals(argument))
157 {
158 if ((argument.startsWith("-")) && (scriptFile == null))
159 {
160
161
162 printUsage("Unrecognized argument: " + argument + ".");
163 return null;
164 }
165 if (scriptFile == null)
166 {
167
168 scriptFile = new File(argument);
169 if (!scriptFile.exists()) {
170 printUsage("Can‘t open specified script file");
171 return null;
172 }
173 if (!scriptFile.canRead()) {
174 printUsage("Can‘t open specified script file");
175 return null;
176 }
177 } else {
178 argumentBuilder.add(argument);
179 }
180 }
181 }
182
183 return new MonkeyRunnerOptions(hostname,
port,
scriptFile,
backend,
logLevel,
pluginListBuilder.build(),
argumentBuilder.build());
184 }
185 }代码8-2-2 MonkeyRunnerOptions - processOptions这里首先请看99-101行的几个变量初始化,假设用户在命令行中没有指定相应的參数,那么这些默认參数就会被使用,我们且看下这些默认值各自是什么:
这里须要注意的是这是一个隐藏參数。命令行的help没有显示该參数
代码往下就是对用户输入的參数的解析并保存了,这里要注意几个隐藏的參数:
所以这段代码的意思事实上就是在用户输入了不支持的參数的时候依据不同的情况给用户提示而已
在解析出全部的參数之后,processOptions方法最后依据这些參数来初始化MonkeyRunnerOptions类。
我们进入到该构造函数看下它到底做了什么事情:
38 private MonkeyRunnerOptions(String hostname, int port, File scriptFile, String backend, Level logLevel, Collection<File> plugins, Collection<String> arguments)
39 {
40 this.hostname = hostname;
41 this.port = port;
42 this.scriptFile = scriptFile;
43 this.backend = backend;
44 this.logLevel = logLevel;
45 this.plugins = plugins;
46 this.arguments = arguments;
47 }代码8-2-3 MonkeyRunnerOptions - 构造函数所做的事情很easy。就是把解析出来的全部參数保存到MonkeyRunnerOptions类的实例里面。今后须要的时候就进去拿就好了。
注:很多其它文章请关注公众号:techgogogo或个人博客http://techgogogo.com。当然。也很欢迎您直接微信(zhubaitian1)勾搭。
本文由天地会珠海分舵原创。转载请自觉,是否投诉维权看心情。
第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数
标签:length runner acea 进程 dex plugins 说明 ntb android
原文地址:http://www.cnblogs.com/cynchanpin/p/6985635.html