我創建了一個Android Unity插件(.aar文件),它爲我的Unity遊戲提供了一些自定義的定位數據。在我的統一的腳本,我使用,Unity錯誤:無法將類轉換爲dex格式
var x = customClass.CallStatic<float>("getHeadX");
爲了得到一些位置數據。這種方法被稱爲每幀獲取更新的數據(輪詢方法),這使得它效率低下。相反,當更新的數據準備就緒時,我決定從我的java代碼(插件端)調用Unity腳本中的C#方法。要做到這一點,在我的Java插件,我寫的,
import com.unity3d.player.UnityPlayer;
...
UnityPlayer.UnitySendMessage("Manager", // gameObject name
"PluginCallback", // this is a callback in C#
"Hello from android plugin"); // msg which is not needed actually
然而,編譯器抱怨包com.unity3d.player.UnityPlayer不存在。所以我複製classes.jar文件從
C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\Variations\mono\Release\Classes \classes.jar
到我的Android插件的項目的「庫」文件夾。我成功構建它,並將生成的.aar文件(mylibrary-release.aar)複製到Unity項目的Assets \ Plugins \ Android文件夾中。
當我建立統一的項目(使用「內部」構建系統),它給了我這個錯誤:
IOException: Failed to Move File/Directory from 'Temp/StagingArea\android-libraries\mylibrary-release\classes.jar' to 'Temp/StagingArea\android-libraries\mylibrary-release\libs\classes.jar'. UnityEditor.Android.PostProcessor.Tasks.ProcessAAR.Execute ...
發生此錯誤是因爲classes.jar依賴與classes.jar名稱衝突(發通過我的插件統一)。所以我改變了依賴名字unity_classes.jar,這解決了這個問題,但我的構建統一的應用程序的時候,現在我得到一個新的錯誤:
CommandInvokationFailure: Unable to convert classes into dex format. C:/Program Files/Java/jdk1.8.0_102\bin\java.exe -Xmx2048M Dcom.android.sdkmanager.toolsdir="C:/Users/kamran.shamloo/AppData/Local/Android/Sdk\tools" -Dfile.encoding=UTF8 -jar "C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\AndroidPlayer/Tools\sdktools.jar"
stderr[ Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lbitter/jnibridge/JNIBridge$a; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/NativeLoader; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper$1; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/ReflectionHelper$a; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$1; Uncaught translation error: java.lang.IllegalArgumentException: already added: Lcom/unity3d/player/UnityPlayer$10; ...
謝謝,它對我有用(Y) –