How can I compile an Android project if the imported class is not in the SDK?

OK, maybe this is very simple, but I just can't figure it out now.

I imported the Google Card Reader sample project into Android Studio 1.5.1, it compiles to compileSdkVerison 23, it works on my mobile device.

Then I went through the SDK, when I came to the source code android.nfc.tech.BasicTagTechnology , I found that TransceiveResult inside android.nfc.tech.BasicTagTechnology#transceive could not be resolved, then I found that the TransceiveResult class is missing in my D:\Android\sdk\platforms\android-23\android.jar , but presented in the Android source code here D:\Android\sdk\sources\android-23\android\nfc\TransceiveResult.java , then I realized that it can be hidden from the public, not exported, in fact it

 /** * Class used to pipe transceive result from the NFC service. * * @hide */ public final class TransceiveResult implements Parcelable 

Then I did some random things, after I synchronized the project, cleared and rebuilt, invalidated caches / restarted, but so far I couldn’t allow TransceiveResult , I was wondering, since the character was lost in the SDK, how can I compile the project?

Edit for me finally aha

We call android.nfc.tech.BasicTagTechnology#transceive in our code, not TransceiveResult , at compilation we do not need to enable TransceiveResult , we only need to allow android.nfc.tech.BasicTagTechnology#transceive , which our code refers to, I was lost at this moment.

+8
android android-studio android-sdk-tools android-build android-studio-import
source share
1 answer

@hide means that it is not included in the documents described here , and it is also removed from the classes in your android.jar, which is used in the collection. However, they are available at runtime.

Update: To clarify the IDE in your environment when you enter your SDK classes, you can see links to hidden elements that you cannot solve. This is normal, and it will still compile as long as they are in SDK classes not included in your code.

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

1) select the full version of android-framework.jar and you can compile. this, however, has the disadvantage that compiled code probably won’t work on other versions of Android, since the class or method may not even be there. and BasicTagTechnology, for example, is actually a private package, so you still cannot access it.

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 the best option in the sense that it is more flexible and you can check if a class / method exists to initialize / call them, catch exceptions, etc.

+6
source share

All Articles