Setting Android sdk path in environment variable

Hi all,

I’m using Unity 4.1.5f Pro and I would like to know if there is any Environment variable I can set to allow Unity to find the Android-sdk path itself.

Or if I can select it in an editor script at runtime… ?

I’m trying to make a script to build my android project, and I don’t want to have to go on each of my build machine and open Unity once to select the android-sdk location…

Thanks for any help :wink:

There’s an internal class that seems to hold that information in a static read-only variable. The class is called AndroidSDKTools. It has a private variable like this:

private readonly string m_sdkToolsDir;

You should be able to set it via reflection, but no guarantee that it works. It looks like the class always use this var when executing an android SDK tool, so it should work i think :wink: Don’t have the time to give you a full example at the moment :wink:

Feel free to explore the UnityEditor.dll yourself.

edit
It seems Unity creates it’s own instance when you create a build and overwrites the folder from it’s own setting.

This however should work:

public static class AndroidSDKFolder
{
    public static string Path
    {
        get { return EditorPrefs.GetString("AndroidSdkRoot"); }
        set { EditorPrefs.SetString("AndroidSdkRoot", value); }
    }
}

Unity stores the path in the EditorPrefs, so by using the above property you should be able to set the path to whatever you want :wink:

The EditorPrefs are stored in the registry on windows in this path (for Unity4.x):

HKEY_CURRENT_USER\Software\Unity Technologies\Unity Editor 4.x\

But it’s difficult to set it from outside since Unity appends a hash of the keyname at the end of the key to avoid key-name collisions. The key name looks like “AndroidSdkRoot_h2651068356”

Thanks for the tip Bunny,

Here is the code to set the correct value in m_sdkToolsDir :

System.Type sdktool = typeof(AndroidSdkRoot).Assembly.GetType("UnityEditor.AndroidSDKTools");
ConstructorInfo sdktoolConstructor = sdktool.GetConstructor(new System.Type[] {typeof(string)});
FieldInfo sdktoolInstance = sdktool.GetField("JAVA_TOOLS", BindingFlags.Static | BindingFlags.NonPublic);
sdktoolInstance.SetValue(null, sdktoolConstructor.Invoke(new object[] {System.Environment.GetEnvironmentVariable("ANDROID_HOME")}));
		
FieldInfo field = sdktool.GetField("m_sdkToolsDir", BindingFlags.Instance|BindingFlags.NonPublic);
Debug.Log("Value : " + field.GetValue(sdktoolInstance.GetValue(null)));

By the way, Unity still ask me about the android sdk directory, and modifying this value does not change the path in the Unity Preferences window…

So this is an old question but I was installing a new version of Unity (5.2.1) on a build server and this cropped up. Essentially the Android SDK and JDK location are lost if you set them via the command line. But if you, say, log in to the build server via RDP and open unity, change the location, and close unity, it remembers it forever. This has to happen upon each new install, so the accepted answer is most likely better. But this is an alternative workaround.