How to access jar file from Unity with different package id in Unity & package name in Java?

First of all .. Hi guys ..

I created a plugin for unity in a java bank.

In Java, my package name is "com.android.test"

In Unity, my package id is "com.android.test" This works fine with the code below. But if you change your Bundle ID to com.android.test2 " in Unity, below is the throw code exception " Class not found: com.android.test2.myplugin " ,

Is there a way I can access a jar file whose package name is different from the name of the Unity node identifier?

Here is my unity of C # code for accessing the Jar:

static IntPtr cls_Activity;
static IntPtr fid_Activity;
static IntPtr obj_Activity;
static IntPtr cls_OurAppNameActivityClass;
static IntPtr startAdsMethod;

cls_Activity = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
fid_Activity = AndroidJNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
obj_Activity = AndroidJNI.GetStaticObjectField(cls_Activity, fid_Activity);

cls_OurAppNameActivityClass = AndroidJNI.FindClass("com/android/test/myplugin");
            startAdsMethod = AndroidJNI.GetMethodID(cls_OurAppNameActivityClass, "Instantiate", "()V");


  if (AndroidJNI.IsInstanceOf(obj_Activity, cls_OurAppNameActivityClass) != false)
    {
       Debug.Log("Activity IS a OurAppNameActivity");               
       jvalue[] myArray = new jvalue[1];
       AndroidJNI.CallVoidMethod(obj_Activity, startAdsMethod, myArray);
       Debug.Log("Activity Leaving a OurAppNameActivity");
    }
+3
2

, java, .

0

, . .

1. ( MainActivity. , MYAndroidPlugin)

2. MYAndroidPlugin.java MYAndroidPlugin, :

package com.mydomain.androidplugin;

import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.Toast;

import com.unity3d.player.UnityPlayer;



public class MyAndroidPlugin   {

   public MyAndroidPlugin(){

   //default constructor
  }

   public void makeToast(final String message, final int length) {
       final Activity a = UnityPlayer.currentActivity;
       a.runOnUiThread(new Runnable() {
        public void run() {
            Toast.makeText(a, message, length).show();
        }
    });
}

3. jar gradle jar unityproject/Assets/Plugins/Android

4. # script ( )

public void TestToast(){

 using(AndroidJavaObject jo =  new      AndroidJavaObject("com.mydomain.androidplugin.MYAndroidPlugin"))
        jo.Call ("makeToast", "my plugin worked", 2);
 }

5. ,

0

All Articles