Skip to content

Commit

Permalink
Do instrumentation, push, pull on all devices too.
Browse files Browse the repository at this point in the history
  • Loading branch information
hugojosefson committed Jun 21, 2011
1 parent 2a5b810 commit 33e52c7
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 188 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2009, 2010 Jayway AB
* Copyright (C) 2009-2011 Jayway AB
* Copyright (C) 2007-2008 JVending Masa
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -32,6 +32,7 @@
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.codehaus.plexus.util.DirectoryScanner;
Expand All @@ -40,6 +41,7 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.apache.commons.lang.StringUtils.isBlank;

Expand Down Expand Up @@ -543,41 +545,51 @@ private void waitUntilConnected(AndroidDebugBridge adb) {
* @param apkFile the file to deploy
* @throws MojoExecutionException If there is a problem deploying the apk file.
*/
protected void deployApk(File apkFile) throws MojoExecutionException {
AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();
protected void deployApk(final File apkFile) throws MojoExecutionException, MojoFailureException {

doWithDevices(new DeviceCallback(){
public void doWithDevice(final IDevice device) throws MojoExecutionException {
try {
device.installPackage(apkFile.getAbsolutePath(), undeployBeforeDeploy);
getLog().info("Successfully installed to " + device.getSerialNumber() + " (avdName="
+ device.getAvdName() + ")");
} catch (InstallException e) {
throw new MojoExecutionException("Install failed.", e);
}
}
});

}

/**
* Determines which {@link IDevice}(s) to use, and performs the callback action on it/them.
*
* @param deviceCallback the action to perform on each device
* @throws org.apache.maven.plugin.MojoExecutionException in case there is a problem
* @throws org.apache.maven.plugin.MojoFailureException in case there is a problem
*/
protected void doWithDevices(final DeviceCallback deviceCallback) throws MojoExecutionException, MojoFailureException {
final AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();

if (androidDebugBridge.isConnected()) {
List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
int numberOfDevices = devices.size();
getLog().info("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");
if (devices.size() > 0) {
getLog().info("Continuing with install of " + apkFile.getAbsolutePath());
if (StringUtils.isNotBlank(device)) {
getLog().info("android.device parameter set to " + device);
for (IDevice idevice : devices) {
// deploy to specified device or all emulators or all devices
// use specified device or all emulators or all devices
if (("emulator".equals(device) && idevice.isEmulator())
|| ("usb".equals(device) && !idevice.isEmulator())
|| (idevice.getAvdName() != null && idevice.getAvdName().equals(device))) {
try {
idevice.installPackage(apkFile.getAbsolutePath(), undeployBeforeDeploy);
getLog().info("Successfully installed to " + idevice.getSerialNumber() + " (avdName="
+ idevice.getAvdName() + ")");
} catch (InstallException e) {
throw new MojoExecutionException("Install failed.", e);
}
deviceCallback.doWithDevice(idevice);
}
}
} else {
getLog().info("android.device parameter not set, deploying to all attached devices");
getLog().info("android.device parameter not set, using all attached devices");
for (IDevice idevice : devices) {
try {
idevice.installPackage(apkFile.getAbsolutePath(), undeployBeforeDeploy);
getLog().info("Successfully installed to " + idevice.getSerialNumber() + " (avdName="
+ idevice.getAvdName() + ")");
} catch (InstallException e) {
throw new MojoExecutionException("Install failed.", e);
}
deviceCallback.doWithDevice(idevice);
}
}
} else {
Expand All @@ -586,25 +598,17 @@ protected void deployApk(File apkFile) throws MojoExecutionException {
} else {
throw new MojoExecutionException("Android Debug Bridge is not connected.");
}
// AndroidDebugBridge.terminate();
}

/**
* Checks if a specific device should be used, and adds any relevant parameter(s) to the parameters list.
* Adds relevant parameter to the parameters list for chosen device.
*
* @param commands the parameters to be used with the {@code adb} command
* @param device the device to be used
*/
protected void addDeviceParameter(List<String> commands) {
if (StringUtils.isNotBlank(device)) {
if ("usb".equals(device)) {
commands.add("-d");
} else if ("emulator".equals(device)) {
commands.add("-e");
} else {
commands.add("-s");
commands.add(device);
}
}
protected void addDeviceParameter(List<String> commands, IDevice device) {
commands.add("-s");
commands.add(device.getSerialNumber());
}

/**
Expand All @@ -614,7 +618,7 @@ protected void addDeviceParameter(List<String> commands) {
* @param apkFile the file to undeploy
* @return <code>true</code> if successfully undeployed, <code>false</code> otherwise.
*/
protected boolean undeployApk(File apkFile) throws MojoExecutionException {
protected boolean undeployApk(File apkFile) throws MojoExecutionException, MojoFailureException {
final String packageName;
packageName = extractPackageNameFromApk(apkFile);
return undeployApk(packageName);
Expand All @@ -628,58 +632,26 @@ protected boolean undeployApk(File apkFile) throws MojoExecutionException {
* @param packageName the package name to undeploy.
* @return <code>true</code> if successfully undeployed, <code>false</code> otherwise.
*/
protected boolean undeployApk(String packageName)
throws MojoExecutionException {
AndroidDebugBridge androidDebugBridge = initAndroidDebugBridge();

if (androidDebugBridge.isConnected()) {
List<IDevice> devices = Arrays.asList(androidDebugBridge.getDevices());
int numberOfDevices = devices.size();
getLog().info("Found " + numberOfDevices + " devices connected with the Android Debug Bridge");
if (devices.size() > 0) {
getLog().info("Continuing with uninstall of " + packageName);
if (StringUtils.isNotBlank(device)) {
getLog().info("android.device parameter set to " + device);
for (IDevice idevice : devices) {
if (("emulator".equals(device) && idevice.isEmulator())
|| ("usb".equals(device) && !idevice.isEmulator())
|| (idevice.getAvdName() != null && idevice.getAvdName().equals(device))) {

try {
// (not sure though) might be implied
idevice.uninstallPackage(packageName);
getLog().info("Successfully uninstalled from " + idevice.getSerialNumber() + " (avdName="
+ idevice.getAvdName() + ")");
} catch (InstallException e) {
throw new MojoExecutionException("Uninstall failed.", e);
}
}
}
} else {
getLog().info("android.device parameter not set, removing on all attached devices");
for (IDevice idevice : devices) {
try {
idevice.uninstallPackage(packageName);
getLog().info("Successfully uninstalled from " + idevice.getSerialNumber() + " (avdName="
+ idevice.getAvdName() + ")");
} catch (InstallException e) {
throw new MojoExecutionException("Uninstall failed.", e);
}
}
protected boolean undeployApk(final String packageName)
throws MojoExecutionException, MojoFailureException {

final AtomicBoolean result = new AtomicBoolean(true); // if no devices are present, it counts as successful

doWithDevices(new DeviceCallback() {
public void doWithDevice(final IDevice device) throws MojoExecutionException {
try {
device.uninstallPackage(packageName);
getLog().info("Successfully uninstalled from " + device.getSerialNumber() + " (avdName="
+ device.getAvdName() + ")");
result.set(true);
} catch (InstallException e) {
result.set(false);
throw new MojoExecutionException("Uninstall failed.", e);
}
// AndroidDebugBridge.terminate();
return true;
} else {
getLog().error("No online devices attached.");
// AndroidDebugBridge.terminate();
return false;
}
} else {
getLog().error("Android Debug Bridge is not connected.");
// AndroidDebugBridge.terminate();
return false;
}
});

return result.get();
}

/**
Expand Down
Expand Up @@ -89,7 +89,7 @@ protected boolean isEnableIntegrationTest() throws MojoFailureException, MojoExe

}

protected void deployDependencies() throws MojoExecutionException {
protected void deployDependencies() throws MojoExecutionException, MojoFailureException {
Set<Artifact> directDependentArtifacts = project.getDependencyArtifacts();
if (directDependentArtifacts != null) {
for (Artifact artifact : directDependentArtifacts) {
Expand All @@ -108,7 +108,7 @@ protected void deployDependencies() throws MojoExecutionException {
}
}

protected void deployBuiltApk() throws MojoExecutionException {
protected void deployBuiltApk() throws MojoExecutionException, MojoFailureException {
// If we're not on a supported packaging with just skip (Issue 112)
// http://code.google.com/p/maven-android-plugin/issues/detail?id=112
if (! SUPPORTED_PACKAGING_TYPES.contains(project.getPackaging())) {
Expand All @@ -125,7 +125,7 @@ protected void deployBuiltApk() throws MojoExecutionException {
* @param apkFile the apk file to deploy
* @throws MojoExecutionException
*/
protected void deployFile(File apkFile) throws MojoExecutionException {
protected void deployFile(File apkFile) throws MojoExecutionException, MojoFailureException {
if (undeployBeforeDeploy) {
undeployApk(apkFile);
}
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/jayway/maven/plugins/android/DeviceCallback.java
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2011 Jayway AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jayway.maven.plugins.android;

import com.android.ddmlib.IDevice;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;

/**
* Used for declare an action to perform on one or several {@link IDevice}s.
*
* @author hugo.josefson@jayway.com
*/
public interface DeviceCallback {
/**
* What to do with an {@link IDevice}.
*
* @param device the device
* @throws MojoExecutionException in case there is a problem, you may throw this
* @throws org.apache.maven.plugin.MojoFailureException in case there is a problem, you may throw this
*/
void doWithDevice(IDevice device) throws MojoExecutionException, MojoFailureException;
}

3 comments on commit 33e52c7

@mosabua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh man.. I started implementing the same independently.. haha. I will check this out and update the api/configuration log. We have to get a release out. All the new features ROCK!

@mosabua
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you are not using the ddmlib for these things yet.. so my stuff will still apply. However I think this is good enough for now and works. We should release!

@zwooshi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly, we should migrate all code to use libraries instead of executing external CLI processes throughout.

Please sign in to comment.