Integrating Smack with Android Studio project for

2019-06-14 22:24发布

问题:

I am trying to implement a chat messenger using ejabberd server and smack library but having a hard time to integrate all the jars and dependencies of smack. I am using android Studio.

My build.gradle(module):

apply plugin: 'com.android.application'

    android {
        compileSdkVersion 22
        buildToolsVersion "22.0.1"

        defaultConfig {
            applicationId "com.example.nit.xmppclient"
            minSdkVersion 18
            targetSdkVersion 22
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.2.0'

        compile "org.igniterealtime.smack:smack-android:4.1.0"
        compile "org.igniterealtime.smack:smack-tcp:4.1.0"
        compile "org.igniterealtime.smack:smack-android-extensions:4.1.0"

        compile 'org.ogce:xpp3:1.1.6'

    }

Firstly I was getting XMLpullparser error then I added xpp3. But after I added xpp3 I am getting

Error:Gradle: Execution failed for task ':app:preDexDebug'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' finished with non-zero exit value 1

build.gradle(project):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.2'



        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots'
        }
        mavenCentral()
    }

}

Questions:

How to remove the error or what can be the cause of error. I might be missing some dependency ?

Using smock will increase the app-size. can I acheive IM without using any library and writing server side code instead. Like using ejabberd library for node-js and the android app talking to nodejs instead of ejabberd?

And yes I more thing :

public class XMPPClient {
    private static int port = 5222;
    private static String host = "my-ip";
    private static String service = "my.com";


    static XMPPTCPConnectionConfiguration conf = XMPPTCPConnectionConfiguration.builder()
            .setServiceName(service)
            .setPort(port)
            .setHost(host)
            .setCompressionEnabled(false).build();
    static XMPPTCPConnection connection = new XMPPTCPConnection(conf);


    public static void register(String username, String password) throws SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NoResponseException {

        AccountManager accountManager = AccountManager.getInstance(connection);
        accountManager.createAccount(username,password);
    }

    public static  void main (String args[]) throws SmackException.NotConnectedException, XMPPException.XMPPErrorException, SmackException.NoResponseException {

        register("user", "password");

    }


}

I am getting the error when i am running the XMPPClient class

回答1:

It seems you need to add multidex support. Add multidex support in your app's build.gradle file

 android {
  compileSdkVersion 21
 buildToolsVersion "21.1.0"

defaultConfig {
    ...
    minSdkVersion 14
    targetSdkVersion 21
    ...

    // Enabling multidex support.
    multiDexEnabled true
}
...
}

dependencies {
compile 'com.android.support:multidex:1.0.0'
}


回答2:

If one does what's done in the question he/she will be able to integrate the library.

I was just running the file not the whole project, this might sound a silly mistake but after building the project everything worked fine.



回答3:

try to change your dependencies and build with gradles

compile 'org.igniterealtime.smack:smack-android:4.1.6'
compile 'org.igniterealtime.smack:smack-tcp:4.1.6'
compile 'org.igniterealtime.smack:smack-im:4.1.6'
compile 'org.igniterealtime.smack:smack-extensions:4.1.6'