I am trying to use some functionality from a java JAR file, and specifically need to initialize it before usage of the API functions. This is done by accessing a static “builder” member of an “ApiClient” class which is itself an instance of a static class defined within the “ApiClient” class. Here is that java class:
package com.gs.common.client;
public class ApiClient {
public static Builder builder;
public static class Builder {
public Builder setContext(Context context) {}
public Builder addApi(String api) {}
public ApiClient build() {}
public void destroy() {}
}
public static class ApiCallBack {}
public static ApiClient getInstance() {}
}
FWIW, this is part of the GrandStream IP phone API for their Android line of devices.
The issue I’m having is that when I import ApiClient as an AndroidJavaClass, and then call GetStatic on it to get the “builder” member as an AndroidJavaObject, that works. But then when I try to call any functions of that member, I get a NullReferenceException. Here is my code:
AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivityObject = playerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass clientClass = new AndroidJavaClass("com.gs.common.client.ApiClient");
AndroidJavaClass builderClass = new AndroidJavaClass("com.gs.common.client.ApiClient$Builder");
AndroidJavaObject builder = clientClass.GetStatic<AndroidJavaObject>("builder");
// Any of these calls will result in a NullReferenceException
AndroidJavaObject junk1 = builder.Call<AndroidJavaObject>("setContext", currentActivityObject);
AndroidJavaObject junk2 = builder.Call<AndroidJavaObject>("addApi", "com.gs.phone.api.setting.CallSettingApi");
AndroidJavaObject junk3 = builder.Call<AndroidJavaObject>("build");
It looks like my call to access the static member is returning a null object, but I’m not sure why. I’m pretty sure the JAR is imported properly, because I’m able to get some other Exceptions that it implements when I run different code.
I’ve looked all over at existing answers on this topic, but none seem relevant to my case. I can only assume that I am doing something wrong when trying to access a static member of an AndroidJavaClass. Do I call it through some kind of ‘.’ notation instead? I’ve tried all kinds of combinations of AndroidJavaClass vs AndroidJavaObject, call vs get, void vs static, etc. I’m hoping that I’ve simply missed some edge case in the documentation or something.