7

i am working on xamarin application. When i enable "ProGuard" in android properties, while building the application, I'm getting the following error:

"java.exe" exited with code1.

enter image description hereproguard cfg file has the following:

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service

Developing Environment:
Visual Studio 2015
Xamarin 4.0.4.4

ravindar
  • 406
  • 4
  • 13
  • When you have the error? Runtime? Compile time? Add your proguard file too. – AndroidRuntimeException Aug 23 '16 at 16:51
  • 2
    Can you add your full diagnostic build output to your question? The error should have a trace leading to it. – Jon Douglas Aug 23 '16 at 17:04
  • 2
    The most common issue's I've seen has to do with your JDK version (which you would see in the output if you set it to diagnostic) and also the heap size being insufficient. There's still a few open bugs about this in their bugtracker, namely: https://bugzilla.xamarin.com/show_bug.cgi?id=35255 – disappointed in SO leadership Aug 23 '16 at 17:41
  • 1
    Sounds like you need to add lot of more keeps in your proguard. Check this link http://proguard.sourceforge.net/manual/examples.html#androidapplication – AndroidRuntimeException Aug 23 '16 at 18:10
  • @AndroidRuntimeException - adding more details to what you are saying: the error code "1" suggests missing `keep`s resulting in link failures (but first make sure JDK and Android SDK are up-to-date). Would need to see the diagnostic build output to know for sure. FWIW, for Xamarin, there might be a project-specific proguard cfg, with any needed customizations. This adds on to the default one, under the Android SDK path referenced in Xamarin Tools Options, e.g. (username)/AppData/Local/Xamarin/Universal/AndroidSDK/tools/proguard/proguard-android.txt – ToolmakerSteve Feb 11 '17 at 23:15
  • ... can also try turning off optimizations (temporarily, as a test): `-dontoptimize` in project's `proguard.cfg` (add if needed, with Build Action "`ProguardConfiguration`"). If the link errors go away, then that confirms you need to add relevant `keep`s. HOWEVER, if you are using Google Play, and don't have "`Enable Multi-Dex`", that may push you over 64K method limit, resulting in an exit with error code "2" at CompileToDalvik. – ToolmakerSteve Feb 11 '17 at 23:23

2 Answers2

14

I had the same issue when enabling ProGuard. By following the advice on this link, I fixed the issue by updating my proguard manually. The steps are fairly easy to follow and they fixed the problem. Hope this helps you.

  1. Download the ProGuard zip file available from: https://sourceforge.net/projects/proguard/files/. At time of writing the latest version of ProGuard was 5.3.
  2. ProGuard does not have a setup program, so you will need to unzip the file and copy the proguard folder structure into the location identified in the steps below.
  3. Launch the SDK Manager and note the path in the top left under the menu options. In my case this was C:\Users\Sahar\AppData\Local\Android\android-sdk. The proguard folder is located in the tools folder of this path (in my case this was C:\Users\Sahar\AppData\Local\Android\android-sdk\tools\proguard).
  4. Close any development environments that might be accessing the SDK and rename the proguard folder to proguard.old.
  5. Copy the proguard folder of the new version into the tools folder and rename it to proguard if necessary (in my case the copied folder was renamed from proguard5.3).
  6. Finally copy the configuration files from the proguard.old folder to the new folder. In my case these were:- proguard-android.txt, proguard-android-optimize.txt and proguard-project.txt.
  7. Clean and rebuild your project with ProGuard enabled.
TheTallWitch
  • 404
  • 3
  • 14
  • The best thing to do is post the relevant details **and** include the link to the page. Otherwise, if the link goes dead, so does the effectiveness of your answer, unfortunately. – gravity Oct 19 '16 at 20:46
  • 1
    @gravity thanks for the advice, I have just started helping out with what I can, I will update my answer – TheTallWitch Oct 21 '16 at 16:52
  • Excellent edit. Great details now, and a much better answer as a result. – gravity Oct 21 '16 at 17:20
  • @gravity thank you so much, I'll do better next time – TheTallWitch Oct 21 '16 at 20:07
  • 1
    AFAIK, all that Xamarin uses are the three `.jar` files under `proguard/lib`. So an alternative to steps 4-6 is to replace those three files with the new ones. (Someone gave this solution in another stackoverflow post, but I don't have the link to credit them.) – ToolmakerSteve Feb 11 '17 at 22:58
4

I had a problem where Proguard was removing the Google Play Services libraries from my app.

I had to add the following lines of text to the proguard-android.txt file found in this folder:

/(Path to your Android SDK Folder)/tools/proguard

-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

Complete proguard-android.txt File

-keep public class com.google.android.gms.* { public *; }
-dontwarn com.google.android.gms.**

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}
Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123