82

In my android app, i want to test some features with proguard on.

I don't need to really "debug" it, but i want proguard to run when i hit run in eclipse. I don't want to export the binary every time (so, in release mode) and save as apk and get it to the device to test.

Is there any way to run proguard in this way?

Update:

It seems like this is possible if you are not using Eclipse; as question title does not include Eclipse, there are multiple correct answers to this question.

frankish
  • 6,738
  • 9
  • 49
  • 100
  • 1
    You might want to change the accepted answer here, since everyone including the poster agrees it's wrong but they won't delete it – Michael Mrozek Jun 29 '15 at 19:13
  • 1
    Tank you. I will update the question. Current answer is correct for Eclipse, but question title does not include Eclipse. Other answers are correct for IDEs other than Eclipse. – frankish Jun 30 '15 at 08:56

6 Answers6

76

If you want to make the whole build process easier for you, you should switch over to gradle and Android Studio IDE.

Then you could easily add the following to your build.gradle file to run ProGuard:

android {
    buildTypes {
        release {
        }
        debug {
            minifyEnabled true
            proguardFile 'proguard-android.txt'
            zipAlignEnabled true
        }
    }
}

This will run ProGuard on your debug build, configured with the file "proguard-android.txt", which should be put at your project's root folder. And in addition your apk is being zip aligned (Just remove "zipAlignEnabled true", if you don't want that to happen). If you want to do the same for your release build, just add those three lines under "release".

Slightly off-topic: Stuff like adding dependencies, signing your apk or adding other custom tasks to your build process is also way more uncomplicated with gradle. In addition you'll be able to not only build your apk via Android Studio IDE, but also via a simple command on the command line (e.g. ./gradlew assembleDebug). So if you are working on a team, the setup process for new members is just one "./gradlew assembleDebug" away. Without the need for any IDE configuration at all. Importing your project including all dependencies is as simple as a one-click process

EDIT: As of Gradle Android Build Tools version 0.14.0 the property names have changed (http://tools.android.com/tech-docs/new-build-system):

  • BuildType.runProguard -> minifyEnabled
  • BuildType.zipAlign -> zipAlignEnabled

I've updated the above code.

MrMaffen
  • 1,647
  • 1
  • 17
  • 22
48

Old Answer :

http://developer.android.com/tools/help/proguard.html

ProGuard runs only when you build your application in release mode, so you do not have to deal with obfuscated code when you build your application in debug mode.

When you build your application in release mode, either by running ant release or by using the Export Wizard in Eclipse, the build system automatically checks to see if the proguard.config property is set. If it is, ProGuard automatically processes the application's bytecode before packaging everything into an .apk file. Building in debug mode does not invoke ProGuard, because it makes debugging more cumbersome.

Update: 13-3-2016

It is possible with the new gradle build system. You need to set minifyEnabled to true in your build.gradle file. Generally you have pro-guard running in release mode. There are other options available like shrinking resources. You can find some useful info @ http://tools.android.com/tech-docs/new-build-system

Also do have a look @

http://developer.android.com/tools/building/configuring-gradle.html

 android {
   ...
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
     debug {

        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
   }
 }
JJD
  • 50,076
  • 60
  • 203
  • 339
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • 5
    But is there a way? I accept it to be cumbersome. I don't need the obfuscated code but i'll use proguard's processing power to strip log functions, etc. according to it's config file. – frankish May 15 '13 at 08:09
  • @frankish according to what the doc says its a no. However i do not know of an altenative way. Quoting from doc "ProGuard runs only when you build your application in release mode". according to this statement its a straight no – Raghunandan May 15 '13 at 08:12
  • 13
    You should delete your answer, be fair to others. It is misleading. – Radu Jun 06 '15 at 19:47
  • 1
    This answer seems like it is still correct if you are using Eclipse. Other answers are also correct but only if you are not using Eclipse. It is hard to both change the accepted answer and edit the question contents and title at this very moment as this question and it's answers include lots of information. – frankish Jun 30 '15 at 09:04
  • @frankish you can always edit the post if you feel it needs to be edited with a better information – Raghunandan Jun 30 '15 at 09:08
  • @frankish the info is want was there in the docs when i posted the asnwer – Raghunandan Jun 30 '15 at 09:12
  • @Radu i cannot delete a accepted answer. Only moderators can. – Raghunandan Aug 31 '15 at 07:52
11

Regarding custom Ant builds (and based on Victor's answer), adding the following to my build.xml file works for me:

<target name="-debug-obfuscation-check">
    <!-- enable proguard even in debug mode -->
    <property name="proguard.enabled" value="true"/>

    <!-- Secondary dx input (jar files) is empty since all the jar files will be in the obfuscated jar -->
    <path id="out.dex.jar.input.ref" />
</target>

Notice that I had to override (actually pre-set) the out.dex.jar.input.ref; otherwise, the later running of dx will attempt to merge non-disjoint jars and throw the DexException: Multiple dex files define Xxx.

Jo Jo
  • 7,245
  • 5
  • 34
  • 49
7

It is possible if you build with Ant. See Android custom build using Ant on how to build your project with ant. Then, simply override in the project's build.xml the target "-debug-obfuscation-check" and set proguard.enabled to true:

<target name="-debug-obfuscation-check">
    <!-- proguard is never enabled in debug mode -->
    <property name="proguard.enabled" value="true"/>
</target>
Community
  • 1
  • 1
Victor Ionescu
  • 1,967
  • 2
  • 21
  • 24
2

If you are using AGP (Android Gradle Plugin) 7.2.0 or newer, beware that we have a bug open without a solution so far. Workaround as of now is to downgrade AGP to 7.1.3 so you can obfuscate your debug APK.

https://issuetracker.google.com/issues/242214899?pli=1

Henrique Vasconcellos
  • 1,144
  • 1
  • 8
  • 13
0

With Android Studio you can use -dontobfuscate option in your Proguard rules file and debugger will work fine. I'm not sure if it works with Eclipe as well.

sealskej
  • 7,281
  • 12
  • 53
  • 64