How to modify the compiled resource ID value in Android (the default value is 0x7f ...). Can be arbitrarily changed to 0x02~0x7e)

Source: Internet
Author: User
<span id="Label3"></p><strong><strong><span style="font-size:18px;">first, Technical Preparation</span></strong></strong><p><p><span style="font-size:14px;">today, Let's look at how to modify the value of the resource ID at compile time in android, and before we do this, we need to look at the structure and the compilation process of the resources compiled in Android.</span> <span style="font-size:14px;">here is not much to say, specifically, you can view this article:</span></p></p><p><p><span style="font-size:14px;">http://blog.csdn.net/jiangwei0910410003/article/details/50628894</span></p></p><p><p><span style="font-size:14px;">In this article, Describes how to parse the android after compiling the RESOURCE.ARSC file, here on the Android resource file compiled after the type and format, in fact, after the compilation of resources in android, will produce an R file, all the resource ID is stored in this file, by default we see all I D all have a common feature, is that they are 0x7f beginning, in fact, This 0x7f is the ID value of the package, we in the resolution RESOURCE.ARSC article, the ID value in Android is actually an int type, his value is composed of three parts: packageid+ Typeid+entryid</span></p></p><p><p><span style="font-size:14px;"><strong>PackageId: is the ID value of the package, Android if it is a third-party application, the default is 0x7f, the system is the use of 0x01, specifically, we can see AAPT source, he occupies two bytes.</strong></span></p></p><p><p><span style="font-size:14px;"><strong>TypeId: is the type ID value of the resource, generally Android has these types: attr,drawable,layout,dimen,string,style, and these types of values are gradually increased from 1, and the order can not be changed, attr= 0x01,drawable=0x02 .... He occupies two bytes.</strong></span></p></p><p><p><span style="font-size:14px;"><strong>EntryId: is the ID value of the resource entity under the specific type, starting with 0, incrementing in turn, he occupies four bytes.</strong></span></p></p><p><p><span style="font-size:14px;"></span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><strong><strong><span style="font-size:18px;">second, the problems encountered</span></strong></strong><p><p><span style="font-size:14px;">Now that we know the structure of the resource ID in android, Let's talk about the problem we're having:</span></p></p><p><p></p></p><p style="font-size:14px;"><p style="font-size:14px;">1, occasionally in the Android project to rely on Third-party Library packages, the resource ID (packageid+typeid+itemvalue) conflict occurs (there are many solutions on the web, not listed, such as public limit, etc.). So for the library package we provide ourselves, if we can specify the command space of its package (by default, starting with 127=0x7f), especially considering the situation of mutidex, it is significant to customize the packages ID.</p></p><p style="font-size:14px;"><p style="font-size:14px;">2, We in the development of Android plug-in technology, in order to prevent the plug-in project resource ID and resource ID in the host project does not conflict, but also need to modify the plug-in after compiling the resource ID value, to reduce the Conflict.</p></p><p style="font-size:14px;"><p style="font-size:14px;">So above is the problem we have, in fact, our solution is very simple, is to compile the time to modify the resource ID value, to a qualified Value.</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><strong><strong><span style="font-size:18px;">third, The solution of ideas</span></strong></strong><p style="font-size:14px;"><p style="font-size:14px;">Before we explained the structure of the resource id, we found that the high two bytes represent the value of packageid, and the default value of the Third-party app is 0x7f, can we change this value? For example, PackageID in the resource ID in plug-in 1 is 0x30, and PackageID in the resource ID in plug-in 2 is 0x31 ... So that each plug-in resource is divided into a certain region value, while ensuring that the main project in the 0x7f conflict can be, then these values can be from the 0x02~0x7e, the interval value we are can use, why 0x01 can not use it? Because he is the system application of ah, so we have 0x7e-0x02=124 a range, haha, listen to good excitement, then we can operate it? The answer is yes, we know that the compiled resources in Android are using the AAPT command, then we can check his source code to see if it is Possible.</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">AAPT command is a tool that is provided in Android to compile apk, so source code can be <strong>/tools/from android source directory</strong> ... see below:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">The source of the tool is still not complex, not many files, of course, the entrance is sure to find Main what the key word, sure enough to see a <strong>Main.cpp</strong> file, Open view, Find the entry function Main, here we can see, he made a judgment on the input parameters:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Here the main function is a bit long, we look directly at the final processing function:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Here is a <strong>Handlecommand</strong> function, Here is the function of the main processing command:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">There are a lot of functions here, but what we need to focus on here is the Dopackage function, he is the key to the package, but at this time we find the global search this function, can not find, then this function is definitely referenced, the source code to find the specific function,</p></p><p style="font-size:14px;"><p style="font-size:14px;"><strong><span style="color:#ff0000;">Brain Repair:</span></strong></p></p><p style="font-size:14px;"><p style="font-size:14px;">Because it is a window system, do not want to be a Linux system, you can directly use the Find+grep can quickly find the file containing the specified content, but the Windows provides a visual file search, but he by default in the search, just search the file name, do not search the contents of the included , so it needs to be set up and can be set in the folder Options:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">At this point we can search in the tools directory:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">At this time see, we found three files,<strong>Main.cpp</strong> can not see, because have already seen, then in <strong>Command.cpp</strong> inside:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Here we look below:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Here is a method, and we look at the comment, here is the core function of the compilation:<strong>buildresources</strong>, we search this function in the global, not found, then we still go to the entire directory Search:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">search, in <strong>Resource.cpp</strong> :</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Here you see, a packgetype field, This is the package type, here are three types: shared, system, Third-party</p></p><p style="font-size:14px;"><p style="font-size:14px;">Suddenly it seems to have something to do with the value of packageid, and then we look down:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">here, PackageType is used, and there is an important type of <strong>resourcetable</strong>, this is the resource index table, and Resid has mapped the data structure, so we look at <strong>his definition: In ResourceTable.cpp</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">I wipe, sure enough, see the results, here see there are three values,<strong>0x00,0x01,0x7f</strong>. It means we've found the core of the Place. Then look down:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Here to build a package, here passed the PackageID value, well, We analyze the source is here, then we look at the <strong>source flow</strong> :</p></p><p style="font-size:14px;"><p style="font-size:14px;"><strong>First find the entry class: Main.cpp:main function, parse the parameters, and then call the Handlecommand function to deal with the logic of the argument, we see a function dopackage, here is the process of compiling Work.</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><strong>Then we searched for Command.cpp: a function of compiling the tool in his internal dopackage function: buildresources function, in the global search, found Resource.cpp: the specific view buildresources function, found here is to deal with the compilation work, At the same time here we also see the core, the construction of resourcetable logic, in the ResourceTable.cpp, is also the place to obtain packageid, to this we know the general logic, then know the logic, let's see how to modify it?</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><strong>In fact, the best way is to be able to modify the AAPT source code, add a parameter, we want to compile the PackageID as input value, passed in the best, in fact, we look at the source of the time found that there is a type always pass this, that is the bundle type, He passed the main function from Main.cpp to the last buildresources function, so we can carry this parameter with Bundles.</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><strong><strong><span style="font-size:18px;">Iv. Operating Practices</span></strong></strong><p style="font-size:14px;"><p style="font-size:14px;">Now that you know the idea of modification, the following is to modify the source code:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><strong>The first step: modify the main function in Main.cpp, get the PackageID value that is passed externally, and then deposit into the bundle</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">The parameters we use here are:<strong>-apk-module</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><strong>Step two: We only need to read this value in the ResourceTable.cpp construction method</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">To this, we have modified, and then compiled, where the compilation because the environment is different, so here is not listed to how to compile, I use VC6.0 to compile, get the final modification after the appt command:<strong>aapt_win.exe</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><strong><strong><span style="font-size:18px;">v. Use of tools</span></strong></strong><p style="font-size:14px;"><p style="font-size:14px;">So since the above we have so hard to modify the AAPT command, the following can be a change of the novice to try it, with a simple demo to try, but there is a problem here, it is here we use the ant script to compile the apk, because we need to modify the path of the AAPT command, In the case of compiled aapt_win.exe, there is no explanation for how to compile the apk with an ant script, so you can read this article:</p></p><p style="font-size:14px;"><p style="font-size:14px;">http://blog.csdn.net/jiangwei0910410003/article/details/50740026<br></p></p><p style="font-size:14px;"><p style="font-size:14px;">and, I just use the demo in this article to do the case, is to change the compilation script:</p></p><p style="font-size:14px;"><p style="font-size:14px;">Modify the path of the AAPT command with our modified command</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">When compiling the build R file, Add the Parameter:<strong>-apk-module</strong></p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p style="font-size:14px;"><p style="font-size:14px;">Compiling RESOURCE.ARSC also needs to be modified:</p></p><p style="font-size:14px;"><p style="font-size:14px;"><br></p></p><p><p><span style="font-size:14px;">It's all changed to <strong>0x78</strong>, and then we ran an ant script:<strong>ant release</strong></span></p></p><p><p><span style="font-size:14px;">Then look at the contents of the R file:</span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><p><p><span style="font-size:14px;">hahaha, Here we see the changes succeeded, do not believe, we can use a tool we wrote Before: parse RESOURCE.ARSC file tool class, Print to see the results ( <strong>do not know the classmate can view this article: http://blog.csdn.net/ Jiangwei0910410003/article/details/50628894)</strong>:</span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><p><p><span style="font-size:14px;">The printing results are normal, ok, let's look at the following, we use dynamic loading to load this apk (the <strong>specific code here is not pasted, not clear classmate can view this article: http://blog.csdn.net/jiangwei0910410003/ article/details/48104581</strong>):</span></p></p><p><p><span style="font-size:14px;">First we can look at the log information, we use reflection in the code to get the ID value of the App_name field in a plugin Apk.</span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><p><p><span style="font-size:14px;">The Log display ID is 0x78050000, the value displayed is also correct, so we let the resid scope of the plug-in to distinguish the host project 0x7f, there will be no problem of resource conflicts, look at the effect of the operation:</span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><p><p><span style="font-size:14px;">well, Here we have the content of this chapter.</span></p></p><p><p><span style="font-size:18px;"><br></span></p></p><p><p><span style="font-size:18px;color:#3366ff;"><strong>AAPT modified source code and Tools: http://download.csdn.net/detail/jiangwei0910410003/9454867</strong></span></p></p><p><p><br></p></p><strong><strong><span style="font-size:18px;">six, Learn the technical point</span></strong></strong><p><p><span style="font-size:14px;">1. Learn how to find source content in Windows</span></p></p><p><p><span style="font-size:14px;">2. Learn the whole process of AAPT compilation</span></p></p><p><p><span style="font-size:14px;">3, learned to modify the compiled resource ID value</span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><strong><strong><span style="font-size:18px;">Vii. issues to be resolved</span></strong></strong><p><p><span style="font-size:14px;">By modifying the AAPT source code, We can customize the Resid value after compiling, solve the problem of the resource ID value conflict when we refer to the third party package or project and develop the plug-in, so here's to remember that we can modify the resource ID value after compiling in Android.</span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><strong><strong><span style="font-size:18px;">Viii. Summary</span></strong></strong><p><p><span style="font-size:14px;">The end of this article, feel the harvest is still a lot of, at least we know that after the compilation of Android resource ID can be customized, although some students may not be able to use this function, but I believe that one day you will be used, so long as you remember that the technical solution is GOOD.</span></p></p><p><p><span style="font-size:14px;"></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: ‘microsoft yahei‘; font-size: 15px; line-height: 35px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: ‘microsoft yahei‘; font-size: 15px; line-height: 35px;"><span style="color: rgb(51, 102, 255);"><span style="font-size: 18px;">PS: attention, The latest Android technology real-time push</span></span></p></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: ‘microsoft yahei‘; font-size: 15px; line-height: 35px;"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: ‘microsoft yahei‘; font-size: 15px; line-height: 35px;"><span style="font-size: 14px;"></span></p></p><br><p><p><span style="font-size:14px;"><br></span></p></p><p><p><span style="font-size:14px;"><br></span></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><p><p> How to modify the compiled resource ID value in Android (the default value is 0x7f ...) Can be arbitrarily changed to 0x02~0x7e) </p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.