1 /* 2 * Copyright (C) 2021 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.config.Option; 20 import com.android.tradefed.util.RunUtil; 21 22 import java.io.File; 23 import java.nio.file.Paths; 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * Common preparer for launching a local emulator. 29 * 30 * <p>Handles input and processing of common arguments. 31 */ 32 public abstract class BaseEmulatorPreparer extends BaseTargetPreparer { 33 @Option(name = "gpu", description = "emulator gpu mode to use") 34 private String mGpu = "swiftshader_indirect"; 35 36 @Option(name = "emulator-path", description = "path to emulator binary") 37 private String mEmulatorPath = "emulator"; 38 39 @Option(name = "window", description = "launch emulator with a window") 40 private boolean mWindow = false; 41 42 @Option(name = "feature", description = "comma separated list of emulator features to enable") 43 private String mFeatures = ""; 44 45 @Option(name = "avd-name", description = "avd name to use") 46 private String mAvdName = null; 47 48 @Option( 49 name = "sdk-root", 50 description = "Android SDK root file path. If set, --emulator-path will be ignored") 51 private File mSdkRoot = null; 52 53 @Option( 54 name = "avd-root", 55 description = 56 "file path to custom AVD storage location. " 57 + "If unset, emulator will use the default AVD folder.") 58 private File mAvdRoot = null; 59 buildEmulatorLaunchArgs()60 protected List<String> buildEmulatorLaunchArgs() { 61 List<String> args = new ArrayList<>(); 62 if (mSdkRoot != null) { 63 args.add(Paths.get(mSdkRoot.getAbsolutePath(), "emulator", "emulator").toString()); 64 } else { 65 args.add(mEmulatorPath); 66 } 67 args.add("-gpu"); 68 args.add(mGpu); 69 if (!mWindow) { 70 args.add("-no-window"); 71 } 72 if (!mFeatures.isEmpty()) { 73 args.add("-feature"); 74 args.add(mFeatures); 75 } 76 if (mAvdName != null) { 77 args.add("-avd"); 78 args.add(mAvdName); 79 } 80 return args; 81 } 82 buildRunUtilForEmulatorLaunch()83 protected RunUtil buildRunUtilForEmulatorLaunch() { 84 RunUtil runUtil = new RunUtil(); 85 if (mSdkRoot != null) { 86 runUtil.setEnvVariable("ANDROID_HOME", mSdkRoot.getAbsolutePath()); 87 } 88 if (mAvdRoot != null) { 89 runUtil.setEnvVariable("ANDROID_AVD_HOME", mAvdRoot.getAbsolutePath()); 90 } 91 return runUtil; 92 } 93 } 94