What exactly does the Android @hide annotation do?

Many internal APIs in Android are @hide . What exactly does this do?

Another answer says that it only hides methods from Javadoc, but you can use reflection to access them.

This doesn't make any sense, although if they are hidden only from Javadoc, you probably wonโ€™t need to think to access them. In fact, I found that I am not doing this. Can I still call some @hide methods (maybe only static)? And the application compiles and works just fine, as far as I can tell. I just get lint error:

Unable to resolve method

Please note that the code above is still compiling.

I don't care about the possibility of changing the API, so I'm happy to use a private API, but can anyone explain this behavior? Also, if there was any way to disable the lint in each case, it would be useful.

+7
java android hide lint
source share
2 answers

What exactly does this do?

It controls what is in android.jar with which you are compiling.

If you have, say, compileSdkVersion 19 in your build.gradle file, what is actually happening is that $ANDROID_SDK/platforms/android-19/android.jar added to your compilation class path.

This JAR is created as part of the Android compilation. The classes of the Android framework are analyzed and a copy is created. This copy:

  • Resets all classes, methods, fields, etc., marked with @hide

  • Has empty implementations of all remaining methods (literally throw new RuntimeException("Stub!") , The last time I looked)

  • Saves JavaDoc comments for everything that remains

JavaDocs are built from this source tree (therefore, JavaDocs does not display hidden methods), and the SDK version of the JAR infrastructure is compiled from this source tree.

but you can use reflection to access them

This is because at runtime, the real JAR Framework is in your path to the runtime compiled from the real source for the infrastructure classes. It contains everything that was noted with @hide and was removed from the JAR compilation framework.

Can I still call some @hide methods (maybe only static)? And the application compiles and works just fine, as far as I can tell. I just get lint error

As Karakuri remarked, this probably looks like a compilation error. If I try your code in the compileSdkVersion 22 project, I get a compilation error. And when I go to run it, I get:

 /tmp/MyApplication/app/src/main/java/com/commonsware/myapplication/MainActivity.java Error:(16, 23) error: cannot find symbol method isEmailAddress(String) Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. Information:BUILD FAILED 

Now you can compile the methods previously noted by @hide because they were hidden in the later Android SDK, and your compileSdkVersion is at this API level or higher. Using these methods at API levels before they were officially added to the SDK was risky.

I don't care about the possibility of changing the API

You should, unless you create only one device in which you control the firmware, including all OS updates. And in this case, you are probably creating your own firmware, and so you can create your own JAR SDK structure from your Android fork, where you remove @hide from what you really want to use.

Also, if there was any way to disable lint in each case, it would be useful.

Based on what I see in the screenshot and on my PC, this is a compilation error from the IDE. You cannot disable compilation errors.

+15
source share

Somehow, Google uses the @hide annotation to exclude classes and methods from which they do not want to be part of the public SDK, from the compiled javascript framework for Android, which you download and use to compile your code. (I donโ€™t know the features of how this works, so donโ€™t ask.) Therefore, your IDE cannot compile your code against them - they literally do not exist. However, the android framework on the actual device contains these classes and methods, so they can be accessed at run time using reflection.

What you displayed in your message is not a lint error, i.e. a compilation error. He cannot resolve the method, that is, he believes that this is not a valid method call.

+3
source share

All Articles