28

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?

0

2 Answers 2

56

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.

6
  • Thanks! I was skipping checkboxes in part :D
    – Umair M
    Commented Aug 18, 2016 at 9:24
  • I knew you were missing something. Just didn't know which part so I posted all part. Good luck with Android Studio.
    – Programmer
    Commented Aug 18, 2016 at 13:55
  • What do you do if step C doesn't show up? (Import Destination Directory) It just prompted me if I wanted to use the Gradle wrapper or configure it manually. I selected "Gradle wrapper" but it didn't give me a change to pick the destination folder, so I guess it just uses the Unity exported project folder as the Android Studio project folder? Commented Mar 2, 2017 at 19:01
  • @bigp There are multiple C's here. Which category of C? Also please bear in mind that this post is old and I plan to update it to work with Gradle.
    – Programmer
    Commented Mar 2, 2017 at 20:09
  • Ah I just realized after I re-read it there was several 'C's. I was pointing out the one where it's followed by C.A new Window will pop up. Set the Import Destination Directory to be. Commented Mar 3, 2017 at 0:16
3

"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
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.