Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
481 views
in Technique[技术] by (71.8m points)

android - Best way to sign an apk using ant in a build.gradle file

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------------------");
      }
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...