Class: Android::Manifest

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

Overview

parsed AndroidManifest.xml class

Defined Under Namespace

Modules: IntentFilter Classes: Component, Meta

Constant Summary collapse

APPLICATION_TAG =
'/manifest/application'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, rsc = nil) ⇒ Manifest

Returns a new instance of Manifest.

Parameters:

  • data (String)

    binary data of AndroidManifest.xml



156
157
158
159
160
# File 'lib/android/manifest.rb', line 156

def initialize(data, rsc=nil)
  parser = AXMLParser.new(data)
  @doc = parser.parse
  @rsc = rsc
end

Instance Attribute Details

#docREXML::Document (readonly)

Manifest class definitions

Returns:

  • (REXML::Document)

    manifest xml



153
154
155
# File 'lib/android/manifest.rb', line 153

def doc
  @doc
end

Instance Method Details

#componentsArray<Android::Manifest::Component>

Note:

return empty array when the manifest include no components

Returns all components in apk.

Returns:



175
176
177
178
179
180
181
182
183
# File 'lib/android/manifest.rb', line 175

def components
  components = []
  unless @doc.elements['/manifest/application'].nil?
    @doc.elements['/manifest/application'].each do |elem|
      components << Component.new(elem) if Component.valid?(elem)
    end
  end
  components
end

#label(lang = nil) ⇒ String?

application label

Parameters:

  • lang (String) (defaults to: nil)

    language code like ‘ja’, ‘cn’, …

Returns:

  • (String)

    application label string(if resouce is provided), or label resource id

  • (nil)

    when label is not found

Since:

  • 0.5.1



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/android/manifest.rb', line 221

def label(lang=nil)
  label = @doc.elements['/manifest/application'].attributes['label']
  if label.nil?
    # application element has no label attributes.
    # so looking for activites that has label attribute.
    activities = @doc.elements['/manifest/application'].find{|e| e.name == 'activity' && !e.attributes['label'].nil? }
    label = activities.nil? ? nil : activities.first.attributes['label']
  end
  unless @rsc.nil?
    if /^@(\w+\/\w+)|(0x[0-9a-fA-F]{8})$/ =~ label
      opts = {}
      opts[:lang] = lang unless lang.nil?
      label = @rsc.find(label, opts)
    end
  end
  label
end

#min_sdk_verInteger

Returns minSdkVersion in uses element.

Returns:

  • (Integer)

    minSdkVersion in uses element



212
213
214
# File 'lib/android/manifest.rb', line 212

def min_sdk_ver
  @doc.elements['/manifest/uses-sdk'].attributes['minSdkVersion'].to_i
end

#package_nameString

application package name

Returns:

  • (String)


187
188
189
# File 'lib/android/manifest.rb', line 187

def package_name
  @doc.root.attributes['package']
end

#to_xml(indent = 4) ⇒ String

return xml as string format

Parameters:

  • indent (Integer) (defaults to: 4)

    size(bytes)

Returns:

  • (String)

    raw xml string



242
243
244
245
246
247
# File 'lib/android/manifest.rb', line 242

def to_xml(indent=4)
  xml =''
  formatter = REXML::Formatters::Pretty.new(indent)
  formatter.write(@doc.root, xml)
  xml
end

#use_permissionsArray<String>

Note:

return empty array when the manifest includes no use-parmission element

used permission array

Returns:

  • (Array<String>)

    permission names



165
166
167
168
169
170
171
# File 'lib/android/manifest.rb', line 165

def use_permissions
  perms = []
  @doc.each_element('/manifest/uses-permission') do |elem|
    perms << elem.attributes['name']
  end
  perms.uniq
end

#version_codeInteger

application version code

Returns:

  • (Integer)


193
194
195
# File 'lib/android/manifest.rb', line 193

def version_code
  @doc.root.attributes['versionCode'].to_i
end

#version_name(lang = nil) ⇒ String

application version name

Returns:

  • (String)


199
200
201
202
203
204
205
206
207
208
209
# File 'lib/android/manifest.rb', line 199

def version_name(lang=nil)
  vername = @doc.root.attributes['versionName']
  unless @rsc.nil?
    if /^@(\w+\/\w+)|(0x[0-9a-fA-F]{8})$/ =~ vername
      opts = {}
      opts[:lang] = lang unless lang.nil?
      vername = @rsc.find(vername, opts)
    end
  end
  vername
end