Android jar包混淆

具体可参考http://proguard.sourceforge.net/manual/examples.html#library

1、找到android的adt目录下的 D:\soft\adt-bundle-windows-x86\sdk\tools\proguard 文件夹,这个文件夹中有个examples文件夹,打开可看到 android.pro 这是个android混淆代码的demo,你可以完全复制里面的东西,把里面对应的东西改成自己的,然后再添加不要混淆的自己的 类接口等信息 保存为  .pro 文件

2、找到  D:\soft\adt-bundle-windows-x86\sdk\tools\proguard\bin 目录下的 proguardgui.bat 文件双击 会弹出一个图形界面选择左上角的 ProGuard 按钮,然后再右下角有个 Load configuration... 点击选择你的 .pro 文件导入

3、点击图形界面的第二个按钮 Input/Output  添加你要混淆的 jar 或文件夹或其他,对应的也要选择好输出文件对应的东西

4、设置完成后找到左边菜单栏的 Process,选中,然后再页面的右下角找到 process 点击即可完成。

5、 主要混淆后的包一定要测试下。

示例混淆配置文件如下:

#
# This ProGuard configuration file illustrates how to process Android
# applications.
# Usage:
# java -jar proguard.jar @android.pro
#
# If you're using the Android SDK (version 2.3 or higher), the android tool
# already creates a file like this in your project, called proguard.cfg.
# It should contain the settings of this file, minus the input and output paths
# (-injars, -outjars, -libraryjars, -printmapping, and -printseeds).
# The generated Ant build file automatically sets these paths. # Specify the input jars, output jars, and library jars.
# Note that ProGuard works with Java bytecode (.class),
# before the dex compiler converts it into Dalvik code (.dex).

-injars IPS.jar #要混淆的jar包名,注意路径。只写jar包名,则jar包和本混淆配置文件在同一个路径下。
#-injars libs
-outjars IPSPro.jar #输出的混淆后的jar包 -libraryjars F:/adt-bundle-windows-x86_64-20140321/sdk/platforms/android-19/android.jar #依赖的jar包也需导入,否则会出现找不到类的错误
#-libraryjars /usr/local/android-sdk/add-ons/google_apis-7_r01/libs/maps.jar
# ... # Save the obfuscation mapping to a file, so you can de-obfuscate any stack
# traces later on. #-printmapping bin/classes-processed.map # You can print out the seeds that are matching the keep options below. #-printseeds bin/classes-processed.seeds # Preverification is irrelevant for the dex compiler and the Dalvik VM. -dontpreverify # Reduce the size of the output some more. -repackageclasses ''
-allowaccessmodification # Switch off some optimizations that trip older versions of the Dalvik VM. -optimizations !code/simplification/arithmetic # Keep a fixed source file attribute and all line number tables to get line
# numbers in the stack traces.
# You can comment this out if you're not interested in stack traces. -renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable # RemoteViews might need annotations. -keepattributes *Annotation* # Preserve all fundamental application classes. -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 # Preserve all View implementations, their special context constructors, and
# their setters. -keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
} # Preserve all classes that have special context constructors, and the
# constructors themselves. -keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
} # Preserve all classes that have special context constructors, and the
# constructors themselves. -keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
} # Preserve the special fields of all Parcelable implementations. -keepclassmembers class * implements android.os.Parcelable {
static android.os.Parcelable$Creator CREATOR;
} # Preserve static fields of inner classes of R classes that might be accessed
# through introspection. -keepclassmembers class **.R$* {
public static <fields>;
} # Preserve the required interface from the License Verification Library
# (but don't nag the developer if the library is not used at all). -keep public interface com.android.vending.licensing.ILicensingService -dontnote com.android.vending.licensing.ILicensingService # The Android Compatibility library references some classes that may not be
# present in all versions of the API, but we know that's ok. -dontwarn android.support.** # Preserve all native method names and the names of their classes. -keepclasseswithmembernames class * {
native <methods>;
} # Preserve the special static methods that are required in all enumeration
# classes. -keepclassmembers class * extends java.lang.Enum {
public static **[] values();
public static ** valueOf(java.lang.String);
} # Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
# You can comment this out if your application doesn't use serialization.
# If your code contains serializable classes that have to be backward
# compatible, please refer to the manual. -keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
} # Your application may contain more items that need to be preserved;
# typically classes that are dynamically created using Class.forName: -keep public class com.dkdh.ips.StepDetector{
# public <fields>;
public <methods>;
}
-keep public class com.dkdh.ips.SensorFusion{
# public <fields>;
public <methods>;
}
#不混淆异常,内部类等
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
SourceFile,LineNumberTable,*Annotation*,EnclosingMethod # -keep public class com.dkdh.ips.Util{
# public <fields>;
# public <methods>;
# }
-keep class com.dkdh.ips.IPSSensorManager{
public static boolean getRotationMatrix(float[],float[],float[],float[]);#只保留该方法不混淆,其他所有全部混淆
# public <fields>;
#public <methods>;
}
# -keep class com.dkdh.ips.SensorFusion.calculateFusedOrientationTask{
# public <fields>;
# public <methods>;
# }
# -keep public interface mypackage.MyInterface
# -keep public class * implements mypackage.MyInterface

出现Error: The output jar is empty. Did you specify the proper '-keep' options?错误,

是因为混淆的代码没有一个入口,也就是说,这个程序或者jar包被全部混淆了,外部没有任何一个地方可以正常调用到它,换句话说就是“混淆出来的东西没法用”。

混淆时,要暴露的接口、变量和方法不能混淆,一般是public修饰不要混淆。

上一篇:获取JDBC中的ResultSet的记录的条数


下一篇:2018.8.15 AOP面向切面编程简单理解