Search Unity

Google License Verification - Need help.

Discussion in 'Android' started by DanRP, Aug 27, 2013.

  1. DanRP


    May 26, 2013
    Hello Everyone,

    I'm working on adding Google Play license verification to my app and am having an issue I can't resolve. I am able to run the LVL_Example (CheckLVLButton) that came with the add-on. When I run the example code, the call works and returns the correct return code.

    Now I am trying to integrate this code to my app and am running into a problem with this code:

    Code (csharp):
    1. public TextAsset ServiceBinder;
    3. private void LoadServiceBinder() {
    4.     byte[] classes_jar = ServiceBinder.bytes;
    6.     m_Activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    7.     m_PackageName = m_Activity.Call<string>("getPackageName");
    9.     string cachePath = System.IO.Path.Combine(m_Activity.Call<AndroidJavaObject>("getCacheDir").Call<string>("getPath"), m_PackageName);
    10.     System.IO.Directory.CreateDirectory(cachePath);
    12.     System.IO.File.WriteAllBytes(cachePath + "/classes.jar", classes_jar);
    13.     System.IO.Directory.CreateDirectory(cachePath + "/odex");
    15.     AndroidJavaObject dcl = new AndroidJavaObject("dalvik.system.DexClassLoader",
    16.                                                       cachePath + "/classes.jar",
    17.                                                       cachePath + "/odex",
    18.                                                       null,
    19.                                                       m_Activity.Call<AndroidJavaObject>("getClassLoader"));
    20.     m_LVLCheckType = dcl.Call<AndroidJavaObject>("findClass", "com.unity3d.plugin.lvl.ServiceBinder");
    22.     System.IO.Directory.Delete(cachePath, true);
    23. }
    When the following line of code gets executed:
    Code (csharp):
    1. m_Activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
    ... I get a run-time error of "Exception: JNI: Init'd AndroidJavaClass with null ptr!".

    This exact same code runs fine in the example code, but fails when I use it in my own code.

    Last edited: Aug 27, 2013
  2. DanRP


    May 26, 2013
    I think I was getting this error because I was running in the editor. It seems to work fine on an android device...
  3. Meltdown


    Oct 13, 2010
    Yes, the JNI will not run in the Unity editor.. so you will always get an exception when anything tries to do so.
  4. Viltro


    Oct 11, 2013
    I too am having trouble getting this to work. I am running it on an android device (nook HD), I managed to get the debugger to attach to the process running on the device but when I get to the line "m_LVLCheckType = dcl>Call<AndroidJavaObject>("findClass", "com.unity3d.plugin.lvl.ServiceBinder");" and try to step over it, the app hangs. I am getting no errors in the output window. I assumed in the properties of the game object where I attached the C# code I am supposed to assign "AndroidManifest" to the variable "Service Binder". Without assigning "AndroidManifest" the app crashes. Is that the right thing to assign to this variable? I wish there were a little more documentation on how to incorporate this into an app, seems like I am just stabbing in the dark.
    jimrota likes this.
  5. Spierek


    Mar 4, 2013
    There seems to be a similar issue in current builds (from Unity 2019.1 to the latest version, 2019.2.17f1) that hasn't been resolved at least since May 2019 -

    Calling this line: m_LVLCheckType = dcl>Call<AndroidJavaObject>("findClass", "com.unity3d.plugin.lvl.ServiceBinder"); seems to throw the following exception, and as a result, the license cannot be properly verified:

    AndroidPlayer(samsung_SM-G903F@ AndroidJavaException: java.lang.ClassNotFoundException: Didn't find class "com.unity3d.plugin.lvl.ServiceBinder" on path: DexPathList[[zip file "/data/user/0/com.Afterburn.GolfPeaks/cache/com.Afterburn.GolfPeaks/classes.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    java.lang.ClassNotFoundException: Didn't find class "com.unity3d.plugin.lvl.ServiceBinder" on path: DexPathList[[zip file "/data/user/0/com.Afterburn.GolfPeaks/cache/com.Afterburn.GolfPeaks/classes.jar"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(
    at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
    at com.unity3d.player.UnityPlayer.c(Unknown Source)
    at com.unity3d.player.UnityPlayer$e$1.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(
    at android.os.Looper.loop(
    at com.unity3d.player.UnityPlayer$ Source)
    Suppressed: No original dex files found for dex location (arm) /data/user/0/com.Afterburn.GolfPeaks/cache/com.Afterburn.GolfPeaks/classes.jar (/data/user/0/com.Afterburn.GolfPeaks/cache/com.Afterburn.GolfPeaks/odex/classes.dex)
    at dalvik.system.DexFile.openDexFileNative(Native Method)
    at dalvik.system.DexFile.openDexFile(
    at dalvik.system.DexFile.<init>(
    at dalvik.system.DexFile.loadDex(
    at dalvik.system.DexPathList.loadDexFile(
    at dalvik.system.DexPathList.makePathElements(
    at dalvik.system.DexPathList.<init>(
    at dalvik.system.BaseDexClassLoader.<init>(
    at dalvik.system.DexClassLoader.<init>(
    ... 6 more
    at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <00000000000000000000000000000000>:0
    at UnityEngine.AndroidJNISafe.CallObjectMethod (System.IntPtr obj, System.IntPtr methodID, UnityEngine.jvalue[] args) [0x00000] in <00000000000000000000000000000000>:0
    at UnityEngine.AndroidJavaObject._Call[ReturnType] (System.String methodName, System.Object[] args) [0x00000] in <00000000000000000000000000000000>:0
    at UnityEngine.AndroidJavaObject.Call[ReturnType] (System.String methodName, System.Object[] args) [0x00000] in <00000000000000000000000000000000>:0
    at CheckLVLButton.LoadServiceBinder () [0x00000] in <00000000000000000000000000000000>:0
    at CheckLVLButton.Start () [0x00000] in <00000000000000000000000000000000>:0

    (Filename: currently not available on il2cpp Line: -1)

    According to this post the issue has been successfully reproduced back in September but it doesn't look like there has been any progress. cc @Yury-Habets
  6. JeffDUnity3D


    Unity Technologies

    May 2, 2017
    Are you able to reproduce?
  7. Spierek


    Mar 4, 2013
    Since my previous post we managed to figure out a solution together with some other developers in the Github issue I linked previously - (for anyone wrestling with the problem, please refer to that link for more info)

    This behaviour should be easily reproducible using steps outlined by user MigrantP:

    Download the project from and open in Unity 2019.1.10f1.
    Add the RSA key info needed in CheckLVLButton.cs.
    Set the Android package name.
    Set Install Location to Automatic.
    Build and run to a device.
    adb logcat will show the exception.

    You can also wrap the code in LoadServiceBinder() with a try/catch and look at the exception in more detail.

    The issue seems to stem from the fact that the provided example uses a dexed version of the plugin - switching it out for the regular plugin and replacing relevant code with simpler calls (below) should eliminate errors.

    Code (CSharp):
    1. m_LVLCheck = new AndroidJavaObject("com.unity3d.plugin.lvl.ServiceBinder", m_Activity);
    The rest seems to be making sure that the resulting AndroidManifest.xml file contains a CHECK_LICENSE permission, that relevant tester e-mail addresses have been inputted into the Google Play Console, and that the build with said permission has been uploaded to any of the release tracks (even Internal) at least once.
    kmedved likes this.