1 使用proguard防止反编译
反编译了很多程序,发现基本上没有能读懂的,使用proguard防止反编译基本上已经普遍了,不会这个怎敢发布程序。
一般人这样告诉你,2.3就带了proguard.cfg文件,2.2没有(但其实也有了),但你可以拷过去,然后在系统属性文件中加入config就行了。
proguard.cfg的文件如下:
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-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 <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(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 *;
}
default.properties文件内容如下,其实现在是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 use,
# "ant.properties", and override values to adapt the script to your
# project structure.
# Project target.
target=android-8
proguard.config=proguard.cfg
2 需要注意的问题
(1)全部设定好后,run下,然后看看bin里生成的apk,反编译下。结果还是可以很好的反编译出来。因为代码的混淆只有在签名打包时才有用。签名打包的最方便方法就是在工程右击,然后一步步的来就行啦
一步步的next即可
(2)“conversion to dalvik format failed with error 1”,这可能是遇到的第一个问题。有人说clean下工程就行了。如果不行的话就要找到,proguard的在android sdk目录\tools\proguard\bin\proguard.bat文件,将
set java_exe=
call "%PROGUARD_HOME%"\..\lib\find_java.bat
call %java_exe% -jar "%PROGUARD_HOME%"\lib\proguard.jar %*
改为
set java_exe=
call "%PROGUARD_HOME%"\..\lib\find_java.bat
call %java_exe% -jar "%PROGUARD_HOME%"\lib\proguard.jar %1 %2 %3 %4 %5 %6 %7 %8 %9
参见: http://stackoverflow.com/questions/6605971/android-sdk-tools-revision-12-has-problem-with-proguard-error-conversion-to
(3)“proguard returned with error code 1.See console ....找不到文件”或“proguard returned with error code 1.See console ....no such directory or file”。错误原因:sdk文件所在目录,或者工程所在目录中间有空格,proguard不能识别带空格的文件名。
(4)还是“proguard returned with error code 1.See console ....can’t find referenced class”然后是出一堆警告。按提示要加入“-dontskipnonpubliclibraryclasses”,但是文件中已经有了啊。编程都知道,error不行,但warning应该没事啊!直接加入“--ignorewarnings ”选项。这样,基本上就能签名打包了。
再次反编译自己的apk,不但自己都看不懂了,而且,体积还小了很多。