How Android uses the hidden API (using the @hide API)

Source: Internet
Author: User

Objective:

The first point to note is thatGoogle has a reason to hide some APIs ( the public class, method, or constant that adds @hide markup). The big reason for this is that the Android system itself is in constant evolutionary development. From 1.0,1.1 to the now-coming Android 2.3.4. These hidden APIs themselves may be unstable, so using a hidden API means that theprogram is worse for compatibility.


If you want me to give advice, it's best not to use the hidden API. Sometimes , however, the hidden API can play an unexpected role in order to achieve some special features or effects for Android applications . I'm not here to give an example of what these APIs can do specifically. Well, if you don't know what the hidden API can do, then quit using them as soon as possible ...


Don't want to be lengthy, go straight to the chase. There are two main ways to use the hidden API : First, the reflection method I referred to in the study of the Java reflection mechanism gets the hidden API, and thesecond is the fully compiled jar package generated when compiling with the source code. Classes.jar.


The use of reflection, in this is not described in detail, see the "Java reflection mechanism of learning ." The advantage of using the reflection mechanism to hide The API is the flexibility to catch exceptions in incompatible systems without crashing the program, the drawback being that the process is too complicated and that it is difficult to implement the inheritance of hidden classes. If you need to use a lot of hidden APIs, it can be annoying for programmers to use reflection.


The main thing this article introduces is to implement the use of the hidden API by importing the fully compiled Classes.jar package . This approach is simple and programmer programming is simple, just as the hidden APIs become visible in the SDK . Its disadvantage is the compatibility problem, I hope users pay attention to, in the use of a clear consideration.

When we develop an Android project using the SDK, we often don't use the hidden APIs, such as:
1. We want to implement a silent installation of the application, we cannot use

1 pm.installPackage(mPackageURI, observer, installFlags,installerPackageName);

2. When using the relevant features of mass storage, you cannot use the

1 StorageManager s = (StorageManager) getSystemService(Context.STORAGE_SERVICE);

Wait a minute..
For example, with the AlarmClock that comes with Android, there are often many errors after importing eclipse, as shown in:

For example, intent.action_alarm_changed cannot be accessed, so let's find the source code to see why?

The method I introduced must be compiled under Linux, but just compile the SDK, development or under Windows, if you do not have Android source code compiled now, please do this first, then look down. The following method two really is very convenient, solve my many problems, it is strongly recommended that you look patiently.

According to the Java programming specification, we know that this API (static variable) is hidden, so it cannot be used in the SDK. After knowing why, we have several solutions (we strongly recommend using method two):

method One: own the source code in the @hide Remove, and then compile a SDK to replace the default SDK .
Using the Make PRODUCT-SDK-SDK command on Linux, compile a new SDK, note that after compiling we do not need the entire SDK, just android.jar this file to replace the SDK Android.jar, For example: The directory in the author's SDK for the jar corresponds to:

F:/program Files/android/android-sdk-windows/platforms/android-8/android.jar

The specific way to compile the SDK is to use the command make PRODUCT-SDK-SDK in the Linux compilation environment, after successful, there will be the following prompt:

Package Sdk:out/host/linux-x86/sdk/android-sdk_eng.stevewang_linux-x86.zip

We go into the Linux build environment out/host/linux-x86/sdk/android-sdk_eng.stevewang_linux-x86/platforms/android-2.2/ directory, you can see the android.jar file. Replace F:/program Files/android/android-sdk-windows/platforms/android-8/android.jar with this file. Remember to back up before replacing

This method is more troublesome, it is recommended to use method two

method Two: Add Framework compiled out of Classes.jar files to Eclipse of the Build Path
actually in the compilation Android , we will Framework compile to a temporary Jar It's in the bag. Jar the path to the package is typically :
Out/target/common/obj/java_libraries/framework_intermediates/classes.jar
We only need to use the Make command under the Android source code directory on Linux to generate this file.

Since the API in this JAR file has not been repackaged, the API that was @hide out is not removed. So we can still quote the API that was @hide inside. While the Android.jar file in the SDK was repackaged and generated, the API labeled @hide was removed. So we copied the Out/target/common/obj/java_libraries/framework_intermediates/classes.jar to the local PC. Then add this jar package in the project.
Specific methods:
1.CopyLinuxcompile the generatedOut/target/common/obj/java_libraries/framework_intermediates/classes.jarto this machinePC.
2, inEclipseof theAndroidproject, select the project Properties->java Build path->libraries->add library->user library->next-> userlibrariesinto theUser Librariesmanagement interface, clickNewCreate a newUser Library, such asandroid_framework, clickADD JarsputJarpackage added to the establishedUser Libraryin the last clickOK.
3.Select Project Properties->java Build Path->order and Export, use the " up"Key, put the establishedUser Librariesmove toAndroid SDKthe top. This means that priority is given to usingClasses.jarin theAPI, followed by the useAndroid.jarin theAPI. Why not use it directlyclasses. Jar?because I found it when I was using it,Android.jarSome of the thingsClasses.jarnot in.
Such as:

Then our engineering error disappears:

To the basic resolution of this issue.

Reference: How we use the Hide API

How Android uses the hidden API (using the @hide API)

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.