How to retrieve path to ADB in build.gradle

前端 未结 6 1327
無奈伤痛
無奈伤痛 2021-02-05 13:01

I trying to start application via gradle task.


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

task run(type: Exec) {
comm         


        
6条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-05 13:24

    The problem was solved.
    The variable must contain

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

    And complete task looks like

    
    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
    Another way without using 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'
        }
    }
    

提交回复
热议问题