Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default value of editor property export/android/android_sdk_path for Windows, Linux, and macOS #97992

Merged
merged 1 commit into from
Nov 18, 2024

Conversation

Nikitf777
Copy link
Contributor

@Nikitf777 Nikitf777 commented Oct 8, 2024

[It is reopened #97978]

It was always annoying to me to start android development in Godot because it doesn't work out of the box, despite the fact that it's completely possible (Android Studio is a prove).
In particular, for some reason, I always need to manually set Android SDK path, which is almost (if not completely) always the same within each OS family when installed from Android Studio with default settings.

So now Godot has default value of Android SDK path for every supported OS:

Windows: %LOCALAPPDATA%/Android/Sdk
Linux: $HOME/Android/Sdk
macOS: $HOME/Library/Android/Sdk

Tested on Linux and Windows (through wine).

@m4gr3d m4gr3d modified the milestones: 4.x, 4.4 Oct 9, 2024
@Nikitf777 Nikitf777 changed the title Add default value of editor propetry "export/android/android_sdk_path" for Windows, Linux, and macOS Add default value of editor propetry export/android/android_sdk_path for Windows, Linux, and macOS Oct 9, 2024
@@ -1180,6 +1180,10 @@ String OS_LinuxBSD::get_system_ca_certificates() {
return f->get_as_text();
}

String OS_LinuxBSD::get_default_android_sdk_path() const {
return OS::get_singleton()->get_environment("HOME") + "/Android/Sdk";
Copy link
Member

@Calinou Calinou Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Handle trailing slashes if present in the environment variable (very unlikely here, but could technically happen):

Suggested change
return OS::get_singleton()->get_environment("HOME") + "/Android/Sdk";
return OS::get_singleton()->get_environment("HOME").path_join("Android/Sdk");

@@ -763,6 +763,10 @@
return certs;
}

String OS_MacOS::get_default_android_sdk_path() const {
return OS::get_singleton()->get_environment("HOME") + "/Library/Android/Sdk";
Copy link
Member

@Calinou Calinou Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return OS::get_singleton()->get_environment("HOME") + "/Library/Android/Sdk";
return OS::get_singleton()->get_environment("HOME").path_join("Library/Android/Sdk");

@@ -2004,6 +2004,10 @@ String OS_Windows::get_system_ca_certificates() {
return certs;
}

String OS_Windows::get_default_android_sdk_path() const {
return OS::get_singleton()->get_environment("LOCALAPPDATA") + "\\Android\\Sdk";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return OS::get_singleton()->get_environment("LOCALAPPDATA") + "\\Android\\Sdk";
return OS::get_singleton()->get_environment("LOCALAPPDATA").path_join("Android\\Sdk");

@@ -52,7 +52,9 @@ void register_android_exporter() {
#ifndef ANDROID_ENABLED
EDITOR_DEF_BASIC("export/android/java_sdk_path", OS::get_singleton()->get_environment("JAVA_HOME"));
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/java_sdk_path", PROPERTY_HINT_GLOBAL_DIR));
EDITOR_DEF_BASIC("export/android/android_sdk_path", OS::get_singleton()->get_environment("ANDROID_HOME"));

EDITOR_DEF_BASIC("export/android/android_sdk_path", OS::get_singleton()->has_environment("ANDROID_HOME") ? OS::get_singleton()->get_environment("ANDROID_HOME") : OS::get_singleton()->get_default_android_sdk_path());
Copy link
Member

@Calinou Calinou Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why the Android installers don't define ANDROID_HOME to the user environment variables after installing Android Studio. If they consistently did this, then this step wouldn't be needed.

Copy link
Contributor Author

@Nikitf777 Nikitf777 Oct 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's due to Linux's mess in environment variables. While in windows they are stored compactly and conveniently in one place in registry, in Linux there plenty of places for both user and global variables. And what makes it even worse is the fact that this set of places depends on DE and shell you currently use (and maybe distro, if we're taking about global vars). Yes, we have "/etc/environment", but you need sudo to write to it. So there's very few examples of non system apps which automatically edit variables

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have '/etc/environment', but you need sudo to write to it.

On macOS, it's not writable at all, there's no way to set global environment variables. ANDROID_HOME can be set for the shell, but Godot usually is not started from the shell.

@@ -52,7 +52,9 @@ void register_android_exporter() {
#ifndef ANDROID_ENABLED
EDITOR_DEF_BASIC("export/android/java_sdk_path", OS::get_singleton()->get_environment("JAVA_HOME"));
EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "export/android/java_sdk_path", PROPERTY_HINT_GLOBAL_DIR));
EDITOR_DEF_BASIC("export/android/android_sdk_path", OS::get_singleton()->get_environment("ANDROID_HOME"));

EDITOR_DEF_BASIC("export/android/android_sdk_path", OS::get_singleton()->has_environment("ANDROID_HOME") ? OS::get_singleton()->get_environment("ANDROID_HOME") : OS::get_singleton()->get_default_android_sdk_path());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we have '/etc/environment', but you need sudo to write to it.

On macOS, it's not writable at all, there's no way to set global environment variables. ANDROID_HOME can be set for the shell, but Godot usually is not started from the shell.


String get_default_android_sdk_path() {
#ifdef WINDOWS_ENABLED
return OS::get_singleton()->get_environment("LOCALAPPDATA").path_join("Android/Sdk");
Copy link
Contributor Author

@Nikitf777 Nikitf777 Oct 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced backslash with forward slash because String::path_join() method uses these slashes as well (So in any case sdk path on Windows will contain both back and forward slashes, because Windows' environment variables always use backslashes.

@@ -37,6 +37,8 @@
#include "editor/editor_settings.h"
#include "editor/export/editor_export.h"

String get_default_android_sdk_path();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this method to EditorPaths for consistency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think this should be in platform/android/export/export.cpp as it's specific to the Android export pipeline and we should aim to keep solutions local.

@@ -126,6 +126,7 @@ class OS_MacOS : public OS_Unix {
virtual Error move_to_trash(const String &p_path) override;

virtual String get_system_ca_certificates() override;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be reverted?

@Nikitf777 Nikitf777 requested a review from a team as a code owner October 14, 2024 14:39
Copy link
Contributor

@m4gr3d m4gr3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Can you squash the commits.

@akien-mga akien-mga changed the title Add default value of editor propetry export/android/android_sdk_path for Windows, Linux, and macOS Add default value of editor property export/android/android_sdk_path for Windows, Linux, and macOS Nov 11, 2024
Comment on lines 104 to 105
#elif LINUXBSD_ENABLED
return OS::get_singleton()->get_environment("HOME").path_join("Android/Sdk");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify what installation process leads to the Android SDK being in this path on Linux?

In my case I install the Android SDK from the command line tools from Google and thus the path is whatever I unzip the tools at, and pass as --sdk_root to sdkmanager.

Is ~/Android/Sdk the default path used by Android Studio on Linux?

Copy link
Contributor Author

@Nikitf777 Nikitf777 Nov 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in my case (and according to web search, not only in mine) Android Studio (no matter if it's snap, flatpak or official version) always use ~/Android location for its files, including the SDK.

@akien-mga
Copy link
Member

Sorry for the back and forth on this but I think the helper method to get the default SDK path should be in the Android export.cpp file as it's only useful there, and it's not a generic Godot path.

@Repiteo Repiteo merged commit 8811b39 into godotengine:master Nov 18, 2024
@Repiteo
Copy link
Contributor

Repiteo commented Nov 18, 2024

Thanks! Congratulations on your first contribution! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants