Class: AndroidAdb::Adb

Inherits:
Object
  • Object
show all
Defined in:
lib/android-adb/Adb.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Adb

Contructs an Adb object for issuing commands to the connected device or emulator.

If the adb path is not specified in the opts hash it is determined in the following order:

  1. Try and use the unix which command to locate the adb binary.

  2. Use the ANDROID_HOME environment variable.

  3. Default to adb (no path specified).

Parameters:

  • opts (Hash) (defaults to: {})

    The options to create the Adb object with.

Options Hash (opts):

  • :show_stderr (Boolean)

    Used for diagnosing issues, it will dump the stderr of the adb command being processed.

  • :dry_run (Boolean)

    Does not execute the adb command but will output the command that would be run.

  • :adb_path (Boolean)

    Manually set the path to the adb binary.



24
25
26
27
28
# File 'lib/android-adb/Adb.rb', line 24

def initialize(opts = {})
  @show_stderr = opts[:show_stderr] || false
  @dry_run = opts[:dry_run] || false
  @adb_path = opts[:adb_path] || Adb.find_adb
end

Instance Attribute Details

#adb_pathObject

Returns the value of attribute adb_path.



11
12
13
# File 'lib/android-adb/Adb.rb', line 11

def adb_path
  @adb_path
end

#dry_runObject

Returns the value of attribute dry_run.



11
12
13
# File 'lib/android-adb/Adb.rb', line 11

def dry_run
  @dry_run
end

#last_commandObject

Returns the value of attribute last_command.



11
12
13
# File 'lib/android-adb/Adb.rb', line 11

def last_command
  @last_command
end

#show_stderrObject

Returns the value of attribute show_stderr.



11
12
13
# File 'lib/android-adb/Adb.rb', line 11

def show_stderr
  @show_stderr
end

Instance Method Details

#get_devicesArray<Device>

Returns a list of all connected devices. The collection is returned as a hash containing the device name :name and the serial :serial.

Returns:

  • (Array<Device>)

    THe list of connected devices/emulators.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/android-adb/Adb.rb', line 33

def get_devices
  devices = []
  run_adb("devices") do |pout|
    pout.each do |line|
      line = line.strip
      if (!line.empty? && line !~ /^List of devices/)
        parts = line.split
        device = AndroidAdb::Device.new(parts[0], parts[1])
        devices << device
      end
    end
  end
  return devices
end

#get_packages(adb_opts = {}) ⇒ Hash

Returns a list of all installed packages on the device.

Parameters:

  • adb_opts (Hash) (defaults to: {})

    Options for the adb command (@see #run_adb)

Returns:

  • (Hash)

    THe list of installed packages. The hash returned contains the apk file name :apk and the name of the package :name.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/android-adb/Adb.rb', line 51

def get_packages(adb_opts = {})
  packages = []
  run_adb_shell("pm list packages -f", adb_opts) do |pout|
    pout.each do |line|
      parts = line.split(":")
      if (parts.length > 1)
        info = parts[1].strip.split("=")
        package = AndroidAdb::Package.new(info[1], info[0]);
        packages << package;
      end
    end
  end
  return packages
end

#install(package, opts = {}, adb_opts = {}) ⇒ Object

Installs a package from the APK file package onto the device/emulator.

Parameters:

  • package (String)

    The APK package file

  • opts (Hash) (defaults to: {})

    The options used to install the package.

  • adb_opts (Hash) (defaults to: {})

    Options for the adb command (@see #run_adb)

Options Hash (opts):

  • :forwardlock (Boolean)

    Forward lock the package being installed.

  • :reinstall (Boolean)

    Reinstall the package, keeping existing data.

  • :sdcard (Boolean)

    Install the package to the SD card instead of internal storage.



73
74
75
76
77
78
79
# File 'lib/android-adb/Adb.rb', line 73

def install(package, opts = {}, adb_opts = {})
  opt_arg = ""
  opt_arg += " -l" if opts[:forwardlock]
  opt_arg += " -r" if opts[:reinstall]
  opt_arg += " -s" if opts[:sdcard]
  run_adb("install#{opt_arg} #{package}", adb_opts)
end

#run_adb(args, adb_opts = {}) {|stdout| ... } ⇒ Object

Run the adb command with the give args.

Parameters:

  • args (String)

    Arguments to the adb command.

  • adb_opts (Hash) (defaults to: {})

    Options for the adb command.

Options Hash (adb_opts):

  • :serial (String)

    directs command to the USB device or emulator with the given serial number.

  • :emulator (Boolean)

    Directs command to the only running emulator. Returns an error if more than one emulator is running.

  • :device (Boolean)

    Directs command to the only connected USB device. Returns an error if more than one USB device connected.

Yields:

  • (stdout)

    The standard output of the adb command.



88
89
90
91
92
93
94
95
96
# File 'lib/android-adb/Adb.rb', line 88

def run_adb(args, adb_opts = {}, &block) # :yields: stdout
  adb_arg = ""
  adb_arg += " -s #{adb_opts[:serial]}" if !adb_opts[:serial].nil? && !adb_opts[:serial].empty?
  adb_arg += " -e #{adb_opts[:emulator]}" if adb_opts[:emulator]
  adb_arg += " -d #{adb_opts[:device]}" if adb_opts[:emulator]
  path = "#{@adb_path}#{adb_arg} #{args}"
  last_command = path
  run(path, &block)
end

#run_adb_shell(args, adb_args = {}) {|stdout| ... } ⇒ Object

Run the device shell command given by args.

Parameters:

  • args (String)

    Arguments to the adb shell command.

  • adb_opts (Hash)

    Options for the adb command (@see run_adb).

Yields:

  • (stdout)

    The standard output of the adb command.



102
103
104
105
# File 'lib/android-adb/Adb.rb', line 102

def run_adb_shell(args, adb_args = {}, &block) # :yields: stdout
  args = "shell #{args}"
  run_adb(args, adb_args, &block)
end