class Decompiler

This class uses apktool and dex2jar to perform the de-compilation of APKs and outputs the results into their respective directories

Public Class Methods

convert_apk_to_jar() click to toggle source

This method takes the file name of the .apk and converts it to a .jar

# File lib/decompiler/decompiler.rb, line 73
def self.convert_apk_to_jar
      file_name = File.basename(@apk).sub(/.apk/, ".jar")
end
convert_apkname_to_underscore() click to toggle source

This method converts periods “.” within filenames to underscore

# File lib/decompiler/decompiler.rb, line 67
def self.convert_apkname_to_underscore
      file_name = File.basename(@apk).underscore.tr(".", "_")
end
execute() click to toggle source

This method executes the run_apktool and run_dex2jar methods

# File lib/decompiler/decompiler.rb, line 61
def self.execute
      run_apktool
      run_dex2jar
end
exists(data_type, entity) click to toggle source

This method validates that the files and directories being referenced exist

# File lib/decompiler/decompiler.rb, line 32
def self.exists(data_type, entity)  
      val = instance_variable_get ("@#{entity}".underscore)
      case data_type
      when "dir"
              Dir.exists?(val)
      when "file"
              File.exists?(val)
      end 
end
my_hash() click to toggle source

This method creates a hash of the 4 inputs for the tool

# File lib/decompiler/decompiler.rb, line 6
def self.my_hash
 {
"apk" => "file",
"apktool" => "file",
"output directory" => "dir",
"d2j-dex2jar .sh or .bat" => "file"
}
end
run() click to toggle source

This method creates instance variable @k and fills up the array my_hash with the 4 values its expecting

# File lib/decompiler/decompiler.rb, line 17
def self.run
       my_hash.each do |k,v|
              puts "Please specify the absolute location of the #{k}"
              instance_variable_set("@#{k}".underscore, gets.chomp)
              unless self.exists(v,k)
                      redo
              end
       end
       execute
       rescue ::Interrupt
            puts "\nGoodbye"
end
run_apktool() click to toggle source

This method runs apktool and outputs the results into its own directory

# File lib/decompiler/decompiler.rb, line 79
def self.run_apktool
      system "java", "-jar", @apktool, "d", "-f", @apk, "#{@output_directory}/apktool_output/#{self.convert_apkname_to_underscore}" 
end
run_dex2jar() click to toggle source

This method executes dex2jar and outputs the file under its own dex2jar directory

# File lib/decompiler/decompiler.rb, line 84
def self.run_dex2jar
      path = "#{@output_directory}/dex2jar_output"
      Dir.mkdir path if not Dir.exist? path     
      system @d2j_dex2jar_sh_or_bat, @apk, "-f", "-o", "#{path}/#{convert_apk_to_jar}" 
end
set_apk_var(apk_loc = "") click to toggle source

This method defines the instance variable @apk as the location

of the APK file that the user has provided.

# File lib/decompiler/decompiler.rb, line 54
def self.set_apk_var(apk_loc = "")
   raise "NotAValidApkLocationValue" if !(apk_loc.kind_of?(String))
   raise "NoAPKProvidedFromConfigFile" if apk_loc.empty?
   @apk = apk_loc
 end
set_static_vars(opts={}) click to toggle source

This method has the tool revert to having the user manually enter the 4 parameters needed to run the tool in the case the YAML config file does not exist

# File lib/decompiler/decompiler.rb, line 45
def self.set_static_vars(opts={})
  raise "NoOptionsProvidedFromConfigFile" if opts.empty?
  @apktool = opts[:apktool]
  @output_directory = opts[:output_directory]
  @d2j_dex2jar_sh_or_bat = opts[:d2j]
end