java.lang.NoClassDefFoundError Android Studio Unity [英] java.lang.NoClassDefFoundError Android Studio Unity

查看:161
本文介绍了java.lang.NoClassDefFoundError Android Studio Unity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Android Studio中生成包含另一个库的库时遇到了麻烦.首先,我正在使用Android Studio 2.0和Unity 5.3.4f1.我想做的是将FFMpeg集成到Unity中,因此我决定创建自己的库,将FFMpeg库用作中介. FFMpeg库位于此处.

I'm having some troubles when I try to generate a library that contains another one in Android Studio. First of all I am using Android Studio 2.0 and Unity 5.3.4f1. What I want to do is integrate FFMpeg in Unity so I decided to create my own library that use FFMpeg library as an intermediary. The FFMpeg library is here.

首先,我在Android Studio中创建了一个新的空项目,并在其中创建了一个新模块,该模块将是我的库,使用 New-> Module-> Android Library 并将其命名为"ffmpegcodec".之后,我在新模块文件夹中打开 build.gradle 文件并粘贴:

To start I created a new empty project in Android Studio and inside of it, created a new module, which will be my library, using New->Module->Android Library and named it "ffmpegcodec". After that I opened build.gradle file inside the new module folder and paste:

compile 'com.writingminds:FFmpegAndroid:0.3.2'

在相关性括号内,然后单击 Sync (未显示任何错误).

inside dependencies brackets, and click Sync (It did not show any error).

之后,在 src/main/java/package-name/中创建一个新的Java类,并将其命名为 FFMpegCodec .在此代码中粘贴以下代码:

After that create a new Java Class inside src/main/java/package-name/ and named it FFMpegCodec. Inside this paste this code:

public class FFMpegCodec {
private static FFmpeg ffmpeg;
private static Context context;
private static FFMpegCodec INSTANCE = null;

public FFMpegCodec(){
    INSTANCE = this;
}

public static FFMpegCodec instance(){
    if(INSTANCE == null){
        INSTANCE = new FFMpegCodec();
    }
    return INSTANCE;
}

public void setContext(Context ctx){
    this.context = ctx;
}

public void loadFFMpegBinary() {
    try {
        if (ffmpeg == null) {

            ffmpeg = FFmpeg.getInstance(context);
        }
        ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
            @Override
            public void onFailure() {
                Log.e("FFMPEG", "ffmpeg : NOT correct Loaded");
            }

            @Override
            public void onSuccess() {
                Log.e("FFMPEG", "ffmpeg : correct Loaded");
            }
        });
    } catch (FFmpegNotSupportedException e) {

    } catch (Exception e) {

    }
}

public void execFFmpegCommand(final String[] command) {
    try {
        ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
            @Override
            public void onFailure(String s) {
                Log.e("FFMPEG", "FAILED with output : " + s);
            }

            @Override
            public void onSuccess(String s) {
                Log.e("FFMPEG", "SUCCESS with output : " + s);
            }

            @Override
            public void onProgress(String s) {
                Log.e("FFMPEG", "Started command : ffmpeg " + command);
                Log.e("FFMPEG", "progress : " + s);
            }

            @Override
            public void onStart() {
                Log.e("FFMPEG", "Started command : ffmpeg " + command);

            }

            @Override
            public void onFinish() {
                Log.e("FFMPEG", "Finished command : ffmpeg " + command);

            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        // do nothing for now
    }
}

它基本上会生成FFmpeg的实例并调用ffmpeg命令.

It basically generates an instance of FFmpeg and makes calls of ffmpeg commands.

一旦我完成了我的库,我决定在我的项目中对其进行测试,因此我可以使用以下方法将我的库包含在我的应用程序Gradle中:

Once I have my lib done I decided to test it in my project so I include my lib in my app graddle using:

compile project(':ffmpegcodec')

之后,我只需将此代码粘贴到我的MainActivity中:

After that I just paste this code to my MainActivity:

public class MainActivity extends AppCompatActivity {

FFMpegCodec ffmpeg;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button execCommandButton = (Button) findViewById(R.id.execCommandButton);

    ffmpeg = FFMpegCodec.instance();


    execCommandButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "This is a Toast!!", Toast.LENGTH_SHORT).show();
            ffmpeg.setContext(MainActivity.this);
            ffmpeg.loadFFMpegBinary();
            String []cmd = new String[1];
            cmd[0] = "-version";
            ffmpeg.execFFmpegCommand(cmd);
        }
    });

}

在那之后,我运行我的项目,当我按下按钮时,它返回一切正常,显示ffmpeg版本. 在检查完我的库是否可用之后,我决定将其移至Unity,因此将 ffmpegcodec-realese.arr 文件复制到 ffmpegcodec/build/outputs/aar 文件夹中,并将其粘贴进入我的Unity项目.然后,我编写了一个C#脚本来使用包含以下内容的lib:

After that, I run my project and when I press the button it returns that everythings its ok, showing ffmpeg version. Once I have check that my lib works I decide to move it to Unity so copy the ffmpegcodec-realese.arr file inside ffmpegcodec/build/outputs/aar folder and paste it into my Unity project. Then I wrote an C# script to use my lib that contains this:

    using System;
    using System.Runtime.InteropServices;
    using UnityEngine;
    using System.Collections;
    using System.Diagnostics;

    public class ScreenRecorder {

    private AndroidJavaObject unityActivity = null;
    private AndroidJavaObject captureObject = null;

        // Use this for initialization
        public ScreenRecorder () {
            try{

                using (AndroidJavaClass unityPlayerActivityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
                    unityActivity = unityPlayerActivityClass.GetStatic<AndroidJavaObject>("currentActivity");
                }

                AndroidJavaClass captureClass = new AndroidJavaClass ("com.promineostudios.ffmpegcodec.FFMpegCodec");
                if (captureClass != null) {
                    captureObject = captureClass.CallStatic<AndroidJavaObject>("instance");
                    captureObject.Call("setContext", unityActivity);
                    captureObject.Call("loadFFMpegBinary");
                }
            }catch(Exception ex){
                UnityEngine.Debug.Log(ex);
            }
        }

    }

问题来了.当我使用以下方法创建类实例时:

The problems comes here. When I create a class instance using:

AndroidJavaClass captureClass = new AndroidJavaClass ("com.promineostudios.ffmpegcodec.FFMpegCodec");

它可以正确创建一个实例,也可以在我使用以下对象创建对象时创建:

It creates an instance correctly, and also when I create an object using:

captureObject = captureClass.CallStatic<AndroidJavaObject>("instance");

但是,当我尝试访问"setContext"中的FFMpeg库方法时,它将失败并返回以下内容:

But when I try to get access to FFMpeg library methods like in "setContext" it fails and returns this:

UnityEngine.AndroidJavaException: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/github/hiteshsondhi88/libffmpeg/FFmpeg;
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/github/hiteshsondhi88/libffmpeg/FFmpeg;
  at com.promineostudios.ffmpegcodec.FFMpegCodec.loadFFMpegBinary(FFMpegAndroidCodec.java:39)
  at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
  at com.unity3d.player.UnityPlayer.a(Unknown Source)
  at com.unity3d.player.UnityPlayer$b.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.github.hiteshsondhi88.libffmpeg.FFmpeg" on path: DexPathList[[zip file "/data/app/com.promineostudios.ffmpegmodule-1/base.apk"],nativeLibraryDirectories=[/data/app/com.promineostudios.ffmpegmodule-1/lib/arm, /vendor/lib, /system/lib]]
  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
  at com.promineostudios.ffmpegcodec.FFMpegCodec.loadFFMpegBinary(FFMpegAndroidCodec.java:39) 
  at com.unity3d.player.UnityPlayer.nativeRender(Native Method) 
  at com.unity3d.player.UnityPlayer.a(Unknown Source) 
  at com.unity3d.player.UnityPlayer$b.run(Unknown Source)

我认为问题出在我将lib导出到Unity时,但我不知道出了什么问题.如果有人可以帮助我,我将不胜感激.

I think that the problem is in when I export my lib into Unity but I do not know what is going wrong. If anybody could help me I will be really appreciated.

谢谢,请原谅我的英语.

Thanks and excuse me for my English.

推荐答案

我知道您不再从事Nestoraj这个项目,但是我已经找到了解决方案:

I know you're no longer working on this project Nestoraj, however I have figured out a solution:

  1. 您遇到的错误是由UnityPlayerActivity找不到com/github/hiteshsondhi88/libffmpeg/FFmpeg的源引起的,因此为了修复该问题,您必须放入'com.writingminds:FFmpegAndroid:0.3. Unity项目的Assets/Plugins/Android文件夹中的2'jar文件

  1. The error you're getting is caused by the UnityPlayerActivity not finding the source for com/github/hiteshsondhi88/libffmpeg/FFmpeg so in order to fix that you must put the 'com.writingminds:FFmpegAndroid:0.3.2' jar file inside your Unity project's Assets/Plugins/Android folder

执行完该项目并运行项目后,仍然会在加载ffmpeg二进制文件时出错,这是因为它也没有正确打包在Unity中,要解决此问题,您需要将项目导出到Gradle/Android Studio,然后在src/main/jniLibs目录中,根据每种架构,您必须放置ffmpeg .so二进制文件,因此jniLibs/armeabi-v7a/libARM_ARCH.so jniLibs/armeabi/libARM_ARCH.so等.然后您还必须将ffmpeg对象文件放置在src/main/assets内,并根据每种架构再次设置,例如src/main/assets/armeabi-v7a/ffmpeg等.

After doing that and running the project, you're still going to get an error loading the ffmpeg binary file, this is because it's also not properly packaged in Unity, to fix this you need to export your project to Gradle/Android Studio, and then inside your src/main/jniLibs directory you have to place the ffmpeg .so binary files, according to each architecture so jniLibs/armeabi-v7a/libARM_ARCH.so jniLibs/armeabi/libARM_ARCH.so etc. Then you must also place the ffmpeg object files inside src/main/assets and again according to each architecture like so : src/main/assets/armeabi-v7a/ffmpeg etc.

完成这两个步骤后,您应该能够在Android Studio中运行项目并将ffmpeg二进制文件加载到Unity项目中

Once those 2 steps are done you should be able to run your project in Android Studio and load the ffmpeg binary inside your Unity project

这篇关于java.lang.NoClassDefFoundError Android Studio Unity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆