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.
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.
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'
}
}
}
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.
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>
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.
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
Related
I would like to make my app debuggable.
When I deploy to my nexus 5x I get this error:
Error running AndroidLauncher: Cannot debug application from module android on device lge-nexus_5x-(code here). This application does not have the debuggable attribute enabled in its manifest. If you have manually set it in the manifest, then remove it and let the IDE automatically assign it. If you are using Gradle, make sure that your current variant is debuggable.
If I add the debuggable="true" attribute to the android tag in my manifest it works.
But I'm forgetful, so I'd rather do it the proper way with gradle.
I've tried to add a lot of the things I've found on the internet to both my Android module build.gradle, and my project's root level build.gradle.
All to no avail.
What should work?
Thanks,
Chase
On an tangent, I also tried running my HTML module using the instructions from their site, and it says it's failing because I'm using java 1.7 features but the gradle source is 1.6. I've also tried googling that to no avail. I was adding some lines like this:
sourceCompatibility 1.7
targetCompatibility 1.7
You can use build types (Build Variants) in your android gradle file:
project/android/build.gradle
buildTypes {
debug {
applicationIdSuffix ".debug"
debuggable true
}
release {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-project.txt'
minifyEnabled true
pseudoLocalesEnabled false
debuggable false
signingConfig signingConfigs.AndroidKey
}
}
* There is some proguard stuff and signing config you might not need. Just remove from the release build variant.
You don't need any other configuration in other files. You can see the full source I used in my project here.
If you are using Android Studio you can change between your build variants here:
When you run the project if release is selected it will install the non debugable apk in your device. Otherwise, if debug is selected it will install the debugable apk. Both can be installed at the same time. It will show the app twice in your device.
The problem is that it should be debugable by default. You could remove the debuggable true line and the debug build variant should still be debugable.
If it does not work paste your manifest and your build.gradle files here so we can see what is going on.
You can look all theses files in my project. It is also a libGDX project, but it don't have the HTML module.
The Build Variant docs might be helpful.
I switched my IDE from eclipse to android studio and generated apk after some very minor changes (No class or files were added).
The APK generated from eclipse was around of size 3 MB but when I generated APK from android studio, the apk size was around 5.5 MB.
I even tried to clean and generate APK in Release mode but still APK size was same i.e. 5.5 MB.
Is there any specific reason behind this in android studio or am I missing something ?
your problem certainly have nothing to do with Android Studio or Eclipse. It's todo with Gradle and Ant. Those are the built systems used by each respectively.
On a general approach it seems to be that the Ant build was using ProGuard to remove unused resources and classes whilst your build.gradle file is not instructing Gradle to do the same.
Trying adding the minifyEnabled to your release build, like in the example below.
PS: change the name of the proguard file to the one on your project
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
I have been using IDEA 13.1 and created a Gradle project with it. When I compile the app and start it, it creates only the unaligned APK.
Opening the same project with Android Studio, and rebuilding it and starting the app, generates both unaligned and regular unsigned APK.
Is this a bug in IntelliJ IDEA? If not, where can I set that it always generates a regular, aligned, APK?
The alignment/output does not depend on the IDE but on your gradle config. Both IntelliJ and AndroidStudio just run Gradle in background.
By default, the autogenerated Gradle file build.gradle does not run the zipAlign task, i.e. the one aligning the apk. So you end up with an unaligned apk.
You can easily change it by adding the zipAlignEnabled true property, like this:
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
zipAlignEnabled true
}
But let me remind you that archive alignment is just an optimization, it does not change your app.
Also, from the docs:
Caution: zipalign must only be performed after the .apk file has been signed with your private key. If you perform zipalign before signing, then the signing procedure will undo the alignment.
For further information, please have a look at this thread, this thread and the docs.
I switched over from Eclipse to Android Studio in the last few days and have gotten most everything working. However, when I generate a signed APK it appears as though ProGuard is never running.
I am using the Generate Signed APK Wizard, selecting 'Run ProGuard' and specifying my proguard.cfg as the config file. The build process runs without errors and generates a functional apk, but that apk is 65% larger than the one generated by Eclipse. When I generate the apk through Android Studio's APK Wizard and do not select 'Run Proguard' the resulting apk is the same size as the one that should have had ProGuard run on it. No mapping.txt, seeds.txt, or usage.txt is generated anywhere in my project directory. I have tried adding
buildTypes {
release {
runProguard true
proguardFile file('proguard.cfg')
proguardFile getDefaultProguardFile('project-android.txt')
}
}
and variations to my build.gradle file but that has had no effect either.
This is occurring on Android Studio 0.2.0, though I was seeing the same behavior on 0.1.9. I am working on Windows 7.
Can anyone tell me what might be going on? I would be happy if I could find the logs ProGuard is supposed to generate.
Just update your build.gradle
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
Details Reference.
I hope it will helps you
Happily, I have found a solution. The issue was that before creating the signed apk, I had modified the package name in the AndroidManifest in order to overwrite a particular build in the Google Play Store. However, this change of package name had not refactored all of the corresponding "import 'package name'.R;" lines throughout the code. Today, after re-importing the project, it would no longer build because of errors attempting to import R. Once I modified all the import lines, not only did my project build properly, but exporting the signed apk properly ran ProGuard.
I'm guessing that Android Studio was somehow caching the "import R" lines and that when ProGuard was attempting to run it did not have those cached values and then crashed. Why there was no error output for me to see, I do not know.
**in new Gradle system**
BuildType.runProguard -> minifyEnabled
BuildType.zipAlign -> zipAlignEnabled
BuildType.jniDebugBuild -> jniDebuggable
BuildType.renderscriptDebug -> renderscriptDebuggable
ProductFlavor.renderscriptSupportMode -> renderscriptSupportModeEnabled
ProductFlavor.renderscriptNdkMode -> renderscriptNdkModeEnabled
or visit at
http://tools.android.com/tech-docs/new-build-system
Is there any obfuscation tool to use with Android Studio? IntelliGuard plugin is declared to be supported by the Studio, but it doesn't work actually due to missing AntSupport plugin. I wan't able to find one in the repository. Any ideas?
P.S. Android Studio build process is based on Gradle, so I wouldn't expect to see Ant support there at all. May be I'm wrong.
Basic Obfuscation
To obfuscate code in Android studio just go to your build.gradle file in your Android Studio project:
Change the minifyEnabled property from false to true
This is a basic Obfuscation.
After generating the apk you can see the obfuscation result by decompiling the apk with any software. This page could help you:
http://www.decompileandroid.com/
In the obfuscation result you will see classes with name: a,b,c....
And the obfuscation variables and methods will have also names like aa,c,ac...
Normal obfuscation:
To obfuscate the code in a more complex form you could go to your root directory app and create a .pro file. For example in the following picture I have created the file: proguard-rules-new.pro. In the same directory you should see a file called proguard-rules.pro
Now add the file you have created to the build.gradle file
And edit the .pro file you have create with your own custom proguard rules
First enable minifyEnabled in your build.gradle file, like
minifyEnabled true
After this, add below lines in progurad-rules.txt file
-keep class yourpackage.** { *; }
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
For checking that its working fine go to:
http://www.javadecompilers.com/apktool website so that you can verify after decompilation.
It will work and your classes will be hidden completely.
Update: R8 is by default enabled in android studio version 3.4.0 and above
In android studio 3.4+, R8 is enabled by default so no need to add additional property though you can opt for deep optimizations by adding fullMode property in gradle.properties as:
android.enableR8.fullMode=true
You can disable R8 and enable proguard by adding following properties in gradle.properties as:
android.enableR8 = false
useProguard = true
Android September 2018 release a new tool R8 shrinker and obfuscation tool.
R8 - R8 is a java code shrinker and minifying tool that converts java byte code to optimized dex code
For AS version below 3.4.0.
Open gradle.properties
Add android.enableR8 = true
as
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
android.enableR8 = true
Minimum Requirements:
Android studio 3.2 September 2018 release or above
Java 8
R8 Tool
R8 supports Proguard:
Keep in mind, R8 is designed to work with your existing ProGuard rules, so you’ll likely not need to take any actions to benefit from R8. However, because it’s a different technology to ProGuard that’s designed specifically for Android projects, shrinking and optimization may result in removing code that ProGuard may have not. So, in this unlikely situation, you might need to add additional rules to keep that code in your build output.
To Disable R8 in AS 3.4.0 and above:
# Disables R8 for Android Library modules only.
android.enableR8.libraries = false
# Disables R8 for all modules.
android.enableR8 = false
Note: For a given build type, if you set useProguard to false in your app module's build.gradle file, the Android Gradle plugin uses R8 to shrink your app's code for that build type, regardless of whether you disable R8 in your project's gradle.properties file.
Proguard is well-supported on Android studio. You have to configure Gradle to run it. Instructions: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Running-ProGuard
after setting minifyEnabled to true there are two version of apk you can get, so that you have to add debug option in your build.gradle to obfuscate debug one:
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sync, build and build apk