- Home /
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 ;)
Answer by Bunny83 · Jul 19, 2013 at 01:54 AM
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 ;) Don't have the time to give you a full example at the moment ;)
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 ;)
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"
I tried to use your static property and it work perfectly ;)
Anyway, I just wanted to set the path before building so doing it inside is perfect for me :) Thanks a lot !
Answer by pitimoi · Jul 19, 2013 at 03:41 PM
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...
Answer by pushxtonotdie · Nov 10, 2015 at 06:41 AM
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.
Your answer
Follow this Question
Related Questions
Error at Build step. Android. 0 Answers
Failed to update Android SDK Package List - Unity 2019.2.10f1 10 Answers
Unity can't detect SDK tools thougt I've installed them! 0 Answers
Android SDK is missing build-tools 2 Answers
I am having trouble building the project for android (i can't build) 0 Answers