Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

android - ADB Error codes

We have an android device and as part of testing I need to excute a console test application on the target device. If the test application detects an error it returns -1.

I can use adb shell to run the test applications remotely on the target but I can't find a way of getting back the return code. I need this so I that I can build this into an automated test suite.

I could try grepping the console output for some failure text but that is a bit grubby. Does anyone know of a more elegant solution?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a workaround to get the exit code: adb shell '{your command here} > /dev/null 2>&1; echo $?'

This is a wrapper around adb in Ruby:

def adb(opt)
  input = "#{adb_command} #{opt[:command]} #{opt[:params]}"
  puts "Executing #{input}...
"
  output = nil
  exit_code = 0

  def wait_for(secs)
    if secs
      begin
        Timeout::timeout(secs) { yield }
      rescue
        print 'execution expired'
      end
    else
      yield
    end
  end

  wait_for(opt[:timeout]) do
    case opt[:command]
    when :install, :push, :uninstall
      output, exit_code = `#{input}`, $?.to_i
    when :shell
      input = "#{adb_command} shell "#{opt[:params]}; echo \$?""
      output = `#{input}`.split("
")
      exit_code = output.pop.to_i
      output = output.join("
")
    else
      raise 'Error: param command to adb not defined!'
    end
  end

  return if opt[:ignore_fail] and output =~ /#{opt[:ignore_fail]}/
  raise output unless exit_code == 0
end

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...