Source

APKinspector / APKtool.py

The default branch has multiple heads

Full commit
import os
import sys

from startQT import SYSPATH

class APKtool:
    
    # succesFlag == 1: It calls the apktool to decompile the apk to smali codes successfully
    successFlag = None
    firstFlag = None
    lastClassName = None
    
    
    
    def __init__(self, filename):
        self.successFlag = 0
        self.firstFlag = 0
        self.lastClassName = ""
        self.callAPKtool(filename)
        
    
    # use the apktool to get the smali codes and AndroidManifest.xml
    # return 1: success ; return 0: fail
    def callAPKtool(self, filename):
        outputPath = SYSPATH + "/temp/ApktoolOutput"
        cmd = "apktool d -d " + filename + " " + outputPath
        if os.system(cmd) !=0:
            self.successFlag = 0
        else:
            self.successFlag = 1


    def getManifest(self):
        if self.successFlag == 0:
            return [0, ""]
        
        path = SYSPATH + "/temp/ApktoolOutput/AndroidManifest.xml"
        try:
            data = open(path, "r").read()
        except IOError:
            print "IOError"
            data = ""
        return [1, data]


    # get the smali codes
    def getSmaliCode(self, className):
        if self.successFlag == 0:
            return [0, ""]
        
        className = className[1:-1] + ".java"
        # the first time to call method "getSmaliCode"
        if self.firstFlag == 0:
            self.firstFlag ==1
            self.lastClassName = className
            classPath = SYSPATH + "/temp/ApktoolOutput/smali/" + className
            try:
                data = open(classPath, "r").read()
            except IOError:
                print "IOError"
                data = ""
            return [1, data]
        
        # if the lastClassName is equal to className, the smali codes need not to be updated
        if self.firstFlag == 1:
            if self.lastClassName == className:
                return [0, ""] 
            else:
                self.lastClassName = className
                classPath = SYSPATH + "/temp/ApktoolOutput/" + className
                data = open(classPath, "r").read()
                return [1, data]