Use internal (com.android.internal) and hidden (@hide) API [Part 2, customize android.jar]

ZT:http://mogoweb.net/archives/92

This article is translated from https://devmaze.wordpress.com/2011/01/18/using-com-android-internal-part-2-hacking-around/

In the previous article, I explained why it is not easy to use the internal API of the com.android.internal package and the hidden API marked as @hide without the reflection mechanism . This is because the android.jar file does not contain the internal API and Hide the API classes so that there is no way to reference these classes at compile time.

This article will explain how to restore the original android.jar so that you can use the internal API and the hidden API, just like using the public API.

How to get the original (uncut) android.jar?

We need to modify android.jar so that it contains all the original class files (including internal and hidden APIs). There are two ways:

1) Android is an open source project. We can download the source code and customize the build system without removing internal and hidden classes from android.jar. This method is more difficult.

2) Each simulator or real device has a package equivalent to android.jar for runtime. We can get this jar, extract the original .class file, and copy it to android.jar.

I prefer the second method, which is easier and does not require a linux machine (if you are working under windows), nor does it require compiling all the source code, customizing the build system, and so on.

Obtain framework.jar from the device

You can use the command line (adb pull) or DDMS (start separately from eclipse, or android sdk) to download files from the device or emulator.

(Note: The simulator always contains the code in the .dex file, while the real device usually contains the code in the optimized version of the dex-odex file. It is usually difficult to use the odex file, so this article recommends using the simulator)

The file equivalent to android.jar in the Android SDK at runtime is framework.jar. This file is located in /system/framework/framework.jar.

adb pull /system/framework/framework.jar

After downloading framework.jar, rename it to framework.zip, and unzip it to a separate directory. You should get the following content:

image3 

The file classes.dex is what we want.

Create framework-classes.zip

First, we need to convert the .dex file into a .jar file format. This can be done with a small tool dex2jar, you only need to run:

dex2jar classes.dex

When the conversion is complete, you will get the classes.dex.dex2jar.jar file and name it framework-class.zip. Using the zip file viewer, go to framework-class.zip/com/android/internal:

image4

Wow, we got the internal API and hidden API .class files (although the screenshot only confirms the internal API).

Create original-android.jar

The android.jar in the Android SDK is located in ANDROID_SDK/ platforms/android-X/android.jar (where X stands for API Level, such as X==9).

Copy android.jar to custom-android.zip. Unzip to the custom-android folder. Copy all .class files from framework-class.zip to the custom-android folder (you need to replace all existing .class files).

Then the zip folder custom-android is original-android.zip and renamed to original-android.jar.

Summary of steps

  1. Select the target platform X (I use the platform of API Leve 9, so X == 9)
  2. Create an emulator for platform X
  3. Start the simulator and download the /system/framework/framework.jar file from it
  4. Rename framework.jar to framework.zip
  5. Extract classes.dex from framework.zip
  6. Use dex2jar to convert classes.dex to classes.jar
  7. Rename classes.jar to framework-classes.zip
  8. Copy android.jar from ANDROID_SDK /platforms/android-X/ and rename it to custom-android.zip
  9. Unzip custom-android.zip to the custom-android directory
  10. Copy all files from framework-classes.zip to custom-android folder (replace existing files)
  11. zip the custom-android folder to original-android.zip
  12. Rename original-android.zip to original-android.jar

carry out.

in conclusion

We restored the original android.jar, the .class file containing the internal API and hidden API. This is just the first step. The next step is to create a customized platform, use the uncut version of android.jar, and then add it to the Android SDK platforms directory.


Guess you like

Origin blog.csdn.net/lgdlchshg/article/details/23618759