忙了三四周,总算把购开心的ios版(appStore搜“购开心”)成功翻译成了android版,UI效果基本保持一致,上一张UI效果图吧,很养眼的:
还不错吧,进入正题吧。
以前一直是让我徒弟混淆和打包的,这个就剩下我一个人了,弄了一次,时间太久了,还太麻烦了,于是,我就研究了下ant打包,下面就简单说下吧:
首先,做任何开发前,肯定是各种环境搭建和配置啊,ant的搭建很简单:
一、安装ant
到官方主页http://ant.apache.org下载新版(目前为Ant-1.9.2)的ant,得到的是一个apache-ant-1.8.1-bin.zip的压缩包。将其解压到你的硬盘上,例如:D:\Android\apache-ant-1.9.2
二、配置环境变量
window中设置ant环境变量:
ANT_HOME D:/Android/apache-ant-1.9.2
path D:/Android/apache-ant-1.9.2/bin
classpath D:/Android/apache-ant-1.9.2/lib
linux中设置ant环境变量:
root用户登陆,用vi编辑器修改root目录下的".bash_profile"属性文件,假设Ant安装在/user/local/ant目录下,要在文件的最后加上
"exportANT_HOME=/user/local/ant"和"export PATH=$PATH:$ANT_HOME/bin"
三、验证ant
为了验证ant是否成功安装,可以进行如下操作:
依次选择:开始->运行->cmd,输入如下命令:ant
如果出现如下内容,说明安装成功:
Buildfile: build.xml does not exist!
Build failed
当然,JAVA_HOME、ANDROID_SDK_HOME等的配置这里就不说了
四、我们就在项目中添加build.xml
code如下:
如果不需要混淆代码,去掉上面的 proguard相关的配置就可以了Initializing all output directories... Generating R.java from the resources... Compiling .aidl into java files... Compiling java source code... Proguad .... Converting compiled files and external libraries into a .dex file... Packaging resources and assets... Packaging unsigned apk for release... It will need to be signed with jarsigner before being published. Packaging signed apk for release... delete unsigned apk copy apk from ${out-signed-package-ospath} to D:/workspaceAndroid/Release/happyShopping.apk clean the workspace bin folder....
五、配置proguard属性:
proguard-project.txt
# To enable ProGuard in your project, edit project.properties# to define the proguard.config property as described in that file.## Add project specific ProGuard rules here.# By default, the flags in this file are appended to flags specified# in ${sdk.dir}/tools/proguard/proguard-android.txt# You can edit the include path and order by changing the ProGuard# include property in project.properties.## For more details, see# http://developer.android.com/guide/developing/tools/proguard.html# Add any project specific keep options here:# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {# public *;#}-optimizationpasses 5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-dontskipnonpubliclibraryclassmembers-dontskipnonpubliclibraryclasses-dontpreverify-libraryjars libs/android-support-v4.jar-libraryjars libs/core.jar-verbose-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*-dontwarn android.support.v4.**-keep class android.support.v4.app.** { *; }-keep interface android.support.v4.app.** { *; }-keep class com.actionbarsherlock.** { *; }-keep interface com.actionbarsherlock.** { *; }-keepattributes *Annotation*-keep public class * extends android.app.Activity-keep public class * extends android.support.v4.app.Fragment-keep public class * extends android.support.v4.app.FragmentActivity-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 * extends blockcheng.BssicAct-keep public class * extends blockcheng.controller.BCFragment-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);}-keepclassmembers class * { public (org.json.JSONObject);}-keep public class blockcheng.R$*{ public static final int *;}-keep public class blockcheng.controller.BlockchengClass.JavaScriptInterface { public ;}-keepnames class blockcheng.controller.BlockchengClass$* { public ; public ;}-keepnames class blockcheng.controller.BlockchengClass$* { public ; public void showContacts();}
上面的配置中,blockcheng、BC等开头的类,都是被我替换的,不是真实的,具体的类包名和类名,以各人自己的为主。
注意,这个混淆中,开始几行配置很重要,网上很多同学问的ant、proguard相关的错误(什么找不到app.android.v4类等),都是因为缺少这几行导致,但没人回答,这里,我就直接给出了:
-libraryjars libs/android-support-v4.jar-libraryjars libs/core.jar-verbose-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*-dontwarn android.support.v4.**-keep class android.support.v4.app.** { *; }-keep interface android.support.v4.app.** { *; }-keep class com.actionbarsherlock.** { *; }-keep interface com.actionbarsherlock.** { *; }-keepattributes *Annotation*-keep public class * extends android.app.Activity-keep public class * extends android.support.v4.app.Fragment-keep public class * extends android.support.v4.app.FragmentActivity
这个配置主要是针对 android-support.v4和库相关的,请需要的同学注意。
最后的几行配置,是因为我代码中调用了asset中js文件导致的,js相关的操作代码,是不可以混淆的,故需要避开。
六、运行ant命令,构建开始:
命令行输入类似: D:/Android/apache-ant-1.9.2/bin/ant.bat -buildfile D:/workspa
ceAndroid/naomiHappyShopping/build.xml就可以完成代码混淆和apk输出:
.......release: [echo] [echo] delete unsigned apk [echo] [delete] Deleting: D:\workspaceAndroid\naomiHappyShopping\bin\HappyShoppingAnt-unsigned.apk [echo] [echo] copy apk from D:\workspaceAndroid\naomiHappyShopping/bin/HappyShoppingAnt.apk to D:/workspaceAndroid/Release/happyShopping.apk [echo] [copy] Copying 1 file to D:\workspaceAndroid\Release [echo] [echo] clean the workspace bin folder.... [echo] [delete] Deleting directory D:\workspaceAndroid\naomiHappyShopping\bin [echo] [echo]BUILD SUCCESSFULTotal time: 41 seconds看看时间,是不是比手动方便多了啊,而且反编译一看,代码真的不能再读了。
另外注意:不同人的路径等配置,需要根据各人自己的来,不能直接拷贝引用。