How can Android project compile when imported class is missing in SDK?

拥有回忆 提交于 2019-12-05 01:49:34

@hide means its not included in the docs as described here and it is also stripped from the classes within your android.jar which is used in compilation. These however are available in runtime.

Update: To clarify in your IDE when you dig into your SDK classes you might see references to hidden members which you cannot resolve. This is OK and it will still compile as long as those are in the SDK classes not in your code.

If you want to use those hidden classes/methods/fields you have 2 main options:

1) fetch a full version of android-framework.jar and be able to compile. this however has a drawback that the compiled code will probably not run in other Android versions as the Class or the method might not be even there. and also BasicTagTechnology for example is actually package private so you cannot access it like that anyway

2) use reflection:

Class tr = Class.forName("android.nfc.TransceiveResult");
        Constructor constructor =
                tr.getConstructor(new Class[]{int.class, byte[].class});
        Object trObj = constructor.newInstance(1, new byte[]{1,2});

this is a better option in the sense that it is more flexible and you can check if the class/method exists to init/call them, catch exception etc.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!