How to use Smack 4.1 in Android?

An SO User picture An SO User · Nov 17, 2014 · Viewed 23.1k times · Source

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.

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.

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-Guide seem to be for 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.

Answer

Flow picture Flow · Nov 17, 2014

How do I include Smack 4.1 in my Android project in Eclipse?

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 and the Android Ant build system will make them available to the projects classpath and include them in the resulting .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

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.

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.

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

So we are back at working with Eclipse/Ant for our Android project. The Smack 4.1 README tells you to use the MavenToAndroidAnt Python3 script, 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.

Of course you could also add the 11 .jar files manually, but who wants to do that and have binaries in their source repository?

Will the code stay the same for 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.

1: Correct me if I'm wrong, but last time I tried it was not possible. The situation may got better in the meantime.