簡體   English   中英

如何在build.gradle中檢索ADB的路徑

[英]How to retrieve path to ADB in build.gradle

我試圖通過gradle task啟動應用程序。


task runDebug(dependsOn: ['installDebug', 'run']) {
}

task run(type: Exec) {
commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}

但是這段代碼不起作用,我得到錯誤:
a problem occurred starting process 'command 'adb''

但是,當我明確指定adb的路徑時,將啟動應用程序。


task run(type: Exec) {
    commandLine 'D:\\android\\android-studio\\sdk\\platform-tools\\adb', 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}

那么我怎么能得到一個包含路徑的變量並將其傳遞給commandLine

您應該使用Android Gradle插件已有的邏輯來查找SDK和adb位置,以確保您的腳本使用相同的腳本。

# Android Gradle >= 1.1.0
File sdk = android.getSdkDirectory()
File adb = android.getAdbExe()

# Android Gradle < 1.1.0
File sdk = android.plugin.getSdkFolder()
File adb = android.plugin.extension.getAdbExe()

問題解決了。
變量必須包含

def adb = "$System.env.ANDROID_HOME/platform-tools/adb"

完成任務看起來像


task run(type: Exec) {
    def adb = "$System.env.ANDROID_HOME/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}

UPD
另一種不使用ANDROID_HOME


task run(type: Exec) {
    def rootDir = project.rootDir
    def localProperties = new File(rootDir, "local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { 
            instr -> properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        def adb = "$sdkDir/platform-tools/adb"
        commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
    }
}
def androidPlugin = project.plugins.findPlugin("android")
def adb = androidPlugin.sdkHandler.sdkInfo?.adb

在Windows中,您只需使用以下.reg文件注冊adb.exeapplication path

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\adb.exe]
@="D:\\android\\android-studio\\sdk\\platform-tools\\adb.exe"
"Path"="D:\\android\\android-studio\\sdk\\platform-tools"

並保持原來的命令行

我對此問題的默認解決方案是將adb添加到路徑變量中,以便您可以使用每個路徑中的adb命令。
您可以在控制台中設置它,如下所示:

set path=%path%;x:\path\to\adb

您也可以通過UI進行設置。 另請參閱java.com上的此解釋。

我們可以從android擴展獲得。

android.adbExe

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM