Trying to make a native android plugin with LISNR sdk for Unity

Hello everyone,

I’ve been trying to do this for some days now and I keep getting blocked at a few errors, hoping I can get some help with this from the community.

I’m trying to wrap the LISNR sdk ( http://lisnr.com ) into an android plugin for Unity, and I’ve been having some issues, tried many things to fix each issue and I just end up having another issue:

  1. The demo for LISNR extends AppCompatActivity, when I run this natively, it works. However if I try to export this into a jar for use with Unity, I get this error java.lang.NoClassDefFoundError: Failed resolution of: Lcom/site/nativeplugin/LISNR; - This is my LISNR java class
public class LISNR extends AppCompatActivity {
...
}

I even tried to manually import the jar library containing the AppCompatActivity class into Unity, and still the same result.

  1. Instead of extending AppCompatActivity, I made it extend UnityPlayerActivity, it then recognized the class, but got a different error. The app crashes, but before logging the fatal error, it gives this warning:
 W/System.err: java.io.IOException: Resource not found: "org/joda/time/tz/data/ZoneInfoMap"

And this is the fatal error

134/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.site.nativeplugin, PID: 16134
                                                   java.lang.Error: FATAL EXCEPTION [main]
                                                   Unity version     : 5.5.1f1
                                                   Device model      : LGE LG-H831
                                                   Device fingerprint: lge/h1_rgs_ca/h1:7.0/NRD90U/1632310030db6:user/release-keys
                                                  
                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int retrofit2.Response.code()' on a null object reference

So, I’ve googled the warning and found some answers saying that JodaTime should be initialized, and also the app needs to use MultiDex, so I’ve done that, and when I export the jar, it gives me the error that it can’t find the joda time library. (Even though I include it in the jar, and also tried manually adding the jar in Unity)

java.lang.NoClassDefFoundError: Failed resolution of: Lnet/danlew/android/joda/R$raw

Everything else I tried just gave me this error again Attempt to invoke virtual method 'int retrofit2.Response.code()' on a null object reference

This is my gradle build file

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
        }
    }
    lintOptions {
        abortOnError false
    }
    dexOptions {
        incremental true
        javaMaxHeapSize '4g'
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'
    compile 'com.lisnr.sdk:sdk:5.0.1.+'
    compile 'net.danlew:android.joda:2.9.5.1'
    compile 'com.android.support:multidex:1.0.0'
    testCompile 'junit:junit:4.12'
    testCompile 'joda-time:joda-time:2.9.4'
}

task exportJar(type: Copy) {
    from('build/intermediates/bundles/release/classes.jar')
    //include('UnityPlayer.jar')
    exclude('libs/UnityPlayer.jar')
    //include('android.joda-2.9.5.1')
    into('../../Assets/Plugins/Android')
    rename('classes.jar', 'UnityLISNR.jar')
}

task deleteOldJar(type: Delete) {
    delete 'release/UnityLISNR.jar'
}

exportJar.dependsOn(deleteOldJar, build)

I’ve also created a Test class which initializes the LISNR class:

package com.site.nativeplugin;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;

import org.joda.time.DateTimeZone;
import org.joda.time.tz.ZoneInfoProvider;

import java.io.IOException;

/**
* Created by nhaidar on 2/2/2017.
*/

public class Test extends Application {

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public void StartLISNR(UnityPlayerActivity activity) throws IOException {
        JodaTimeAndroid.init(activity.getApplicationContext());

        Intent intent = new Intent(activity, LISNR.class);
        activity.startActivity(intent);
}

And this is my C# Unity code which calls the Java functions

        AndroidJavaClass unityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject unityActivity = unityClass.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject jc = new AndroidJavaObject("com.fuel.nhaidar.nativeplugin.Test");
        jc.Call("StartLISNR", unityActivity);

Thanks for reading through this long thread and any attempt to help is appreciated!

Small update:

I tried to directly use the LISNR provided jar with Unity, without making a wrapper in Java, still getting the same exact issue. The “Resource not found: “org/joda/time/tz/data/ZoneInfoMap”” warning and then the fatal error with the java.lang.NullPointerException: Attempt to invoke virtual method ‘int retrofit2.Response.code()’ on a null object reference

were u able to solve this issue?