3

I`m moving my project to Gradle build system. After APK build I need to sign it with manufacturer certificate.

How to execute .bat file by Gradle after APK was built?

task runSign(type:Exec) {
    println "Sign apk..."
    commandLine = ['cmd','/c','sign.bat']
}

I know just how to run .bat before build (but I need after):

preBuild.doLast {
    runSign.execute()
}
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
yuralife
  • 1,545
  • 2
  • 21
  • 35
  • Is there a reason you can't use `signingConfigs`? http://stackoverflow.com/questions/18328730/how-to-create-a-release-signed-apk-file-using-gradle – Floern Jan 30 '16 at 15:37
  • @Floern I use custom certificate from manufacture, that is not usual debug/release sign. – yuralife Sep 29 '16 at 16:27

2 Answers2

2

I've found the solution.

Go to Run -> Edit Configurations...

Choose module where you want to run task after APK build. Add new configuration after "Gradle-aware Make".

Picture 1

Click on icon at picture below to choose module where task is implemented and write name of it.

Picture 2

After this steps your custom Gradle task will be executed after APK build.

yuralife
  • 1,545
  • 2
  • 21
  • 35
  • how to execute batch file after APK is generated at this location "\app\build\outputs\apk" ? My requirement is like once xyz.apk generated at "\app\build\outputs\apk" location I want to move this file to "D:/Xyz" location automatically. – Kush Patel Jan 25 '17 at 06:08
  • @KushPatel your batch file should be located in root of your module and be like: xcopy build\outputs\apk d:\Xyz (there is xcopy command on Windows platform) – yuralife Jan 25 '17 at 09:47
  • i want to execute batch file from gradle after apk is generated in gradle. – Kush Patel Jan 25 '17 at 09:49
1

I needed to perform something similar, but additionally to that I needed to know with which product favour was built, and with what configuration.

I've ended up adding following line into build.gradle:

android {
    applicationVariants.all { variant -> variant.assemble.doLast { signAndInstall.execute() } }
    ...

And with following helper function:

//
//  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)
}

With following 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

You can of course configure this batch to perform anything what you like, %1 receives your product favour (e.g. arm7, arm8, fat...), and %2 receives 'debug' or 'release' as configuration.

TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62
  • I got the error `Could not find method execute() for arguments [] on task` so I changed `execute()` to `exec()` – denispyr Jun 03 '20 at 17:11