原文地址:Unlocking Android Hidden APIs (外网访问)
Beak limit on your Android apps development with custom android.jar
Some people asked me, how to access hidden APIs without reflection? What is the easiest way to access those hidden/internal classes, methods and resources? Let’s dig it up!
The internal APIs are located in package com.android.internal and available in the framework.jar, while the hidden APIs are located in the android.jar file with @hide javadoc attribute.
Although the classes & methods are public, but you cannot access them directly. There are pretty methods and resources you can use from this package. I will assume both as one API and I will refer to it as hidden API.
The advantage is you don’t need to create new classes, constants, methods or resources whenever you want to use the similar function in your project. You can access hidden features from Android SDK as well.
I already have an open source project that can help you to access the hidden APIs. You can download it here: Android Hidden APIs on GitHub. This repo provides custom android.jar. You can use it on your app development.
Sometimes, I am very busy so I don’t have time to handle this repo alone, hence I am open to all people who want to contribute on this project.
Of course, you can use Java reflection to access all of those hidden APIs. But this is a painful way. In this article, I will cover how to create your own custom android.jar and use it to access the hidden APIs. So it is no longer a blocker for your project to just wait the latest version of custom android.jar is released on the repo.
Preparation
You need to prepare these two files: framework.jar and android.jar.
android.jar
Go to
Don’t forget to keep the original android.jar because when we accidentally put something wrong, we can go back to the original version. Anyway, you can download the original one from: https://dl.google.com/android/repository/platform-X_rY.zip
Where X is the API level and Y is the revision number. For example: https://dl.google.com/android/repository/platform-29_r04.zip
Extract the ZIP and you will find the original android.jar.
framework.jar
framework.jar is located in /system/framework. If you want to create custom android.jar for API 29, then you need to find a physical device or an emulator running Android API 29. Some devices may contain more than one framework.jar.
Let’s take a look into my device:
Now connect your Android device to your computer. We need to download framework.jar via ADB command:
adb pull /system/framework/framework.jar
If framework2.jar exists, download it as well.
If the file size of downloaded framework.jar is only few KBs, then there might be something wrong. You need to find another devices or emulators. BTW, Android Virtual Device is enough. Normally, the file size of a framework.jar should be more than 5MB.
Create Custom android.jar
Now we have collected all required JARs. Next step is combining framework.jar with original android.jar. Let’s try to create custom android.jar for API 29. Follow the following steps.
Rename framework.jar to framework.zip and unzip this archive.
We need to extract *.class files from these DEX files with Dex Tools. If Dex Tools does not work, you can try Dex2Jar.
With Dex Tools, run this command on your terminal:
./d2j-dex2jar.sh
For example:
Open directory dex-tools-2.1-20171001-lanchon and you’ll see a new file named classes-dex2jar.jar. Rename its extension to zip so we can extract it. Once you extracted it, rename directory android to classes so we can distinguish them later.
Repeat the same steps to classes2.dex and classes3.dex.
Now we have extracted those DEX files:
We need to merge these extracted DEX files into android.jar located in
Create a folder named custom.
Move all files from inside folder android into folder custom. Then open folder classes and move everything inside it into folder custom as well. Repeat the steps for the rest classes folders. Whenever you are asked to replace or merge, please select merge.
On Windows you may select Copy and replace.
Open your terminal and set the directory to folder custom. We will do the final step, creating the JAR.
Run the following command:
jar cvf android.jar *
Copy this android.jar into
Finally, we can use it in our project:
Wohooo… We did it! 👏
Resources Helper
If you need a helper to access the hidden resources, e.g. String, Drawable and color, then add this dependency:
implementation ‘com.anggrayudi:android-hidden-api:xx’
Check the latest version and its usage here.
Final Words
I don’t know how long this project would survive, because latest version of Android Studio prevents custom android.jar from working. Google also restrict the hidden APIs usage as posted on this page:
https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces
But I am not thinking to give up now. Let’s find another workaround to make it work! Do not hesitate to create pull requests if you intended to contribute on this project.