在build.gradle文件中使用ant签署apk的最佳方法

时间:2014-09-25 21:26:41

标签: android ant gradle apk

我有一个项目,我最近不得不使用build.xml和custom_rules.xml转换为android studio中的build.gradle版本。这是@ $$的痛苦。现在很少有文档可以很容易地完成我需要的工作。其中一个关键点是用两种不同的方法签署我的apk。一个是drm库,另一个是签名apk。这是我的解决方案。我只是看了android sdk工具根目录下的build.xml,并弄清楚他们如何使用signapk。看看吧!

<?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>

然后在我的gradle构建文件中,我只使用此

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

0 个答案:

没有答案