Home | History | Annotate | Download | only in scripts
      1 #!/usr/bin/env ruby
      2 #
      3 # This script mavenizes all dependencies from the Android SDK required to build Robolectric.
      4 #
      5 # Usage:
      6 #   install-dependencies.rb
      7 #
      8 # Assumptions:
      9 #  1. You've got one or more Android SDKs and Google APIs installed locally.
     10 #  2. Your ANDROID_HOME environment variable points to the Android SDK install directory.
     11 #  3. You have installed the Android Support Repository and Google Repository libraries from the SDK installer.
     12 #
     13 require 'tmpdir'
     14 
     15 def concat_maven_file_segments(repo_root_dir, group_id, artifact_id, version, extension)
     16   # Also don't move further if we have invalid parameters
     17   if group_id.to_s == '' || artifact_id.to_s == '' || version.to_s == '' || extension.to_s == ''
     18     raise ArgumentError, "Group ID, Artifact ID, Version, and/or Extension arguments are invalid. Please check your inputs."
     19   end
     20   # Generate dependency path segments
     21   dep_path_segments = []  
     22   artifact_file_name = "#{artifact_id}-#{version}.#{extension}"
     23   # Start with the root repo dir
     24   dep_path_segments << repo_root_dir
     25 
     26   # Add the split group id segments into the path segments
     27   dep_path_segments << group_id.split(".")
     28   
     29   # Then add the artifact id
     30   dep_path_segments << artifact_id
     31   
     32   # Then add the version ID
     33   dep_path_segments << version
     34   
     35   # Finally, add the version file
     36   dep_path_segments << artifact_file_name
     37   
     38   # Concatenate the segments into the target archive
     39   dep_path_segments.join("/")
     40 end
     41 
     42 def install(group_id, artifact_id, version, archive)
     43   run("mvn -q install:install-file -DgroupId='#{group_id}' -DartifactId='#{artifact_id}' -Dversion='#{version}' -Dfile='#{archive}' -Dpackaging=jar") || exit(1)
     44 end
     45 
     46 def get_dependency(group_id, artifact_id, version, packaging)
     47   run("mvn -q dependency:get -DrepoUrl=http://maven.google.com -DgroupId='#{group_id}' -DartifactId='#{artifact_id}' -Dversion='#{version}' -Dpackaging='#{packaging}' -Dtransitive=false") || exit(1)
     48 end
     49 
     50 def run(args)
     51   puts "> #{args}"
     52   system args
     53 end
     54 
     55 def install_jar(group_id, artifact_id, version, archive, &block)
     56   unless File.exists?(archive)
     57     puts "#{group_id}:#{artifact_id} not found!"
     58     puts "Make sure that the 'Android Support Repository' and 'Google Repository' is up to date in the SDK manager."
     59     exit 1
     60   end
     61 
     62   puts "Installing JAR #{group_id}:#{artifact_id}, version #{version} from \'#{archive}\'."
     63   install(group_id, artifact_id, version, archive)
     64   block.call(dir) if block_given?
     65 end
     66 
     67 def install_aar(repo_root_dir, group_id, artifact_id, version, &block)
     68   return if already_have?(group_id, artifact_id, version, "jar")
     69 
     70   # Don't move further if we have an invalid repo root directory
     71   unless File.exists?(repo_root_dir)
     72     puts "Repository #{repo_root_dir} not found!"
     73     puts "Make sure that the 'ANDROID_HOME' Environment Variable is properly set in your development environment pointing to your SDK installation directory."
     74     exit 1
     75   end
     76 
     77   archive = concat_maven_file_segments(repo_root_dir, group_id, artifact_id, version, "aar")
     78 
     79   puts "Installing AAR #{group_id}:#{artifact_id}, version #{version} from \'#{archive}\'."
     80   Dir.mktmpdir('robolectric-dependencies') do |dir|
     81     system("cd #{dir}; jar xvf #{archive} > /dev/null")
     82     install(group_id, artifact_id, version, "#{dir}/classes.jar")
     83     block.call(dir) if block_given?
     84   end
     85 end
     86 
     87 def already_have?(group_id, artifact_id, version, extension)
     88   jar_file = concat_maven_file_segments(MVN_LOCAL, group_id, artifact_id, version, extension)
     89   exists = File.exist?(jar_file)
     90   puts "Already have #{jar_file}!" if exists
     91   exists
     92 end
     93 
     94 def install_stubs(api)
     95   return if already_have?("com.google.android", "android-stubs", "#{api}", "jar")
     96 
     97   path  = "#{ANDROID_HOME}/platforms/android-#{api}/android.jar"
     98   unless File.exists?(path)
     99     puts "#{path} not found!"
    100     puts "Make sure that 'SDK Platform' is up to date in the SDK manager for API #{api}."
    101     exit 1
    102   end
    103 
    104   puts "Installing stubs jar at com.google.android:android-stubs:#{api}."
    105   install("com.google.android", "android-stubs", "#{api}", path)
    106 end
    107 
    108 def install_supportlib_from_gmaven(artifact_id)
    109   install_from_gmaven(ANDROID_SUPPORT_GROUP_ID, artifact_id, SUPPORT_LIBRARY_VERSION)
    110 end
    111 
    112 def install_from_gmaven(group_id, artifact_id, version)
    113   return if already_have?(group_id, artifact_id, version, "jar")
    114 
    115   get_dependency(group_id, artifact_id, version, "aar")
    116   install_aar(MVN_LOCAL, group_id, artifact_id, version)
    117 end
    118 
    119 # Local repository paths
    120 ANDROID_HOME = ENV['ANDROID_HOME']
    121 ADDONS = "#{ANDROID_HOME}/add-ons"
    122 GOOGLE_REPO  = "#{ANDROID_HOME}/extras/google/m2repository"
    123 ANDROID_REPO = "#{ANDROID_HOME}/extras/android/m2repository"
    124 MVN_LOCAL = File.expand_path("~/.m2/repository")
    125 
    126 # Android Support libraries maven constants
    127 ANDROID_SUPPORT_GROUP_ID = "com.android.support"
    128 MULTIDEX_ARTIFACT_ID = "multidex"
    129 SUPPORT_V4_ARTIFACT_ID = "support-v4"
    130 SUPPORT_COMPAT_ARTIFACT_ID = "support-compat"
    131 SUPPORT_CORE_UI_ARTIFACT_ID = "support-core-ui"
    132 SUPPORT_CORE_UTILS_ARTIFACT_ID = "support-core-utils"
    133 SUPPORT_FRAGMENT_ARTIFACT_ID = "support-fragment"
    134 APPCOMPAT_V7_ARTIFACT_ID = "appcompat-v7"
    135 INTERNAL_IMPL_ARTIFACT_ID = "internal_impl"
    136 
    137 # Android Support library versions (plus trailing version)
    138 # SUPPORT_LIBRARY_TRAILING_VERSION = "23.2.0"
    139 SUPPORT_LIBRARY_VERSION = "26.0.1"
    140 MULTIDEX_TRAILING_VERSION = "1.0.0"
    141 MULTIDEX_VERSION = "1.0.1"
    142 
    143 # Android Support test versions
    144 ANDROID_SUPPORT_TEST_GROUP_ID = "com.android.support.test"
    145 RUNNER_ARTIFACT_ID = "runner"
    146 MONITOR_ARTIFACT_ID = "monitor"
    147 ANDROID_SUPPORT_TEST_VERSION = "1.0.2-alpha1"
    148 
    149 # Play Services constants
    150 PLAY_SERVICES_GROUP_ID = "com.google.android.gms"
    151 
    152 # Play Services 6.5.87 version constants, which pulls in all of the play-services
    153 # submodules in classes.jar
    154 PLAY_SERVICES_VERSION_6_5_87 = "6.5.87"
    155 PLAY_SERVICES_LEGACY = "play-services"
    156 
    157 # Play Services Base and Basement modules, version 8.4.0 (plus trailing version)
    158 # Current "play-services" artifact no longer references each sub-module directly. When you
    159 # Extract its AAR, it only contains a manifest and blank res folder.
    160 # 
    161 # As a result, we now have to install "play-services-base" and "play-services-basement"
    162 # separately and use those versions instead.
    163 PLAY_SERVICES_TRAILING_VERSION = "8.3.0"
    164 PLAY_SERVICES_VERSION = "8.4.0"
    165 PLAY_SERVICES_BASE = "play-services-base"
    166 PLAY_SERVICES_BASEMENT = "play-services-basement"
    167 
    168 # Mavenize all dependencies
    169 
    170 install_stubs(27)
    171 
    172 install_aar(ANDROID_REPO, ANDROID_SUPPORT_GROUP_ID, MULTIDEX_ARTIFACT_ID, MULTIDEX_TRAILING_VERSION)
    173 
    174 install_aar(ANDROID_REPO, ANDROID_SUPPORT_GROUP_ID, MULTIDEX_ARTIFACT_ID, MULTIDEX_VERSION)
    175 
    176 # install_aar(ANDROID_REPO, ANDROID_SUPPORT_GROUP_ID, APPCOMPAT_V7_ARTIFACT_ID, SUPPORT_LIBRARY_TRAILING_VERSION)
    177 
    178 install_supportlib_from_gmaven(APPCOMPAT_V7_ARTIFACT_ID)
    179 
    180 install_aar(GOOGLE_REPO, PLAY_SERVICES_GROUP_ID, PLAY_SERVICES_LEGACY, PLAY_SERVICES_VERSION_6_5_87)
    181 
    182 install_aar(GOOGLE_REPO, PLAY_SERVICES_GROUP_ID, PLAY_SERVICES_BASEMENT, PLAY_SERVICES_TRAILING_VERSION)
    183 
    184 install_aar(GOOGLE_REPO, PLAY_SERVICES_GROUP_ID, PLAY_SERVICES_BASEMENT, PLAY_SERVICES_VERSION)
    185 
    186 install_aar(GOOGLE_REPO, PLAY_SERVICES_GROUP_ID, PLAY_SERVICES_BASE, PLAY_SERVICES_TRAILING_VERSION)
    187 
    188 install_aar(GOOGLE_REPO, PLAY_SERVICES_GROUP_ID, PLAY_SERVICES_BASE, PLAY_SERVICES_VERSION)
    189 
    190 # install_aar(MVN_LOCAL, ANDROID_SUPPORT_GROUP_ID, SUPPORT_V4_ARTIFACT_ID, SUPPORT_LIBRARY_TRAILING_VERSION) do |dir|
    191   # install_jar(ANDROID_SUPPORT_GROUP_ID, INTERNAL_IMPL_ARTIFACT_ID, SUPPORT_LIBRARY_TRAILING_VERSION, "#{dir}/libs/#{INTERNAL_IMPL_ARTIFACT_ID}-#{SUPPORT_LIBRARY_TRAILING_VERSION}.jar")
    192 # end
    193 
    194 install_supportlib_from_gmaven(SUPPORT_V4_ARTIFACT_ID)
    195 install_supportlib_from_gmaven(SUPPORT_COMPAT_ARTIFACT_ID)
    196 install_supportlib_from_gmaven(SUPPORT_CORE_UI_ARTIFACT_ID)
    197 install_supportlib_from_gmaven(SUPPORT_CORE_UTILS_ARTIFACT_ID)
    198 install_supportlib_from_gmaven(SUPPORT_FRAGMENT_ARTIFACT_ID)
    199 install_supportlib_from_gmaven('support-media-compat')
    200 
    201 install_from_gmaven(ANDROID_SUPPORT_TEST_GROUP_ID, MONITOR_ARTIFACT_ID, ANDROID_SUPPORT_TEST_VERSION)
    202 install_from_gmaven(ANDROID_SUPPORT_TEST_GROUP_ID, RUNNER_ARTIFACT_ID, ANDROID_SUPPORT_TEST_VERSION)
    203