Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
November 27, 2014
arrowPress Releases
November 27, 2014
PR Newswire
View All
View All     Submit Event






If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 
Bug Hunting: Unity throws an ArgumentException when building for Android
by Lior Tal on 02/26/14 03:33:00 pm   Featured Blogs

9 comments Share on Twitter Share on Facebook    RSS

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

This post was originally posted on my blog Random Bits: http://www.tallior.com

Prologue

A complaint about a mysterious ArgumentException when building an Android project in Unity led me to this debugging session. Hopefully some of these techniques will be useful to other developers.

When using a tool such as Unity, a simple click of a “Build” button often hides a long and complex process involving different modules and tools. When this process fails, it may be hard to determine the exact reason. One approach would be to try looking for what you did wrong (what did I install? did I pass the wrong parameters somewhere?).

A different approach – the one we’ll explore today,  is to dig deeper under the surface to look for the cause of error.

Reproduction

The first stop when attempting to fix an issue is to reproduce it at will. Since the original issue was reported by another user, I started by attempting to get the same exception on my machine: I launched Unity, and tried to build an empty project for Android.

Lucky for me, the stars aligned, and I got the exception on the first attempt, without doing anything special. Many times, reproducing another user’s problem won’t be that easy, and in that case these steps should be performed directly on the machine that demonstrated the error.

How did I get here? (Getting context)

As a developer, you are in control of what errors are shown to your users. This means that internal errors and exceptions may be caught internally (“swallowed”) and silenced or replaced by some other, more friendly message.

In our case, the only piece of information at hand is this message displayed in the Unity console:

Error

Unfortunately, this doesn’t reveal any information as to where this happened, nor what Unity attempted to do at that time.

Getting a proper context is important for understanding what went wrong.

Since this exception looks like it comes from internal Unity code written in C# (Mono), we can use the debugger and break execution when this specific exception occurs.

Breaking on a specific exception

This is a very useful debugger feature that allows breaking execution when particular exception types are thrown (similar to setting a breakpoint).

We need to configure the debugger to break on ArgumentException. This is done by launching MonoDevelop and running these steps:

  1. Attach to the running Unity process (Run –> Attach to Process)
  2. Open the Exceptions menu (Run –> Exceptions)
  3. Add System.ArgumentException to Stop in exceptions:

Configuring Stop in exceptions in MonoDevelop

After hitting OK, the debugger is properly configured, and indeed when repeating the build in Unity, the debugger breaks exactly as the ArgumentException is thrown,  and we can examine the stack trace:

Stack trace

At this point, we have the first piece of information we need – the exception is thrown after calling Path.Combine with some weird first argument (see path1).

Going in reverse

With the stack trace in hand, we must dig a bit deeper to understand how unity got that weird looking path that was used for the call to Path.Combine.

Decompiling

Using a C# decompiler (Reflector, dotPeek), we can peek at the code in the UnityEditor.Android.dll assembly (located under the Unity installation folder).

Looking at the method at the top of the stack trace, we can see the call to Path.Combine:

reflector_path_combine

Since the first argument to Path.Combine is the interesting one, we’d like to know how does SDKBuildToolsDir receive its value. This is pretty easy using the decompiler, and we can see that this is how it gets its value:

reflector_sdkbuildtoolsdir

It appears that Unity is running some external command whose output is captured and is assigned to SDKBulidToolsDir. We can now attempt to see how the code assembles the command line and invokes the tool, but there’s a better and easier option.

Sniffing for processes

Process Monitor (procmon) is a nifty little tool that acts as a “sniffer” for various real-time machine wide activities (process information, networking, registry access, file access). It is particularly useful for figuring out what processes were invoked (by Unity), and what were their arguments.

Running procmon and then starting a new build gives us a nice capture of the data we need. After the build fails we can stop procmon from capturing (CTRL-E) to keep the captured trace clean and focused on the issue at hand. The trace will probably contain information from many running processes, but we can filter it to show events originating from Unity.exe. This is done by right-clicking a trace line from Unity.exe and selecting “Include Unity.exe”):

Filter by process in Procmon

We should further filter the results for showing only process/thread activities (from the toolbar), as seen in this image:

Viewing only process/thread activity

This gets us only Unity.exe  traces of process and thread events. From this point it’s pretty straightforward to find that Unity creates a new Java process with the following details (paths may vary on your machine):

“C:\Program Files\Java\jdk1.7.0_17\bin\java.exe” -Xmx1024M -Dcom.android.sdkmanager.toolsdir=“D:/Android/sdk\tools” -Dfile.encoding=UTF8 -jar “D:/Unity/Editor/Data/BuildTargetTools/AndroidPlayer\sdktools.jar” -

Running this exact command from a command prompt generates this output:

picked_up

Eureka!

Putting it all together, from all we’ve learned it looks like Unity is using a custom Java based tool, captures its output in a variable and uses that information as part of the build process. In cases where _JAVA_OPTIONS environment variable is defined, Java will print out the options used to the console (this will happen also when invoking java with no arguments), however Unity’s build process does not deal with this scenario very well.

The issue was reported and acknowledged by Unity in this Bug Report

Takeaways

  1. Reproduce the issue before attempting to fix it
  2. Determine a context for the failure (e.g: stack trace)
  3. Reverse engineering can be your friend

Tools Used


Related Jobs

InnoGames GmbH
InnoGames GmbH — Hamburg, Germany
[11.27.14]

Software Developer PHP (m/f)
InnoGames GmbH
InnoGames GmbH — Hamburg, Germany
[11.27.14]

Software Developer Flash (m/f)
Gameloft New Orleans
Gameloft New Orleans — New Orleans, Louisiana, United States
[11.26.14]

Lead Programmer
Blizzard Entertainment
Blizzard Entertainment — San Francisco, California, United States
[11.26.14]

iOS Engineer, San Francisco





Loading Comments

loader image