135

For the Activity source code, line 3898 (close to the bottom):

/**
 * @hide
 */
public final boolean isResumed() {
    return mResumed;
}

What does @hide mean?

I found my public class ChildActivity extends Activity { ... } cannot use/see Activity.isResumed(). Is this normal? How can I access it?

stkent
  • 19,772
  • 14
  • 85
  • 111
midnite
  • 5,157
  • 7
  • 38
  • 52

3 Answers3

200

Android has two types of APIs that are not accessible via SDK.

The first one is located in package com.android.internal. The second API type is a collection of classes and methods that are marked with the @hide Javadoc attribute.

Starting from Android 9 (API level 28), Google introduces new restrictions on the use of non-SDK interfaces, whether directly, via reflection, or via JNI. These restrictions are applied whenever an app references a non-SDK interface or attempts to obtain its handle using reflection or JNI.

But before API level 28, the hidden methods could still be accessed via Java reflection. The @hide attribute is just part of Javadoc (droiddoc also), so the @hide just simply means the method/class/field is excluded from the API docs.

For example, the checkUidPermission() method in ActivityManager.java uses @hide:

/** @hide */
public static int checkUidPermission(String permission, int uid) {
    try {
        return AppGlobals.getPackageManager()
                .checkUidPermission(permission, uid);
    } catch (RemoteException e) {
        // Should never happen, but if it does... deny!
        Slog.e(TAG, "PackageManager is dead?!?", e);
    }
    return PackageManager.PERMISSION_DENIED;
}

However, we can call it by reflection:

Class c;
c = Class.forName("android.app.ActivityManager");
Method m = c.getMethod("checkUidPermission", new Class[] {String.class, int.class});
Object o = m.invoke(null, new Object[]{"android.permission.READ_CONTACTS", 10010});
0xCursor
  • 2,242
  • 4
  • 15
  • 33
StarPinkER
  • 14,081
  • 7
  • 55
  • 81
  • 1
    hello @StarPinkER can i grant "android.permission.CHANGE_COMPONENT_ENABLED_STATE" permissio using hidden or internal api or by reflaction? – Hardik Feb 08 '14 at 09:39
  • 1
    Check [this answer](http://stackoverflow.com/a/15289944/1365960) first. This permission is a signature/system permission. In most cases, you can't get this permission unless it is system applications. That means you need to modify Android Source to accept your app or make your app a system-app and sign it. However, you won't be able to do this unless you are making your own Android System. Reflection can handle "hiding" but it cannot change the logic of the Android Security System. You can imagine how we can easily attack an Android Device if we are able to do this. @Hardik – StarPinkER Feb 08 '14 at 10:42
  • 2
    Thanks for the answer, but I think there are two problems in the answer, correct me if I'm wrong. I get classnotfound error if try to find it by "ActivityManager" instead of "android.app.ActivityManager" and "m.invoke(c," seems to should be "m.invoke(null," for static methods and "m.invoke(o,", where o is an object of type c, for dynamic methods. Sorry for my Polish grammar:) – lindenrovio Feb 18 '14 at 21:04
  • You are absolutely correct. Already edited this answer. I didn't think too much about this when I wrote this. Thanks. @lindenrovio – StarPinkER Feb 19 '14 at 03:21
  • 3
    Just a note regarding reflection: As those methods/fields are not part of the official SDK, there is no guarantee that they will be present in any future Android revision. – sstn Oct 21 '14 at 11:13
  • 2
    If the annotation only removes the method from the documentation, why can't I still use it in code? – Javier Delgado Oct 17 '17 at 11:35
  • That may be removed in future releases, and your code will be broken because of that. @JavierDelgado – StarPinkER Apr 13 '18 at 03:25
  • Usage of methods with `@hide` was removed to large extend for API 28+. See my answer – leonardkraemer Oct 11 '18 at 13:19
  • API functions become deprecated then disappear, while reflection calls are in try-catch which will never crash your application. – McX Nov 17 '20 at 08:55
27
  1. @hide is used for things that need to be visible for various reasons but are not part of the published API. They will not be included in the documentation when it automagically extracts the API from the source.

  2. You're right, you cannot override it. This is normal, that's by design, since it's marked as final. You should be able to use it, although an editor may not show it to you as one of the choices in whatever intellisense it uses because it's marked with @hide, and you should take note of point 3 below.

  3. You should not use it at all since it's not part of the API and the developers can remove it whenever they wish. They would even be within their rights, were they sadistically inclined, to replace it with a function that bricked the device it ran on (though maybe not in a strict legal sense).

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Oh yea... it's `final` of course i cant override it. Sorry that's my mistake :x – midnite Jun 11 '13 at 01:47
  • Do you mean, it is `public` across all the classes during the development stage. But it acts like `private` or `/*package*/` to users like us? – midnite Jun 11 '13 at 01:51
  • Hmm... That is just a comment. i understand its meanings. But what and where to enforce this behaviour at the code-level? – midnite Jun 11 '13 at 01:53
  • 1
    _Why_ it's public, I cannot really comment. Maybe the code implementing `Activity` is spread across a lot of classes and they all need to access this member. Bottom line is, it _is_ public but not part of the API meaning you use it at your own risk. – paxdiablo Jun 11 '13 at 01:54
  • As to enforcement, you're right, the hiding isn't enforced at the code level, at least by the standard Java compiler. But it can affect _other_ things like the automated API doc generation and intellisense in whatever editor you're using. – paxdiablo Jun 11 '13 at 01:55
  • i am using the Eclipse to develop Android. i got the error message `The method isResumed() is undefined for the type Activity` and i cannot compile it :( – midnite Jun 11 '13 at 02:04
  • 1
    @midnite, Eclipse has it's own Java compiler which is no doubt integrated with the intellisense stuff. I would suggest if you compiled this with the Java SDK, it would compile just fine. Not that I'm suggesting this of course, see point 3. – paxdiablo Jun 11 '13 at 02:06
5

The @hide annotation means that this interface is not part of the public API and should not be used in your code. The methods are only for internal use of the AOSP.

Google has actually started to restrict the usage of non-sdk interfaces. This includes interfaces marked with @hide

The methods are classified into four lists:

  • whitelist: the SDK
  • light-greylist: non SDK methods / fields that are still accessible.
  • dark-greylist:
  • For apps whose target SDK is below API level 28: each use of a dark greylist interface is permitted.
  • For apps whose target SDK is API level 28 or higher: same behavior as blacklist
  • blacklist: restricted regardless of target SDK. The platform will behave as if the interface is absent. For example, it will throw NoSuchMethodError/NoSuchFieldException whenever the app is trying to use it, and will not include it when the app wants to know the list of fields/methods of a particular class.

The lists can be found here

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54