Unity3d and Android call each other and step on the pit summary

Source: https://blog.csdn.net/kongxingxing/article/details/81119831

1. Introduce the classes.jar package of unity. I will install unity-5.6.1 in D: \ program files \ unity-561 \ unity \ editor \ data \ playbackengines \ androidplayer \ variations \ mono \ release \ classes \ classes.jar

If you directly import classes.jar into the androidstudio project and compile AAR, unity compilation will report an error:

IOException: Failed to Move File / Directory from 'Temp/StagingArea\android-libraries\app-debug\classes.jar' to 'Temp/StagingArea\android-libraries\app-debug\libs\classes.jar'. UnityEditor.Android.PostProcessor.Tasks.ProcessAAR.Execute (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context) UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry) UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args) UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTargetGroup targetGroup, BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.buildreporting.buildreport Report) (at C: / buildslave / unity / build / Editor / mono / buildpipeline / postprocessbuildplayer. CS: 186) unityeditor. Hostview: ongui () 1 2 3 4 5 6 7 after renaming classes.jar unity-classes.jar, the following error is reported:

CommandInvokationFailure: Unable to convert classes into dex format. D:/Program Files/Java/jdk1.8.0_ 181\bin\java.exe -Xmx2048M -Dcom.android.sdkmanager.toolsdir="D:/AndroidSDK\tools" -Dfile.encoding=UTF8 -jar "D:\Program Files\unity-561\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 transla tion 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 t ranslation 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;

UNEXPECTED TOP-LEVEL EXCEPTION: java.lang.RuntimeException: Translation has been interrupted at com.android.dx.command.dexer.Main.processAllFiles(Main.java:614) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:310) at com.android.dx.command.dexer.Main.runDx(Main.java:288) at com.android.dx.command.dexer.Main.main(Main.java:244) at com.android.dx.command.Main.main(Main.java:95) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at SDKMain.main(SDKMain.java:130) Caused by: java.lang.InterruptedException: Too many errors at com.android.dx.command.dexer.Main.processAllFiles(Main.java:606) ... 9 more ] stdout [ processing archive D:\UnityWorkSpace\New Unity Project\Temp\StagingArea\android-libraries\app-debug\libs\.\classes.jar... processing com/example/cyk/myapplication1/BuildConfig.class... processing com/example/cyk/myapplication1/UnityActivity.class... processing com/example/cyk/myapplication1/UnityActivity$1.class... processing archive D:\UnityWor kSpace\New Unity Project\Temp\StagingArea\android-libraries\app-debug\libs\.\unity-classes.jar... processing bitter/jnibridge/JNIBridge.class... processing bitter/jnibridge/JNIBridge$a.class... processing com/unity3d/player/NativeLoader.class... processing com/unity3d/player/ReflectionHelper.class... processing com/unity3d/player/ReflectionHelper$ 1.class... processing com/unity3d/player/ReflectionHelper$a.class... processing com/unity3d/player/UnityPlayer.class... processing com/unity3d/player/UnityPlayer$1.class... processing com/unity3d/player/UnityPlayer$10.class... processing com/unity3d/player/UnityPlayer$1$1.class... processing com/unity3d/player/UnityPlayer$11.class... processing co m/unity3d/player/UnityPlayer$12.class... processing com/unity3d/player/UnityPlayer$12$1.class... processing com/unity3d/player/UnityPlayer$13.class... processing com/unity3d/player/UnityPlayer$14.class... processing com/unity3d/player/UnityPlayer$15.class... processing com/unity3d/player/UnityPlayer$16.class... processing com/unity3d/player/UnityP layer$17.class... processing com/unity3d/player/UnityPlayer$18.class... processing com/unity3d/player/UnityPlayer$19.class... processing com/unity3d/player/UnityPlayer$2.class... processing com/unity3d/player/UnityPlayer$20.class... processing com/unity3d/player/UnityPlayer$21.class... processing com/unity3d/player/UnityPlayer$3.class... processin g com/unity3d/player/UnityPlayer$4.class... processing com/unity3d/player/UnityPlayer$5.class... processing com/unity3d/player/UnityPlayer$6.class... processing com/unity3d/player/UnityPlayer$7.class... processing com/unity3d/player/UnityPlayer$8.class... processing com/unity3d/player/UnityPlayer$9.class... processing com/unity3d/player/UnityPlaye r$a.class... processing com/unity3d/player/UnityPlayer$b.class... processing com/unity3d/player/UnityPlayer$c.class... processing com/unity3d/player/UnityPlayer$c$1.class... processing com/unity3d/player/UnityPlayer$d.class... processing com/unity3d/player/UnityPlayerActivity.class... processing com/unity3d/player/UnityPlayerNativeActivity.class.. . processing com/unity3d/player/UnityPlayerProxyActivity.class... processing com/unity3d/player/UnityWebRequest.class... processing com/unity3d/player/WWW.class... processing com/unity3d/player/a.class... processing com/unity3d/player/a$a.class... processing com/unity3d/player/b.class... processing com/unity3d/player/b$1.class... processing com/un ity3d/player/b$2.class... processing com/unity3d/player/b$3.class... processing com/unity3d/player/b$4.class... processing com/unity3d/player/c.class... processing com/unity3d/player/d.class... processing com/unity3d/player/e.class... processing com/unity3d/player/f.class... processing com/unity3d/player/f$1.class... processing com/unity3d/player/ g.class... processing com/unity3d/player/h.class... processing com/unity3d/player/h$1.class... processing com/unity3d/player/h$2.class... processing com/unity3d/player/h$3.class... processing com/unity3d/player/i.class... processing com/unity3d/player/i$1.class... processing com/unity3d/player/i$a.class... processing com/unity3d/player/j.class... processing com/unity3d/player/k.class... processing com/unity3d/player/l.class... processing com/unity3d/player/l$a.class... processing com/unity3d/player/m.class... processing com/unity3d/player/m$a.class... processing com/unity3d/player/m$b.class... processing org/fmod/FMODAudioDevice.class... processing org/fmod/a.class... processing bin\classe s\.\com\example\cyk\myapplication1\R.class... processing bin\classes\.\com\example\cyk\myapplication1\R$attr.class... processing bin\classes\.\com\example\cyk\myapplication1\R$drawable.class... processing bin\classes\.\com\example\cyk\myapplication1\R$id.class... processing bin\classes\.\com\example\cyk\myapplication1\R$layout.class... processing bin\classes\.\com\example\cyk\myapplication1\R$mipmap.class... processing bin\classes\.\com\example\cyk\myapplication1\R$string.class... processing bin\classes\.\com\example\cyk\myapplication1\R$style.class... processing archive bin\classes.jar... processing bitter/jnibridge/JNIBridge.class... processing bitter/jnibridge/JNIBridge$a.class... proce ssing com/unity3d/player/NativeLoader.class... processing com/unity3d/player/ReflectionHelper.class... processing com/unity3d/player/ReflectionHelper$1.class... processing com/unity3d/player/ReflectionHelper$a.class... processing com/unity3d/player/UnityPlayer.class... processing com/unity3d/player/UnityPlayer$1.class... processing com/unity3d/pla yer/UnityPlayer$10.class... processing com/unity3d/player/UnityPlayer$1$1.class... processing com/unity3d/player/UnityPlayer$11.class... processing com/unity3d/player/UnityPlayer$12.class... processing com/unity3d/player/UnityPlayer$12$1.class... processing com/unity3d/player/UnityPlayer$13.class... processing com/unity3d/player/UnityPlayer$14.cla ss... processing com/unity3d/player/UnityPlayer$15.class... processing com/unity3d/player/UnityPlayer$16.class... processing com/unity3d/player/UnityPlayer$17.class... processing com/unity3d/player/UnityPlayer$18.class... processing com/unity3d/player/UnityPlayer$19.class... processing com/unity3d/player/UnityPlayer$2.class... processing com/unity 3d/player/UnityPlayer$20.class... processing com/unity3d/player/UnityPlayer$21.class... processing com/unity3d/player/UnityPlayer$3.class... processing com/unity3d/player/UnityPlayer$4.class... processing com/unity3d/player/UnityPlayer$5.class... processing com/unity3d/player/UnityPlayer$6.class... processing com/unity3d/player/UnityPlayer$7.class ... processing com/unity3d/player/UnityPlayer$8.class... processing com/unity3d/player/UnityPlayer$9.class... processing com/unity3d/player/UnityPlayer$a.class... processing com/unity3d/player/UnityPlayer$b.class... processing com/unity3d/player/UnityPlayer$c.class... processing com/unity3d/player/UnityPlayer$c$1.class... processing com/unity3d/pl ayer/UnityPlayer$d.class... processing com/unity3d/player/UnityPlayerActivity.class... processing com/unity3d/player/UnityPlayerNativeActivity.class... processing com/unity3d/player/UnityPlayerProxyActivity.class... processing com/unity3d/player/UnityWebRequest.class... processing com/unity3d/player/WWW.class... processing com/unity3d/player/a.cla ss... processing com/unity3d/player/a$a.class... processing com/unity3d/player/b.class... processing com/unity3d/player/b$1.class... processing com/unity3d/player/b$2.class... processing com/unity3d/player/b$3.class... processing com/unity3d/player/b$4.class... processing com/unity3d/player/c.class... processing com/unity3d/player/d.class... proce ssing com/unity3d/player/e.class... processing com/unity3d/player/f.class... processing com/unity3d/player/f$1.class... processing com/unity3d/player/g.class... processing com/unity3d/player/h.class... processing com/unity3d/player/h$1.class... processing com/unity3d/player/h$2.class... processing com/unity3d/player/h$3.class... processing com/uni ty3d/player/i.class... processing com/unity3d/player/i$1.class... processing com/unity3d/player/i$a.class... processing com/unity3d/player/j.class... processing com/unity3d/player/k.class... processing com/unity3d/player/l.class... processing com/unity3d/player/l$a.class... processing com/unity3d/player/m.class... processing com/unity3d/player/m$a .class... processing com/unity3d/player/m$b.class... processing org/fmod/FMODAudioDevice.class... processing org/fmod/a.class... ] exit code: 2 UnityEditor.Android.Command.Run (System.Diagnostics.ProcessStartInfo psi, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.Android.AndroidSDKTools.RunCommandInternal (System.String javaExe, System.String sdkToolsDir, System.String[] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.Android.AndroidSDKTools.RunCommandSafe (System.String javaExe, System.String sdkToolsDir, System.String[] sdkToolCommand, Int32 memoryMB, System.String workingdir, UnityEditor.Android.WaitingForProcessToExit waitingForProcessToExit, System.String errorMsg) UnityEditor.HostView:OnGUI() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 134 135 136 137 138 140 141 142 144 146 147 148 149 150 151 152 154 155 156 157 158 159 160 161 162 163 164 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 184 185 186 187 188 189 190 solution: change the LIBS file name, such as unit Ylibs, classes.jar can be used without changing the name or taking any name, as shown in the figure

2. Androidmanifest.xml configuration

<? xml version="1.0" encoding="utf-8"?> < manifest xmlns:android=" http://schemas.android.com/apk/res/android " package="com.example.cyk.myapplication1">

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@android:style/Theme.NoTitleBar"> <activity android:name=".UnityActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!-- Fixed writing -- > < meta data Android: name = "unityplayer. Unityactivity" Android: value = "true" / > < / activity > < / Application >

< / manifest > 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 3. Android activity settings

package com.example.cyk.myapplication1;

import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Toast;

import com.unity3d.player.UnityPlayer; import com.unity3d.player.UnityPlayerActivity;

import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale;

public class UnityActivity extends UnityPlayerActivity {

private LinearLayout mLlUnityContainer; private Button mBtnRotate;

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

private void initView() { mLlUnityContainer = (LinearLayout) findViewById(R.id.ll_unity_container); mBtnRotate = (Button) findViewById(R.id.btn_rotate);

//Add unity's view to the layout. View scene = munityplayer. Getview(); mLlUnityContainer.addView(scene);

Mbtnrotate. Setonclicklistener (new view. Onclicklistener() {@ override public void onclick (view view) {/ / send a message to the rotatecube method in the script on the cube object of unity. / / the third parameter is the passed message unityplayer. Unitysendmessage ("Cube", "rotatecube", "80");}});}

Invalid Sign

5. Create c#script

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

public class RotateTest : MonoBehavIoUr {

// Use this for initialization void Start() { Debug.Log("RotateTest Start"); }

// Update is called once per frame void Update() { //Debug.Log("RotateTest Update"); }

Invalid Sign

Public void unitybtnclick() {debug. Log ("rotatetest unitybtnclick"); / / obtain Java class (fixed writing method, good pit, don't think it's your own Android package name) androidjavaclass JC = new androidjavaclass ("com. Unity3d. Player. Unityplayer");

//Get the current activity (fixed writing method, do not think it is the activity name defined by yourself...) androidjavaobject Jo = JC. Getstatic < androidjavaobject > ("currentactivity");

//Call the method in the activity, and pass in the method name and parameter Jo. Call ("getnowtime", "get Android time");}

}Copyright notice: This is the original article of CSDN blogger "Dexing", which follows the CC 4.0 by-sa copyright agreement. Please attach the source link of the original text and this notice for reprint. Original link: https://blog.csdn.net/kongxingxing/article/details/81119831

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>