Java 如何在 Android 中使用 Smack 4.1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26965506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Smack 4.1 in Android?
提问by Little Child
I was looking at this answer Using your own XMPP server for android chat app (Smack API)to learn about the available XMPP APIs. Smack seems like a good choice.
我正在查看这个答案Using your own XMPP server for android chat app (Smack API)以了解可用的 XMPP API。Smack 似乎是一个不错的选择。
Prior to Smack 4.1 one had to rely on aSmack. Starting with 4.1, Smack will run natively on Android. I have a couple of "getting started" questions.
在 Smack 4.1 之前,必须依赖 aSmack。从 4.1 开始,Smack 将在 Android 上本地运行。我有几个“入门”问题。
Question 1:
How do I include Smack 4.1 in my Android project in Eclipse?
The instructions here https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guideseem to be for Android Studio
问题 1:
如何在 Eclipse 的 Android 项目中包含 Smack 4.1?
这里的说明https://github.com/igniterealtime/Smack/wiki/Smack-4.1-Readme-and-Upgrade-Guide似乎适用于 Android Studio
Question 2:
Will the code stay the same for Android?
I am referring to their official documentation here: https://www.igniterealtime.org/builds/smack/docs/latest/documentation/index.html
What I would like to know is whether the usage of the API will stay the same in Android, too.
问题 2:
Android 的代码会保持不变吗?
我在这里指的是他们的官方文档:https: //www.igniterealtime.org/builds/smack/docs/latest/documentation/index.html
我想知道的是 API 的使用是否会保持不变安卓也一样。
采纳答案by Flow
How do I include Smack 4.1 in my Android project in Eclipse?
如何在 Eclipse 的 Android 项目中包含 Smack 4.1?
Like you would include any other Java library in Eclipse Android project. By putting the .jar
files in the libs/
directory, the Android plugin of Eclipse andthe Android Ant build system will make them available to the projects classpath and include them in the resulting .apk
.
就像您将在 Eclipse Android 项目中包含任何其他 Java 库一样。通过将.jar
文件放在libs/
目录中,Eclipse 的 Android 插件和Android Ant 构建系统将使它们可用于项目类路径并将它们包含在生成的.apk
.
The thing with Smack 4.1 is, that it is heavily modularized. If you want a full featured Smack on Android you need something around 11 .jar
files. Now, you could use Android Studio using gradle, this way including Smack as is simple as adding
Smack 4.1 的特点是,它高度模块化。如果你想在 Android 上使用全功能的 Smack,你需要大约 11 个.jar
文件。现在,您可以使用 gradle 使用 Android Studio,这种方式包括 Smack 就像添加一样简单
dependencies {
compile "org.igniterealtime.smack:smack-android:4.1.0-rc1"
compile "org.igniterealtime.smack:smack-tcp:4.1.0-rc1"
// optional features
compile "org.igniterealtime.smack:smack-android-extensions:4.1.0-rc1"
}
and gradle will take care of resolving the transitive dependencies.
gradle 将负责解决传递依赖项。
But, Android Studio, or the Android gradle plugin to be precise, does not support downloading and debugging with source attachments1. And this is something you clearly want when working with open source libraries.
但是,Android Studio,或者准确地说是Android gradle 插件,不支持使用源附件1下载和调试。在使用开源库时,这显然是您想要的。
That's for example one of the reasons I don't use the gradle plugin myself. I could not develop Smack then (I also like Eclipse, but that's a different story).
例如,这就是我自己不使用 gradle 插件的原因之一。那时我无法开发 Smack(我也喜欢 Eclipse,但那是另一回事了)。
So we are back at working with Eclipse/Ant for our Android project. The Smack 4.1 README tells you to use the MavenToAndroidAnt Python3script, which will automatically download the artifacts and delete the old ones on an update. If you hook it into your build process, it's nearly as good as using gradle but also supports source attachments.
所以我们又回到了使用 Eclipse/Ant 为我们的 Android 项目工作。Smack 4.1 README 告诉您使用MavenToAndroidAnt Python3脚本,它会自动下载工件并在更新时删除旧工件。如果你将它挂接到你的构建过程中,它几乎和使用 gradle 一样好,而且还支持源附件。
Of course you could also add the 11 .jar
files manually, but who wants to do that and have binaries in their source repository?
当然,您也可以.jar
手动添加 11 个文件,但谁愿意这样做并在其源存储库中包含二进制文件?
Will the code stay the same for Android?
Android 的代码会保持不变吗?
Yes, starting with Smack 4.1, Smack is Smack. So you can refer to the same javadoc, no matter if you are on Android or not. Note that this was also true for aSmack, since it's just a port of Smack to Android.
是的,从 Smack 4.1 开始,Smack 就是 Smack。因此,无论您是否使用 Android,都可以参考相同的 javadoc。请注意,对于 aSmack 也是如此,因为它只是 Smack 到 Android 的一个端口。
1: Correct me if I'm wrong, but last time I tried it was not possible. The situation may got better in the meantime.
1:如果我错了,请纠正我,但我上次尝试是不可能的。与此同时,情况可能会好转。