From 938adb539fdef4cbd6b63b431665fe391c540616 Mon Sep 17 00:00:00 2001 From: Michael Ruhl Date: Mon, 5 Jul 2021 20:38:30 +0200 Subject: [PATCH] Reuse existent avd for faster boot up if the image is the same Can be disabled with the new options: cold_boot --- README.md | 5 +- .../actions/android_emulator_action.rb | 46 ++++++++++++------- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 785455a..5a019dc 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ With additional features: ## Example -**Available Options:** sdk_dir, package, name, device, port, location, demo_mode +**Available Options:** sdk_dir, package, name, device, port, location, demo_mode, cold_boot ```ruby android_emulator( @@ -29,7 +29,8 @@ android_emulator( package: "system-images;android-24;google_apis;x86_64", demo_mode: true, sdk_dir: "PATH_TO_SDK", - device: "Nexus 5" + device: "Nexus 5", + cold_boot: false ) ``` diff --git a/lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb b/lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb index 2b0848c..37e92d1 100644 --- a/lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb +++ b/lib/fastlane/plugin/android_emulator/actions/android_emulator_action.rb @@ -4,6 +4,11 @@ module Fastlane module Actions class AndroidEmulatorAction < Action + def self.avd_active(params) + image = params[:package].gsub(";", "/") + return File.readlines("#{Dir.home}/.android/avd/#{params[:name]}.avd/config.ini").grep(/#{image}/).size > 0 + end + def self.run(params) sdk_dir = params[:sdk_dir] port = params[:port] @@ -11,22 +16,24 @@ def self.run(params) UI.message("Stopping emulator") system("#{adb} emu kill > /dev/null 2>&1 &") - sleep(2) - - UI.message("Creating new emulator") - FastlaneCore::CommandExecutor.execute( - command: "#{sdk_dir}/tools/bin/avdmanager create avd -n '#{params[:name]}' -f -k '#{params[:package]}' -d '#{params[:device]}'", - print_all: true, - print_command: false - ) - - UI.message("Override configuration") - open("#{Dir.home}/.android/avd/#{params[:name]}.avd/config.ini", 'a') { |f| - f << "hw.gpu.mode=auto\n" - f << "hw.gpu.enabled=yes\n" - f << "skin.dynamic=yes\n" - f << "skin.name=1080x1920\n" - } + sleep(3) + + if !avd_active(params) || params[:cold_boot] + UI.message("Creating new emulator") + FastlaneCore::CommandExecutor.execute( + command: "#{sdk_dir}/tools/bin/avdmanager create avd -n '#{params[:name]}' -f -k '#{params[:package]}' -d '#{params[:device]}'", + print_all: true, + print_command: false + ) + + UI.message("Override configuration") + open("#{Dir.home}/.android/avd/#{params[:name]}.avd/config.ini", 'a') { |f| + f << "hw.gpu.mode=auto\n" + f << "hw.gpu.enabled=yes\n" + f << "skin.dynamic=yes\n" + f << "skin.name=1080x1920\n" + } + end # Verify HAXM installed on mac if FastlaneCore::Helper.mac? @@ -113,7 +120,12 @@ def self.available_options env_name: "AVD_DEMO_MODE", description: "Set the emulator in demo mode", is_string: false, - default_value: true) + default_value: true), + FastlaneCore::ConfigItem.new(key: :cold_boot, + env_name: "AVD_COLD_BOOT", + description: "Create a new AVD every run", + is_string: false, + default_value: false) ] end