Gamasutra: The Art & Business of Making Gamesspacer
arrowPress Releases
October 24, 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

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

Activision Publishing
Activision Publishing — Santa Monica, California, United States
[10.24.14]

Tools Programmer-Central Team
Bluepoint Games, Inc.
Bluepoint Games, Inc. — Austin, Texas, United States
[10.23.14]

Senior Graphics (or Generalist) Engineer
Intel
Intel — Folsom, California, United States
[10.23.14]

Senior Graphics Software Engineer
Wargaming.net
Wargaming.net — Hunt Valley, Maryland, United States
[10.23.14]

Graphics Software Engineer






Comments


Wes Jurica
profile image
That sure is a lot to go through for that error. I'll have to remember this post if I have a problem like that.

I've received a few errors during build time on Android and the Console always gives more information about what happened and where it happened in the bottom panel of the Console when that error line is selected. Did this specific error not give that info?

Lior Tal
profile image
The problem is that this error did not have any extra information at all. Also, keep in mind that this post elaborated on how to do everything in (attempted) great detail. Performing this, especially for an experienced developer should take very little time. I also posted a link to this specific post in all relevant Unity forums that discussed similar issues, so people may have a look to see if this solves their problem.

Wes Jurica
profile image
Don't get me wrong, I wasn't minimizing your post here.

One of the main downsides to using Unity is it being closed source. Having a way like this to workaround problems while Unity devs work on fixing the problem is great. Thanks.

Lior Tal
profile image
BTW: i received an update from Unity today, that this issue was resolved.

Clive Henrick
profile image
Great Post. Unity to Android process might seem like Magic, but in its heart it still needs to turn all that Unity C# and runtime engine into Android (Java) and the error codes can be bit of a challenge.

Thanks for the Tips!

Rakib Solewalker
profile image
How about building the project manually by exporting as google android project? And that's a neat way to dig out such kind of bug, thanks!

Lior Tal
profile image
@Rakib: I didn't try exporting as android project, but most likely it will fail for the same reason (it fails early on in the build process).

Wendelin Reich
profile image
Thanks for this write-up - it reads almost like a detective story!

Could you clarify which version of Reflector needs to be purchased to have access to procmon? I have MS Visual Studio, but other than that only use an open-source decompiler (ILSpy).

Lior Tal
profile image
@Wendelin: Bug hunting is very similar to doing detective work (you have your suspects, evidence. Some lead to nowhere, some lead to the criminal).

Reflector is totally optional. Any C# decompiler will do (you can use dotPeek which is free).


Procmon is also free, and comes as part of the SysInternals tool suite.

All tools are linked at the button of the article.


none
 
Comment: