我试图通过Gradle脚本自动安装一些Android SDK工具。
我们的想法是在Docker机器中运行这个build.gradle脚本,该脚本将在发布主构建之前准备环境。
我遇到的问题是自动接受我安装的相关软件包的许可证。
关注this SO question后,我尝试使用此方法:
echo "y" | android update sdk -u -a -t 2,4,56,57,58
如果我在终端中运行它,它在一个命令中最多可以运行6个包,但如果我想安装更多的包,例如:
echo "y" | android update sdk -u -a -t 2,6,7,4,30,153,160,161,167,54,53,63,56,57,58,59
然后命令失败:
Do you accept the license 'intel-android-extra-license-3626590a' [y/n]:
Unknown response ''.
Do you accept the license 'intel-android-extra-license-3626590a' [y/n]:
Unknown response ''.
Max number of retries exceeded. Rejecting 'intel-android-extra-license-3626590a'
Package Android TV Intel x86 Atom System Image, Android API 24, revision 6 not installed due to rejected license 'android-sdk-preview-license-d099d938'.
Package Android Wear ARM EABI v7a System Image, Android API 24, revision 1 not installed due to rejected license 'android-sdk-preview-license-d099d938'.
Package ARM 64 v8a System Image, Android API 24, revision 6 not installed due to rejected license 'android-sdk-preview-license-d099d938'.
更重要的是,我需要Gradle为我运行它,当我这样做时,似乎第一个回声(echo" y")的输出由于某种原因没有被重定向到管道
所以当我运行相关的Gradle任务时:
12:46:01.178 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTING
12:46:01.179 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Waiting until process started: command '/bin/echo'.
12:46:01.183 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTED
12:46:01.183 [DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled...
12:46:01.183 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'command '/bin/echo''
12:46:01.184 [QUIET] [system.out] y | /usr/local/bin/android update sdk -u -a -t 2,6,7,4,30,153,160,161,167,54,53,63,56,57,58,59
12:46:01.184 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: SUCCEEDED
12:46:01.185 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Process 'command '/bin/echo'' finished with exit value 0 (state: SUCCEEDED)
12:46:01.185 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':installSdkBuildTools'
12:46:01.185 [INFO] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] :installSdkBuildTools (Thread[main,5,main]) completed. Took 0.023 secs.
12:46:01.185 [DEBUG] [org.gradle.execution.taskgraph.AbstractTaskPlanExecutor] Task worker [Thread[main,5,main]] finished, busy: 0.023 secs, idle: 0.001 secs
12:46:01.185 [DEBUG] [org.gradle.execution.taskgraph.DefaultTaskGraphExecuter] Timing: Executing the DAG took 0.044 secs
12:46:01.185 [LIFECYCLE] [org.gradle.BuildResultLogger]
12:46:01.186 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD SUCCESSFUL
echo命令以退出状态0成功结束,但未安装软件包。
我已尝试将命令分解为3个迷你命令,每次安装6个包,但如上所述,echo "y" | android...
命令无法正确解释。
相关的Gradle任务如下所示:
task installSdkBuildToolsPart2(type: Exec) {
commandLine '/bin/echo', "y", '|', '/usr/local/bin/android ', 'update ', 'sdk ', '-u -a -t 160,161,167,54,53'
}
我也尝试过这样但无济于事:
task installSdkBuildToolsPart1(type: Exec) {
commandLine 'bash', '-c'," '/bin/echo', "y", '|', '/usr/local/bin/android ', 'update ', 'sdk ', '-u -a -t 2,6,7,4,30,153'"
}
这是一个Gradle问题...我应该如何正确地在build.gradle文件中编写commandLine
指令,以便正确解释命令并安装软件包并接受许可证?< /强>
任何人都知道如何克服这个问题?
提前致谢,
答案 0 :(得分:1)
你的问题是Gradle中的exec任务不知道管道,所以你写的只是传递一些字符串来回显; “y”,管道“|”,android路径,更新命令等
执行此操作的一种方法是将要执行的包含管道或其他shell重定向字符的命令放入shell脚本文件中,然后让Gradle任务执行该shell脚本。
答案 1 :(得分:0)
我们有这个命令,它对我们来说非常有用:
( sleep 5 && while [ 1 ]; do sleep 1; echo y; done ) \
| android update sdk --all -u -t