问题
OK, perhaps it is very simple, but I just can not figure it out right now.
I have imported google sample project Card Reader in Android Studio 1.5.1, it compiles at compileSdkVerison 23, it works on my Mobile.
Then I walked through the SDK while came to source code to android.nfc.tech.BasicTagTechnology
, I found TransceiveResult
inside android.nfc.tech.BasicTagTechnology#transceive
couldn't not be resolved then I found the class TransceiveResult
is missing in my D:\Android\sdk\platforms\android-23\android.jar
, but presents in Android source code here D:\Android\sdk\sources\android-23\android\nfc\TransceiveResult.java
then I realized it could be hidden from public, not exported, actually it is
/**
* Class used to pipe transceive result from the NFC service.
*
* @hide
*/
public final class TransceiveResult implements Parcelable
Then I have done some random things, after I have sync the project, clean and rebuild, invalidate caches / restarted, still not able to resolve TransceiveResult
by the way, I am wondering since the symbol has been lost in SDK, how can the project compile smoothly?
EDIT for I finally Aha
We call android.nfc.tech.BasicTagTechnology#transceive
in our code rather than TransceiveResult
, in the compile-time we no need to resolve TransceiveResult
, we only need to resolve android.nfc.tech.BasicTagTechnology#transceive
which is referenced in our code, I was lost at that moment.
回答1:
@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.
来源:https://stackoverflow.com/questions/36004651/how-can-android-project-compile-when-imported-class-is-missing-in-sdk