开发者

Using ProGuard with Eclipse (without private key)

开发者 https://www.devze.com 2023-03-22 02:09 出处:网络
I\'ve been trying to see ProGuard in action with my test Android app. Do I need to register as an Android developer and have a key to do that? I know app needs to be built in release mode. I read thes

I've been trying to see ProGuard in action with my test Android app. Do I need to register as an Android developer and have a key to do that? I know app needs to be built in release mode. I read these instructions several times on Android site among other things, it talks about what ProGuard does but not how to achieve it obfu开发者_StackOverflow中文版scation in Android properly. Found another blog that shows how to do it with Ant, but not with Eclipse.


No, you do not need to register as an Android developper to try the Proguard obfuscation. You just need to have Proguard installed and properly configured for your app.

Since a few month, the Android SDK comes with a distribution of Proguard directly integrated. Open your <android-home>\tools directory and check whether you find a directory called proguard in it. If it is not the case, the best is to upgrade your SDK.

Using Proguard is very simple with the integrated version of the Android SDK: you just have to declare what follows in the file default.properties of your project:

# ProGuard
proguard.config=proguard.cfg

Then, you have to write your proguard.cfg if it does not already exist. The Android SDK writes this file for you for any new project you create since it integrates Proguard, but if your project was created with a former version of the SDK this file won't exist. The following file is suitable for most Android projects:

-printmapping proguard.map
-renamesourcefileattribute ProGuard
-keepattributes SourceFile,LineNumberTable

-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 com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
}

When you compile in debug mode, there is no obfuscation. To obfuscate your code, you have to:

1- Ensure that your manifest file sets the debug mode to false:

<application android:icon="@drawable/icon" 
             android:label="@string/app_name"
             android:debuggable="false">

2- Export your APK using the "File/Export" menu of Eclipse (provided you are using Eclipse, but who doesn't? :-) ). There will be no obfuscation by just using the "Run" function as this is actually debugging.

Once done the obfuscation, you will find a proguard directory in your project's root directory. It will contain files that will allow you to inspect the obfuscated code, see what was obfuscated and what wasn't, etc. The Proguard's documentation will help you on that, that's pretty simple.

0

精彩评论

暂无评论...
验证码 换一张
取 消