Skip to content

Instantly share code, notes, and snippets.

@pboos
Last active July 6, 2020 08:56
Show Gist options
  • Save pboos/c4ae04b62641d7e9897c to your computer and use it in GitHub Desktop.
Save pboos/c4ae04b62641d7e9897c to your computer and use it in GitHub Desktop.
gradle-s3-upload

Android publish APK to S3

build.gradle in your project which generates the apk:

apply from:'https://gist.githubusercontent.com/pboos/c4ae04b62641d7e9897c/raw/45515edf3cf00925b59a51d0ca893c44a2b06851/gradle-s3-upload.gradle'

~/.gradle/gradle.properties

AWS_S3_KEY_ID=****Your Amazon Key Id****
AWS_S3_KEY_SECRET=****Your Amazon Key Secret****

gradle.properties in your project which generates the apk:

AWS_S3_APK_PREFIX=MyApp
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.MessageDigest
import java.text.SimpleDateFormat
def publishAllTask = project.tasks.create("publishAllApksToS3")
android.applicationVariants.all { variant ->
def task = project.tasks.create("publish${variant.name}ApkToS3") << {
if (!project.hasProperty('AWS_S3_KEY_ID') || !project.hasProperty('AWS_S3_KEY_SECRET')) {
throw new GradleException("Please set AWS_S3_KEY_ID and AWS_S3_KEY_SECRET properties.")
}
def bucket = "apps.pboos.ch"
def apkFile = variant.outputs[0].outputFile
def prefix = project.hasProperty('AWS_S3_APK_PREFIX') ? project.AWS_S3_APK_PREFIX + "-" : ""
def targetPath = prefix + variant.name + ".apk"
// def contentType = "application/octet-stream"
def contentType = "application/vnd.android.package-archive"
def keyId = project.AWS_S3_KEY_ID
def keySecret = project.AWS_S3_KEY_SECRET
def date = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss ZZZZ").format(new Date())
def digest = MessageDigest.getInstance("MD5")
apkFile.eachByte(4096) { buffer, length ->
digest.update(buffer, 0, length)
}
def md5 = digest.digest().encodeBase64()
def publicReadHeader = "x-amz-acl:public-read"
def data = "PUT\n$md5\n$contentType\n$date\n$publicReadHeader\n/$bucket/$targetPath"
def signingKey = new SecretKeySpec(keySecret.getBytes(), "HmacSHA1");
def mac = Mac.getInstance("HmacSHA1");
mac.init(signingKey);
def sig = mac.doFinal(data.getBytes()).encodeBase64()
ProcessBuilder pb = new ProcessBuilder([
'curl',
'-T', apkFile.getAbsolutePath(),
'-H', publicReadHeader,
'-H', 'Date: ' + date,
'-H', 'Authorization: AWS ' + keyId + ':' + sig,
'-H', 'Content-Type: ' + contentType,
'-H', 'Content-MD5: ' + md5,
'http://' + bucket + '.s3.amazonaws.com/' + targetPath
])
pb.start().waitFor()
}
task.dependsOn variant.assemble
publishAllTask.dependsOn task
}
@anchit1704
Copy link

If you are running it from a cmd terminal, try running it from git shell or cygwin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment