Initialization exception “NoClassDefFoundError: ja

2019-06-24 15:04发布

问题:

I am trying to use Smack for my project. I have previously used aSmack in some of my earlier projects which worked really well. However Smack is giving me weird exceptions.

Here's my initialization code looks like -

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);
    }

Here's the Logcat output -

java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.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$1.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$1.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$1.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$1.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$1.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)

I have checked some forum posts which says that this problem comes when proguard is enabled. However in my project Progaurd is disabled.

Any idea what could cause this exception?

Update:-
This is how my app.gradle file looks like -

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;
    }
}

回答1:

Don't add smack-java7 on Android projects, use smack-android instead.

Also Smack 4.2.0 is the latest Smack version.