Initialization exception "NoClassDefFoundError: javax.naming.directory.InitialDirContext" when using Smack 4.1 on Android

Initialization exception "NoClassDefFoundError: javax.naming.directory.InitialDirContext" when using Smack 4.1 on Android

我正在尝试将 Smack 用于我的项目。我以前在我的一些早期项目中使用过 aSmack,效果非常好。然而,Smack 给了我奇怪的例外。

这是我的初始化代码 -

public void init(String username, String password) throws IOException, XMPPException, SmackException {
        mConnection = new XMPPTCPConnection(username, password, HOST_NAME); //This is the line which generates the exception

        mConnection.connect();
        mConnection.login();

        mChatManager = ChatManager.getInstanceFor(mConnection);
        mChatManager.addChatListener(this);
    }

这是 Logcat 输出 -

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View.onClick(View.java:3690)
            at android.view.View.performClick(View.java:4192)
            at android.view.View$PerformClick.run(View.java:17248)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View.onClick(View.java:3685)
            at android.view.View.performClick(View.java:4192)
            at android.view.View$PerformClick.run(View.java:17248)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ExceptionInInitializerError
            at test.Managers.XMPPManager.init(XMPPManager.java:42)
            at test.LoginActivity.onLoginClick(LoginActivity.java:46)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View.onClick(View.java:3685)
            at android.view.View.performClick(View.java:4192)
            at android.view.View$PerformClick.run(View.java:17248)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ExceptionInInitializerError
            at org.jivesoftware.smack.SmackConfiguration.getVersion(SmackConfiguration.java:96)
            at org.jivesoftware.smack.AbstractXMPPConnection.<clinit>(AbstractXMPPConnection.java:98)
            at test.Managers.XMPPManager.init(XMPPManager.java:42)
            at test.LoginActivity.onLoginClick(LoginActivity.java:46)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View.onClick(View.java:3685)
            at android.view.View.performClick(View.java:4192)
            at android.view.View$PerformClick.run(View.java:17248)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ExceptionInInitializerError
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:217)
            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:98)
            at test.Managers.XMPPManager.init(XMPPManager.java:42)
            at test.LoginActivity.onLoginClick(LoginActivity.java:46)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View.onClick(View.java:3685)
            at android.view.View.performClick(View.java:4192)
            at android.view.View$PerformClick.run(View.java:17248)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: javax.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:217)
            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:98)
            at test.Managers.XMPPManager.init(XMPPManager.java:42)
            at test.LoginActivity.onLoginClick(LoginActivity.java:46)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View.onClick(View.java:3685)
            at android.view.View.performClick(View.java:4192)
            at android.view.View$PerformClick.run(View.java:17248)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4950)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:997)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
            at dalvik.system.NativeStart.main(Native Method)

我查看了一些论坛帖子,说启用混淆器时会出现此问题。但是在我的项目中 Progaurd 被禁用了。

知道是什么导致了这个异常吗?

更新:-
这是我的 app.gradle 文件的样子 -

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.igniterealtime.smack:smack-java7:4.1.0'
    // Optional for XMPPTCPConnection
    compile 'org.igniterealtime.smack:smack-tcp:4.1.0'
    // Optional for XMPP-IM (RFC 6121) support (Roster, Threaded Chats, …)
    compile 'org.igniterealtime.smack:smack-im:4.1.0'
    // Optional for XMPP extensions support
    compile 'org.igniterealtime.smack:smack-extensions:4.1.0'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile project(':xxx')
    compile project(':xxx')
    compile project(':xxx')
    compile('com.crashlytics.sdk.android:crashlytics:2.2.4@aar') {
        transitive = true;
    }
}

不要在 Android 个项目上添加 smack-java7,而是使用 smack-android。

另外 Smack 4.2.0 是最新的 Smack 版本。