A lot of code quote from selendroid,but we will foucs on simplify ddmlib's usage
A distributed android device monitor system based on device-api
You require the following to build:
- Latest stable Oracle JDK 7+
- Latest stable Gradle 2.4+
- Android SDK
- node.js and bower
And be sure that JAVA_HOME,ANDROID_HOME at your environment path.
Plug a android device via usb or boot an emulator
git clone https://github.com/cosysoft/device.git
cd device/device-keeper
bower install
cd ..
gradle bootRun
Open http://localhost:8080/keeper in your browser
Focus on stabilized android device operation via Android Debug Bridge
Maven
<dependency>
<groupId>com.github.cosysoft</groupId>
<artifactId>device-api</artifactId>
<version>0.9.1</version>
</dependency>
Gradle
dependencies {
compile 'com.github.cosysoft:device-api:0.9.1'
}
TreeSet<AndroidDevice> devices = AndroidDeviceStore.getInstance()
.getDevices();
AndroidDevice device = devices.pollFirst();
System.out.println(device.getName());
BufferedImage image = device.takeScreenshot();
String imagePath = new File(System.getProperty("java.io.tmpdir"),
"screenshot.png").getAbsolutePath();
ImageUtils.writeToFile(image, imagePath);
AndroidApp app = new DefaultAndroidApp(new File(
"d:\\uat\\com.android.chrome.apk"));
device.install(app);
if (device.isInstalled(app)) {
device.uninstall(app);
}
final LogCatFilter filter = new LogCatFilter("", "", "com.android", "",
"", LogLevel.WARN);
final LogCatListener lcl = new LogCatListener() {
@Override
public void log(List<LogCatMessage> msgList) {
for (LogCatMessage msg : msgList) {
if (filter.matches(msg)) {
System.out.println(msg);
}
}
}
};
device.addLogCatListener(lcl);
Thread.sleep(60000);
Ddmlib can monitor one app's cpu/heap/threads and much more,but we need list running client first.
List<ClientDataInfo> clientDataInfos = device.getClientDatasInfo();
for (ClientDataInfo client : clientDataInfos) {
System.out.println(client.getName());
System.out.println(client.getPid());
}