Smack 4.3.2在Android 9上引发java.lang.ExceptionInInitializerError异常

时间:2019-07-04 06:57:57

标签: java android xmpp smack

我正在尝试使用Android上的smack创建聊天应用程序。最近,当我检查crashlytics日志时,我发现了一个奇怪的错误,该错误正在Android 9设备上发生。 m掷user

我正在使用smack 4.3.2。它似乎可以与其他Android版本一起使用。这是异常堆栈;

java.lang.ExceptionInInitializerError

内部异常清楚地表明它是Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference (Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference) at org.apache.commons.logging.impl.LogFactoryImpl.newInstance + 571(LogFactoryImpl.java:571) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance + 292(LogFactoryImpl.java:292) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance + 269(LogFactoryImpl.java:269) at org.apache.commons.logging.LogFactory.getLog + 655(LogFactory.java:655) at org.apache.http.conn.ssl.AbstractVerifier.(AbstractVerifier.java:2) at org.apache.http.conn.ssl.StrictHostnameVerifier.(StrictHostnameVerifier.java:1) at org.apache.http.conn.ssl.StrictHostnameVerifier.(StrictHostnameVerifier.java:1) at org.jivesoftware.smack.android.AndroidSmackInitializer.initialize + 41(AndroidSmackInitializer.java:41) at org.jivesoftware.smack.SmackInitialization.loadSmackClass + 212(SmackInitialization.java:212) at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad + 173(SmackInitialization.java:173) at org.jivesoftware.smack.SmackInitialization.processConfigFile + 143(SmackInitialization.java:143) at org.jivesoftware.smack.SmackInitialization.processConfigFile + 128(SmackInitialization.java:128) at org.jivesoftware.smack.SmackInitialization.(SmackInitialization.java:12) at org.jivesoftware.smack.SmackConfiguration.getVersion + 93(SmackConfiguration.java:93) at org.jivesoftware.smack.ConnectionConfiguration.(ConnectionConfiguration.java:1) at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder + 80(XMPPTCPConnectionConfiguration.java:80) at com.davidoff.chat.ChatServiceConnection.createConnection + 234(ChatServiceConnection.java:234) at com.davidoff.chat.ChatServiceConnection.connect + 281(ChatServiceConnection.java:281) at com.davidoff.chat.ChatService.initConnection + 167(ChatService.java:167) at com.davidoff.chat.ChatService$1.run + 145(ChatService.java:145) at java.lang.Thread.run + 764(Thread.java:764) Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.trim()' on a null object reference at org.apache.commons.logging.impl.LogFactoryImpl.createLogFromClass + 1067(LogFactoryImpl.java:1067) at org.apache.commons.logging.impl.LogFactoryImpl.discoverLogImplementation + 844(LogFactoryImpl.java:844) at org.apache.commons.logging.impl.LogFactoryImpl.newInstance + 541(LogFactoryImpl.java:541) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance + 292(LogFactoryImpl.java:292) at org.apache.commons.logging.impl.LogFactoryImpl.getInstance + 269(LogFactoryImpl.java:269) at org.apache.commons.logging.LogFactory.getLog + 655(LogFactory.java:655) at org.apache.http.conn.ssl.AbstractVerifier.(AbstractVerifier.java:2) at org.apache.http.conn.ssl.StrictHostnameVerifier.(StrictHostnameVerifier.java:1) at org.apache.http.conn.ssl.StrictHostnameVerifier.(StrictHostnameVerifier.java:1) at org.jivesoftware.smack.android.AndroidSmackInitializer.initialize + 41(AndroidSmackInitializer.java:41) at org.jivesoftware.smack.SmackInitialization.loadSmackClass + 212(SmackInitialization.java:212) at org.jivesoftware.smack.SmackInitialization.parseClassesToLoad + 173(SmackInitialization.java:173) at org.jivesoftware.smack.SmackInitialization.processConfigFile + 143(SmackInitialization.java:143) at org.jivesoftware.smack.SmackInitialization.processConfigFile + 128(SmackInitialization.java:128) at org.jivesoftware.smack.SmackInitialization.(SmackInitialization.java:12) at org.jivesoftware.smack.SmackConfiguration.getVersion + 93(SmackConfiguration.java:93) at org.jivesoftware.smack.ConnectionConfiguration.(ConnectionConfiguration.java:1) at org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration.builder + 80(XMPPTCPConnectionConfiguration.java:80) at com.davidoff.chat.ChatServiceConnection.createConnection + 234(ChatServiceConnection.java:234) at com.davidoff.chat.ChatServiceConnection.connect + 281(ChatServiceConnection.java:281) at com.davidoff.chat.ChatService.initConnection + 167(ChatService.java:167) at com.davidoff.chat.ChatService$1.run + 145(ChatService.java:145) at java.lang.Thread.run + 764(Thread.java:764) 处触发的NullPointerException

这是org.apache.commons.logging.impl.LogFactoryImpl.createLogFromClass 1067(LogFactoryImpl.java:1067)中的catch块,它导致异常。 ...

LogFactoryImpl.java

...

您可以清楚地看到未找到日志适配器,并且触发了异常,因为内部异常包含空消息,并且 } catch (ExceptionInInitializerError e) { // A static initializer block or the initializer code associated // with a static variable on the log adapter class has thrown // an exception. // // We treat this as meaning the adapter's underlying logging // library could not be found. String msg = e.getMessage(); logDiagnostic("The log adapter '" + logAdapterClassName + "' is unable to initialize itself when loaded via classloader " + objectId(currentCL) + ": " + msg.trim()); break; 尝试在记录前对其进行修整。他们以某种方式不需要对消息进行空检查。我不知道是什么原因导致smack触发了此异常以及如何处理它。

这就是我们开始XMPP连接的方式。

...

LogFactoryImpl

...

build.grandle

...

    InetAddress addr = InetAddress.getByName(user.chat_server.serverURL);
    HostnameVerifier verifier = new HostnameVerifier() {
                @Override
                public boolean verify(String hostname, SSLSession session) {
                    return false;
                }
    };

    DomainBareJid serviceName =JidCreate.domainBareFrom(user.chat_server.serverURL);
    XMPPTCPConnectionConfiguration.Builder configBuilder = XMPPTCPConnectionConfiguration.builder();
    configBuilder.setXmppDomain(serviceName).setUsernameAndPassword(user.user_id, 
    user.chat_server.jid_password).
    setHost(user.chat_server.serverURL).
    setHostAddress(addr).
    setHostnameVerifier(verifier).
    setSendPresence(false).
    setPort(5222).
    setResource(CONNECTION_RESOURCE_ID).
    setSecurityMode(ConnectionConfiguration.SecurityMode.disabled).
                    setSendPresence(true);

    connection = new XMPPTCPConnection(configBuilder.build());
    connection.addConnectionListener(this);

    createChatManager();

    SASLAuthentication.blacklistSASLMechanism("SCRAM-SHA-1");
    SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
    SASLAuthentication.unBlacklistSASLMechanism("PLAIN");

    ConnectAndLoginAsyncTask connectAndLoginAsyncTask = new ConnectAndLoginAsyncTask();
    connectAndLoginAsyncTask.execute(connection);

...

    private static class ConnectAndLoginAsyncTask extends     
    AsyncTask<AbstractXMPPConnection, Integer, Integer> {
    @Override
    protected Integer doInBackground(AbstractXMPPConnection... params) {
    AbstractXMPPConnection connection = params[0];
        try {
          if (!connection.isConnected())
            connection.connect();
        } catch (Exception e) {
          e.printStackTrace();
        }

        try {
          if (!connection.isAuthenticated())
            connection.login();
        } catch (Exception e) {
          e.printStackTrace();
        }
       return 1;
      }
    }

...

1 个答案:

答案 0 :(得分:0)

如果有人遇到此问题。这是解决方案。将此行添加到您的proguard 设置文件

-keep class org.apache.** { *; }