Best way to sign apk using ant in build.gradle file

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

<?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------------------");
      }
   }
}

      

+3


source to share





All Articles