我可以在独立项目中使用 android.os.* 库吗?
我正在尝试开发一个外部库(不确定这是否是正确的术语)来在 Android 项目中提供预打包的功能。在 Eclipse 中工作,我已将适当的 android.jar 文件添加到构建路径中,并且在编辑和编译时一切都很顺利。
但是,当我使用 Android 的 Handler 和 Message 类(android.os.Handler、android.os.Message)进行线程间通信时,除非我在 Android 应用程序、模拟器或设备上运行,否则我会遇到异常。我无法再以“独立”方式测试我的库,而不必通过正在运行的 Android 目标。
有什么方法可以让我包含这两个 Android 类并且仍然能够独立测试我的库吗?我需要提供 Android 源吗?或者是否需要某种条件编译?
I'm trying to develop an external library (not sure if that's the right term) to provide prepackaged functionality in Android projects. Working in Eclipse, I've added the appropriate android.jar file to the build path, and everything is happy both while editing and upon compilation.
However, when I use Android's Handler and Message classes (android.os.Handler, android.os.Message) for inter-thread communication, I get exceptions unless I'm running within an Android app, on the emulator or a device. I can no longer test my library in a "standalone" way without having to go through a running Android target.
Is there any way for me to include these two Android classes and still be able to test my library standalone? Do I need to have the Android source available? Or would it require some sort of conditional compilation hand-waving?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论如何,我能想到的,都不容易。
我不知道您还能从哪里获得实施。但更重要的是,这些东西并不是为了在操作系统之外独立工作而设计的,就像您可以只获取一两个 Cocoa 类并将它们拉入您的 Objective-C 库并期望它们在 Windows 机器上运行一样。
即兴发挥,对您正在构建的内容一无所知,我会让您在
Handler
和Message
上引入的任何依赖项都更加可插入。使用纯 Java 实现在 Android 之外进行测试,甚至可能只是一些模拟。使用真实的实现在 Android 内部进行测试。Not readily, by any means I can think of.
I don't know where else you would get the implementation from. But, more importantly, those things are not designed to work in isolation outside of the OS, any more than you could just grab a Cocoa class or two and pull them into your Objective-C library and expect them to run on a Windows box.
Off the cuff, knowing nothing about what you're building, I would make whatever dependency you are introducing on
Handler
andMessage
be more pluggable. Test outside of Android using a pure-Java implementation, perhaps even just some mocks. Test inside of Android using the real implementation.您可以尝试使用 Robolectric 库,它实现了 android API,这样您就可以为您拥有的一些独立代码创建 JUnit 测试:
http: //robolectric.org/
You could try the lib Robolectric, that implements the android API so you would be able to create JUnit tests for some isolated code you have:
http://robolectric.org/