另辟蹊径的 Android 包体积优化之 Jar 包压缩
1、背景
2、常量池解析
2.1 方案一:使用 javap 反编译类文件
javap -c -p xxx .class
private static java.util.ArrayList<com.ibm.icu.text.CharsetRecognizer> createRecognizers();
Code:
0: new #14 // class java/util/ArrayList
3: dup
4: invokespecial #15 // Method java/util/ArrayList."<init>":()V
7: astore_0
8: aload_0
9: new #39 // class com/ibm/icu/text/CharsetRecog_UTF8
12: dup
13: invokespecial #40 // Method com/ibm/icu/text/CharsetRecog_UTF8."<init>":()V
16: invokevirtual #23 // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
19: pop
20: aload_0
21: new #41 // class com/ibm/icu/text/CharsetRecog_Unicode$CharsetRecog_UTF_16_BE
24: dup
25: invokespecial #42 // Method com/ibm/icu/text/CharsetRecog_Unicode$CharsetRecog_UTF_16_BE."<init>":()V
// Method
这个关键字来获取这个类文件引用的类。不过这种方式存在一个问题,即,如果一个类不是在方法中引用而是通过
extends
的方式继承引用的则获取不到。不过,对于继承的方式也有一个解决办法,即对于继承,编译器会默认为子类生成构造方法并且在构造方法中调用父类的构造方法,所以我们可以在构造方法中获取到被引用的类。但是,如果是接口的话就无能为力了。所以,这种方案还是存在这样一个问题。
2.2 方案二:使用 `javap -verbose` 指令输出常量池
Constant pool:
#1 = Methodref #102. #196 // java/lang/Object."<init>":()V
#2 = Fieldref #101. #197 // com/ibm/icu/text/CharsetDetector.fInputBytes:[B
#3 = Fieldref #101. #198 // com/ibm/icu/text/CharsetDetector.fByteStats:[S
#4 = Fieldref #101. #199 // com/ibm/icu/text/CharsetDetector.fC1Bytes:Z
#5 = Fieldref #101. #200 // com/ibm/icu/text/CharsetDetector.fStripTags:Z
#6 = Fieldref #101. #201 // com/ibm/icu/text/CharsetDetector.fDeclaredEncoding:Ljava/lang/String;
#7 = Fieldref #101. #202 // com/ibm/icu/text/CharsetDetector.fRawInput:[B
#8 = Fieldref #101. #203 // com/ibm/icu/text/CharsetDetector.fRawLength:I
#9 = Fieldref #101. #204 // com/ibm/icu/text/CharsetDetector.fInputStream:Ljava/io/InputStream;
#10 = Methodref #205. #206 // java/io/InputStream.mark:(I)V
#11 = Methodref #205. #207 // java/io/InputStream.read:([BII)I
#12 = Methodref #205. #208 // java/io/InputStream.reset:()V
#13 = Methodref #101. #209 // com/ibm/icu/text/CharsetDetector.detectAll:()[Lcom/ibm/icu/text/CharsetMatch;
#14 = Class #210 // java/util/ArrayList
#15 = Methodref #14. #196 // java/util/ArrayList."<init>":()V
#16 = Methodref #101. #211 // com/ibm/icu/text/CharsetDetector.MungeInput:()V
#17 = Fieldref #101. #212 // com/ibm/icu/text/CharsetDetector.fCSRecognizers:Ljava/util/ArrayList;
#18 = Methodref #14. #213 // java/util/ArrayList.iterator:()Ljava/util/Iterator;
#19 = InterfaceMethodref #214. #215 // java/util/Iterator.hasNext:()Z
#20 = InterfaceMethodref #214. #216 // java/util/Iterator.next:()Ljava/lang/Object;
2.3 方案三:手动解析常量池
javap -verbose
输出的结果)。这很重要,因为常量池中常量会通过这个编号引用其他常量。
class PoolParser:
def __init__(self):
self.constants = {
3: 4, 4: 4, 5: 8, 6: 8, 7: 2, 8: 2, 9: 4, 10: 4,
11: 4, 12: 4, 15: 3, 16: 2, 17: 4, 18: 4, 19: 2, 20: 2
}
def parse(self, path: str) -> List[str]:
'''Parse constant pool of class file with path.'''
try:
with open(path, 'rb') as fp:
data = fp.read()
constant_count = data[ 8] * 16 * 16 + data[ 9]
cur_constant_index = 1
index = 10
sequences = {}
classes_nums = []
while cur_constant_index < constant_count:
constant_code = data[index]
if constant_code == 1:
char_length = data[index+ 1]* 16* 16+data[index+ 2]
char_start = index+ 3
char_end = char_start+char_length
# print(str(cur_constant_index) + '/' + str(constant_count) + ': ' + str(data[char_start:char_end], 'utf-8'))
sequences[cur_constant_index] = str(data[char_start:char_end], 'utf-8')
index = index + 2 + char_length + 1
cur_constant_index = cur_constant_index + 1
elif constant_code == 5 or constant_code == 6:
# Special guideline for double and long type, see https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.5
index = index + self.constants[constant_code] + 1
cur_constant_index = cur_constant_index + 2
else:
# Get number of class in the constant pool.
if constant_code == 7:
classes_nums.append(data[index+ 1]* 16* 16+data[index+ 2])
index = index + self.constants[constant_code] + 1
cur_constant_index = cur_constant_index + 1
return [sequences[num] for num in classes_nums]
except BaseException as e:
logging.error( "Error while parsing constant pool:\n%s" % traceback.format_exc())
print( "Error while parsing constant pool for [%s]" % path)
return []
对于 tag 为 5 和 6 的常量(实际就是 long 和 double 类型定义的常量)他们占用两个编号。这个定义感觉有些奇葩。如果不是看了 Oracle 的官方文档,一般的书还真查不到这个。就是说,如果一个 class 文件的常量池的大小部分的值是 30 并且常量池中包含了一个 double 类型,那么它实际只有 29 个常量而不是 30 个,因为 double 占了两个编号,尽管 class 文件上的布局是连续的。
确定常量池中的 class 的时候用到了 tag 为 7 的常量。这个常量 tag 之后的两个字节就是 class 的内容。所以,这是一种准确的找到常量池中的 class 的方法。(class 被放到 tag 为 1 的常量中,实际是作为 uft8 类型的字符串处理的)
最后,Oracle 对于 JVM 定义详细的文档:https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.5,感兴趣的可以自己查找,比任何书籍都更加全面。
3、整体的脚本实现
3.1 jar 包解析
def _unzip_jar(self):
'''Unzip the jar.'''
print( "Unzip jar ...")
logging.info( "Unzip jar ...")
self.unzip_to = str(int(time.time()))
try:
with open(self.jar_path, 'rb') as fp:
data = fp.read()
self.unzip_to = hashlib.md5(data).hexdigest()
except BaseException as e:
logging.error( "Error while reading the jar md5:\n%s" % traceback.format_exc())
print( "Error while reading the jar md5 [%s]" % self.jar_path)
self.unzip_to = 'space/%s/unzip' % self.unzip_to
if not os.path.exists(self.unzip_to):
try:
zipFile = zipfile.ZipFile(self.jar_path)
zipFile.extractall(self.unzip_to)
zipFile.close()
except BaseException as e:
logging.error( "Error while unzip the jar:\n%s" % traceback.format_exc())
print( "Error while unzip the jar [%s]" % self.jar_path)
else:
logging.info( "Unziped resources exists, ignore!")
print( "Unziped resources exists, ignore!")
3.2 广度优先搜索
def _search_by_read_constants(self):
'''Search by parsing constants.'''
print( "Searching classes by parsing constant pool ...")
logging.info( "Searching classes by parsing constant pool ...")
start = int(time.time())
self.classes = [os.path.join(self.unzip_to, cls.replace( '.', '/')) + '.class' for cls in self.classes]
visits = []
visits.extend(self.classes)
count = 0
parser = PoolParser()
while len(visits) > 0:
count = count + 1
cur_path = visits.pop()
exists = os.path.exists(cur_path)
logging.info( "Searching under [%d][%s][%s]" % (count, str(exists), self._simplify_class_path(cur_path)))
print( "Searching under [%d][%s][%s]" % (count, str(exists), self._simplify_class_path(cur_path)), end= '\r')
# Ignore if the class file not exists.
if not exists:
self.classes.remove(cur_path)
continue
# Parse classes from class constant pool.
classes = parser.parse(cur_path)
for cls in classes:
self._handle_classes(cls, cur_path, visits)
logging.info( "Searching classes by parsing constant pool done in [%d]" % (int(time.time())-start))
print( "Searching classes by parsing constant pool done in [%d]" % (int(time.time())-start))
def _handle_classes(self, cls: str, cur_path: str, visits: List[str]):
'''Handle classes.'''
pathes = [cls]
# Handle if the class is an inner class.
if cls.rfind( '$') > 0:
pos = cls.rfind( '$')
while pos > 0:
cls = cls[ 0:pos]
pathes.append(cls)
pos = cls.rfind( '$')
for path in pathes:
cls_path = os.path.join(self.unzip_to, path) + '.class'
# Only search one class a time.
if cls_path not in self.classes:
self.classes.append(cls_path)
visits.append(cls_path)
logging.info( "Found [%s] under [%s]" % (path, self._simplify_class_path(cur_path)))
AAA$BBB$CCC
有一个要求,我们在打包的时候必须将外部类同时打入到包中,否则内部类不会被打进最终的 jar 文件中。这里我写了一段程序来解析上述类型的内部类及其父类,即
_handle_classes()
方法中的前几行代码。
3.3 jar 打包
def _pack_classes(self):
'''Packing casses.'''
print( "Packing classes ...")
logging.info( "Packing classes ...")
if not os.path.exists(self.output):
os.mkdir(self.output
# Copy all filted classes to a directory.
for cls in self.classes:
dst = cls.replace(self.unzip_to, self.copy_to)
par = os.path.dirname(dst)
if not os.path.exists(par):
os.makedirs(par)
shutil.copyfile(cls, dst)
# Get into the copied directory and use jar to package the directory.
pack_command = 'cd %s && jar cvf %s/final.jar %s' % (self.copy_to, self.output, '.')
logging.info( "PACK COMMAND: %s" % pack_command)
ret = os.popen(pack_command).read().strip()
4、效果
5、限制
现在的方案还不是最极致的优化:因为现在的方案只要在一个类文件中出现就会被加入到最终的 jar 包当中,而我们需要用到的方法可能并不会用到这些类。所以,如果做方法级别的扫描就能够排除更多的没必要的类文件。不过,如果做方法级别的扫描,还需要面对崩溃堆栈可能和实际的代码对不上的问题。
此外,对于在 so 中引用到的方法现在还无能为力。
总结
我是 code小生
,喜欢可以随手点个在看
、转发给你的朋友,谢谢~
觉得不错,点个在看呀