static bool CheckADBDevices() { var adbTool = new Pvr_ADBTool(Pvr_ADBTool.GetAndroidSDKPath()); if (adbTool.isReady) { List <string> devices = adbTool.GetDevices(); if (devices.Count == 0) { UnityEngine.Debug.LogError("No ADB devices connected. Cannot perform Build and Run."); return(false); } else if (devices.Count > 1) { UnityEngine.Debug.LogError("Multiple ADB devices connected. Cannot perform Build and Run."); return(false); } } else { UnityEngine.Debug.LogError("ADB Tool failed to initialize. Check the Android SDK path in [Edit -> Preferences -> External Tools]"); return(false); } return(true); }
public static bool DeployAPK() { // Create new instance of ADB Tool var adbTool = new Pvr_ADBTool(androidSdkPath); if (adbTool.isReady) { string apkPathLocal; string gradleExportFolder = Path.Combine(Path.Combine(gradleExport, productName), "build\\outputs\\apk\\debug"); // Check to see if gradle output directory exists gradleExportFolder = gradleExportFolder.Replace("/", "\\"); if (!Directory.Exists(gradleExportFolder)) { UnityEngine.Debug.LogError("Could not find the gradle project at the expected path: " + gradleExportFolder); return(false); } // Search for output APK in gradle output directory apkPathLocal = Path.Combine(gradleExportFolder, productName + "-debug.apk"); if (!File.Exists(apkPathLocal)) { UnityEngine.Debug.LogError(string.Format("Could not find {0} in the gradle project.", productName + "-debug.apk")); return(false); } string output, error; DateTime timerStart; // Ensure that the Oculus temp directory is on the device by making it IncrementProgressBar("Making Temp directory on device"); string[] mkdirCommand = { "-d shell", "mkdir -p", REMOTE_APK_PATH }; if (adbTool.RunCommand(mkdirCommand, null, out output, out error) != 0) { return(false); } // Push APK to device, also time how long it takes timerStart = DateTime.Now; IncrementProgressBar("Pushing APK to device . . ."); string[] pushCommand = { "-d push", "\"" + apkPathLocal + "\"", REMOTE_APK_PATH }; if (adbTool.RunCommand(pushCommand, null, out output, out error) != 0) { return(false); } // Calculate the transfer speed and determine if user is using USB 2.0 or 3.0 TimeSpan pushTime = System.DateTime.Now - timerStart; bool trivialPush = pushTime.TotalSeconds < TRANSFER_SPEED_CHECK_THRESHOLD; long? apkSize = (trivialPush ? (long?)null : new System.IO.FileInfo(apkPathLocal).Length); double? transferSpeed = (apkSize / pushTime.TotalSeconds) / BYTES_TO_MEGABYTES; bool informLog = transferSpeed.HasValue && transferSpeed.Value < USB_TRANSFER_SPEED_THRES; UnityEngine.Debug.Log("Pvr ADB Tool: Push Success"); // Install the APK package on the device IncrementProgressBar("Installing APK . . ."); string apkPath = REMOTE_APK_PATH + "/" + productName + "-debug.apk"; apkPath = apkPath.Replace(" ", "\\ "); string[] installCommand = { "-d shell", "pm install -r", apkPath }; timerStart = DateTime.Now; if (adbTool.RunCommand(installCommand, null, out output, out error) != 0) { return(false); } TimeSpan installTime = System.DateTime.Now - timerStart; UnityEngine.Debug.Log("Pvr ADB Tool: Install Success"); // Start the application on the device IncrementProgressBar("Launching application on device . . ."); string playerActivityName = "\"" + applicationIdentifier + "/com.unity3d.player.UnityPlayerNativeActivityPico\""; string[] appStartCommand = { "-d shell", "am start -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -S -f 0x10200000 -n", playerActivityName }; if (adbTool.RunCommand(appStartCommand, null, out output, out error) != 0) { return(false); } UnityEngine.Debug.Log("Pvr ADB Tool: Application Start Success"); // Send back metrics on push and install steps IncrementProgressBar("Success!"); // If the user is using a USB 2.0 cable, inform them about improved transfer speeds and estimate time saved if (informLog) { var usb3Time = apkSize.Value / (USB_3_TRANSFER_SPEED * BYTES_TO_MEGABYTES); UnityEngine.Debug.Log(string.Format("Build has detected slow transfer speeds. A USB 3.0 cable is recommended to reduce the time it takes to deploy your project by approximatly {0:0.0} seconds", pushTime.TotalSeconds - usb3Time)); return(true); } } else { UnityEngine.Debug.LogError("Could not find the ADB executable in the specified Android SDK directory."); } return(false); }
static void BuildAndRun() { GetWindow(typeof(Pvr_BuildTools)); showCancel = false; buildFailed = false; totalBuildTime = 0; InitializeProgressBar(NUM_BUILD_AND_RUN_STEPS); IncrementProgressBar("Exporting Unity Project . . ."); if (!CheckADBDevices()) { buildFailed = true; return; } apkOutputSuccessful = null; syncCancelToken = null; gradleBuildProcess = null; UnityEngine.Debug.Log("PvrBuild: Starting Unity build ..."); SetupDirectories(); // 1. Get scenes to build in Unity, and export gradle project var buildResult = UnityBuildPlayer(); if (buildResult.summary.result == UnityEditor.Build.Reporting.BuildResult.Succeeded) { totalBuildTime += buildResult.summary.totalTime.TotalSeconds; // Set static variables so build thread has updated data showCancel = true; gradlePath = Pvr_ADBTool.GetGradlePath(); jdkPath = Pvr_ADBTool.GetJDKPath(); androidSdkPath = Pvr_ADBTool.GetAndroidSDKPath(); applicationIdentifier = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android); #if UNITY_2019_3_OR_NEWER productName = "launcher"; #else productName = Application.productName; #endif dataPath = Application.dataPath; buildThread = new Thread(delegate() { BuildRun(); }); buildThread.Start(); return; } else if (buildResult.summary.result == UnityEditor.Build.Reporting.BuildResult.Cancelled) { UnityEngine.Debug.Log("Build canceled."); } else { UnityEngine.Debug.Log("Build failed."); } buildFailed = true; }