1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.tradefed.targetprep; 18 19 import com.android.tradefed.build.IBuildInfo; 20 import com.android.tradefed.config.Option; 21 import com.android.tradefed.config.Option.Importance; 22 import com.android.tradefed.config.OptionClass; 23 import com.android.tradefed.device.DeviceNotAvailableException; 24 import com.android.tradefed.device.ITestDevice; 25 import com.android.tradefed.log.LogUtil.CLog; 26 import com.android.tradefed.util.FileUtil; 27 28 import java.io.File; 29 import java.util.ArrayList; 30 import java.util.Collection; 31 32 /** 33 * A {@link ITargetPreparer} that installs one or more test apks from an Android platform build env. 34 * 35 * <p>Uses the <code>ANDROID_PRODUCT_OUT</code> env variable to determine location for build apks. 36 */ 37 @OptionClass(alias = "install-apk-from-env") 38 public class InstallBuildEnvApkSetup extends BaseTargetPreparer { 39 40 private static final String APK_SUFFIX = ".apk"; 41 42 @Option(name = "apk-name", description = 43 "the file name of the apk to install. Can be repeated.", 44 importance = Importance.IF_UNSET) 45 private Collection<String> mApkNames = new ArrayList<String>(); 46 47 /** 48 * {@inheritDoc} 49 */ 50 @Override setUp(ITestDevice device, IBuildInfo buildInfo)51 public void setUp(ITestDevice device, IBuildInfo buildInfo) throws TargetSetupError, 52 BuildError, DeviceNotAvailableException { 53 File testAppDir = getDataAppDirFromBuildEnv(device); 54 for (String apkName : mApkNames) { 55 String apkPrimaryFileName = apkName; 56 if (apkName.endsWith(APK_SUFFIX)) { 57 apkPrimaryFileName = apkName.substring(0, apkName.length() - APK_SUFFIX.length()); 58 } 59 File apk = FileUtil.getFileForPath(testAppDir, apkPrimaryFileName, apkName); 60 CLog.i("Installing %s on %s", apk.getName(), device.getSerialNumber()); 61 String result = device.installPackage(apk, true); 62 if (result != null) { 63 throw new TargetSetupError(String.format( 64 "Failed to install %s on device %s. Reason: %s", apk.getAbsolutePath(), 65 device.getSerialNumber(), result), device.getDeviceDescriptor()); 66 } 67 } 68 } 69 getDataAppDirFromBuildEnv(ITestDevice device)70 private File getDataAppDirFromBuildEnv(ITestDevice device) throws TargetSetupError { 71 String buildRoot = System.getenv("ANDROID_PRODUCT_OUT"); 72 if (buildRoot == null) { 73 throw new TargetSetupError("ANDROID_PRODUCT_OUT is not set. Are you in Android " 74 + "build env?", device.getDeviceDescriptor()); 75 } 76 File testAppDir = new File(FileUtil.getPath(buildRoot, "data", "app")); 77 if (!testAppDir.exists()) { 78 throw new TargetSetupError(String.format("Could not find test app dir %s", 79 testAppDir.getAbsolutePath()),device.getDeviceDescriptor()); 80 } 81 return testAppDir; 82 } 83 } 84