Search code examples
javaandroidsmack

simple smack jabber program taking very long to connect on android


Using smack jabber library 4.3.1, my android program takes 84 seconds to connect, which is fairly long. I have seen a discussion in a forum about a similar problem, but it affected earlier versions of SMACK.

What am I doing wrong?

Below is the smack the code am using to connect.

                XMPPTCPConnectionConfiguration cc= XMPPTCPConnectionConfiguration.builder()
                        .setCompressionEnabled(true)
                        .setUsernameAndPassword("smackuser","ilovesmack")
                        .setXmppDomain("xmpp.jp")
                        .setSecurityMode(ConnectionConfiguration.SecurityMode.required)
                        .build();
                connection=new XMPPTCPConnection(cc);
                connection.addConnectionListener(MainActivity.this);
                connection.connect();
                connection.login();

Solution

  • simply call once. For example in Application class

    AndroidSmackInitializer.initialize(Context);
    

    before any XMPP connections

    [OLD ANSWER. DONT USE]
    I found the problem. SMACK jabber uses minidns and minidns has a recently fixed bug on android 8. I solved the problem by using the code below. Thanks to @Flow

    //////////////////////smack///////////////////////////////////////
    implementation "org.igniterealtime.smack:smack-android-extensions:4.3.1"
    implementation "org.igniterealtime.smack:smack-experimental:4.3.1"
    implementation "org.igniterealtime.smack:smack-tcp:4.3.1"
    implementation 'de.measite.minidns:minidns-hla:0.2.4'//added this
    

    added minidns dependency

    import org.minidns.dnsserverlookup.android21.AndroidUsingLinkProperties;
    AndroidUsingLinkProperties.setup(context);//add this
    cc=XMPPTCPConnectionConfiguration.builder()
                            .setCompressionEnabled(true)
                            .setUsernameAndPassword(username,password)
                            .setXmppDomain(domain)
                            .build();
                    xmpptcpConnection=new XMPPTCPConnection(cc);
                    xmpptcpConnection.addConnectionListener(this);
                    xmpptcpConnection.connect();
                    xmpptcpConnection.login();