APK构建后从Android gradle执行.bat

APK构建后从Android gradle执行.bat,android,batch-file,gradle,android-gradle-plugin,Android,Batch File,Gradle,Android Gradle Plugin,我正在将我的项目转移到Gradle构建系统。在APK构建之后,我需要在其上签署制造商证书 APK构建后Gradle如何执行.bat文件 task runSign(type:Exec) { println "Sign apk..." commandLine = ['cmd','/c','sign.bat'] } 我只知道如何在构建前运行.bat(但我需要在构建后运行): 我找到了解决办法 转到运行->编辑配置 选择要在APK生成后运行任务的模块。在“Gradle aware Ma

我正在将我的项目转移到Gradle构建系统。在APK构建之后,我需要在其上签署制造商证书

APK构建后Gradle如何执行.bat文件

task runSign(type:Exec) {
    println "Sign apk..."
    commandLine = ['cmd','/c','sign.bat']
}
我只知道如何在构建前运行.bat(但我需要在构建后运行):


我找到了解决办法

转到运行->编辑配置

选择要在APK生成后运行任务的模块。在“Gradle aware Make”之后添加新配置

单击下面图片中的图标,选择执行任务的模块,并写下该模块的名称


完成此步骤后,您的自定义Gradle任务将在APK构建后执行。

我需要执行类似的操作,但除此之外,我还需要知道构建的产品和配置

最后,我在build.gradle中添加了以下行:

android {
    applicationVariants.all { variant -> variant.assemble.doLast { signAndInstall.execute() } }
    ...
并具有以下辅助功能:

//
//  Returns array for CommandLine, path, variant (arm7), configuration (debug / release)
//
def getCommandLine(path)
{
    String taskReqStr = getGradle().getStartParameter().getTaskRequests().toString()
    Pattern pattern = Pattern.compile("(assemble|generate)(\\w+)(Release|Debug)")
    Matcher matcher = pattern.matcher(taskReqStr)
    if (!matcher.find())
        return [ path ]

    String flavor = matcher.group(2).toLowerCase() + " " + matcher.group(3).toLowerCase()
    return [ path, matcher.group(2).toLowerCase(), matcher.group(3).toLowerCase() ]
}

task signAndInstall(type: Exec) {
    def batch = projectDir.toString() + '\\postbuild.bat'
    commandLine = getCommandLine(batch)
}
使用以下
postbuild.bat

@echo off
rem echo %0 %*
if %1. == . exit /b 0
if %2. == . exit /b 0
set InPath=%~dp0build\outputs\apk\%1\%2\app-%1-%2.apk
set OutPath=%~dp0build\outputs\apk\app-%1-%2.apk
copy /y %InPath% %OutPath% 1>NUL

当然,您可以将此批处理配置为执行您喜欢的任何操作,%1接收您的产品支持(例如,
arm7、arm8、fat
..),而%2接收
'debug'或'release'
作为配置。

有什么原因不能使用
签名配置吗@Floern我使用来自制造商的自定义证书,这不是常见的调试/发布标志。在该位置“\app\build\outputs\APK”生成APK后如何执行批处理文件?我的要求类似于在“\app\build\outputs\apk”位置生成一次xyz.apk,我想自动将此文件移动到“D:/xyz”位置。@KushPatel您的批处理文件应该位于模块的根目录中,类似于:xcopy build\outputs\apk D:\xyz(Windows平台上有xcopy命令)在gradle中生成apk后,我想从gradle中执行批处理文件。我得到错误
无法为任务上的参数[]找到方法execute()
,因此我将
execute()
更改为
exec()
@echo off
rem echo %0 %*
if %1. == . exit /b 0
if %2. == . exit /b 0
set InPath=%~dp0build\outputs\apk\%1\%2\app-%1-%2.apk
set OutPath=%~dp0build\outputs\apk\app-%1-%2.apk
copy /y %InPath% %OutPath% 1>NUL