Class: Android::Apk

Inherits:
Object
  • Object
show all
Defined in:
lib/android/apk.rb

Overview

apk object class

Constant Summary collapse

MANIFEST =

AndroidManifest file name

'AndroidManifest.xml'
DEX =

dex file name

'classes.dex'
RESOURCE =

resource file name

'resources.arsc'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ Apk

create new apk object

Parameters:

  • filepath (String)

    apk file path

Raises:



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
# File 'lib/android/apk.rb', line 38

def initialize(filepath)
  @path = filepath
  raise NotFoundError, "'#{filepath}'" unless File.exist? @path
  begin
    @zip = Zip::ZipFile.open(@path)
  rescue Zip::ZipError => e
    raise NotApkFileError, e.message 
  end

  @bindata = File.open(@path, 'rb') {|f| f.read }
  @bindata.force_encoding(Encoding::ASCII_8BIT)
  raise NotApkFileError, "manifest file is not found." if @zip.find_entry(MANIFEST).nil?
  begin
    @manifest = Android::Manifest.new(self.file(MANIFEST))
  rescue => e
    $stderr.puts "failed to parse manifest:#{e}"
    #$stderr.puts e.backtrace
  end
  begin
    @dex = Android::Dex.new(self.file(DEX))
  rescue => e
    $stderr.puts "failed to parse dex:#{e}"
    #$stderr.puts e.backtrace
  end
  begin
    @resource = Android::Resource.new(self.file(RESOURCE))
  rescue => e
    $stderr.puts "failed to parse resource:#{e}"
    #$stderr.puts e.backtrace
  end
end

Instance Attribute Details

#bindataString (readonly)

Returns binary data of apk.

Returns:

  • (String)

    binary data of apk



22
23
24
# File 'lib/android/apk.rb', line 22

def bindata
  @bindata
end

#dexAndroid::Dex? (readonly)

Returns:

  • (Android::Dex)

    dex instance

  • (nil)

    when parsing dex is failed.



20
21
22
# File 'lib/android/apk.rb', line 20

def dex
  @dex
end

#manifestAndroid::Manifest? (readonly)

Returns:

  • (Android::Manifest)

    manifest instance

  • (nil)

    when parsing manifest is failed.



17
18
19
# File 'lib/android/apk.rb', line 17

def manifest
  @manifest
end

#pathString (readonly)

Returns apk file path.

Returns:

  • (String)

    apk file path



14
15
16
# File 'lib/android/apk.rb', line 14

def path
  @path
end

#resourceResource? (readonly)

Returns:

  • (Resource)

    resouce data

  • (nil)

    when parsing resource is failed.



25
26
27
# File 'lib/android/apk.rb', line 25

def resource
  @resource
end

Instance Method Details

#digest(type = :sha1) ⇒ String

return hex digest string of apk file

Parameters:

  • type (Symbol) (defaults to: :sha1)

    hash digest type(:sha1, sha256, :md5)

Returns:

  • (String)

    hex digest string

Raises:

  • (ArgumentError)

    type is knknown type



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/android/apk.rb', line 80

def digest(type = :sha1)
  case type
  when :sha1
    Digest::SHA1.hexdigest(@bindata)
  when :sha256
    Digest::SHA256.hexdigest(@bindata)
  when :md5
    Digest::MD5.hexdigest(@bindata)
  else
    raise ArgumentError
  end
end

#each_entry {|entry| ... } ⇒ Object

Yields:

Yield Parameters:

  • entry (Zip::Entry)

    zip entry



119
120
121
122
123
124
# File 'lib/android/apk.rb', line 119

def each_entry
  @zip.each do |entry|
    next unless entry.file?
    yield entry
  end
end

#each_file {|name, data| ... } ⇒ Object

Yields:

  • (name, data)

Yield Parameters:

  • name (String)

    file name in apk

  • data (String)

    file data in apk



102
103
104
105
106
107
# File 'lib/android/apk.rb', line 102

def each_file
  @zip.each do |entry|
    next unless entry.file?
    yield entry.name, @zip.read(entry)
  end
end

#entry(name) ⇒ Zip::ZipEntry

find and return zip entry with name

Parameters:

  • name (String)

    file name in apk(fullpath)

Returns:

  • (Zip::ZipEntry)

    zip entry object

Raises:



130
131
132
133
134
# File 'lib/android/apk.rb', line 130

def entry(name)
  entry = @zip.find_entry(name)
  raise NotFoundError, "'#{name}'" if entry.nil?
  return entry
end

#file(name) ⇒ String

find and return binary data with name

Parameters:

  • name (String)

    file name in apk(fullpath)

Returns:

  • (String)

    binary data

Raises:



113
114
115
# File 'lib/android/apk.rb', line 113

def file(name) # get data by entry name(path)
  @zip.read(entry(name))
end

#find {|name, data| ... } ⇒ Array

find files which is matched with block condition

Examples:

apk = Apk.new(path)
elf_files = apk.find  { |name, data|  data[0..3] == [0x7f, 0x45, 0x4c, 0x46] } # ELF magic number

Yields:

  • (name, data)

    find condition

Yield Parameters:

  • name (String)

    file name in apk

  • data (String)

    file data in apk

Yield Returns:

  • (Array)

    Array of matched entry name

Returns:

  • (Array)

    Array of matched entry name



145
146
147
148
149
150
151
152
# File 'lib/android/apk.rb', line 145

def find(&block)
  found = []
  self.each_file do |name, data|
    ret = block.call(name, data)
    found << name if ret
  end
  found
end

#sizeInteger

return apk file size

Returns:

  • (Integer)

    bytes



72
73
74
# File 'lib/android/apk.rb', line 72

def size
  @bindata.size
end

#timeTime

returns date of AndroidManifest.xml as Apk date

Returns:

  • (Time)


95
96
97
# File 'lib/android/apk.rb', line 95

def time
  entry(MANIFEST).time
end