1. # =========================
  2. # ANDROID STUDIO LAB INSTALLER
  3. # =========================
  4.  
  5. $ErrorActionPreference = "SilentlyContinue"
  6.  
  7. $basePath = "C:\Android"
  8. $sdkPath = "C:\Android\Sdk"
  9. $installPath = "C:\Program Files\Android\Android Studio"
  10.  
  11. $installerFile = "$env:TEMP\android-studio.exe"
  12.  
  13. Write-Host "=== ANDROID STUDIO INSTALLER ===" -ForegroundColor Cyan
  14.  
  15. # =========================
  16. # CHECK ADMIN
  17. # =========================
  18. if (-not ([Security.Principal.WindowsPrincipal] `
  19. [Security.Principal.WindowsIdentity]::GetCurrent()
  20. ).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
  21. Write-Host "Execute como ADMINISTRADOR!" -ForegroundColor Red
  22. exit
  23. }
  24.  
  25. # =========================
  26. # CRIAR PASTAS
  27. # =========================
  28. Write-Host "[1/5] Criando diretorios..."
  29.  
  30. New-Item -ItemType Directory -Force -Path $basePath | Out-Null
  31. New-Item -ItemType Directory -Force -Path $sdkPath | Out-Null
  32.  
  33. # =========================
  34. # REMOVER VERSÕES ANTIGAS
  35. # =========================
  36. Write-Host "[2/5] Verificando versoes antigas..."
  37.  
  38. $keys = @(
  39. "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*",
  40. "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
  41. )
  42.  
  43. foreach ($k in $keys) {
  44. Get-ItemProperty $k |
  45. Where-Object {
  46. $_.DisplayName -and $_.DisplayName -like "*Android Studio*"
  47. } |
  48. ForEach-Object {
  49. Write-Host "Removendo: $($_.DisplayName)"
  50.  
  51. try {
  52. if ($_.UninstallString) {
  53. Start-Process "cmd.exe" -ArgumentList "/c $($_.UninstallString) /S" -Wait
  54. }
  55. } catch {}
  56. }
  57. }
  58.  
  59. # Remove pastas antigas
  60. Remove-Item $installPath -Recurse -Force
  61.  
  62. # =========================
  63. # DOWNLOAD
  64. # =========================
  65. Write-Host "[3/5] Baixando Android Studio..."
  66.  
  67. try {
  68. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
  69.  
  70. if (Get-Command wget.exe -ErrorAction SilentlyContinue) {
  71. & wget.exe $installerUrl -O $installerFile
  72. }
  73. else {
  74. Invoke-WebRequest -Uri $installerUrl -OutFile $installerFile -UseBasicParsing
  75. }
  76.  
  77. if (!(Test-Path $installerFile)) {
  78. throw "Falha no download"
  79. }
  80.  
  81. Write-Host "Download OK"
  82. }
  83. catch {
  84. Write-Host "Erro no download: $($_.Exception.Message)" -ForegroundColor Red
  85. exit
  86. }
  87.  
  88. # =========================
  89. # INSTALAÇÃO
  90. # =========================
  91. Write-Host "[4/5] Instalando Android Studio..."
  92.  
  93. Start-Process -FilePath $installerFile -ArgumentList "/S" -Wait
  94.  
  95. # =========================
  96. # CONFIGURAÇÃO GLOBAL SDK
  97. # =========================
  98. Write-Host "[5/5] Configurando SDK global..."
  99.  
  100. [Environment]::SetEnvironmentVariable("ANDROID_HOME", $sdkPath, "Machine")
  101. [Environment]::SetEnvironmentVariable("ANDROID_SDK_ROOT", $sdkPath, "Machine")
  102.  
  103. $path = [Environment]::GetEnvironmentVariable("Path", "Machine")
  104.  
  105. $androidPaths = "$sdkPath\platform-tools;$sdkPath\tools;$sdkPath\emulator"
  106.  
  107. if ($path -notlike "*platform-tools*") {
  108. [Environment]::SetEnvironmentVariable("Path", "$path;$androidPaths", "Machine")
  109. }
  110.  
  111. # =========================
  112. # FINAL
  113. # =========================
  114. Write-Host "=== INSTALAÇÃO CONCLUÍDA ===" -ForegroundColor Green
  115. Write-Host "SDK: $sdkPath"
  116. Write-Host "Android Studio instalado."