user3304953
user3304953

Reputation:

How to make Twilio Client Sdk compatible with latest android versions?

I am using twilioclient-android-1.1.2-3635733 in my application, the MonkeyPhone Class file seems like -

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

I am using this with targetSdkVersion:19 Things work fine with emulator I have tested on following emulator devices - Nexus One (Android 4.4) Nexus 7 (Android 4.4) Nexus 10 (Android 4.4) and on devices with android 4.2 and 4.3 it works fine in emulator

It works fine with real device Xolo A600 (Android version 4.2.2) But fails with following stacktree on any higher version to that in real devices not in emulator - Why so

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

I mean is it that the twilio sdk is for now compatible with only 4.2 for now but if so then why it runs fine in emulator Regarding the error I already posted here before How to resolve java.lang.NoClassDefFoundError UserAgentConfig$Callbacks with Twilio but now with the code provided I guess you guys can help and understand my frustration - Thanks in advance

Upvotes: 3

Views: 1344

Answers (2)

vvbYWf0ugJOGNA3ACVxp
vvbYWf0ugJOGNA3ACVxp

Reputation: 1116

I've solved it by adding Twilio to Proguard exception list:

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

Upvotes: 0

agildehaus
agildehaus

Reputation: 21

Disable ART, reenable Dalvik. I had this exact error come across my device (not the emulator) and that was the fix.

Upvotes: 2

Related Questions