Extract signature from keystore in Android Gradle build

I need an approach to retrieve a signature from a keystore in a build procedure. We already have it in Java this way:

PackageInfo packageInfo = Application.getContext().getPackageManager().getPackageInfo(Application.getContext().getPackageName(), PackageManager.GET_SIGNATURES);
for (Signature signature : packageInfo.signatures) {
    byte[] ba = signature.toByteArray());
    // do actions with signatures here ....
}

      

But we don't want this to be done in Java anymore. Instead, we need the same thing as in the Gradle build, and the result of the actions should be written to BuildConfig constants or some generated class.

So, currently my plan is below:

  • take the signature (first) from the keystore;
  • run JavaExec to follow my steps on signature and return result. Actions - Get the SHA of this signature and apply Base64 encoding the result. Another possible problem that can arise is that not all tasks are available in the android Gradle plugin. Therefore, it may be possible that I will not be able to start JavaExec;
  • write the result to one of the BuildConfig constants with a trick like this ["buildConfigField" string "," signatureSHA ", result"]. Again, not sure if it will work, as I have no idea if the BuildConfig class is generated before or after any other actions in the build.

How can I get the signature from the keystore in step 1?

+3


source to share





All Articles