本文记录一下monkeyrunner环境搭建遇到的各种坑,以免以后再次踩坑。首先要提一下巨坑,务必要安装java 8(本文记录于2023.3),安装其他版本java,运行monneyrunner会有很多问题,见第七节。
oracle官网Java Archive Downloads - Java SE 8下载jdk-8u361-windows-x64.exe,双击安装。如果没有账号,注册一个就可以下载了。
坑1:千万不要用百度输入法,否则双击安装文件没反应。
安装完成后,设置java环境变量。
右击桌面图标“此电脑”-->高级系统设置-->环境变量-->系统环境变量,点击新建:
变量名:JAVA_HOME
变量值:C:\Program Files\Java\jdk1.8.0_361
双击系统环境变量Path值,新建:%JAVA_HOME%\bin
官网下载https://developer.android.com/studio,双击安装。
安装完成后打开,首次打开可能会出现下面的弹窗:
选择Open SDK Manager安装platform packages和sdk tools,见下一步说明。
通过Android Studio菜单栏的Tools-->SDK Manager进入下载管理器设置SDK路径。
坑2:路径中不要有中文字符;如果网速慢需要等一两分钟,Next按钮才可用。
然后一路Next,直至Finish。
坑3:win10系统按上面操作,会有tools这个文件夹,但在win7系统没有tools目录。win7的解决方法在最后。
接着勾选手机对应的android版本(比如Android 11),右下角点击Apply。
下载完成后,sdk/tools中就有monkeyrunner脚本了。
检查一下,SDK Tools中相关工具,是否也installed了,如果没有,也下载一下。
右击桌面图标“此电脑”-->高级系统设置-->环境变量-->系统环境变量,点击新建:
变量名:ANDROID_HOME
变量值:E:\sdk(SDK Manager设置的下载路径,第1步设置的是E:\sdk)
双击系统环境变量Path值,新建:
%ANDROID_HOME%\tools\bin
%ANDROID_HOME%\platform-tools
坑4:网上有单下sdk的,比如这个链接 https://dl.google.com/android/android-sdk_r24.4.1-windows.zip,功能不全,monkeyrunner一些功能可以用,一些功能用不了(比如截屏),所以最好用Android Studio下载与手机对应android版本的sdk。
坑5:执行完第三步,按理应该可以运行monkeyrunner了,试试看:
C:\Users\gsf>monkeyrunner
SWT folder '..\framework\x86_64' does not exist.
Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
坑5解决:修改monkeyrunner.bat脚本中frameworkdir目录路径
修改前:
set java_exe=
call ..\lib\find_java.bat
if not defined java_exe goto :EOF
for /f %%a in ("%APP_HOME%\lib\monkeyrunner-26.0.0-dev.jar") do set jarfile=%%~nxa
set frameworkdir=.
set libdir=
if exist %frameworkdir%\%jarfile% goto JarFileOk
set frameworkdir=lib
if exist %frameworkdir%\%jarfile% goto JarFileOk
set frameworkdir=..\framework <<=====当前路径在E:\sdk\tools\bin,父目录没有framework这个目录,所以需要改成swt.jar文件所在的lib目录E:\sdk\tools\lib
:JarFileOk
修改后
set java_exe=
call ..\lib\find_java.bat
if not defined java_exe goto :EOF
for /f %%a in ("%APP_HOME%\lib\monkeyrunner-26.0.0-dev.jar") do set jarfile=%%~nxa
set frameworkdir=.
set libdir=
if exist %frameworkdir%\%jarfile% goto JarFileOk
set frameworkdir=lib
if exist %frameworkdir%\%jarfile% goto JarFileOk
set frameworkdir=..\lib <=====修改后的路径
:JarFileOk
再次运行,坑6:找不到adb等工具:
C:\Users\gsf>monkeyrunner
02:52:11 E/adb: Cannot run program "..\framework\adb.exe": CreateProcess error=2, 系统找不到指定的文件。
Exception in thread "main" java.lang.IllegalArgumentException: java.io.IOException: Cannot run program "..\framework\adb.exe": CreateProcess error=2, 系统找不到指定的文件。
at com.android.ddmlib.AndroidDebugBridge.(AndroidDebugBridge.java:600)
at com.android.ddmlib.AndroidDebugBridge.createBridge(AndroidDebugBridge.java:378)
at com.android.chimpchat.adb.AdbBackend.(AdbBackend.java:69)
at com.android.chimpchat.ChimpChat.createBackendByName(ChimpChat.java:78)
at com.android.chimpchat.ChimpChat.getInstance(ChimpChat.java:51)
at com.android.monkeyrunner.MonkeyRunnerStarter.(MonkeyRunnerStarter.java:60)
at com.android.monkeyrunner.MonkeyRunnerStarter.main(MonkeyRunnerStarter.java:188)
Caused by: java.io.IOException: Cannot run program "..\framework\adb.exe": CreateProcess error=2, 系统找不到指定的文件。
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1048)
at com.android.ddmlib.AndroidDebugBridge$1.run(AndroidDebugBridge.java:658)
at java.lang.Thread.run(Thread.java:750)
Caused by: java.io.IOException: CreateProcess error=2, 系统找不到指定的文件。
at java.lang.ProcessImpl.create(Native Method)
at java.lang.ProcessImpl.(ProcessImpl.java:458)
at java.lang.ProcessImpl.start(ProcessImpl.java:139)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
... 2 more
坑6解决:修改monkeyrunner.bat脚本中的Dcom.android.monkeyrunner.bindir路径
修改前:
if exist "%swt_path%" goto SetPath
echo SWT folder '%swt_path%' does not exist.
echo Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
exit /B
:SetPath
call "%java_exe%" -Xmx512m "-Djava.ext.dirs=%frameworkdir%;%swt_path%" -Dcom.android.monkeyrunner.bindir=..\framework -jar %jarpath% %*
修改后:
if exist "%swt_path%" goto SetPath
echo SWT folder '%swt_path%' does not exist.
echo Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
exit /B
:SetPath
call "%java_exe%" -Xmx512m "-Djava.ext.dirs=%frameworkdir%;%swt_path%" -Dcom.android.monkeyrunner.bindir=..\..\platform-tools -jar %jarpath% %*
至此,monkeyrunner就可以正常运行了。
cmd终端输入monkeyrunner,能看到下面的界面,表明环境搭建成功了,可以在提示符下面输入命令进行测试了。按Ctrl+z退出monkeyrunner。
比如执行一次滑屏操作。
也可以写一个.py脚本,将命令放在python脚本中,比如screen.py,每隔60s上滑一次屏幕,内容如下:
坑7:screen.py内容,注意python的缩进要求
#!/usr/bin/env monkeyrunner
import time
from com.android.monkeyrunner import MonkeyRunner, MonkeyRunner, MonkeyImage
device = MonkeyRunner.waitForConnection(5)
for i in range(1,150):
device.drag((200,1000),(200,800),0.1,10)
time.sleep(60)
然后执行monkeyrunner screen.py就可以了。
所有的操作同win10,额外需要:
1)拷贝tools目录(从win10中拷贝或者从网盘拷贝)放到Android Studio指定的sdk目录中
链接:https://pan.baidu.com/s/1F0thGYXVW0UIkca5IT0EgQ
提取码:yqke
2)修改monkeyrunner.bat脚本如下
坑8:直接运行monkeyrunner会出现下面错误(win10上是正常的)
C:\Users\ge>monkeyrunner
SWT folder '..\lib\location of your Java installation.' does not exist.
Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
C:\Users\ge>
需要将bat脚本改成下面的:
@echo off
rem Copyright (C) 2010 The Android Open Source Project
rem
rem Licensed under the Apache License, Version 2.0 (the "License");
rem you may not use this file except in compliance with the License.
rem You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.
rem don't modify the caller's environment
setlocal
rem Set up prog to be the path of this script, including following symlinks,
rem and set up progdir to be the fully-qualified pathname of its directory.
set prog=%~f0
rem Change current directory and drive to where the script is, to avoid
rem issues with directories containing whitespaces.
cd /d %~dp0
rem Check we have a valid Java.exe in the path.
set java_exe=
call ..\lib\find_java.bat
if not defined java_exe goto :EOF
for /f %%a in ("%APP_HOME%\lib\monkeyrunner-26.0.0-dev.jar") do set jarfile=%%~nxa
set frameworkdir=.
set libdir=
if exist %frameworkdir%\%jarfile% goto JarFileOk
set frameworkdir=lib
if exist %frameworkdir%\%jarfile% goto JarFileOk
set frameworkdir=..\lib
:JarFileOk
set jarpath=%frameworkdir%\%jarfile%
if not defined ANDROID_SWT goto QueryArch
set swt_path=%ANDROID_SWT%
goto SwtDone
:QueryArch
for /f "delims=" %%a in ('%frameworkdir%\..\bin\archquery') do set swt_path=%frameworkdir%\%%a
:SwtDone
set swt_path=E:\sdk\tools\lib <=======这个地方改成对应的目录
if exist "%swt_path%" goto SetPath
echo SWT folder '%swt_path%' does not exist.
echo Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
exit /B
:SetPath
call "%java_exe%" -Xmx512m "-Djava.ext.dirs=%frameworkdir%;%swt_path%" -Dcom.android.monkeyrunner.bindir=..\..\platform-tools -jar %jarpath% %*
比如java19,
1)首先你会遇到monkeyrunner.bat脚本中一些变量找不到路径的问题:
Please set ANDROID_SWT to point to the folder containing swt.jar for your platform.
2)调试后修改bat脚本,用set swt_path=你的swt文件路径后,又会遇到:
-Djava.ext.dirs=..\lib;..\lib\x86_64 is not supported. Use -classpath instead.
3)根据提示修改成-classpath,又提示unregonized option,cmd输入java查看支持的option,改成-class-path可解决这个问题,但又遇到4)问题
4)运行时又提示class找不到,已经按提示用-class-path怎么还找不到class呢,那么从网上下载一个ChimpChat?ChimpChat的错误没了,又会遇到其他的错误。google了原因:
It turns out that when you use the -jar
directive, JRE
will ignore the -classpath
directive and so it will again not be able to load any external class
or jar
files…看来也遇到了跟我同样的问题(我用的java 19,他用的java 11),解决方案是:Instead of reinventing the wheel (We tried, we failed, it was painful. Still worth the shot!) we decided to install Java
version 8
on Ubuntu 18.04 LTS
side by side with Java
version 11
and just used that.
总而言之一句话,用java 8跑monkeyrunner就不会遇到这么多乱七八糟的问题了。