javaxmppsmack

how to instantiate an StaxXmlPullParserFactory in android-8?


I keep getting below error message while trying to run simple app on emulator and I can't figure out why. Any help would be really appreciated:

2022-01-19 21:00:38.935 13581-13581/com.thecircle.circle_beta E/AndroidRuntime: FATAL EXCEPTION: main Process: com.thecircle.circle_beta, PID: 13581 java.util.ServiceConfigurationError: org.jivesoftware.smack.xml.XmlPullParserFactory: Provider org.jivesoftware.smack.xml.stax.StaxXmlPullParserFactory could not be instantiated at java.util.ServiceLoader.fail(ServiceLoader.java:233) at java.util.ServiceLoader.access$100(ServiceLoader.java:183) at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:392) at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:416) at java.util.ServiceLoader$1.next(ServiceLoader.java:494) at org.jivesoftware.smack.xml.SmackXmlParser.getXmlPullParserFactory(SmackXmlParser.java:44) at org.jivesoftware.smack.xml.SmackXmlParser.newXmlParser(SmackXmlParser.java:65) at org.jivesoftware.smack.util.PacketParserUtils.getParserFor(PacketParserUtils.java:80) at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:159) at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:154) at org.jivesoftware.smack.SmackInitialization.(SmackInitialization.java:103) at org.jivesoftware.smack.Smack.getVersion(Smack.java:38) at org.jivesoftware.smack.Smack.ensureInitialized(Smack.java:64) at org.jivesoftware.smack.ConnectionConfiguration.(ConnectionConfiguration.java:115) at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder(XMPPTCPConnectionConfiguration.java:64) at com.thecircle.circle_beta.xmpp.XMPPHandler.connect(XMPPHandler.java:30) at com.thecircle.circle_beta.PhoneNumberActivity.startXmppService(PhoneNumberActivity.java:62) at com.thecircle.circle_beta.PhoneNumberActivity.access$400(PhoneNumberActivity.java:27) at com.thecircle.circle_beta.PhoneNumberActivity$1.onClick(PhoneNumberActivity.java:53) at android.view.View.performClick(View.java:7448) at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131) at android.view.View.performClickInternal(View.java:7425) at android.view.View.access$3600(View.java:810) at android.view.View$PerformClick.run(View.java:28305) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/stream/XMLInputFactory; at org.jivesoftware.smack.xml.stax.StaxXmlPullParserFactory.(StaxXmlPullParserFactory.java:30) at java.lang.Class.newInstance(Native Method) at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:388) at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:416)  at java.util.ServiceLoader$1.next(ServiceLoader.java:494)  at org.jivesoftware.smack.xml.SmackXmlParser.getXmlPullParserFactory(SmackXmlParser.java:44)  at org.jivesoftware.smack.xml.SmackXmlParser.newXmlParser(SmackXmlParser.java:65)  at org.jivesoftware.smack.util.PacketParserUtils.getParserFor(PacketParserUtils.java:80)  at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:159)  at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:154)  at org.jivesoftware.smack.SmackInitialization.(SmackInitialization.java:103)  at org.jivesoftware.smack.Smack.getVersion(Smack.java:38)  at org.jivesoftware.smack.Smack.ensureInitialized(Smack.java:64)  at org.jivesoftware.smack.ConnectionConfiguration.(ConnectionConfiguration.java:115)  at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder(XMPPTCPConnectionConfiguration.java:64)  at com.thecircle.circle_beta.xmpp.XMPPHandler.connect(XMPPHandler.java:30)  at com.thecircle.circle_beta.PhoneNumberActivity.startXmppService(PhoneNumberActivity.java:62)  at com.thecircle.circle_beta.PhoneNumberActivity.access$400(PhoneNumberActivity.java:27)  at com.thecircle.circle_beta.PhoneNumberActivity$1.onClick(PhoneNumberActivity.java:53)  at android.view.View.performClick(View.java:7448)  at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)  at android.view.View.performClickInternal(View.java:7425)  at android.view.View.access$3600(View.java:810)  at android.view.View$PerformClick.run(View.java:28305)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7656)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)  Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.stream.XMLInputFactory" on path: DexPathList[[zip file "/data/app/~~aqFATXxhdyAHQijKIOYWXw==/com.thecircle.circle_beta-k5LiJXZwDyvcFdkQlSjhkw==/base.apk"],nativeLibraryDirectories=[/data/app/~~aqFATXxhdyAHQijKIOYWXw==/com.thecircle.circle_beta-k5LiJXZwDyvcFdkQlSjhkw==/lib/x86_64, /system/lib64, /system_ext/lib64]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:207) at java.lang.ClassLoader.loadClass(ClassLoader.java:379) at java.lang.ClassLoader.loadClass(ClassLoader.java:312) at org.jivesoftware.smack.xml.stax.StaxXmlPullParserFactory.(StaxXmlPullParserFactory.java:30)  at java.lang.Class.newInstance(Native Method)  at java.util.ServiceLoader$LazyIterator.nextService(ServiceLoader.java:388)  at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:416)  at java.util.ServiceLoader$1.next(ServiceLoader.java:494)  at org.jivesoftware.smack.xml.SmackXmlParser.getXmlPullParserFactory(SmackXmlParser.java:44)  at org.jivesoftware.smack.xml.SmackXmlParser.newXmlParser(SmackXmlParser.java:65)  at org.jivesoftware.smack.util.PacketParserUtils.getParserFor(PacketParserUtils.java:80)  at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:159)  at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:154)  at org.jivesoftware.smack.SmackInitialization.(SmackInitialization.java:103)  at org.jivesoftware.smack.Smack.getVersion(Smack.java:38)  at org.jivesoftware.smack.Smack.ensureInitialized(Smack.java:64)  at org.jivesoftware.smack.ConnectionConfiguration.(ConnectionConfiguration.java:115)  at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder(XMPPTCPConnectionConfiguration.java:64)  at com.thecircle.circle_beta.xmpp.XMPPHandler.connect(XMPPHandler.java:30)  at com.thecircle.circle_beta.PhoneNumberActivity.startXmppService(PhoneNumberActivity.java:62)  at com.thecircle.circle_beta.PhoneNumberActivity.access$400(PhoneNumberActivity.java:27)  at com.thecircle.circle_beta.PhoneNumberActivity$1.onClick(PhoneNumberActivity.java:53)  at android.view.View.performClick(View.java:7448)  at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)  at android.view.View.performClickInternal(View.java:7425)  at android.view.View.access$3600(View.java:810)  at android.view.View$PerformClick.run(View.java:28305)  at android.os.Handler.handleCallback(Handler.java:938)  at android.os.Handler.dispatchMessage(Handler.java:99)  at android.os.Looper.loop(Looper.java:223)  at android.app.ActivityThread.main(ActivityThread.java:7656)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

I AM JUST RUNNING BELOW CODE:

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.content.Context;
import android.util.Xml;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.android.AndroidSmackInitializer;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import org.jivesoftware.smack.xml.XmlPullParser;
import org.jxmpp.stringprep.XmppStringprepException;

import java.io.IOException;


public class XMPPHandler extends AppCompatActivity {
    private XMPPTCPConnection connection;
    //private XMPPTCPConnectionConfiguration config;

    public void connect() throws IOException, InterruptedException, XMPPException, SmackException {

        XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration.builder()
                .setXmppDomain("192.168.1.9")
                .setHost("192.168.1.9")
                .setPort(5222)
                .setSecurityMode(ConnectionConfiguration.SecurityMode.ifpossible)
                .build();
        connection = new XMPPTCPConnection(config);
        connection.connect();
    }

    //LOGIN
    public void login(String username, String password) throws XMPPException, InterruptedException, IOException, SmackException {
        if (connection!=null && connection.isConnected()) {
            connection.login(username, password);
        }

    }
}

Solution

  • Your Smack dependency declarations are probably wrong, as smack-xmlparser-stax is not used on Android (because there is no StAX parser in Android). Instead use smack-xmlparser-xpp3 in Android, which is a dependency of smack-android.