简体   繁体   中英

Having android gradle open windows cmd and run a command after build done?

I'm building an android module with this standard build script:

buildscript {// top-level    
  repositories {
      google()
      jcenter()
  }
  dependencies {
      classpath 'com.android.tools.build:gradle:3.0.1'
  }
}
allprojects {
  repositories {
      google()
      jcenter()
  }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

I've googled a lot to know how to have gradle to open windos cmd (I'm on win7) and run a command. I found tasks Exec and added, but it didn't work for me, perhaps I don't still understand groovy and gradle. Please make me clear how to do it.

Edit
By using a forum post mentioned in comments, I added this to android{..} section:

    task testingit(type:Exec) {
      println('try open cmd...............')
      commandLine "cmd","/k",'node', '-v'
    }

I get the string output but cmd does not open and no error message.

If you want to execute this task at the end of build, you can use build.finalizedBy .

task endBuildTask(type: Exec) {
   commandLine  'cmd','/c','start', 'cmd.exe' ,'@cmd' , '/k', 'echo', 'Hello'
}
build.finalizedBy(endBuildTask)
// or gradle.buildFinished{endBuildTask.execute()}

This will create a new command line at the end of build, pass the command after @cmd to the newly created command line. If you want this new command line to close after command executes, use '/c' instead of '/k'.

I just post back how I put the accepted answer from above in my application, in case useful for anyone (because build.finalizedBy(endBuildTask) didn't work and I can't understand why, but gradle.buildFinished(...) did the job)

This is how I got it working:

task endBuildTask(type: Exec) {
  workingDir 'C:\\tmp\\1\\'
  commandLine  'cmd','/c','node', '%FILE_JS%'
}

gradle.buildFinished{
    endBuildTask.execute()
}

I had a nodejs app that I wanted to execute after android build is over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM