Android(安卓)ANT 如何编译Jar 和 APK
16lz
2022-05-05
最近在研究Android 的Ant 编译环境,遂将经验总结写下来。大家都知道我们可以在eclipse 里面编译android 的工程,这是怎么完成的呢?它主要是由于ADT 插件调用android的build tools完成的。其实eclipse 的编译也是通过Ant 脚本来完成编译的。/adnroid-sdk/tools/ant/build.xml 这就是ADT 插件调用的ant脚本,通过执行这个脚本就可以在完成android工程的编译。
那么如果不依赖ADT 插件,我们可以完成android 工程的编译呢?当然可以,首先必须安装Apache Ant,目前最新版本是1.9.4,下载解压放到android sdk 目录下,然后将其bin路径添加到环境变量PATH 。最后只需要在你的android 工程目录下编写build.xml 文件。
1. 如何编译Jar 文件,啥也不说了,直接上代码
下面为编译脚本build.sh 文件,最终会调用build.xml 文件。local.properties 文件是Ant 自动生成的,里面会自动指定 sdk.dir 变量,另外我们还手动指定了一个me.version,指定编译的版本号。
#!/bin/bashPROP_FILE=local.propertiesPROP_VERSION_KEY=me.versionVERSION=`cat ${PROP_FILE} | grep ${PROP_VERSION_KEY} | cut -d'=' -f2`GITSHA1=`git log | sed -n '1p' | awk '{print $2}'`MTL_OUTPUT=targetecho '##################################################### Android Build system####################################################'#预设置android sdk目录android update project -p .#1.编译jnindk-build cleanndk-build#2.编译jarant -f build.xmlecho '################################################### Make the tar package ##################################################'tar -cvf ${MTL_OUTPUT}/aliroot_${VERSION}_${GITSHA1}.tar.gz sdk
local.properties 文件:
# This file is automatically generated by Android Tools.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must *NOT* be checked into Version Control Systems,# as it contains information specific to your local configuration.# location of the SDK. This is only used by Ant# For customization when using a Version Control System, please read the# header note.sdk.dir=/Users/mars/development/AndroidSDK/sdkme.version=1.1.0
build.xml 文件:
Compiling .aidl into java files... Compiling java source code... Start proguard obfuscate... -injar ${classes.obj.bin.dir}/${jarname} -outjar ${output.dir}/${obfuscate.jarname} -libraryjar ${android-jar} -libraryjar ${libs.dir}/securityguard-2.3.39.jar -libraryjar ${libs.dir}/umid-1.3.0.jar
proguard-project.txt 文件:
-optimizationpasses 5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontpreverify#-dontwarn-verbose-optimizations !code/simplification/arithmetic,!class/merging/*-keepattributes InnerClasses,Exceptions-keep public class * extends android.app.Activity-keep public class * extends android.app.Application-keep public class * extends android.app.Service-keep public class * extends android.content.BroadcastReceiver-keep public class * extends android.content.ContentProvider-keep public class * extends android.app.backup.BackupAgentHelper-keep public class * extends android.preference.Preference-keep public class com.android.vending.licensing.ILicensingService-keepclasseswithmembernames class * { native ;}-keepclasseswithmembers class * { public (android.content.Context, android.util.AttributeSet);}-keepclasseswithmembers class * { public (android.content.Context, android.util.AttributeSet, int);}-keepclassmembers class * extends android.app.Activity { public void *(android.view.View);}-keepclassmembers enum * { public static **[] values(); public static ** valueOf(java.lang.String);}-keep class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *;}-keep interface com.xxx.zzz{public *;}-keep class com.yyy.JNIHelper-keepclassmembers class com.yyy.JNIHelper{public *;}
还需要在project.properties 文件中指定proguard.config. 下面是project.properties 文件:
# This file is automatically generated by Android Tools.# Do not modify this file -- YOUR CHANGES WILL BE ERASED!## This file must be checked in Version Control Systems.## To customize properties used by the Ant build system edit# "ant.properties", and override values to adapt the script to your# project structure.## To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt# Project target.target=android-19android.library=falseproguard.config=proguard-project.txt
2. 如何编译APK文件:build.sh,local.properties 文件都跟上面一样。只需要修改build.xml 文件:
<?xml version="1.0" encoding="UTF-8"?> Creating all output directories Generating R.java / Manifest.java from the resources... Compiling aidl files into Java classes... Compiling java source code... Converting compiled files and external libraries into ${outdir-bin}/${dex-file}... Packaging resources and assets... jarsigner ${out-unsigned-package} for release... jarsigner for release succefully...
更多相关文章
- android:background="@drawable/"图片拉伸问题
- 应用组件-应用程序介绍
- android user版本进行编译出现proguard错误混淆
- Android(安卓)NDK
- Android打包成jar文件方法总结
- Android中的Adapter 详解(三)
- Android中的数据持久化技术
- Android(安卓)Studio 简单介绍和使用问题小结
- NPM 和webpack 的基础使用