Android的@hide注释究竟能做什么? [英] What exactly does Android's @hide annotation do?

查看:479
本文介绍了Android的@hide注释究竟能做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android中的许多内部API均标记为@hide. 确切地是做什么的?

Lots of internal APIs in Android are marked @hide. What exactly does this do?

另一个答案说,它只会隐藏Javadoc中的方法,但是您可以使用反射来访问它们.

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

但这没有任何意义-如果仅从Javadoc中隐藏了它们,那么您肯定不需要反射来访问它们.实际上,我发现我没有.我仍然可以调用某些@hide方法(也许只是静态方法?),并且据我所知,该应用程序可以编译并正常运行.我刚收到一个皮棉错误:

That makes no sense though - if they are only hidden from Javadoc then you surely wouldn't need reflection to access them. In fact I've found that I don't. I can still call some @hide methods (maybe just static ones?) and the app compiles and runs fine as far as I can tell. I just get a lint error:

请注意,上面的代码仍然可以正常编译.

Note that the above code still compiles fine.

我不在乎更改API的可能性,因此我很乐意使用私有API,但是有人可以解释这种行为吗?另外,如果有任何方法可以逐案禁用皮棉,那么将很有帮助.

I don't care about the possibility of the API being changed so I am happy using the private API, but can someone explain this behaviour? Also if there's any way to disable the lint on a case-by-case basis that would be helpful.

推荐答案

这到底是做什么的?

What exactly does this do?

它控制您要编译的android.jar中的内容.

It controls what is in the android.jar that you are compiling against.

例如,当您在build.gradle文件中包含compileSdkVersion 19时,实际上是在将$ANDROID_SDK/platforms/android-19/android.jar添加到编译时类路径中.

When you have, say, compileSdkVersion 19 in your build.gradle file, what is really happening is that $ANDROID_SDK/platforms/android-19/android.jar is being added to your compile-time classpath.

该JAR是作为编译Android本身的一部分而创建的.将分析Android框架类,并创建它们的副本.此副本:

That JAR is created as part of compiling Android itself. The Android framework classes are analyzed, and a copy of them is created. This copy:

  • 带出所有标有@hide

具有剩余的所有方法的存根实现(字面上是throw new RuntimeException("Stub!"),这是我上次查看的时间)

Has stub implementations of all the methods that remain (literally throw new RuntimeException("Stub!"), the last time I looked)

为剩下的所有内容保留JavaDoc注释

Retains the JavaDoc comments for everything that remains

JavaDocs是基于此源代码树构建的(这就是JavaDocs不显示隐藏方法的原因),而框架JAR的SDK版本是根据此源代码树编译的.

The JavaDocs are built off of this source tree (which is why the JavaDocs do not show hidden methods), and the SDK edition of the framework JAR is compiled off of this source tree.

但是您可以使用反射来访问它们

but that you can use reflection to access them

这是因为在运行时中, real 框架JAR位于您的运行时类路径中,是从框架类的实际源代码编译而来的.它包含所有标有@hide并已从编译时框架JAR中剥离的内容.

That is because, at runtime, the real framework JAR is in your runtime classpath, compiled off of the real source for the framework classes. It contains everything that was marked with @hide and was stripped out of the compile-time framework JAR.

我仍然可以调用某些@hide方法(也许只是静态方法?),并且据我所知,该应用程序可以编译并正常运行.我刚收到一个皮棉错误

I can still call some @hide methods (maybe just static ones?) and the app compiles and runs fine as far as I can tell. I just get a lint error

正如Karakuri所指出的那样,对我而言,这肯定是一个编译错误.如果在compileSdkVersion 22项目中尝试您的代码,则会出现编译错误.当我运行它时,我得到:

As Karakuri noted, that sure looks like a compile error to me. If I try your code in a compileSdkVersion 22 project, I get a compile 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

现在,您可以针对标有@hide以前的方法进行编译,因为它们在更高版本的Android SDK中未被隐藏,并且您的compileSdkVersion处于该API级别或更高级别.在将这些方法正式添加到SDK之前,先在API级别使用这些方法是有风险的.

Now, you can compile against methods that were formerly marked with @hide, because they were un-hidden in a later Android SDK, and your compileSdkVersion is on that API level or higher. Using those methods on API levels prior to when they were officially added to the SDK is risky.

我不在乎更改API的可能性

I don't care about the possibility of the API being changed

除非您仅针对控制固件的一台设备(包括所有操作系统更新)进行构建,否则应该这样做.而且,在这种情况下,您可能正在构建自己的固件,因此可以从Android fork中构建自己的SDK框架JAR,在其中您要真正使用的东西中删除了@hide.

You should, unless you are building only for one device where you control the firmware, including all OS updates. And, in that case, you are probably building your own firmware, and so you can build your own SDK framework JAR from your Android fork, where you remove @hide from the things you want to really use.

此外,如果有任何方法可以逐案禁用皮棉,将很有帮助.

Also if there's any way to disable the lint on a case-by-case basis that would be helpful.

基于我从屏幕快照和PC中看到的信息,这是IDE的编译错误.您不能禁用编译错误.

Based on what I see from your screenshot and my PC, that is a compile error from the IDE. You cannot disable compile errors.

这篇关于Android的@hide注释究竟能做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆