如何让Twilio Client Sdk与最新的Android版本兼容?

时间:2014-02-19 09:51:38

标签: android twilio

我在我的应用程序中使用twilioclient-android-1.1.2-3635733,MonkeyPhone类文件似乎 -

package com.twilio.example.hellomonkey;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.twilio.client.Connection;
import com.twilio.client.Device;
import com.twilio.client.Twilio;
import com.twilio.client.Connection.State;


public class MonkeyPhone implements Twilio.InitListener
{
    private static final String TAG = "MonkeyPhone";
    private Device device;
    private Connection connection;
    public MonkeyPhone(Context context)
    {
        Twilio.initialize(context, this /* Twilio.InitListener */);
    }

    /* Twilio.InitListener method */
    @Override
    public void onInitialized()
    {
        Log.d(TAG, "Twilio SDK is ready");
        try {
            String capabilityToken = HttpHelper.httpGet("http://www.google.com/twilio/auth.php");
            device = Twilio.createDevice(capabilityToken, null /* DeviceListener */);
        } catch (Exception e) {
            Log.e(TAG, "Failed to obtain capability token: " + e.getLocalizedMessage());
        }
    }
      /* Twilio.InitListener method */
    @Override
    public void onError(Exception e)
    {
        Log.e(TAG, "Twilio SDK couldn't start: " + e.getLocalizedMessage());
    }

    @Override
    protected void finalize()
    {
        if (connection != null)
            connection.disconnect();
        if (device != null)
            device.release();
    }

    public void connect(String phoneNumber) {
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("PhoneNumber", phoneNumber);
        connection = device.connect(parameters, null /* ConnectionListener */);
        if (connection == null)
            Log.w(TAG, "Failed to create new connection");
        // TODO Auto-generated method stub

    }

    public void disconnect()
    {
        if (connection != null) {
            connection.disconnect();
            connection = null;
        }
        Twilio.shutdown();
    }

    public State status()
    {
        connection.getState();
        State statusHere = connection.getState();
        return statusHere;
    }


}

HelloMonkeyActivity

package com.twilio.example.hellomonkey;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

public class HelloMonkeyActivity extends Activity implements View.OnClickListener
{
    private MonkeyPhone phone;
    private EditText numberField;

    @Override
    public void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        if (android.os.Build.VERSION.SDK_INT > 9) {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            }
        setContentView(R.layout.main);
        phone = new MonkeyPhone(getApplicationContext());
        ImageButton dialButton = (ImageButton)findViewById(R.id.dialButton);
        dialButton.setOnClickListener(this);
        ImageButton hangupButton = (ImageButton)findViewById(R.id.hangupButton);
        hangupButton.setOnClickListener(this);
        numberField = (EditText)findViewById(R.id.numberField);
    }

    @Override
    public void onClick(View view)
    {
        if (view.getId() == R.id.dialButton)
            phone.connect(numberField.getText().toString());
        else if (view.getId() == R.id.hangupButton)
            phone.disconnect();
    }
}

我在targetSdkVersion:19使用此功能 我在以下模拟器设备上测试过的模拟器工作正常 - Nexus One(Android 4.4) Nexus 7(Android 4.4) Nexus 10(Android 4.4) 在Android 4.2和4.3的设备上,它在模拟器中工作正常

适用于真实设备Xolo A600(Android版本4.2.2) 但是在任何更高版本上跟随stacktree失败,而不是在模拟器中的实际设备中 - 为什么呢?

02-19 11:58:38.492: E/AndroidRuntime(26854): FATAL EXCEPTION: Thread-18132
02-19 11:58:38.492: E/AndroidRuntime(26854): Process: com.twilio.example.hellomonkey, PID: 26854
02-19 11:58:38.492: E/AndroidRuntime(26854): java.lang.NoSuchFieldError: no type "Lcom/twilio/client/impl/useragent/config/UserAgentConfig$Callbacks;" found and so no field "callbacks" could be found in class "Lcom/twilio/client/impl/useragent/UserAgent;" or its superclasses
02-19 11:58:38.492: E/AndroidRuntime(26854): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.twilio.client.impl.useragent.config.UserAgentConfig$Callbacks" on path: DexPathList[[directory "."],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
02-19 11:58:38.492: E/AndroidRuntime(26854):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-19 11:58:38.492: E/AndroidRuntime(26854):    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-19 11:58:38.492: E/AndroidRuntime(26854):    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
02-19 11:58:38.492: E/AndroidRuntime(26854):    Suppressed: java.lang.ClassNotFoundException: com.twilio.client.impl.useragent.config.UserAgentConfig$Callbacks
02-19 11:58:38.492: E/AndroidRuntime(26854):        at java.lang.Class.classForName(Native Method)
02-19 11:58:38.492: E/AndroidRuntime(26854):        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
02-19 11:58:38.492: E/AndroidRuntime(26854):        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
02-19 11:58:38.492: E/AndroidRuntime(26854):        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
02-19 11:58:38.492: E/AndroidRuntime(26854):        ... 1 more
02-19 11:58:38.492: E/AndroidRuntime(26854):    Caused by: java.lang.NoClassDefFoundError: Class "Lcom/twilio/client/impl/useragent/config/UserAgentConfig$Callbacks;" not found
02-19 11:58:38.492: E/AndroidRuntime(26854):        ... 5 more
02-19 11:58:49.404: I/Process(26854): Sending signal. PID: 26854 SIG: 9

我的意思是twilio sdk目前仅与4.2兼容,但如果是,那么为什么它在模拟器中运行良好关于我之前在这里发布的错误 How to resolve java.lang.NoClassDefFoundError UserAgentConfig$Callbacks with Twilio  但现在提供的代码我猜你们可以帮助和理解我的挫折感 - 提前致谢

2 个答案:

答案 0 :(得分:2)

禁用ART,重新启用Dalvik。我的设备(不是模拟器)遇到了这个确切的错误,那就是修复。

答案 1 :(得分:0)

我已经通过将Twilio添加到Proguard例外列表中来解决它:

  

-keep class com.twilio ** {*; }