尝试 Android(安卓)Scripting Environment
转自:http://blog.csdn.net/zjujoe/article/details/5657868
前言
研究了一下ASE ( Android Scripting Environment: http://code.google.com/p/android-scripting/),这个很火的项目(http://www.javaeye.com /topic/407925)目前还处于Alpha Release状态。
它封装了一些Java API,从而可以通过脚本(Python,LUA等)去访问某些Java API,从而大大提供开发效率。
目前封装的API包括拨号/短信/wifi/bt/barcode,等等,具体见:http://code.google.com/p/android-scripting/wiki/ApiReference
首先从:http://code.google.com/p/android-scripting/downloads/list下载到最新版本的ASE,比如ASE_r25.apk
启动Android设备或者虚拟设备,通过adb install将ASE安装到目标机上。
在目标机上运行ASE,首先通过Menu->Add Interpreter安装一种解释器,比如Python.
安装完成后会自动下载相关脚本,并切换到脚本试图。
现在我们可以上下选择并运行某个脚本。
修改代码
下面我们尝试修改代码。由于直接在目标机上编辑比较麻烦,我们先在PC侧编辑好后再用ADB shell将其push到目标机上。
从Logcat信息我们可以看到ASE会下载脚本包:
http://android-scripting.googlecode.com/files/python_scripts_r7.zip
并解压缩到:/sdcard/ase/scripts
所以,我们:
1)使用wget下载相关脚本:
wgethttp://android-scripting.googlecode.com/files/python_scripts_r7.zip
2)解开这个包。修改代码:
unzip python_scripts_r7.zip
cp hello_world.pyhello_world2.py
vi hello_world2.py
修改一下打印语句,比如droid.makeToast('Goodbye, Android!')
3)将这个修改的文件放到目标机上:
adb push hello_world2.py/sdcard/ase/scripts
4)在目标机上运行ASE,可以看到我们修改的脚本,并可以运行!
通过adb shell执行脚本
通过ASE执行脚本比较慢,需要人工操作!我们尝试在PC侧使用脚本通过adb来直接运行!
看ASE执行脚本的过程,似乎也只是调出了命令行,设置了一些环境变量,最终运行脚本。
所以我们模仿它写了一个脚本,如下脚本以待执行的python脚本为参数,将其通过adb放到目标机上执行
#!/bin/sh
#================================================================
# By:zjujoe@yahoo.com
# YOU SHOULD INSTALL ASE AND PYTHON INTERPRETER ON TARGET FIRSTLY
#
# I am using ase_r25.apk, for other version, blow path may change
# You should change AP_PORT with the one from ASE console
#
#start ASE severfirst:
# adb -s emulator-5554 shell am start -a com.google.ase.action.LAUNCH_SERVER /
#-n com.google.ase/.activity.AseServiceLauncher
#
#the get AP_PORT number for notification
#================================================================
#set -x
#change this which ASE server listion to
AP_PORT="48620"
DEVICE="-s emulator-5554"
#the script file to be run in ase
if [ $# -eq "1" ]; then
FILE=$1
else
echo $0 filename
exit
fi
#shell script file to be run on target(which will run ase)
FILE2=t1.sh
TARGET_RUN_LOCATION=/data
TARGET_PYTHON_SCRIPTS_LOCATION=/sdcard/ase/scripts
#prepare the FILE2
echo export PYTHONPATH="/sdcard/ase/extras/python:/sdcard/ase/scripts/" > ${FILE2}
echo export AP_PORT=${AP_PORT} >> ${FILE2}
echo export TEMP="/sdcard/ase/extras/pythontmp" >> ${FILE2}
echo export PYTHONHOME="/data/data/com.google.ase/python" >> ${FILE2}
echo /data/data/com.google.ase/python/bin/python /sdcard/ase/scripts/${FILE} >> ${FILE2}
#push FILE to target
adb ${DEVICE} push ${FILE} ${TARGET_PYTHON_SCRIPTS_LOCATION}
#push FILE2 to target
adb ${DEVICE} push ${FILE2} ${TARGET_RUN_LOCATION}
adb ${DEVICE} shell chmod 777 ${TARGET_RUN_LOCATION}/${FILE2}
#run FILE2
adb ${DEVICE} shell ${TARGET_RUN_LOCATION}/${FILE2}
#remove FILE2 from target & local
adb ${DEVICE} shell rm ${TARGET_RUN_LOCATION}/${FILE2}
rm ${FILE2}
#remove FILE from target
#adb ${DEVICE} shell rm ${FILE} ${TARGET_PYTHON_SCRIPTS_LOCATION}
如上脚本可以很好的在PC侧直接执行,从而解决了自动化的问题。
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- 关于android语言切换后通知栏显示的问题
- 搭建Android(安卓)repo服务
- AndroidICS4.0---->LockScreen锁屏流程【Android源码解析九】
- Android(安卓)自定义View:TopBar
- Android开发常用代码
- Android(安卓)代码中动态为RadioGroup添加RadioButton
- 跟踪Android(安卓)callback 调用堆栈
- Facebook发布React Native for Android