Sha256: 01265698981f569f95692f2d756222bd6b5c18db862217827896ded393971b77

Contents?: true

Size: 1.51 KB

Versions: 6

Compression:

Stored size: 1.51 KB

Contents

require "tmpdir"
require "securerandom"

module Acouchi
  class ApkModifier
    def initialize apk
      @apk = apk
      @output_path = "#{Dir.tmpdir}/#{SecureRandom.uuid}/"
    end

    def modify_manifest
      if block_given?
        decompile_apk
        manifest_path = File.join(@output_path, "AndroidManifest.xml")
        new_manifest = yield(File.read(manifest_path))
        File.open(manifest_path, "w") {|f| f.write(new_manifest)}
        compile_apk
        sign_apk_in_debug_mode
        overwrite_original_apk
      else
        throw "modify_manifest takes a block"
      end
    end

    private
      def decompile_apk
        ProcessLauncher.new(Executables.apktool, "d", @apk, @output_path).with_inherited_io.start_and_crash_if_process_fails
      end

      def compile_apk
        ProcessLauncher.new(Executables.apktool, "b", @output_path).with_inherited_io.start_and_crash_if_process_fails
      end

      def sign_apk_in_debug_mode
        @new_apk = File.join(@output_path, "dist", File.basename(@apk))
        jarsigner = Executables.jarsigner
        debug_keystore = File.join(ENV["HOME"], ".android", "debug.keystore")
        ProcessLauncher.new(
          jarsigner,
          "-keystore",
          debug_keystore,
          "-storepass",
          "android",
          "-keypass",
          "android",
          @new_apk,
          "androiddebugkey"
        ).with_inherited_io.start_and_crash_if_process_fails
      end

      def overwrite_original_apk
        FileUtils.mv(@new_apk, @apk)
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
acouchi-0.0.14 lib/acouchi/apk_modifier.rb
acouchi-0.0.13 lib/acouchi/apk_modifier.rb
acouchi-0.0.12 lib/acouchi/apk_modifier.rb
acouchi-0.0.11 lib/acouchi/apk_modifier.rb
acouchi-0.0.10 lib/acouchi/apk_modifier.rb
acouchi-0.0.9 lib/acouchi/apk_modifier.rb