Execute java program at the end of gradle build

Lets say I have a compiled Java programm called launchProgramm.class. It takes 2 parametes of String. And I have a gradle build script of some modules. The high-lvl script looks like this :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}
  allprojects {
    repositories {
        mavenCentral()
    }
}

and I have a script in the main module :

apply plugin: ‘com.android.application’

def getVersionName = { ->
    try {
        def stdout = new ByteArrayOutputStream()
        exec {
            commandLine /*something*/
            standardOutput = stdout
        }
        def date = new Date()
        def formattedDate = date.format('_yyyy_MM_dd_HH_mm_ss')
        return stdout.toString().trim() + formattedDate
    }
    catch (ignored) {
        return "unknown version";
    }
}
    android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'
    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName getVersionName()
    }
    signingConfigs {
        debug {
            storeFile ...
            storePassword ...
            keyAlias ...
            keyPassword ...
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            }
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
            }
      lintOptions {
        abortOnError false
    }
    productFlavors {
    }
  }
  dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:19.+'
    compile project(': prg1')
    compile project(':prg2')
    compile project(':prg3')
    compile project(':prg4')
}

I need to do some things with .apk file after all gradle build is finished. Lets say I want to copy it to another directory or open it and change some files. Is ip possible?

Take a look at http://www.gradle.org/docs/current/javadoc/org/gradle/api/Project.html#javaexec(org.gradle.api.Action)

Where can I use that? Where is the end of building? I’m new to gradle

Why do you want to do this at the end of the build, rather than as a regular task? What if no apk was built?

Sorry, I misread your original question (I thought you were trying to fill in your /something/ block).

You can rename the APK (see http://stackoverflow.com/questions/18507660/renaming-apk-with-gradle-getting-same-apk-twice) if that’s why you want to copy it.  It would be a little weird to always copy your APK to a particular directory.

What do you want to copy into the APK?

Lets say - some digital sign by my own algorythm. And I want to check it when I update my app.

I’m not sure what you’re trying to do. You can add additional actions to the APK creation steps (e.g., packageApplication.doLast() { … }), but I don’t know if that’s what you’re trying to do (or if it would make sense as a separate task or something else entirely).

Maybe you can try the Android Developer Group?

Well, I want to unzip *.apk file, count hash from some files and put a *.txt file with hashes to META/ folder. I can do it only when *.apk file is ready. may be there is another way?