且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 build.gradle 中检索 ADB 的路径

更新时间:2023-11-24 21:43:58

问题解决了.
变量必须包含

The problem was solved.
The variable must contain

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'
    }
}