Top Level Namespace

Defined Under Namespace

Modules: Calabash Classes: Env, JavaKeystore

Instance Method Summary collapse

Instance Method Details

#aapt_dump(app, key) ⇒ Object



79
80
81
82
# File 'lib/calabash-android/helpers.rb', line 79

def aapt_dump(app, key)
  lines = `"#{Env.tools_dir}/aapt" dump badging "#{app}"`.lines.collect(&:strip)
  lines.select { |l| l.start_with?("#{key}:") }
end

#build_test_server_if_needed(app_path) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/calabash-android/helpers.rb', line 93

def build_test_server_if_needed(app_path)
  unless File.exist?(test_server_path(app_path))
    if ARGV.include? "--no-build"
      puts "No test server found for this combination of app and calabash version. Exiting!"
      exit 1
    else
      puts "No test server found for this combination of app and calabash version. Recreating test server."
      calabash_build(app_path)
    end
  end
end

#checksum(file_path) ⇒ Object



84
85
86
87
# File 'lib/calabash-android/helpers.rb', line 84

def checksum(file_path)
  require 'digest/md5'
  Digest::MD5.file(file_path).hexdigest
end

#extract_md5_fingerprint(fingerprints) ⇒ Object



172
173
174
175
176
# File 'lib/calabash-android/helpers.rb', line 172

def extract_md5_fingerprint(fingerprints)
  m = fingerprints.scan(/MD5.*((?:[a-fA-F\d]{2}:){15}[a-fA-F\d]{2})/).flatten
  raise "No MD5 fingerprint found:\n #{fingerprints}" if m.empty?
  m.first
end

#extract_signature_algorithm_name(fingerprints) ⇒ Object



178
179
180
181
182
# File 'lib/calabash-android/helpers.rb', line 178

def extract_signature_algorithm_name(fingerprints)
  m = fingerprints.scan(/Signature algorithm name: (.*)/).flatten
  raise "No signature algorithm names found:\n #{fingerprints}" if m.empty?
  m.first
end

#fingerprint_from_apk(app_path) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/calabash-android/helpers.rb', line 142

def fingerprint_from_apk(app_path)
  app_path = File.expand_path(app_path)
  Dir.mktmpdir do |tmp_dir|
    Dir.chdir(tmp_dir) do
      FileUtils.cp(app_path, "app.apk")
      FileUtils.mkdir("META-INF")
      Zip::File.foreach("app.apk") do |z|
        z.extract if /^META-INF\/\w+.(rsa|dsa)/i =~ z.name
      end
      signature_files = Dir["#{tmp_dir}/META-INF/*"]

      log 'Signature files:'

      signature_files.each do |signature_file|
        log signature_file
      end

      raise "No signature files found in META-INF. Cannot proceed." if signature_files.empty?
      raise "More than one signature file (DSA or RSA) found in META-INF. Cannot proceed." if signature_files.length > 1

      cmd = "#{Env.keytool_path} -v -printcert -J\"-Dfile.encoding=utf-8\" -file \"#{signature_files.first}\""
      log cmd
      fingerprints = `#{cmd}`
      md5_fingerprint = extract_md5_fingerprint(fingerprints)
      log "MD5 fingerprint for signing cert (#{app_path}): #{md5_fingerprint}"
      md5_fingerprint
    end
  end
end

#log(message, error = false) ⇒ Object



184
185
186
# File 'lib/calabash-android/helpers.rb', line 184

def log(message, error = false)
  $stdout.puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} - #{message}" if (error or ARGV.include? "-v" or ARGV.include? "--verbose")
end

#main_activity(app) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/calabash-android/helpers.rb', line 20

def main_activity(app)
  unless File.exist?(app)
    raise "Application '#{app}' does not exist"
  end

  begin
    log("Trying to find launchable activity")
    launchable_activity_line = aapt_dump(app, "launchable-activity").first
    raise "'launchable-activity' not found in aapt output" unless launchable_activity_line
    m = launchable_activity_line.match(/name='([^']+)'/)
    raise "Unexpected output from aapt: #{launchable_activity_line}" unless m
    log("Found launchable activity '#{m[1]}'")
    m[1]
  rescue => e
    log("Could not find launchable activity, trying to parse raw AndroidManifest. #{e.message}")

    manifest_data = `"#{Env.tools_dir}/aapt" dump xmltree "#{app}" AndroidManifest.xml`
    regex = /^\s*A:[\s*]android:name\(\w+\)\=\"android.intent.category.LAUNCHER\"/
    lines = manifest_data.lines.collect(&:strip)
    indicator_line = nil

    lines.each_with_index do |line, index|
      match = line.match(regex)

      unless match.nil?
        raise 'More than one launchable activity in AndroidManifest' unless indicator_line.nil?
        indicator_line = index
      end
    end

    raise 'No launchable activity found in AndroidManifest' unless indicator_line

    intent_filter_found = false

    (0..indicator_line).reverse_each do |index|
      if intent_filter_found
        match = lines[index].match(/\s*E:\s*activity-alias/)

        raise 'Could not find target activity in activity alias' if match

        match = lines[index].match(/^\s*A:\s*android:targetActivity\(\w*\)\=\"([^\"]+)/){$1}

        if match
          log("Found launchable activity '#{match}'")

          return match
        end
      else
        unless lines[index].match(/\s*E: intent-filter/).nil?
          log("Read intent filter")
          intent_filter_found = true
        end
      end
    end

    raise 'Could not find launchable activity'
  end
end

#package_name(app) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/calabash-android/helpers.rb', line 8

def package_name(app)
  unless File.exist?(app)
    raise "Application '#{app}' does not exist"
  end

  package_line = aapt_dump(app, "package").first
  raise "'package' not found in aapt output" unless package_line
  m = package_line.match(/name='([^']+)'/)
  raise "Unexpected output from aapt: #{package_line}" unless m
  m[1]
end

#resign_apk(app_path) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/calabash-android/helpers.rb', line 105

def resign_apk(app_path)
  Dir.mktmpdir do |tmp_dir|
    log "Resign apk"
    unsigned_path = File.join(tmp_dir, 'unsigned.apk')
    unaligned_path = File.join(tmp_dir, 'unaligned.apk')
    FileUtils.cp(app_path, unsigned_path)
    unsign_apk(unsigned_path)
    sign_apk(unsigned_path, unaligned_path)
    zipalign_apk(unaligned_path, app_path)
  end
end

#sign_apk(app_path, dest_path) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/calabash-android/helpers.rb', line 132

def sign_apk(app_path, dest_path)
  java_keystore = JavaKeystore.get_keystores.first

  if java_keystore.nil?
    raise 'No keystores found. You can specify the keystore location and credentials using calabash-android setup'
  end

  java_keystore.sign_apk(app_path, dest_path)
end

#test_server_path(apk_file_path) ⇒ Object



89
90
91
# File 'lib/calabash-android/helpers.rb', line 89

def test_server_path(apk_file_path)
  "test_servers/#{checksum(apk_file_path)}_#{Calabash::Android::VERSION}.apk"
end

#unsign_apk(path) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/calabash-android/helpers.rb', line 117

def unsign_apk(path)
  files_to_remove = `"#{Env.tools_dir}/aapt" list "#{path}"`.lines.collect(&:strip).grep(/^META-INF\//)
  if files_to_remove.empty?
    log "App wasn't signed. Will not try to unsign it."
  else
    system("\"#{Env.tools_dir}/aapt\" remove \"#{path}\" #{files_to_remove.join(" ")}")
  end
end

#zipalign_apk(inpath, outpath) ⇒ Object



126
127
128
129
130
# File 'lib/calabash-android/helpers.rb', line 126

def zipalign_apk(inpath, outpath)
  cmd = %Q("#{Env.zipalign_path}" -f 4 "#{inpath}" "#{outpath}")
  log "Zipaligning using: #{cmd}"
  system(cmd)
end