Skip to content

Commit a5d4a80

Browse files
committed
Merge pull request #1653 from NativeScript/kerezov/setup-scripts-update
Improve Setup Scripts' Android bits
2 parents e869f23 + 2fc8990 commit a5d4a80

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

setup/native-script.ps1

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,21 @@ Install "Google Chrome" "Installing Google Chrome (required to debug NativeScrip
7171
Install "Java Development Kit" "Installing Java Development Kit" "cinst jdk8 --force --yes"
7272

7373
Install "Android SDK" "Installing Android SDK" "cinst android-sdk --force --yes"
74-
75-
# setup android sdk
76-
echo yes | cmd /c "$env:localappdata\Android\android-sdk\tools\android" update sdk --filter "tools,platform-tools,android-23" --all --no-ui
77-
echo yes | cmd /c "$env:localappdata\Android\android-sdk\tools\android" update sdk --filter "build-tools-23.0.1,extra-android-m2repository" --all --no-ui
78-
7974
# setup environment
8075

8176
if (!$env:ANDROID_HOME) {
82-
[Environment]::SetEnvironmentVariable("ANDROID_HOME", "$env:localappdata\Android\android-sdk", "User")
83-
$env:ANDROID_HOME = "$env:localappdata\Android\android-sdk";
77+
# in case the user has `android` in the PATH, use it as base for setting ANDROID_HOME
78+
$androidExecutableEnvironmentPath = Get-Command android -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Definition
79+
if ($androidExecutableEnvironmentPath -ne $null) {
80+
$androidHomeJoinedPath = [io.path]::combine($androidExecutableEnvironmentPath, "..", "..")
81+
$androidHome = Resolve-Path $androidHomeJoinedPath | Select-Object -ExpandProperty Path
82+
}
83+
else {
84+
$androidHome = "$env:localappdata\Android\android-sdk"
85+
}
86+
87+
$env:ANDROID_HOME = $androidHome;
88+
[Environment]::SetEnvironmentVariable("ANDROID_HOME", "$env:ANDROID_HOME", "User")
8489
}
8590

8691
if (!$env:JAVA_HOME) {
@@ -90,5 +95,16 @@ if (!$env:JAVA_HOME) {
9095
$env:JAVA_HOME = $javaHome;
9196
}
9297

98+
# setup android sdk
99+
# following commands are separated in case of having to answer to license agreements
100+
# the android tool will introduce a --accept-license option in subsequent releases
101+
$androidExecutable = [io.path]::combine($env:ANDROID_HOME, "tools", "android")
102+
echo y | cmd /c "$androidExecutable" update sdk --filter "tools" --all --no-ui
103+
echo y | cmd /c "$androidExecutable" update sdk --filter "platform-tools" --all --no-ui
104+
echo y | cmd /c "$androidExecutable" update sdk --filter "android-23" --all --no-ui
105+
echo y | cmd /c "$androidExecutable" update sdk --filter "build-tools-23.0.2" --all --no-ui
106+
echo y | cmd /c "$androidExecutable" update sdk --filter "extra-android-m2repository" --all --no-ui
107+
108+
93109
Write-Host -ForegroundColor Green "This script has modified your environment. You need to log off and log back on for the changes to take effect."
94110
Pause

setup/native-script.rb

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,42 @@ def install(program_name, message, script, run_as_root = false, show_all_option
8282
end
8383

8484
install("Java SE Development Kit", "Installing the Java SE Development Kit... This might take some time, please, be patient. (You will be prompted for your password)", 'brew cask install java', false, false)
85-
execute('echo "export JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.profile', "Unable to set JAVA_HOME")
86-
8785
install("Android SDK", "Installing Android SDK", 'brew install android-sdk')
88-
execute('echo "export ANDROID_HOME=/usr/local/opt/android-sdk" >> ~/.profile', "Unable to set ANDROID_HOME")
86+
87+
unless ENV["ANDROID_HOME"]
88+
require 'pathname'
89+
# if android-sdk was installed through brew, there should be a symlink in /usr/local/opt/android-sdk pointing to the actual sdk
90+
android_home = "/usr/local/opt/android-sdk"
91+
unless Pathname.new(android_home).exist?
92+
require 'mkmf'
93+
# if there's no such symlink then try to find the `android-sdk` directory through the `android` executable
94+
android_executable_environment_path = find_executable('android')
95+
if android_executable_environment_path
96+
android_home_joined_path = File.join(android_executable_environment_path, "..", "..")
97+
android_home = Pathname.new(android_home_joined_path).realpath
98+
end
99+
end
100+
101+
ENV["ANDROID_HOME"] = android_home
102+
execute('echo "export ANDROID_HOME=%s" >> ~/.profile' % ENV["ANDROID_HOME"], "Unable to set ANDROID_HOME")
103+
end
104+
105+
unless ENV["JAVA_HOME"]
106+
execute('echo "export JAVA_HOME=$(/usr/libexec/java_home)" >> ~/.profile', "Unable to set JAVA_HOME")
107+
end
89108

90109
# the -p flag is set in order to ensure zero status code even if the directory exists
91110
execute("mkdir -p ~/.cocoapods", "There was a problem in creating ~/.cocoapods directory")
92111
install("CocoaPods", "Installing CocoaPods... This might take some time, please, be patient.", 'gem install cocoapods -V', true)
93112

94113
puts "Configuring your system for Android development... This might take some time, please, be patient."
95-
# Note that multiple license acceptances may be required, hence the multiple y answers
114+
# Note that multiple license acceptances may be required, hence the multiple commands
96115
# the android tool will introduce a --accept-license option in subsequent releases
97-
execute("(for i in {1..5}; do echo y; sleep 4; done) | /usr/local/opt/android-sdk/tools/android update sdk --filter tools,platform-tools,android-23,build-tools-23.0.2,extra-android-m2repository --all --no-ui",
98-
"There seem to be some problems with the Android configuration")
116+
android_executable = File.join(ENV["ANDROID_HOME"], "tools", "android")
117+
execute("echo y | #{android_executable} update sdk --filter tools --all --no-ui", "There seem to be some problems with the Android configuration")
118+
execute("echo y | #{android_executable} update sdk --filter platform-tools --all --no-ui", "There seem to be some problems with the Android configuration")
119+
execute("echo y | #{android_executable} update sdk --filter android-23 --all --no-ui", "There seem to be some problems with the Android configuration")
120+
execute("echo y | #{android_executable} update sdk --filter build-tools-23.0.2 --all --no-ui", "There seem to be some problems with the Android configuration")
121+
execute("echo y | #{android_executable} update sdk --filter extra-android-m2repository --all --no-ui", "There seem to be some problems with the Android configuration")
99122

100123
puts "The ANDROID_HOME and JAVA_HOME environment variables have been added to your .profile. Restart the terminal to use them."

0 commit comments

Comments
 (0)