SDK中缺少导入的类时,Android项目如何编译? [英] How can Android project compile when imported class is missing in SDK?

查看:163
本文介绍了SDK中缺少导入的类时,Android项目如何编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,也许很简单,但是我现在无法弄清楚.

OK, perhaps it is very simple, but I just can not figure it out right now.

我已经在Android Studio 1.5.1中导入了google示例项目读卡器,在compileSdkVerison 23上进行编译,可在我的Mobile上运行.

I have imported google sample project Card Reader in Android Studio 1.5.1, it compiles at compileSdkVerison 23, it works on my Mobile.

然后我遍历了SDK,同时找到了android.nfc.tech.BasicTagTechnology的源代码,我发现无法解析android.nfc.tech.BasicTagTechnology#transceive中的TransceiveResult,然后发现我的D:\Android\sdk\platforms\android-23\android.jar中缺少类TransceiveResult,但在此处以Android源代码呈现D:\Android\sdk\sources\android-23\android\nfc\TransceiveResult.java,然后我意识到它可以对公众隐藏,而不是导出,实际上是

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

然后我做了一些随机的事情,在同步项目,清理和重建,使缓存无效/重新启动之后,仍然不能解析TransceiveResult,我想知道符号是否已在SDK中丢失,项目如何顺利编译?

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?

最后为我编辑Aha

我们在代码中调用android.nfc.tech.BasicTagTechnology#transceive而不是TransceiveResult,在编译时我们不需要解析TransceiveResult,我们只需要解析代码中引用的android.nfc.tech.BasicTagTechnology#transceive,我迷路了那一刻.

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.

推荐答案

@hide表示其未包含在文档中,如

@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.

更新: 为了在您深入研究SDK类时在IDE中进行说明,您可能会看到对无法解析的隐藏成员的引用.没关系,只要SDK类中的代码不在您的代码中,它仍会编译.

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.

如果要使用这些隐藏的类/方法/字段,则有2个主要选项:

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

1)获取完整版本的android-framework.jar并能够进行编译.但是,这样做的缺点是编译后的代码可能无法在其他Android版本中运行,因为Class或方法甚至可能不在该版本中. 例如BasicTagTechnology实际上是私有软件包,因此无论如何您都无法访问它

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)使用反射:

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.

这篇关于SDK中缺少导入的类时,Android项目如何编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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