Skip to content

Commit

Permalink
Reuse existent avd for faster boot up if the image is the same
Browse files Browse the repository at this point in the history
Can be disabled with the new options: cold_boot
  • Loading branch information
m-ruhl committed Jul 5, 2021
1 parent 7e2a212 commit 938adb5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ 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(
location: '9.1808 48.7771',
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
)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,36 @@
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]
adb = "#{sdk_dir}/platform-tools/adb"

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?
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 938adb5

Please sign in to comment.