Class: Android::Manifest

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

Overview

parsed AndroidManifest.xml class

Defined Under Namespace

Classes: Activity, ActivityAlias, Application, Component, IntentFilter, Meta, Provider, Queries, QueriesComponent, Receiver, Service

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



426
427
428
429
430
# File 'lib/android/manifest.rb', line 426

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

Instance Attribute Details

#docREXML::Document (readonly)

Returns manifest xml.

Returns:

  • (REXML::Document)

    manifest xml



423
424
425
# File 'lib/android/manifest.rb', line 423

def doc
  @doc
end

Instance Method Details

#activitiesArray<Android::Manifest::Activity&ActivityAlias>

Note:

return empty array when the manifest include no activities

Returns all activities in the apk.

Returns:



475
476
477
478
479
480
481
482
483
484
485
# File 'lib/android/manifest.rb', line 475

def activities
  activities = []
  unless @doc.elements['/manifest/application'].nil?
    @doc.elements['/manifest/application'].each do |elem|
      next unless Activity.valid?(elem)

      activities << (elem.name == 'activity-alias' ? ActivityAlias.new(elem) : Activity.new(elem))
    end
  end
  activities
end

#applicationAndroid::Manifest::Application

Returns the manifest’s application element or nil, if there isn’t any.

Returns:



449
450
451
452
# File 'lib/android/manifest.rb', line 449

def application
  element = @doc.elements['//application']
  Application.new(element) if Application.valid?(element)
end

#componentsArray<Android::Manifest::Component>

Note:

return empty array when the manifest include no components

Returns all components in apk.

Returns:



456
457
458
459
460
461
462
463
464
# File 'lib/android/manifest.rb', line 456

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
Note:

return empty array when the manifest include no http or https scheme of data

Returns all deep link host and schemes in intent filters.

Returns:

  • (Array<String>)

    all deep link host and schemes in intent filters

Since:

  • 2.5.0



497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/android/manifest.rb', line 497

def deep_links
  activities.each_with_object([]) do |activity, obj|
    intent_filters = activity.intent_filters
    next if intent_filters.empty?

    intent_filters.each do |filter|
      next unless filter.deep_links?

      obj << filter.deep_links
    end
  end.flatten.uniq
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



578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# File 'lib/android/manifest.rb', line 578

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

#launcher_activitiesArray<Android::Manifest::Activity&ActivityAlias>

Note:

return empty array when the manifest include no activities

Returns all activities that are launchers in the apk.

Returns:



528
529
530
# File 'lib/android/manifest.rb', line 528

def launcher_activities
  activities.select(&:launcher_activity?)
end

#min_sdk_verInteger

Returns minSdkVersion in uses element.

Returns:

  • (Integer)

    minSdkVersion in uses element



559
560
561
562
563
# File 'lib/android/manifest.rb', line 559

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

#package_nameString

application package name

Returns:

  • (String)


534
535
536
# File 'lib/android/manifest.rb', line 534

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

#queriesAndroid::Manifest::Queries

Returns the manifest’s queries element or nil, if there isn’t any.

Returns:



468
469
470
471
# File 'lib/android/manifest.rb', line 468

def queries
  element = @doc.elements['/manifest/queries']
  Queries.new(element) if Queries.valid?(element)
end

#schemesArray<String>

Note:

return empty array when the manifest not include http or https scheme(s) of data

Returns all schemes in intent filters.

Returns:

  • (Array<String>)

    all schemes in intent filters

Since:

  • 2.5.0



513
514
515
516
517
518
519
520
521
522
523
524
# File 'lib/android/manifest.rb', line 513

def schemes
  activities.each_with_object([]) do |activity, obj|
    intent_filters = activity.intent_filters
    next if intent_filters.empty?

    intent_filters.each do |filter|
      next unless filter.schemes?

      obj << filter.schemes
    end
  end.flatten.uniq
end

#servicesArray<Android::Manifest::Component>

Note:

return empty array when the manifest include no services

Returns all services in the apk.

Returns:

Since:

  • 2.5.0



490
491
492
# File 'lib/android/manifest.rb', line 490

def services
  components.select { |c| c.type == 'service' }
end

#target_sdk_versionInteger

Returns targetSdkVersion in uses element.

Returns:

  • (Integer)

    targetSdkVersion in uses element

Since:

  • 2.5.0



567
568
569
570
571
# File 'lib/android/manifest.rb', line 567

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

#to_xml(indent = 4) ⇒ String

return xml as string format

Parameters:

  • indent (Integer) (defaults to: 4)

    size(bytes)

Returns:

  • (String)

    raw xml string



599
600
601
602
603
604
# File 'lib/android/manifest.rb', line 599

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

#use_featuresArray<String>

Note:

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

used features array

Returns:

  • (Array<String>)

    features names

Since:

  • 2.5.0



443
444
445
# File 'lib/android/manifest.rb', line 443

def use_features
  manifest_values('/manifest/uses-feature')
end

#use_permissionsArray<String>

Note:

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

used permission array

Returns:

  • (Array<String>)

    permission names



435
436
437
# File 'lib/android/manifest.rb', line 435

def use_permissions
  manifest_values('/manifest/uses-permission')
end

#version_codeInteger

application version code

Returns:

  • (Integer)


540
541
542
# File 'lib/android/manifest.rb', line 540

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

#version_name(lang = nil) ⇒ String

application version name

Returns:

  • (String)


546
547
548
549
550
551
552
553
554
555
556
# File 'lib/android/manifest.rb', line 546

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