如何在 Powershell 脚本中绕过安装向导直接启动 Android Studio 的便携式安装?

How to launch a portable install of Android Studio directly, bypassing the Setup Wizard, In Powershell Script?

所以每个人都喜欢 Android Studio 出色的便携性,不是吗?

我已将 SDK 及其所有组件安装在便携式驱动器上,并且可以在任何地方使用它。我只需要一个快速的 Powershell 脚本就可以启动它 运行。 该脚本设置我所有的环境变量,并启动 Studio 和保存在我磁盘上的模拟器。

但是... 每次在新环境(如新计算机)中启动 Studio 时,它都会从内置的安装向导开始。 恼人的。 因此,如果我取消设置,系统会提示我选择在下次启动时跳过向导。选择此选项后,工作室启动正常。

社区可以帮助我在首次启动时覆盖设置向导吗? 这是脚本:

#***************************************************************************************#
#***************************************************************************************#
#                                                                                       #
#                       Portable Android SDK Environment Setup                          #
#                                 Powershell Script                                     #
#                                                                                       #
#                                   By: sh7411usa                                       #
#                                                                                       #
#                      For Use With Portable Drive Installation                         #
#                                                                                       #
#                                                                                       #
#***************************************************************************************#
#***************************************************************************************#

#Configure Execution Policy
Set-ExecutionPolicy Bypass -Scope Process -Force

#Display Welcome to User
Write-Output ""
Write-Output "**************************************************************************"
Write-Output "**************************************************************************"
Write-Output "**                                                                      **"
Write-Output "**                                                                      **"
Write-Output "**                    Android SDK Environment Setup                     **"
Write-Output "**                                                                      **"
Write-Output "**                                                                      **"
Write-Output "**************************************************************************"
Write-Output "**************************************************************************"
Write-Output `n

#Advise of Execution Policy
$outStr = (get-ExecutionPolicy) | Out-String
Write-Host -NoNewline "Checking Execution Policy... "
Write-Host $outStr

#Set Working Directory (containing path to Android SDK installation):
$BpathTo = (Get-Item -Path ".\").FullName

#Update for User Specific data, (usually found under "C:\Users\User\"):
#ANDROID_SDK_HOME
#ANDROID_EMULATOR_HOME
#ANDROID_AVD_HOME 
$pathTo = "$BpathTo\User"
Write-Host -NoNewline "Setting ANDROID_SDK_HOME to:      $pathTo"
[Environment]::SetEnvironmentVariable("ANDROID_SDK_HOME", $pathTo, "Machine")
Write-Output "              ...Done"
$pathTo+="\.android"
Write-Host -NoNewline "Setting ANDROID_EMULATOR_HOME to: $pathTo"
[Environment]::SetEnvironmentVariable("ANDROID_EMULATOR_HOME", $pathTo, "Machine")
Write-Output "     ...Done"
$pathTo+="\avd"
Write-Host -NoNewline "Setting ANDROID_AVD_HOME to:      $pathTo"
[Environment]::SetEnvironmentVariable("ANDROID_AVD_HOME", $pathTo, "Machine")
Write-Output " ...Done"
#Create/Update ANDOID_HOME environmental variable:
### The Program data, usually found under "C:\Program Files (x86)\Android Studio\"
$pathTo = "$BpathTo\SDK"
Write-Host -NoNewline "Setting ANDROID_SDK_ROOT to:      $pathTo"
[Environment]::SetEnvironmentVariable("ANDROID_SDK_ROOT", $pathTo, "Machine")
Write-Output "               ...Done"
Write-Host -NoNewline "Setting ANDROID_HOME to:          $pathTo"
[Environment]::SetEnvironmentVariable("ANDROID_HOME", $pathTo, "Machine")
Write-Output "               ...Done
"
Write-Output "Keep up to date on Android Studio's environmental variables:
https://developer.android.com/studio/command-line/variables
"

#Update PATH environmental variable:
Write-Output "Updating PATH:"
$pathTo = "$BpathTo\SDK"
$new1="$pathTo"+"\tools;"
$new2="$pathTo"+"\tools\bin;"
$new3="$pathTo"+"\platform-tools;"
$newPath=";"+$new1+$new2+$new3
Write-Output "Adding: $new1"
Write-Output "Adding: $new2"
Write-Output "Adding: $new3"
$new4="$BpathTo"+"\User;"
Write-Output "Adding: $new4"
$newPath+=$new4
$env:Path+=$newPath
Write-Output ""
Write-Output "Done... Environment Setup Has Finished."
Write-Output ""

#Install Chocolatey and choco packages:
$InstallChoco = $False
##TODO:##if (Get-Command choco.exe -ErrorAction SilentlyContinue) {$InstallChoco = $true} #tests if choco.exe exists
If(Test-Path -Path "$env:ProgramData\Chocolatey") {} Else {$InstallChoco=$true} #tests if choco exists in $env:
if ($InstallChoco){ #Installs choco if not already installed
    Write-Output "Installing 'Chocolatey Installer'"
    iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
    Write-Output `n}
$Packages = 'ADB', 'NotepadPlusPlus' #<-- Add packages here
ForEach ($PackageName in $Packages){ #installs choco packages listed above, one by one.
    Write-Output "Attempting Chocolatey Install: $PackageName"
    choco install --force $PackageName -y 
    Write-Output `n}

#Launch the Studio:
$pathTo = "$BpathTo\android-studio\bin\studio64.exe"
Write-Output "Starting Process: ANDROID STUDIO (x64)"
Write-Output "Initiating @ $pathTo"
Start-Process $pathTo
Write-Output `n

#Startup the Emulator:
##TODO:##See above link regarding setting emulator variables.
##First, update the emulator's *.ini to match the current directory:
$avdPath="$BpathTo\User\.android\avd\Nexus_x86.ini"
Write-Host -NoNewline "Updating Emulator: $avdPath..."
if (Test-Path $avdPath){Remove-Item $avdPath}
New-Item $avdPath | Out-Null
Clear-Content $avdPath
Set-Content $avdPath "avd.ini.encoding=UTF-8
path=$BpathTo\User\.android\avd\Nexus_x86.avd
path.rel=avd\Nexus_x86.avd
target=android-24"
Write-Output "Done"

##Second, Launch the emulator:
Write-Output "Launching: 'Nexus_x86' Emulator..."
$arg="-avd Nexus_x86"
$pathTo = "$Bpathto\localInstall\emulator\emulator.exe"
SDK\emulator\emulator.exe -avd Nexus_x86
Write-Output `n

#Wait for user response to close the window
Write-Host -NoNewLine "Script Finished. Press any key to close this window..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

在这张图中,我详细说明了上面提到的取消设置向导的步骤。

我想跳过这一步,直接进入工作室。感谢任何帮助。

进一步研究 - 我回来回答我自己的问题。

Android Studio 从“\bin”文件夹运行。这是实际工作室可执行文件所在的文件夹。在该文件夹中有一个名为 idea.properties 的文件,其中将 IDE 指向用户特定的系统和配置文件夹,通常位于 C:\USERS\User.AndroidStudio3.x 下.

在启动 Studio 之前将以下代码添加到脚本中,以使用当前 drive/directory 更新该文件以实现真正可移植的 - 无需设置 - studio 安装:

$BpathTo = (Get-Item -Path ".\").FullName
$pathTo = "$BpathTo\android-studio\bin\idea.properties"
$newCont="
idea.config.path=$BpathTo/User/.AndroidStudio/config
idea.system.path=$BpathTo/User/.AndroidStudio/system
idea.max.intellisense.filesize=2500
idea.max.content.load.filesize=20000
idea.cycle.buffer.size=1024
idea.no.launcher=false
idea.dynamic.classpath=false
idea.popup.weight=heavy
sun.java2d.d3d=false
swing.bufferPerWindow=true
sun.java2d.pmoffscreen=false
sun.java2d.uiScale.enabled=true
javax.swing.rebaseCssSizeMap=true
idea.xdebug.key=-Xdebug
idea.fatal.error.notification=disabled
"
Clear-Content $pathTo
Set-Content $pathTo $newCont

(额外增加的台词是工作室顺利运行所必需的。)