Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting and running Unity3D project to Android Studio

I want to get logs from Unity Application when its running on Android phone. For this I have to export Android project from Unity3D and run it using Android Studio. I am doing following steps:

  1. Export Project from Unity3D by selecting Google Android Project options from player settings.
  2. Open Android Studio and Import the autogenerated project from location drive.
  3. Select Build -> Make Project.
  4. Select Run -> Run 'ModuleName'.

I am getting following errors in Gradle console:

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ': 'ModuleName':transformClassesWithDexForDebug'. java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) Exception in thread "main"

    com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_79\bin\java.exe'' finished with non-zero exit value 1

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 0.788 secs

java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482) Exception in thread "main"

I have been working with Eclipse long ago but never faced this before. Can anyone guide me how to successfully run a Unity-Android project in Android Studio?

like image 790
Umair M Avatar asked Aug 16 '16 17:08

Umair M


People also ask

Can I open Unity project in Android Studio?

You can download Android Studio from the Android Studio website. Once you have downloaded Android Studio, you need to add the Android SDK path to Unity. See Unity documentation on Android SDK/NDK setup for steps on how to do this. Once you have your Android developer environment set up, you can open your Unity Project.

Can I run Unity engine on Android?

Steps to create a Unity game for AndroidDownload and install the Unity Hub. Start the Unity Hub. On the Installs tab, add a version of the Unity Editor that supports 64-bit apps. Note that these versions support Android App Bundles, which enable smaller, more optimized downloads.


Video Answer


2 Answers

Exporting and running Unity3D project to Android Studio is not really hard. Make sure the you have the latest version of Unity(5.4) and Android Studio(2.1.3). This steps were made with the version mentioned above.

EXPORTING TO Android Studio:

1.Create a root folder that will hold the exported folders and files. Let's call it AndroidTestProject in this case and the full Directory will be C:/AndroidTestProject.

2.Exporting to C:/AndroidTestProject.

A.File->Build Settings...

B.Select Android on the left then tick Google Android Project checkbox.

Image Description: enter image description here

C.Click the Export Button.

D.Select C:/AndroidTestProject then click the Select Folder Button.

Image Description:

enter image description here

Unity will build your project.

After Generating the project, a new folder will be created in the C:/AndroidTestProject directory. That folder will be named whatever your Product Name is in the Player Settings. This what my product name is in the Player Settings:

Image Description: enter image description here

So in my case, the new Folder name will be called test and will be located at C:/AndroidTestProject/test.

IMPORTING INTO Android Studio:

1.Create a new Folder called AndroidStudio in the C:/AndroidTestProject/ directory. The full path of that folder should be C:/AndroidTestProject/AndroidStudio.

A.Open Android Studio. When you open Android Studio, click on Import project (Eclipse ADT,Gradle, etc.)

If Import project (Eclipse ADT,Gradle, etc.) is not displayed, close the current project by going to File->Close Project.

Image Description: enter image description here

B.Go to the full directory of the exported project C:/AndroidTestProject/test then select the test folder which is the Product Name of your project then click Ok.

Image Description: enter image description here

C.A new Window will pop up. Set the Import Destination Directory to be C:/AndroidTestProject/AndroidStudio which was created in step 1.

Image Description: enter image description here

D.Click the Next Button. Tick/Select the follwing check boxes:

  • Replace jars with dependencies, when possible.
  • Replace library sources with dependencies, when possible.
  • Create Gradle-style (CamelCase) module names.

then click the Finish Button.

Image Description: enter image description here

2.Wait for Android Studio to load your project.

You will get an error that looks like this:

Error:Minimum supported Gradle version is 2.14.1. Current version is 2.10. If using the gradle wrapper, try editing the distributionUrl in C:\AndroidTestProject\AndroidStudio\gradle\wrapper\gradle-wrapper.properties to gradle-2.14.1-all.zip Fix Gradle wrapper and re-import project
Gradle settings

A.Simply click on the Fix Gradle wrapper and re-import project Gradle settings message. If you don't see this message, make sure that you are in the Messages Tab. Look at the image below for more information.

Image Description:

enter image description here

B.Wait for Android Studio to finish fixing the Gradle problem.

When finished, go to Build->Make Project.

After project is done building, go to Run->Run 'app'.

C.A new Window will pop up. You can either chose the device to run the app on, create an emulator or chose already existing emulator to run the app. In this example we will use a real device to test.

Under Connected Devices, select your device then click the OK button.

Image Description:

enter image description here

Extra:

A.If you want to be able to debug your App, you have to enable that from Unity. Go back to EXPORTING TO Android Studio step 2B and enable Development Build.Also select Script Debugging then click the Export Button.

B.If you want to see Debug.Log messages, go to the Android Monitor Tab in the Android Studio.

The circled items in the image below are important to know when Debugging in Android Studio.

Image Description:

enter image description here

This is not really hard. If you do it once, you won't need this instruction again.

like image 138
Programmer Avatar answered Oct 09 '22 21:10

Programmer


"I want to get logs from Unity Application when its running on Android phone".

You dont need to export to android project , just enable "development build" in build settings then connect your phone to your computer and enable "development mode" and "script debugging" on your phone.

then simply move to path your android sdk is installed , cd to platform-tools , run these commands from command prompt or terminal

adb logcat 

if you want to just see unity logs run

adb logcat -s Unity
like image 20
AminSojoudi Avatar answered Oct 09 '22 23:10

AminSojoudi