这个主要就是用ant+proguard+签名来实现自动打包的,一些解释都在代码里,在这里就不多说了,现在开始把上代码吧。
1 2 3default="deployableAllDevice"> 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 26 27 28 29 32 33 34 3935 36 37 38 40 import="false" /> 41 42 <!-- Custom tasks --> 43 44 45 46 47 48 <!-- Properties --> 49 <!-- Tells adb which device to target. You can change this from the command line 50 by invoking "ant -Dadb.device.arg=-d" for device "ant -Dadb.device.arg=-e" for 51 the emulator. --> 52 53 54 55 <!-- Name of the application package extracted from manifest file --> 56 57 58 <!-- Value of the hasCode attribute (Application node) extracted from manifest file --> 59 default="true" /> 60 <!-- 源文件及资源路径 --> 61 62 63 64 65 66 67 68 69 <!-- Directory for the third party java libraries --> 70 71 72 <!-- create a path with all the jar files, from the main project and the 73 libraries --> 74 75 78 <!-- Directory for the native libraries --> 7976 77 80 81 <!-- 输出路径 --> 82 83 84 85 86 <!-- Intermediate files --> 87 88 89 90 91 <!-- The final package file to generate 92 93 These can be overridden by setting them earlier to 94 95 different values --> 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 <!-- set some properties used for filtering/override. If those weren't defined 118 119 before, then this will create them with empty values, which are then ignored 120 121 by the custom tasks receiving them. --> 122 123 124 125 126 127 128 129 <!-- java源文件编码,编译的目标平台,为1.5 or 1.6都可以 --> 130 131 132 133 134 135 136 <!-- Verbosity --> 137 138 <!-- Verbosity --> 139 140 <!-- This is needed by emma as it uses multilevel verbosity instead of simple 'true' or 'false' 141 The property 'verbosity' is not user configurable and depends exclusively on 'verbose' 142 value.--> 143 else="quiet"> 144 146 <!-- This is needed to switch verbosity of zipalign. Depends exclusively on 'verbose' 147 --> 148145 else=""> 149 151 <!-- This is needed to switch verbosity of dx. Depends exclusively on 'verbose' --> 152150 else=""> 153 155 <!-- properties for signing in release mode --> 156154 157 163158 162159 160 161 164 170 <!-- Tools --> 171165 169166 167 168 else=""> 172 174173 175 176 <!-- Emma configuration --> 177 178 179 182180 181 183 <!-- End of emma configuration --> 184 <!-- Macros --> 185 <!-- Configurable macro, which allows to pass as parameters output directory, 186 output dex filename and external libraries to dex (optional) --> 187 188 218 219 <!-- This is macro that enable passing variable list of external jar files to ApkBuilder 220 Example of use: 221 <package-helper output.filepath="/path/to/foo.apk"> 222189 190 191 <!-- sets the primary input for dex. If a pre-dex task sets it to 192 something else this has no effect --> 193 217194 <!-- set the secondary dx input: the project (and library) jar files 195 If a pre-dex task sets it to something else this has no effect --> 196 <if> 197 198 200 <else> 201199 202 204 else> 205 if> 206203 Converting compiled files and external libraries into ${intermediate.dex.file}… 207208 216209 210 211 212 213 214 215 223 227 package-helper> --> 228224 225 226 229 244 <!-- This is macro which zipaligns in.package and outputs it to out.package. Used by targets 245 debug, -debug-with-emma and release.--> 246230 231 232 243233 242234 235 236 237 238 239 240 241 247 260 <!-- This is macro used only for sharing code among two targets, -install and 261 -install-with-emma which do exactly the same but differ in dependencies --> 262248 249 250 259Running zip align on final apk… 251252 258253 254 255 256 257 263 273 <!-- Rules --> 274 <!-- Creates the output directories if they don't exist yet. --> 275264 272Installing ${out.debug.file} onto default emulator or device… 265266 271267 268 269 270 276 287 <!-- empty default pre-compile target. Create a similar target in 288 your build.xml and it'll be called instead of this one. --> 289Creating output directories if needed… 277278 279 280 <if condition="${manifest.hasCode}"> 281 282 285 if> 286283 284 290 <!-- Compiles this project's .java files into .class files. --> 291 292 <if condition="${manifest.hasCode}"> 293 316 <!-- empty default post-compile target. Create a similar target in 317 your build.xml and it'll be called instead of this one. --> 318294 <!-- If android rules are used for a test project, its classpath should include 295 tested project's location --> 296 311 <else> 312else="."> 297 299298 else="${jar.libs.dir}"> 300 302301 303 310304 305 306 307 309308 hasCode = false. Skipping… 313 else> 314 if> 315319 <!-- Obfuscate target 320 This is only active in release builds when proguard.config is defined 321 in default.properties. 322 To replace Proguard with a different obfuscation engine: 323 Override the following targets in your build.xml, before the call to 324 -release-obfuscation-check 325 Check whether obfuscation should happen, and put the result in a property. 326 -debug-obfuscation-check 327 Obfuscation should not happen. Set the same property to false. 328 -obfuscate 329 ** Make sure unless="do.not.compile" is used in the target definition ** 330 check if the property set in -debug/release-obfuscation-check is set to true. 331 If true: 332 Perform obfuscation 333 Set property out.dex.input.absolute.dir to be the output of the obfuscation 334 --> 335 336 <if condition="${proguard.enabled}"> 337 393338 391 392339 340 341 <!-- input for dex will be proguard's output --> 342 343 344 345 346 347 348 351 352 353 355 356 357 360 361 364 365358 359 366 367 368 369 370 371 372 -optimizationpasses 5 373 -dontusemixedcaseclassnames 374 -dontskipnonpubliclibraryclasses 375 -dontpreverify 376 -verbose 377 -repackageclasses 378 -allowaccessmodification 379 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* 380 -keep public class * extends android.app.Activity 381 -keep public class * extends android.app.Application 382 -keep public class * extends android.app.Service 383 -keep public class * extends android.content.BroadcastReceiver 384 -keep public class * extends android.content.ContentProvider 385 -keep public class com.android.vending.licensing.ILicensingService 386 -injars ${project.jars} 387 -outjars ${obfuscated.jar.file} 388 -libraryjars ${android.libraryjars} 389 390394 395 396 <!-- Converts this project's .class files into .dex files --> 397 <!-- --> 398399 400 401 402 403 404 hasCode = false. Skipping… 405 406 407 408409 410 411 412 <!-- 创建文件夹--> 413 <!--别人的 --> 414 415 416 417 418 <!-- 原来的类文件,使用Bin/classes下的--> 419 420 <!-- 生成的混淆Class位置--> 421 422 <!-- 423 424 --> 425 426 427 428 429 430 431 432 433 434 435 436 437 <!-- --> 438 439 440 <!--这些是原来的Jar --> 441 <!-- --> 442 <!-- 443 444 --> 445 446 447 448 <!-- Puts the project's resources into the output package file 449 This actually can create multiple resource package in case 450 Some custom apk with specific configuration have been 451 declared in default.properties. 452 --> 453 454 Packaging resources 455456 457 <!-- forces no compression on any files in assets or res/raw --> 458 <!-- forces no compression on specific file extensions in assets and res/raw --> 459 460 461 <!-- Packages the application and sign it with a debug key. --> 462 463 464 465 <!-- Packages the application without signing it. --> 466 467 468 469 470 471 472 473 474 475 <!-- proguard is never enabled in debug mode --> 476 477 478 479 <!-- property only set in debug mode. 480 Useful for if/unless attributes in target node 481 when using Ant before 1.8 --> 482 483 <!-- whether the build is a debug build. always set. --> 484 485 <!-- signing mode: debug --> 486 487 488 489 <!-- Builds debug output package, provided all the necessary files are already dexed --> 490 491 492 Debug Package: ${out.debug.file} 493 494 <!-- called through target 'release'. Only executed if the keystore and 495 key alias are known but not their password. --> 496497 <!-- Gets passwords --> 498 Gets passwords ${has.keystore} ${has.password} 499 500 501 502 <!-- called through target 'release'. Only executed if there's no 503 keystore/key alias set --> 504505 No key.store and key.alias properties found in build.properties. 506Please sign ${out.unsigned.file} manually 507and run zipalign from the Android SDK tools. 508 509510 511 512 513 514 515 516 517 518 519 <!-- Secondary dx input (jar files) is empty since all the 520 jar files will be in the obfuscated jar --> 521 522 523 524 525 526 <!-- release mode is only valid if the manifest does not explicitly 527 set debuggable to true. default is false. 528 We actually store build.packaging.debug, not build.release --> 529 530 <!-- signing mode: release --> 531 532 533 534 ************************************************* 535**** Android Manifest has debuggable=true **** 536**** Doing DEBUG packaging with RELEASE keys **** 537************************************************* 538 539540 <!-- property only set in release mode. 541 Useful for if/unless attributes in target node 542 when using Ant before 1.8 --> 543 544 545 546 547 548 <!-- This runs -package-release and -release-nosign first and then runs 549 only if release-sign is true (set in -release-check, 550 called by -release-no-sign)--> 551 552 it is published."> 553 <!-- Signs the APK --> 554Signing final apk… 555556 <!-- Zip aligns the APK --> 557 558 Release Package: ${out.release.file} 559 560561 562 563 564 565 <!-- uid和sdk都是自定义参数,可有可无,有多小个包要打,就在这里copy多小行,修改相关参数,传入到程序里即可 --> 566 567 568 569