Class: Fastlane::Actions::AndroidSdkUpdateAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb

Documentation collapse

Class Method Summary collapse

Class Method Details

.authorsObject



158
159
160
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 158

def self.authors
  ["Philipp Burgk", "Michael Ruhl"]
end

.available_optionsObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 118

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :compile_sdk_version,
                                 env_name: "FL_ANDROID_COMPILE_SDK_VERSION",
                                 description: "Compile-SDK Version of the project. Can also defined in 'gradle.properties'",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :build_tools_version,
                                 env_name: "FL_ANDROID_BUILD_TOOLS_VERSION",
                                 description: "Build-Tools Version of the project. Can also defined in 'gradle.properties'",
                                 optional: true),
    FastlaneCore::ConfigItem.new(key: :additional_packages,
                                 description: "List with additional sdk packages. Examples:\n
                                • extras;google;m2repository
                                • extras;android;m2repository",
                                 is_string: false,
                                 optional: true,
                                 default_value: []),
    FastlaneCore::ConfigItem.new(key: :override_local_properties,
                                 env_name: "FL_ANDROID_SDK_OVERRIDE_LOCAL_PROPERTIES",
                                 description: "Set the sdk-dir in 'local.properties' so Gradle finds the Android home",
                                 is_string: false,
                                 default_value: true),
    FastlaneCore::ConfigItem.new(key: :update_installed_packages,
                                 env_name: "FL_ANDROID_SDK_UPDATE_INSTALLED_PACKAGES",
                                 description: "Update all installed packages to the latest versions",
                                 is_string: false,
                                 default_value: false),
    FastlaneCore::ConfigItem.new(key: :linux_sdk_dir,
                                  env_name: "FL_ANDROID_LINUX_SDK_DIR",
                                  description: "Directory for Android SDK on Linux",
                                  optional: true,
                                  default_value: ENV['ANDROID_HOME'] || ENV['ANDROID_SDK'] || ENV['ANDROID_SDK_ROOT'] || "~/.android-sdk"),
    FastlaneCore::ConfigItem.new(key: :linux_sdk_download_url,
                                  env_name: "FL_ANDROID_LINUX_SDK_DOWNLOAD_URL",
                                  description: "Download URL for Android SDK on Linux",
                                  optional: true,
                                  default_value: "https://dl.google.com/android/repository/commandlinetools-linux-8512546_latest.zip")
  ]
end

.descriptionObject



93
94
95
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 93

def self.description
  "Install and update required Android-SDK packages"
end

.detailsObject



97
98
99
100
101
102
103
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 97

def self.details
  [
    "The initial Android-SDK will be installed with Homebrew.",
    "Updates for the specified packages will be automatically installed.",
    "Instructions to configure 'compile_sdk_version' and 'build_tools_version': https://github.com/NovaTecConsulting/fastlane-plugin-android_sdk_update"
  ].join("\n")
end

.determine_sdk(params) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 49

def self.determine_sdk(params)
  # on mac
  if FastlaneCore::Helper.mac?
    require 'fastlane/plugin/brew'
    Actions::BrewAction.run(command: "list --cask --versions android-commandlinetools || brew install --cask android-commandlinetools")
    sdk_manager = File.realpath(FastlaneCore::CommandExecutor.which("sdkmanager"))
    sdk_path = File.expand_path("../../../..", sdk_manager)

  # on linux
  elsif FastlaneCore::Helper.linux?
    sdk_path = File.expand_path(params[:linux_sdk_dir])
    sdk_manager = File.expand_path("tools/bin/sdkmanager", sdk_path)
    if File.exist?(sdk_manager)
      UI.message("Using existing android-sdk at #{sdk_path}")
    else
      UI.message("Downloading android-sdk to #{sdk_path}")
      download_and_extract_sdk(params[:linux_sdk_download_url], sdk_path)
    end

  else
    UI.user_error! 'Your OS is currently not supported.'
  end

  ENV['ANDROID_SDK_ROOT'] = sdk_path
  [sdk_manager, sdk_path]
end

.download_and_extract_sdk(download_url, sdk_path) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 76

def self.download_and_extract_sdk(download_url, sdk_path)
  FastlaneCore::CommandExecutor.execute(command: "wget -O /tmp/android-commandlinetools.zip #{download_url}",
                                        print_all: true,
                                        print_command: true)
  FastlaneCore::CommandExecutor.execute(command: "unzip -qo /tmp/android-commandlinetools.zip -d #{sdk_path}",
                                        print_all: true,
                                        print_command: true)
ensure
  FastlaneCore::CommandExecutor.execute(command: "rm -f /tmp/android-commandlinetools.zip",
                                        print_all: true,
                                        print_command: true)
end

.example_codeObject



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 105

def self.example_code
  [
    'android_sdk_update(
      additional_packages: ["extras;google;m2repository", "extras;android;m2repository"]
    )',
    'android_sdk_update(
      compile_sdk_version: "25",
      build_tools_version: "25.0.2",
      additional_packages: ["extras;google;m2repository", "extras;android;m2repository"]
    )'
  ]
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 162

def self.is_supported?(platform)
  platform == :android
end

.run(params) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/fastlane/plugin/android_sdk_update/actions/android_sdk_update_action.rb', line 4

def self.run(params)
  # Install Android-SDK via brew
  sdk_manager, sdk_path = determine_sdk(params)

  # Define required packages
  require 'java-properties'
  properties = File.exist?("#{Dir.pwd}/gradle.properties") ? JavaProperties.load("#{Dir.pwd}/gradle.properties") : {}
  tools_version = params[:build_tools_version] || properties[:build_tools_version] || UI.user_error!('No build tools version defined.')
  sdk_version = params[:compile_sdk_version] || properties[:compile_sdk_version] || UI.user_error!('No compile sdk version defined.')

  packages = params[:additional_packages]
  packages << "platforms;android-#{sdk_version}"
  packages << "build-tools;#{tools_version}"
  packages << "tools"
  packages << "platform-tools"

  # Install Packages
  UI.header("Install Android-SDK packages")

  if params[:update_installed_packages]
    UI.message("Updating all installed packages")
    # Ensure all installed packages are updated
    FastlaneCore::CommandExecutor.execute(command: "yes | #{sdk_manager} --update",
                                          print_all: true,
                                          print_command: false)
  end

  UI.message("Installing packages...")
  packages.each { |package| UI.message("#{package}") }
  FastlaneCore::CommandExecutor.execute(command: "yes | #{sdk_manager} '#{packages.join("' '")}'",
                                        print_all: true,
                                        print_command: false)

  # Accept licenses for all available packages
  UI.important("Accepting licenses on your behalf!")
  FastlaneCore::CommandExecutor.execute(command: "yes | #{sdk_manager} --licenses",
                                         print_all: true,
                                         print_command: false)

  if params[:override_local_properties]
    UI.message("Override local.properties")
    JavaProperties.write({ :"sdk.dir" => sdk_path }, "#{Dir.pwd}/local.properties")
  end
end