Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by pitimoi · Jul 18, 2013 at 05:11 PM · androidsdkandroid sdk

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 ;)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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"

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image pitimoi · Jul 23, 2013 at 05:57 PM 0
Share

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 !

avatar image
0

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...

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jul 20, 2013 at 09:03 AM 0
Share

I've updated my answer.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges