3
votes

I'm new at Android programming, about two months now. I have spent quite a bit of time trying to figure out the solution before posting here. So, I will go straight into the question and after that, I will explain what had not worked for me.

The Question is as follows (it is as per title):

How do I use Smack 4.1.5 with Android Studio 2.1.2 to network with Openfire XMPP Server 4.0.2? Ideally, a short, bare bones piece of code that logs on and sends and receives chats would be appreciated. Otherwise, strong pointers would be most welcome. (Did I omit a library? Is there something wrong with the try-n-catch?)

The MainActivity.java file is as follows (I have commented out the last bit of code in order to zoom in on the problem code. The best I can tell, the problem is at the line just before the comment starts (/*). However, this is strange as this line is virtually the same as the one recommended by Smack documentation. That's why I am asking for help and let me say here: "Thanks in advance".)

(Of cos, I have gotten a Openfire XMPP server up and running on my PC. I had also tested spark on the Openfire XMPP server and it connected and sent and received chats okay.):

package com.eg.an_smackandchatclc;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.roster.Roster;
import org.jivesoftware.smack.roster.RosterEntry;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.util.*;
import java.io.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class MainActivity extends AppCompatActivity {
//public static final String HOST = "208.68.163.218"; //write your host name
//public static final int PORT = 5222;
private static final String TAG = "MainActivity.java";
//Log.e(TAG, "declaration");
@Override
protected void onCreate(Bundle savedInstanceState) {
    System.out.println("onCreate");
    Log.e(TAG, "onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println("onCreate");
    Log.e(TAG, "setContentView");
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("admin", "admin", "desktop-pc");
}}
/*
    AbstractXMPPConnection conn1 = new XMPPTCPConnection("username", "password", "jabber.org");
    System.out.println("onCreate");
    Log.e(TAG, "conn1");
}}
*/
/*
        try {
        System.out.println("onCreate");
        Log.e(TAG, "try");
        conn1.connect();
    } catch (SmackException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XMPPException e) {
        e.printStackTrace();
    }
    connect();
    System.out.println("onCreate");
    Log.e(TAG, "connect()");
}

The Logcat (Error messages only) is as follows:

08-06 08:08:46.405 18663-18663/com.eg.an_smackandchatclc E/MainActivity.java: onCreate
08-06 08:08:46.539 18663-18663/com.eg.an_smackandchatclc E/MainActivity.java: setContentView

08-06 08:08:46.672 18663-18663/com.eg.an_smackandchatclc E/AndroidRuntime: FATAL EXCEPTION: main
                                                                  Process: com.eg.an_smackandchatclc, PID: 18663
                                                                       java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/naming/directory/InitialDirContext;
                                                                           at org.jivesoftware.smack.util.dns.javax.JavaxResolver.<clinit>(JavaxResolver.java:50)
                                                                           at java.lang.Class.classForName(Native Method)
                                                                           at java.lang.Class.forName(Class.java:400)
                                                                           at org.jivesoftware.smack.SmackInitialization.loadSmackClass(SmackInitialization.java:213)
                                                                           at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad(SmackInitialization.java:193)
                                                                           at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:163)
                                                                           at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:148)
                                                                           at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:116)
                                                                           at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
                                                                           at org.jivesoftware.smack.AbstractXMPPConnection.<clinit>(AbstractXMPPConnection.java:97)
                                                                           at com.eg.an_smackandchatclc.MainActivity.onCreate(MainActivity.java:62)
                                                                           at android.app.Activity.performCreate(Activity.java:6658)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2584)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2692)
                                                                           at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6044)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
08-06 08:08:46.672 18663-18663/com.eg.an_smackandchatclc E/AndroidRuntime: Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.naming.directory.InitialDirContext" on path: DexPathList[[dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-support-annotations-24.0.0_87b336946543c1f787a3f9b83be41366f9cdbb84-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-tcp-4.1.5_b0d53c1047a32e900a08ccf219813c5bc2e618c2-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-sasl-provided-4.1.5_084129969859dd9e8f37122e5ef57f0cfd22a46c-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-sasl-javax-4.1.0_0ab65da887f48d8d5e384912160eaa1e0908d186-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-resolver-minidns-4.1.5_0ae177b49eafb38d7b12353d2eda6cc149b4fe43-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-resolver-javax-4.1.0_d306adbf26933e7f728102e3672f3f90637ba1f4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-java7-4.1.0_dcb0e760478344b0ca249594ceb7373304752bae-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-im-4.1.5_1ecc996e048dc5efba3acddae625e28aedfd704e-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-extensions-4.1.5_4eacc0e2ea2f430835f5aa7fef27c8a657ed75b2-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-core-4.1.5_21299a3731f708b27e7f5f12a5bc6ebf265c7a80-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-android-extensions-4.1.5_ce6630a8dabc9b47414ad59ad934c206078b768c-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-smack-android-4.1.5_d8cd382b84de7e021e8ac21a53fd3e9213386ed4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_9-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_8-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_7-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_6-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_5-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_3-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_2-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_1-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-slice_0-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-minidns-0.1.7_4e64c8580258e0c1f1f12d1e7412dad623952782-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-jxmpp-util-cache-0.4.2_108c360a78086927507dd4d9b7a2ad0580db469b-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-jxmpp-core-0.4.2_ee26bb5026023b646554c0bf9ba14c39ffd9a0f5-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-internal_impl-24.0.0_f8e857843a2f013dbed9c33184b5eae71c5fa909-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-support-vector-drawable-24.0.0_c612cdcfcff025f7f2ab5366116677fa236ef2b7-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-support-v4-24.0.0_cdc3a4409b42b56d3a7c52c568071a86579f9e63-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-appcompat-v7-24.0.0_50c6a3f385fbb5da5a5fbefc3749fa4
08-06 08:08:46.672 18663-18663/com.eg.an_smackandchatclc E/AndroidRuntime: abfcd41b4-classes.dex", dex file "/data/data/com.eg.an_smackandchatclc/files/instant-run/dex/slice-com.android.support-animated-vector-drawable-24.0.0_5eae71faeeb4e4b2f9cdde61d342e39a2cdcfcb5-classes.dex"],nativeLibraryDirectories=[/data/app/com.eg.an_smackandchatclc-2/lib/x86_64, /system/lib64, /vendor/lib64]]
                                                                           at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                           at com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader.findClass(IncrementalClassLoader.java:90)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                                                                            ... 23 more

I have set up the app build.gradle to import all the appropriate libraries (or at least I think I have). They include:

<Android API 24 Platform>
<1.8>
android-android-24
animated-vector-drawable-24.0.0
appcompat-v7-24.0.0
hamcrest-core-1.3
junit-4.12
jxmpp-core-0.4.2
jxmpp-util-cache-0.4.2
minidns-0.1.7
smack-android-4.1.5
smack-android-extensions-4.1.5
smack-core-4.1.5
smack-extensions-4.1.5
smack-im-4.1.5
smack-java7-4.1.0
smack-resolver-javax-4.1.0
smack-resolver-minidns-4.1.5
smack-sasl-javax-4.1.0
smack-sasl-provided-4.1.5
smack-tcp-4.1.5
support-annotations-24.0.0
support-v4-24.0.0
support-vector-drawable-24.0.0

The above is the basic information relating to the question.

The following was the "homework" that I tried to do before posting this question.

I had poured through every word of the Smack documentation (http://download.igniterealtime.org/smack/docs/latest/documentation/). Initially, I tried using the "few lines of code" suggested in the documentation:

```java AbstractXMPPConnection connection = new XMPPTCPConnection("mtucker", "password", "jabber.org");
connection.connect().login();
Chat chat = ChatManager.getInstanceFor(connection) .createChat("[email protected]", new MessageListener() {
public void processMessage(Chat chat, Message message) {
System.out.println("Received message: " + message);
}
});
chat.sendMessage("Howdy!"); ```

(http://download.igniterealtime.org/smack/docs/latest/documentation/overview.html)

Unfortunately, no joy at all.

Then, I resorted to testing that one line: (Ha-ha-ha. Stack Overflow says that the code I wanted to quote looks like spam so I put this in its place. Now it's not funny anymore cos even the previous sentence looked at spam to SO. Let's see if these two sentences will allow me to get this post through.) (Ha-ha. Stack Overflow says I need at least 10 reputation to post more than 2 links so the link I wanted to put here had to go. Anyway, I got said one line of code from the "Getting Started" page of the Smack documentation.)

Still, in vain.

Trust me, I have tried all the other solutions suggested on the net (including elsewhere on Stack Overflow) but I have failed with all of them. Each time, some error message or other would arise. Alternatively, the code sample given would entail having more coding chops than I had to make work. For example, the code sample may refer to a method which was not defined or to a class that had to be created on my own.

So, once again, thanks in advance for any help on this.

2
Remove smack-resolver-javax-4.1.0 from your dependenciesbabadaba
i think you are importing a lot of things that you dont need, try this compile 'org.igniterealtime.smack:smack-android:4.1.6' compile 'org.igniterealtime.smack:smack-tcp:4.1.6' compile "org.igniterealtime.smack:smack-extensions:4.1.6" compile "org.igniterealtime.smack:smack-im:4.1.6"Lucas Ferraz

2 Answers

0
votes

Use android libraries

  • org.igniterealtime.smack:smack-android:4.1.6
  • org.igniterealtime.smack:smack-tcp:4.1.6
  • org.igniterealtime.smack:smack-im:4.1.6

these libraries are enough to get started. all java libraries are not compatible with android, specially javax package.

0
votes

I had the same problem with Smack 4.1.x (NoClassDefFoundError on InitialDirContext).

Try using Smack 4.2, in build.gradle:

    compile "org.igniterealtime.smack:smack-android-extensions:4.2.0-alpha3"
    compile "org.igniterealtime.smack:smack-tcp:4.2.0-alpha3"

And this is how I initialized the connection.

    private void initializeConnection() {
    try {
        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setHost(mServerAddress)
                .setPort(mServerPort)
                .setXmppDomain(JidCreate.domainBareFrom(mServiceName))
                .setUsernameAndPassword(mUserLogin, mUserPassword)
                .build();

        mXmppTcpConnection = new XMPPTCPConnection(config);

        XmppConnectionListener connectionListener = new XmppConnectionListener();
        mXmppTcpConnection.addConnectionListener(connectionListener);
    } catch (XmppStringprepException e) {
        e.printStackTrace();
    }
}