Search code examples
androidopentoktokbox

Opentok App Crashes when Session is Declared


I'm trying to learn how to use the TokBox SDK. I've jsut started writing a simple app to display a video so as I can get my head around how it works. I've based my code on examples found at https://tokbox.com/opentok/tutorials/connect-session/android/#connecting . Every time I initialise the Session object, however, my app stops working. I've tried surrounding it with a try/catch block and it still crashed. My code is as follows:

package com.example.ben.toktest;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;

import com.opentok.android.Session;

public class MainActivity extends Activity{
    private static final String API_KEY= "12345678"; //replaced api key with 12345678 for privacy purposes
    RelativeLayout container;
    Session session;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        container = (RelativeLayout) findViewById(R.id.main);

        session = new Session(MainActivity.this, "12345678", "2_MX40NTEwMjczMn5-MTQxODAyMDUwMjgwMn5PTjdlRDBvSDVYOHBkelZjby80eGZuclN-fg"); //Crashes here
        /*session.setSessionListener(new Session.SessionListener() {
            @Override
            public void onConnected(Session session) {

            }

            @Override
            public void onDisconnected(Session session) {

            }

            @Override
            public void onStreamReceived(Session session, Stream stream) {
                mStreams.add(stream);
                subscriber = new Subscriber(MainActivity.this, stream);
                subscriber.setSubscriberListener(new SubscriberListener() {
                    @Override
                    public void onConnected(SubscriberKit subscriberKit) {
                        LayoutParams params = new LayoutParams(getResources().getDisplayMetrics().widthPixels, getResources().getDisplayMetrics().heightPixels);
                        container.addView(subscriberKit.getView(), params);
                    }

                    @Override
                    public void onDisconnected(SubscriberKit subscriberKit) {

                    }

                    @Override
                    public void onError(SubscriberKit subscriberKit, OpentokError opentokError) {

                    }
                });
                session.subscribe(subscriber);

            }

            @Override
            public void onStreamDropped(Session session, Stream stream) {

            }

            @Override
            public void onError(Session session, OpentokError opentokError) {

            }
        });*/

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

The output from my logcat is:

12-08 17:35:37.240  13677-13677/com.example.ben.toktest W/ActivityThread﹕ Application com.example.ben.toktest is waiting for the debugger on port 8100...
12-08 17:35:37.250  13677-13677/com.example.ben.toktest I/System.out﹕ Sending WAIT chunk
12-08 17:35:37.355  13677-13684/com.example.ben.toktest I/dalvikvm﹕ Debugger is active
12-08 17:35:37.455  13677-13677/com.example.ben.toktest I/System.out﹕ Debugger has connected
12-08 17:35:37.455  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:37.655  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:37.855  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.055  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.255  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.455  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.655  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:38.855  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:39.060  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:39.260  13677-13677/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:35:39.460  13677-13677/com.example.ben.toktest I/System.out﹕ debugger has settled (1374)
12-08 17:35:40.105  13677-13677/com.example.ben.toktest W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/opentok/android/Session;
12-08 17:35:40.105  13677-13677/com.example.ben.toktest D/AndroidRuntime﹕ Shutting down VM
12-08 17:35:40.105  13677-13677/com.example.ben.toktest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40edb2a0)
12-08 17:35:40.205  13677-13677/com.example.ben.toktest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
            at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            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:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.UnsatisfiedLinkError: Couldn't load opentok: findLibrary returned null
            at java.lang.Runtime.loadLibrary(Runtime.java:365)
            at java.lang.System.loadLibrary(System.java:535)
            at com.opentok.android.Session.<clinit>(Session.java:46)
            at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            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:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)
12-08 17:36:01.010  14367-14367/com.example.ben.toktest W/ActivityThread﹕ Application com.example.ben.toktest is waiting for the debugger on port 8100...
12-08 17:36:01.020  14367-14367/com.example.ben.toktest I/System.out﹕ Sending WAIT chunk
12-08 17:36:01.070  14367-14373/com.example.ben.toktest I/dalvikvm﹕ Debugger is active
12-08 17:36:01.220  14367-14367/com.example.ben.toktest I/System.out﹕ Debugger has connected
12-08 17:36:01.220  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:01.420  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:01.620  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:01.820  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.020  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.225  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.425  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.630  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:02.845  14367-14367/com.example.ben.toktest I/System.out﹕ waiting for debugger to settle...
12-08 17:36:03.045  14367-14367/com.example.ben.toktest I/System.out﹕ debugger has settled (1459)
12-08 17:36:03.395  14367-14367/com.example.ben.toktest W/dalvikvm﹕ Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/opentok/android/Session;
12-08 17:36:03.400  14367-14367/com.example.ben.toktest D/AndroidRuntime﹕ Shutting down VM
12-08 17:36:03.400  14367-14367/com.example.ben.toktest W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40edb2a0)
12-08 17:36:03.425  14367-14367/com.example.ben.toktest E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
            at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            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:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.UnsatisfiedLinkError: Couldn't load opentok: findLibrary returned null
            at java.lang.Runtime.loadLibrary(Runtime.java:365)
            at java.lang.System.loadLibrary(System.java:535)
            at com.opentok.android.Session.<clinit>(Session.java:46)
            at com.example.ben.toktest.MainActivity.onCreate(MainActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5206)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
            at android.app.ActivityThread.access$600(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4898)
            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:1006)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
            at dalvik.system.NativeStart.main(Native Method)

Can someone please tell me why it keeps crashing? I have no idea what I've done wrong!


Solution

  • I had the same problem when trying to run the OpenTok Sample App using Android Studio.

    Try this:

    1) Open your app/build.gradle file and add the line

    compile files('libs/opentok-android-sdk-2.2.1.jar')

    under dependencies.

    2) Create a folder libs under your app folder (if it's not already there)

    3) Copy the OpenTok JAR file (opentok-android-sdk-2.3.1.jar) from the OpenTok SDK libs folder into your app/libs folder

    4) Create the folder jniLibs under app/src/main/

    5) Copy folders armeabi and x86, from the OpenTok SDK libs folder, to the jniLibs folder, and run your app.

    I hope it helps!