The following blog post, unless otherwise noted, was written by a member of Gamasutras 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
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.
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:
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:
- Attach to the running Unity process (Run â€“> Attach to Process)
- Open the Exceptions menu (Run â€“> Exceptions)
- AddÂ System.ArgumentExceptionÂ to Stop in exceptions:
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:
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.
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:
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:
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â€ť):
We should further filter the results for showing onlyÂ process/threadÂ activities (from the toolbar), as seen in this image:
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:
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
- Reproduce the issue before attempting to fix it
- Determine a context for the failure (e.g: stack trace)
- Reverse engineering can be your friend
- Debugger (MonoDevelop) â€“ Installed with Unity
- C# decompiler