Skip to content

Instantly share code, notes, and snippets.

@FiV0
Last active July 10, 2017 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiV0/ac63421b9c03cae277bd356393a70d29 to your computer and use it in GitHub Desktop.
Save FiV0/ac63421b9c03cae277bd356393a70d29 to your computer and use it in GitHub Desktop.
Extracts jar class files for android from aar files.
#!/usr/bin/python
import sys, os
from os.path import isdir
import glob
from shutil import copy
import zipfile
source_dir = ''
destination_dir = ''
def print_usage():
print "Usage:"
print "./extract_classes -src /path/to/android/sdk -des /my/dir"
def relative_to_absolute(dir):
if dir[0] == '.':
return os.path.join(os.getcwd(),dir)
return dir
def check_cmd_arguments(args):
if len(args) != 5:
print "To few or many argmuents given:"
print_usage()
sys.exit(1)
if not((args[1] == '-src' and args[3] == '-des') or \
(args[3] == '-src' and args[1] == '-des')):
print "Wrong options given!"
print_usage()
sys.exit(1)
global source_dir
global destination_dir
if args[1] == '-src':
source_dir = relative_to_absolute(args[2])
destination_dir = relative_to_absolute(args[4])
else :
source_dir = relative_to_absolute(args[4])
destination_dir = relative_to_absolute(args[2])
if not(os.path.isdir(source_dir)) or not(os.path.isdir(destination_dir)):
print "Wrong path specified!"
print_usage()
sys.exit(1)
def extract_class(root_dir):
for dirpath, dir_names, filenames in os.walk(root_dir):
# print dirpath
os.chdir(dirpath)
jars = glob.glob("*.jar")
if jars:
for jar in jars:
if "sources" not in jar and "javadoc" not in jar:
new_file = os.path.join(destination_dir,jar)
copy(jar,new_file)
aars = glob.glob("*.aar")
if aars :
print dirpath
for aar in aars:
#os.mkdir(os.path.splitext(aar)[0])
dir_name = os.path.splitext(aar)[0]
new_dir = os.path.join(dirpath, dir_name)
zipfile.ZipFile(os.path.join(dirpath, aar)).extractall(os.path.join(dirpath, dir_name))
os.chdir(new_dir)
jars = glob.glob("*.jar")
for jar in jars:
new_file = os.path.join(destination_dir,dir_name + ".jar")
copy(jar,new_file)
check_cmd_arguments(sys.argv)
extract_class(source_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment