3

I have a project I recently had to convert from using a build.xml and custom_rules.xml to a build.gradle version in android studio. It was a pain in the @$$. There is very little documentation right now thats put together in a way to make it easy to do what I needed. One of the sticking points was signing my apk with two different methods. One was for a drm library but the other was just to have a signed apk. This is my solution. I just looked at the build.xml that is in the root of the android sdk tools and figured out how they use signapk. Check it out!

<?xml version="1.0" encoding="UTF-8"?>
<project>
   <!-- jar file from where the ANDROID tasks are loaded -->
   <path id="android.antlibs">
      <pathelement path="C:/Program Files (x86)/Android/android-studio/sdk/tools/lib/ant-tasks.jar" />
   </path>

   <!-- Custom ANDROID tasks -->
   <taskdef resource="anttasks.properties" classpathref="android.antlibs" />

   <target name="postPackage">
      <property name="config_path" location="${cert.dir}" />
      <property name="out.pre.signed.file" location="release-pre-sign.apk" />
      <property name="out.signed.file" location="release-signed.apk" />
      <echo>sign with special certificate</echo>
      <touch file="src/main/res/raw/wv.properties"/>
      <touch file="apk/release.apk"/>
      <!--<copy file="apk/V2.5-10-debug-.apk" tofile="apk/release.apk"/>-->
      <java jar="apksigtool.jar" failonerror="true" fork="true">
         <arg value="apk/V2.5-10-debug-.apk"/>
         <arg value="private_key.der" />
         <arg value="my.crt" />
      </java>

      <!-- THIS IS THE MAGICAL ANDROID BASED SIGNING METHOD THAT GETS IMPORTED -->
      <signapk
         input="apk/debug-.apk"
         output="apk/release.apk"
         keystore="apk/debug.keystore"
         storepass="android"
         alias="androiddebugkey"
         keypass="android"/>

      <!--FINALLY WHEN ITS ALL DONE AND SAID I COPY THE SIGNED APK BACK TO THE ORIGINAL FILE SO
         GRADLE CAN USE IT TO PASS INTO THE STUDIO SO I DONT HAVE TO LAUNCH THE APK FROM A
         COMMAND LINE -->

    <copy file="apk/release.apk" tofile="apk/debug-.apk"/>

 </project>

Then in my gradle build file I just use this

android.applicationVariants.all { variant ->

   variant.assemble.doLast {
      runTool(variant, apkLocation)
   }

   def manifestFile = file("C:\\app\\src\\main\\AndroidManifest.xml")
   def pattern = Pattern.compile("versionName=\"(.+)\"")
   def manifestText = manifestFile.getText()
   def matcher = pattern.matcher(manifestText)
   matcher.find()
   def versionName = matcher.group(1)
   pattern = Pattern.compile("versionCode=\"(.+)\"")
   matcher = pattern.matcher(manifestText)
   matcher.find()
   def versionCode = matcher.group(1)

   new File("apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+".apk");
        apkLocation = "apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+".apk";
   variant.outputFile = file("apk/"+apkName+"-V"+versionName+"-"+versionCode+"-"+variant.name+"-"+".apk");

   System.out.println("Non zipalign ran")

}

def runTool(variant, apkPath) {

   if(variant != null && apkPath != null) {
      System.out.println("--------------------- $apkPath ------------------");

      task (runApkSigTool , type: JavaExec) { taskGraph ->
         ant.importBuild 'build.xml'
         postPackage.doFirst{println("IM done")}
         postPackage.execute()

         System.out.println("---------------------runApkSigtool ran for debug------------------");
      }
   }
}
James andresakis
  • 5,335
  • 9
  • 53
  • 88
  • Stack Overflow is for programming questions. What is your question? – CommonsWare Sep 25 '14 at 21:39
  • The [Android Gradle Plugin](http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Signing-Configurations) has built-in support for signing. Is there a reason you want to use Ant other then the fact that it used to be an Ant based build? – Mark Vieira Sep 25 '14 at 22:09
  • @MarkVieira yes when using widevine drm you have to run a special jar tool on the unsigned apk. I couldnt figure out how to do that all in gradle at the moment because I couldnt find any clear solution that worked for me so I back tracked to my original custom_rules.xml I was using and just made it work for the time being. – James andresakis Sep 25 '14 at 22:18
  • @CommonsWare I had asked many questions leading up to my solution so I find it appropriate to leave it here on SO where I end up a lot of the time digging for info. Someone has already up ticked the solution so it has to have helped already :) – James andresakis Sep 25 '14 at 22:20
  • 1
    Stack Overflow is not a blog. If you want to provide information like this, ask an actual question, then answer it yourself. Please see [the site's documentation](http://stackoverflow.com/help/self-answer) for more. – CommonsWare Sep 25 '14 at 22:33
  • What is `runApkSigTool`? It is not documented anywhere. – IgorGanapolsky Jul 25 '16 at 14:31
  • 1
    @IgorGanapolsky it's a signing tool that is used for applications that use the older software only version of widevine. It was a pain in the but to set up and they kept it all locked up with little info on how everything worked. Its no longer supported. – James andresakis Aug 08 '16 at 21:38

0 Answers0