Java Android插件无法与类“未找到”异常统一工作?

Java Android插件无法与类“未找到”异常统一工作?,java,c#,android,unity3d,Java,C#,Android,Unity3d,我尝试创建一个插件,但没有在unity中工作。我对Android studio不太了解,但观看了这段视频以创建一个Android插件。我在没有任何活动的情况下创建了一个新的Android项目。因此我编写了两个类UnityBinder.java和Gallery.java。它们如下所示 package com.techripples.unityplugin; import android.app.Activity; import android.content.Intent; import an

我尝试创建一个插件,但没有在unity中工作。我对Android studio不太了解,但观看了这段视频以创建一个Android插件。我在没有任何活动的情况下创建了一个新的Android项目。因此我编写了两个类UnityBinder.java和Gallery.java。它们如下所示

package com.techripples.unityplugin;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.net.Uri;
import android.database.*;
import com.unity3d.player.*;



public class Gallery extends Activity {


int PHOTO_GALLERY=1;

@Override
protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent,PHOTO_GALLERY);
}

//it will run when user picks a photo from the gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode==RESULT_OK && requestCode==PHOTO_GALLERY && data!=null)
    {

        Uri uri=data.getData();
        String[] fileColoumn={MediaStore.Images.Media.DATA};
        Cursor cursor=getContentResolver().query(uri,fileColoumn,null,null,null);
        cursor.moveToFirst();
        int coloumnIndex=cursor.getColumnIndex(fileColoumn[0]);
        String photoPath=cursor.getString(coloumnIndex);


        //Send path to unity to get photo
                                        //GamObject Name,method name,argument
        UnityPlayer.UnitySendMessage("Gallery","OnPhotoPick",photoPath);
        Gallery.this.finish();//close activity

    }
}
}
UnityBinder.java

package com.techripples.unityplugin;

import android.app.Activity;
import android.content.Intent;

public class UnityBinder {
public static void OpenGallery(Activity activity)
{

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

}

}
下面给出了Gallery.java类

package com.techripples.unityplugin;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.net.Uri;
import android.database.*;
import com.unity3d.player.*;



public class Gallery extends Activity {


int PHOTO_GALLERY=1;

@Override
protected void onCreate(Bundle bundle)
{
    super.onCreate(bundle);
    Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent,PHOTO_GALLERY);
}

//it will run when user picks a photo from the gallery
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode==RESULT_OK && requestCode==PHOTO_GALLERY && data!=null)
    {

        Uri uri=data.getData();
        String[] fileColoumn={MediaStore.Images.Media.DATA};
        Cursor cursor=getContentResolver().query(uri,fileColoumn,null,null,null);
        cursor.moveToFirst();
        int coloumnIndex=cursor.getColumnIndex(fileColoumn[0]);
        String photoPath=cursor.getString(coloumnIndex);


        //Send path to unity to get photo
                                        //GamObject Name,method name,argument
        UnityPlayer.UnitySendMessage("Gallery","OnPhotoPick",photoPath);
        Gallery.this.finish();//close activity

    }
}
}
接下来,我构建jar文件并将其复制到Unity中

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShowPicture : MonoBehaviour
{

Texture2D galleryImage;
bool isGalleryImageLoaded = false;

WWW www;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

void OnGUI()
{

    if(isGalleryImageLoaded)
    {
        GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), galleryImage);

    }
    if(GUI.Button(new Rect(50,50,100,100),"Open"))
    {

        isGalleryImageLoaded = false;
        AndroidJavaClass ajc = new AndroidJavaClass("com.unity.player.UnityPlayer");
        //While asking question used AndroidJavaClass instead of AndroidJavaObject
        AndroidJavaObject ajo = new AndroidJavaObject("com.techripples.unityplugin.UnityBinder");
        ajo.CallStatic("OpenGallery", ajc.GetStatic<AndroidJavaObject>("currentActivity"));
    }

    if(www!=null && www.isDone)
    {
        galleryImage = new Texture2D(www.texture.width, www.texture.height);
        galleryImage.SetPixels32(www.texture.GetPixels32());
        galleryImage.Apply();
        www = null;

        isGalleryImageLoaded = true;

    }

}


public void OnPhotoPick(string filePath)
{
    Debug.Log(filePath);

    www = new WWW("file://" + filePath);
}


 }
01-28 13:25:06.305 27032 27061 E Unity : java.lang.ClassNotFoundException: Didn't find class "com/unity/player/UnityPlayer" on path: DexPathList[[zip file "/data/app/com.testing.androidplugin-Aq1j3vpTxsomWMvd4kK7NQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.testing.androidplugin-Aq1j3vpTxsomWMvd4kK7NQ==/lib/arm, /data/app/com.testing.androidplugin-Aq1j3vpTxsomWMvd4kK7NQ==/base.apk!/lib/armeabi-v7a, /system/lib, /system/vendor/lib]]
插件的视频完成了。我在这里做错了什么。

我不知道调用是如何进行的,或者我应该在清单中写些什么才能运行活动。

如果打开UnityPlayerPractivity,可以看到它的包名是“com.unity3d.player”,类名是“UnityPlayer”

因此,您应该将C#脚本中的unity player类名更改为“com.unity3d.player.UnityPlayer”


另一个解决方案是添加到您的android插件Unity类中,并从UnityPlayerPractivity扩展您的活动,您可以直接从插件调用您的方法

现在另一个类问题出现了java.lang.ClassNotFoundException:找不到类com/techripples/unityplugin/UnityBinder您应该使用AndroidJavaObject,而不是类,正如在视频中一样,我使用了它,但仍然得到了相同的结果,并使用了rebuild
AndroidJavaObject ajo=newandroidjavaobject(“com.techripples.unityplugin.UnityBinder”);ajo.CallStatic(“OpenGallery”,ajc.GetStatic(“currentActivity”)
AndroidJavaException:java.lang.ClassNotFoundException:在路径:DexPathList[[zip文件”/data/app/com.testing.androidplugin-UeK55atMMYver0KFY27PYQ==/base.apk]上未找到类“com/techripples/unityplugin/UnityBinder”
用7zip打开jar,查看从com到类的类似类的路径。您应该只从lib文件夹中删除类