在 Android 的 SmackInitialization 中无法获得 smack 版本

Posted

技术标签:

【中文标题】在 Android 的 SmackInitialization 中无法获得 smack 版本【英文标题】:Could not get smack version in SmackInitialization in Android 【发布时间】:2015-11-21 19:30:07 【问题描述】:

我正在为 XMPP 聊天应用程序使用 Smack。我在 SmackInitialization 类中遇到一个奇怪的异常,错误是无法确定 Smack 版本,然后是应用程序崩溃。我已经搜索了足够多,但没有找到我错的地方。请就相同问题提出可能的解决方案。

崩溃日志:

26905-26905/com.myoneapp.chat E/SmackInitialization﹕ Could not determine Smack version
    java.lang.NullPointerException: lock == null
            at java.io.Reader.<init>(Reader.java:64)
            at java.io.InputStreamReader.<init>(InputStreamReader.java:122)
            at java.io.InputStreamReader.<init>(InputStreamReader.java:57)
            at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:61)
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
            at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
            at com.myoneapp.chat.services.MyXMPP.initialiseConnection(MyXMPP.java:111)
            at com.myoneapp.chat.services.MyXMPP.init(MyXMPP.java:105)
            at com.myoneapp.chat.services.MyXMPP.<init>(MyXMPP.java:68)
            at com.myoneapp.chat.services.MyXMPP.getInstance(MyXMPP.java:78)
            at com.myoneapp.chat.services.MyService.onCreate(MyService.java:37)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
            at android.app.ActivityThread.-wrap4(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-22 00:49:39.572  26905-26905/com.myoneapp.chat D/AndroidRuntime﹕ Shutting down VM
11-22 00:49:39.573  26905-26905/com.myoneapp.chat E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.myoneapp.chat, PID: 26905
    java.lang.ExceptionInInitializerError
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
            at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
            at com.myoneapp.chat.services.MyXMPP.initialiseConnection(MyXMPP.java:111)
            at com.myoneapp.chat.services.MyXMPP.init(MyXMPP.java:105)
            at com.myoneapp.chat.services.MyXMPP.<init>(MyXMPP.java:68)
            at com.myoneapp.chat.services.MyXMPP.getInstance(MyXMPP.java:78)
            at com.myoneapp.chat.services.MyService.onCreate(MyService.java:37)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
            at android.app.ActivityThread.-wrap4(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalStateException: java.lang.IllegalArgumentException: is == null
            at org.jivesoftware.smack.SmackInitialization.<clinit>(SmackInitialization.java:119)
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
            at org.jivesoftware.smack.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
            at com.myoneapp.chat.services.MyXMPP.initialiseConnection(MyXMPP.java:111)
            at com.myoneapp.chat.services.MyXMPP.init(MyXMPP.java:105)
            at com.myoneapp.chat.services.MyXMPP.<init>(MyXMPP.java:68)
            at com.myoneapp.chat.services.MyXMPP.getInstance(MyXMPP.java:78)
            at com.myoneapp.chat.services.MyService.onCreate(MyService.java:37)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
            at android.app.ActivityThread.-wrap4(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.IllegalArgumentException: is == null
            at org.kxml2.io.KXmlParser.setInput(KXmlParser.java:1634)
            at org.jivesoftware.smack.SmackInitialization.processConfigFile(SmackInitialization.java:155)
            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.ConnectionConfiguration.<clinit>(ConnectionConfiguration.java:38)
            at com.myoneapp.chat.services.MyXMPP.initialiseConnection(MyXMPP.java:111)
            at com.myoneapp.chat.services.MyXMPP.init(MyXMPP.java:105)
            at com.myoneapp.chat.services.MyXMPP.<init>(MyXMPP.java:68)
            at com.myoneapp.chat.services.MyXMPP.getInstance(MyXMPP.java:78)
            at com.myoneapp.chat.services.MyService.onCreate(MyService.java:37)
            at android.app.ActivityThread.handleCreateService(ActivityThread.java:2877)
            at android.app.ActivityThread.-wrap4(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1427)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

依赖关系:

compile files('libs/smack-android-extensions-4.1.0-rc4.jar')
    compile files('libs/smack-extensions-4.1.0-rc4.jar')
    compile files('libs/smack-java7-4.1.0-rc2.jar')
    compile files('libs/smack-resolver-dnsjava-4.1.0-rc4.jar')
    compile files('libs/smack-sasl-provided-4.1.0-rc2.jar')
    compile files('libs/smack-tcp-4.1.0-rc4.jar')
    compile('org.apache.httpcomponents:httpmime:4.3.6') 
        exclude module: 'httpclient'
    
    compile 'com.android.support:support-v13:23.0.1'
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.mcxiaoke.volley:library:1.0.19'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.4'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5'
    compile 'com.android.support:recyclerview-v7:23.0.1'
    compile 'com.facebook.android:facebook-android-sdk:4.0.0'
    compile 'com.google.android.gms:play-services-identity:8.1.0'
    compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'xpp3:xpp3:1.1.4c'
    compile 'org.jxmpp:jxmpp-core:0.5.0-alpha6'
    compile 'org.jxmpp:jxmpp-jid:0.5.0-alpha6'
    testCompile 'org.jxmpp:jxmpp-jid:0.5.0-alpha6:tests'
    testCompile 'junit:junit:4.11'
    testCompile 'xmlunit:xmlunit:1.5'
    testCompile 'org.powermock:powermock-module-junit4:1.5.5'
    testCompile 'org.powermock:powermock-api-mockito:1.5.5'
    testCompile 'com.jamesmurty.utils:java-xmlbuilder:0.6'
    testCompile 'net.iharder:base64:2.3.8'

错误代码:

static 
        String smackVersion;
        try 
            BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtils.getStreamForUrl("classpath:org.jivesoftware.smack/version", null)));
            smackVersion = reader.readLine();
            try 
                reader.close();
             catch (IOException e) 
                LOGGER.log(Level.WARNING, "IOException closing stream", e);
            
         catch(Exception e) 
            LOGGER.log(Level.SEVERE, "Could not determine Smack version", e);
            smackVersion = "unkown";
        
        SMACK_VERSION = smackVersion;

        String disabledClasses = System.getProperty("smack.disabledClasses");
        if (disabledClasses != null) 
            String[] splitDisabledClasses = disabledClasses.split(",");
            for (String s : splitDisabledClasses) SmackConfiguration.disabledSmackClasses.add(s);
        
        try 
            FileUtils.addLines("classpath:org.jivesoftware.smack/disabledClasses", SmackConfiguration.disabledSmackClasses);
        
        catch (Exception e) 
            throw new IllegalStateException(e);
        

        try 
            Class<?> c = Class.forName("org.jivesoftware.smack.CustomSmackConfiguration");
            Field f = c.getField("DISABLED_SMACK_CLASSES");
            String[] sa = (String[]) f.get(null);
            if (sa != null) 
                LOGGER.warning("Using CustomSmackConfig is deprecated and will be removed in a future release");
                for (String s : sa)
                    SmackConfiguration.disabledSmackClasses.add(s);
            
        
        catch (ClassNotFoundException e1) 
        
        catch (NoSuchFieldException e) 
        
        catch (SecurityException e) 
        
        catch (IllegalArgumentException e) 
        
        catch (IllegalAccessException e) 
        

        InputStream configFileStream;
        try 
            configFileStream = FileUtils.getStreamForUrl(DEFAULT_CONFIG_FILE, null);
        
        catch (Exception e) 
            throw new IllegalStateException(e);
        

        try 
            processConfigFile(configFileStream, null);
        
        catch (Exception e) 
            throw new IllegalStateException(e);
        

        // Add the Java7 compression handler first, since it's preferred
        SmackConfiguration.compressionHandlers.add(new Java7ZlibInputOutputStream());

【问题讨论】:

我有解决方案请检查:Find solution 【参考方案1】:

您缺少此文件org.jivesoftware.smack/version。通常它位于 smack-core.jar 中。

【讨论】:

【参考方案2】:

问题出在 processConfigFile(...) 内部,他们使用的是 Android 的 XmlPullParser,但在 junit 测试中不可用:

public static void processConfigFile(InputStream cfgFileStream,
                Collection<Exception> exceptions, ClassLoader classLoader) throws Exception 
    XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser();
.....

【讨论】:

以上是关于在 Android 的 SmackInitialization 中无法获得 smack 版本的主要内容,如果未能解决你的问题,请参考以下文章

求教怎么在android的Logcat中输出日志

求问android怎么在代码里获得系统文件的读写权限?

在 Android 4 中启用 android:largeHeap,在 Android 2.3 中禁用它

在android中怎样让按钮漂浮在图片上?

android中怎么设置组件在LinearLayout中居中

如何在 Chrome 上安装 Android 应用