Last active
April 15, 2019 12:49
-
-
Save har5hit/153facd7f8ec36feb88609c8fbf55434 to your computer and use it in GitHub Desktop.
Gradle script to generate App Links for all app variants in an Android project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Created by Harshith Shetty on 11/4/19 12:37 PM. | |
* Copyright (c) 2019 People Interactive. All rights reserved. | |
* | |
*/ | |
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
import groovy.json.JsonBuilder | |
/** | |
* @see <a href="https://developer.android.com/training/app-links">Handling Android App Links</a> | |
*/ | |
ext.getDate = { | |
new Date().format('dd-MMM-yyyy_hh_mm') | |
} | |
task createJson { | |
group = "App Linking" | |
description = "Generate App links Json for Android." | |
doLast { | |
List<AppLink> allApps=new LinkedList<>() | |
android.productFlavors.each { Object product -> | |
try { | |
if (product.signingConfig != null) { | |
def sha256 = getSha256(product.signingConfig.storeFile, product.signingConfig.storePassword) | |
allApps.add(new AppLink(product.applicationId, sha256)) | |
} | |
}catch(Exception e){ | |
println("error for ${product.name}") | |
println("${e.printStackTrace()}") | |
} | |
} | |
println(allApps.toString()) | |
createFile("appLinks/",allApps.toString()) | |
} | |
} | |
static def getSha256(keyFile,password) | |
{ | |
def command = "keytool -list -v -keystore $keyFile -storepass $password".execute() | |
def output = command.inputStream.readLines() | |
def SHA256 = output[17].trim().replace("SHA256: ", "") | |
return SHA256 | |
} | |
/** | |
* Creates a file in app/build/appLinks folder with the applinks json | |
*/ | |
def createFile(dir,content){ | |
def date=getDate() | |
File file=new File("$project.buildDir/$dir/") | |
file.mkdirs() | |
File outputFile=new File("${file.absolutePath}/AppLink_${date}.json") | |
outputFile.text= content | |
println("Json file available at ${outputFile.absolutePath}") | |
} | |
class AppLink{ | |
List<String> relation | |
Target target | |
AppLink(packageName,sha256){ | |
relation= ["delegate_permission/common.handle_all_urls"] | |
target=new Target(packageName) | |
target.sha256_cert_fingerprints=[sha256] | |
} | |
def json(){ | |
def builder=new JsonBuilder() | |
builder{ | |
relation relation | |
target( | |
package_name: target.package_name, | |
namespace: target.namespace, | |
sha256_cert_fingerprints: target.sha256_cert_fingerprints | |
) | |
} | |
return builder.toPrettyString() | |
} | |
@Override | |
String toString() { | |
return json().toString() | |
} | |
} | |
class Target{ | |
List<String> sha256_cert_fingerprints | |
String package_name | |
String namespace | |
Target(packageName){ | |
this.package_name=packageName | |
namespace="android_app" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment