Android apk file resource confusing principle and implementation

By Suzanne Lawson,2015-04-03 04:55
11 views 0
Android apk file resource confusing principle and implementation

    Android apk file resource confusing principle and


    Parts makers of Android applications, the apk in the inside of the res directory and file will do confused.In the names of all the files and folders are a, b, c, d, etc.Doing so has many advantages: 1, reduce the apk file size, I try to confuse apk resources to weibo, reduce the apk from 36.5 M to 36.5 M;2, increase the difficulty of the decompiled and secondary packaging, confusion after the apk can't use apktool tools such as decompiled directly.Is, of course, only increased the difficulty, after all, there is absolutely no security;3 reduce runtime memory footprint, at run time each res in the key are loaded into memory in the form of a String, confusion after much shorter key memory footprint would be less.These advantages are, of course, in some DaTiLiang applications will only be obvious, smaller apk may not be obvious.

    Resource loading process analysis

    Get to the point, to want to achieve resource confusion must first clear the principle of Android resource pack, and load.As you all know, in the process of development we are generated by the aapt R.j constants to use the resource of ava, and where the compiled using constant will be replaced by a constant value.See below.

    That is to say, we through the Resource using an int value to find the use of resources.So the Resource is how to find specific resources through an int value?We extract the apk can see there is a resources. The arsc file, the file is generated by the aapt, file holds the resource id and key mapping relationship.The Resource is find resources according to the mapping relationship.

    Next we specific see the specific content of this arsc file.Our applications in a single package name.The structure below

    String of res string pool is res pool, containing the string (known as the development process in the string. The XML file) increased and resource file path (including images, XML files, raw file. The value of such the res/drawable/icon. The PNG)

    Type string pool resources classification name used in the program, including attr, anim drawable...Note that there is only classification.

    The spec string pool the more critical, the string in the pool are all key resources.No classification and config.In the specific value is R.j ava all the names of the constants.

    In the strings behind the pool is the resource classification list, attr, anim...Each category includes several config, config is hdpi, xhdpi, zh_rCN, land these, as we all know the android resource config latitude 18.Each config is specific res resource list.

    For example, the search process of a resource, such as when we call getDrawable (R.d rawable. The icon), first find the drawable classification, according to the current system config to find matching config table.Then according to the id to find the corresponding res data.Drawable in arsc file is saved as a string type.Res is the resource on the data in the res string pool the index of the pool.According to this index can find the string in the string in the pool.This string is actually a file path, such as: the res/drawable - hdpi/icon. PNG, then reads the apk in the file.

    The details about this process we don't do too, want to know detail can see the blog acepillar Yang the great god, the Android resource management framework (Asset Manager) are briefly introduced and study plan Arsc file format details can see this blog


    Confused resources principle and implementation

    Look at the example, we noticed that normally find in the process of resource will not use the resource name, also is the key.Res data, of course, also have the current res key in the key in string index in the pool, but does not normally use.According to this principle we can after the completion of a package to confuse the key.And just said to saved in the res data are string in the string index in the pool.So we only need to modify the res string pool and spec string pool pool two strings.Other data without modification.

    So we confuse the process looks something like this: 1, extract the apk files;2, read arsc file;3, confuse the spec string pool;(4) according to the step confused confusion results res string pool;5, generate new arsc files;6, output mapping file;7, according to the result of step 3, 4, modify the res directory all file names;Eight, to compress the apk is generated;9, the signature;10. The zipalign.

    Also in drawable, for example, before and after the confusion of such as below:


    Besides, key problems, just said does not normally use res key.But we sometimes by developing key to find the resources.That is need to pay attention to, need to pass the key to use the resources of the key cannot be confused.

Report this document

For any questions or suggestions please email