将Smack集成到Android Studio项目中用于聊天应用

20

我正在尝试使用ejabberd服务器和smack库实现聊天应用,但是在整合所有smack的jar包和依赖方面遇到了一些困难。我的开发环境是Android Studio。

我的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'

    }

一开始我遇到了XMLpullparser错误,然后我添加了xpp3。但是在我添加了xpp3之后,我又遇到了

错误。

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(项目):

// 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()
    }

}

问题:

如何消除错误或错误的原因是什么。我可能会缺少一些依赖关系?

使用smock将增加应用程序的大小。我可以不使用任何库并编写服务器端代码来实现IM吗? 例如,使用node-js的ejabberd库,Android应用程序与nodejs对话而不是ejabberd?

还有一个问题:

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

    }


}

当我运行XMPPClient类时,出现了错误。

3个回答

1
似乎您需要添加multidex支持。在您的应用程序的build.gradle文件中添加multidex支持。
 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'
}

0

尝试更改您的依赖项并使用Gradle构建

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'

0

如果按照问题中所述的步骤进行操作,就能够成功地整合该库。

我只是运行了文件而非整个项目,这听起来可能是一个愚蠢的错误,但在构建项目后一切都正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接