Error generating signed APK due to proguard rules - proguard

Generating signed APK giving error after I updated Android Studio to 3.3-rc01 and enabled R8 code shrinking.
It is giving following proguard error :
Error: ~/app/proguard-rules.pro, offset: 2613, line: 74, column: 7, Expected char '-' at ~/app/proguard-rules.pro:74:7
-dump class_files.txt
before android studio 3.3-rc01 and without R8 code shrinking it was working perfectly fine.
Any workarounds for this or I have to disable the proguard rules now.
Following is my proguard-rules.pro file
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
# Retrofit does reflection on generic parameters and InnerClass is required to use Signature.
-keepattributes Signature, InnerClasses
# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
#retrofit2.http.* <methods>;
}
# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**
# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit
# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.-KotlinExtensions
# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase
# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*
# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-dontwarn javax.xml.stream.**
-dontwarn rx.internal.util.unsafe.**
#Glide proguard rules
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
-keep class android.support.v7.widget.LinearLayoutManager { *; }
-keep public class * extends android.support.v7.widget.RecyclerView$LayoutManager {
public <init>(...);
}
##---------------Begin: proguard configuration common for all Android apps ----------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-repackageclasses ''
-keep class com.qikcircle.qiketask.models.** { *; }
-keep class android.support.v4.** {*;}
-keep public class * extends android.app.Activity
-keep public class * extends android.support.v4.app.Fragment
-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 * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService
# Explicitly preserve all serialization members. The Serializable interface
# is only a marker interface, so it wouldn't save them.
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private 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();
}
# Preserve all native method names and the names of their classes.
-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);
}
# Preserve static fields of inner classes of R classes that might be accessed
# through introspection.
-keepclassmembers class **.R$* {
public static <fields>;
}
# Preserve the special static methods that are required in all enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep public class * {
public protected *;
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
##---------------End: proguard configuration common for all Android apps ----------
##---------------Begin: proguard configuration for Gson ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
# For using GSON #Expose annotation
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
##---------------End: proguard configuration for Gson ----------
#removing logs
-assumenosideeffects class android.util.Log {
public static boolean isLoggable(java.lang.String, int);
public static int v(...);
public static int i(...);
public static int w(...);
public static int d(...);
public static int e(...);
}
#for getting crash reports
-assumenosideeffects class android.util.Log {
public static int v(...);
public static int d(...);
}
#Crashlytics proguard rules
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception
-ignorewarnings
-keep class * {
public private *;
}

I've tried the dump directive with Proguard (I didn't use it before) and all works fine.
[...]
## Strip Log.d
-assumenosideeffects class android.util.Log {
public static *** d(...);
}
-dump foo.txt
[...]
And I find my foo.txt under ./app/foo.txt
Printing classes to [./app/foo.txt]...
Then I enable R8 in legacy mode:
# For the bravests
android.enableR8=true
# For the crazyest. Must be enable also the previous setting
#android.enableR8.fullMode=true
And I got your error.
Without this line I can compile my APK even in full mode without any (apparent) issue.
So I confirm that this directive is not supported. Remember that R8 is still in beta. If you want you can submit an issue on the link I posted in comment.
Opened the issue here.

for me i had to remove the first line " public private *; " in my prograuard.pro file

Related

How to keep method arguments names from obfuscation by ProGuard

For given class:
class KeepMe(val keepThisArgument: Int) {
fun keepMethod(keepThisArgument: Int) {
println(keepThisArgument)
}
}
and proguard configuration:
-keep class com.KeepMe { *; }
following code is produced by proguard:
public final class KeepMe {
private final int keepThisArgument;
public final void keepMethod(int paramInt) {
System.out.println(paramInt);
}
public final int getKeepThisArgument() {
return this.keepThisArgument;
}
public KeepMe(int paramInt) {
this.keepThisArgument = paramInt;
}
}
constructor and method argument names is changed from "keepThisArgument" to "paramInt". Is there a way to keep it from happening? I use net.sf.proguard gradle plugin, version 6.2.2.
Turns out there is separate configuration to keep all argument names in all classes that are kept from obfuscating:
-keepparameternames

Is there a way to keep Kotlin annotations only for public functions/members when obfuscating with proguard?

I am trying to obfuscate a library written in Kotlin using Proguard.
In the library I use default and named arguments. In order for these to work when including the obfuscated jar as a lib I need the flag -keepattributes RuntimeVisibleAnnotations in the Proguard file.
With this flag all private functions are no longer obfuscated, because the annotations for them contain the plain names. Is there a possibility to apply this flag only to public functions and members and to delete the annotations for private functions and members?
This is probably also connected to the fact that public members of an interface only work with the above-mentioned flag. Is there a solution that solves the problem with both the interface and the classes?
current Proguard file:
-dontwarn
-dontoptimize
-keepparameternames
-keepattributes Signature, InnerClasses, Exceptions
-keepattributes RuntimeVisibleAnnotations
-keep class custom.package.** {
!private <fields>;
!private <methods>;
}
-keep interface custom.package.** {
!private <fields>;
!private <methods>;
}
obfuscation result with this config:
public class ExampleClass {
private val clearNameAttr: kotlin.String /* compiled code */
private fun clearPrivateMethod (namedParam: kotlin.String) : kotlin.Boolean { /* compiled code */ }
public fun clearPublicMethod (namedParam: kotlin.String) : kotlin.Boolean { /* compiled code */ }
}
desired obfuscation result:
public class ExampleClass {
private String a
private boolean a (String var1) { /* compiled code */ }
public boolean clearPublicMethod (String namedParam) { /* compiled code */ }
}

proguard+springboot plugin -> no classes in jar

I have to obfuscate lib and application. Lib - simple lib with two classes. Obfuscation done well. Application(with obfuscated lib in dependencies) builded with springboot plugin, works well.
When I try to obfuscate application jar I have got jar without *.classes of my app.
here is proguard conf:
-verbose
-dontwarn
-dontoptimize
-dontpreverify
-printmapping out.map
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
-keepdirectories
-keep class org.springframework.******* { *;}
-keep public class * extends RequestEntity { *;}
-keep public class * extends ResponseEntity { *;}
-keepclassmembers,allowoptimization enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepnames class * implements java.io.Serializable {*; }
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
classes:
public class TestEntity {
private Integer fieldInt;
private String fieldStr
--cut--- (set* get*)
public class TestActions {
private TestEntity entity;
Gson gson = new Gson();
--cut---(set* get*)
import my.act.TestActions;
public class App {
public static void main(String[] args) {
TestActions testActions=new TestActions();
testActions.doPrintEntity();
}
}
out.map:
my.App -> my.App:
6:6:void <init>() -> <init>
8:10:void main(java.lang.String[]) -> main
org.springframework.boot.loader.ExecutableArchiveLauncher -> org.springframework.boot.loader.ExecutableArchiveLauncher:
org.springframework.boot.loader.archive.Archive archive -> archive
37:44:void <init>() -> <init>
--cut---
rows about spring...
and I have got test_app1.jar without my.App, only empty folder:
test_app1.jar\BOOT-INF\classes\my\
Any ideas? Help please...
If you use proguard maven plugin for a springboot application. Don't forget to add spring package plugin as well.

proguard IllegalArgumentException

Trying to build release version of my application. It comes up with an error saying that
Caused by: java.io.IOException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue]
I saw a similar case here:
https://sourceforge.net/p/proguard/bugs/573/
I have already added -optimizations !class/unbosing/enum
but the error still shows
there is no change. Maybe I am missing something else from the proguard-rules?
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !class/unboxing/enum
-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 * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.v4.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.app.Fragment
-keep class android.support.v8.renderscript.** { *; }
-dontwarn android.support.**
-dontobfuscate
-keep public class com.google.android.gms.** { public *; }
-dontwarn com.google.android.gms.**
-dontwarn okio.**
-dontwarn retrofit2.Platform$Java8
# Keep all things of data model classes
-keep public class com.hotmob.sdk.model.** { *; }
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * extends java.lang.Enum {
<fields>;
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keep class MyClass;
-Adagger.android.experimentalUseStringKeys
# gson
-keepattributes Signature
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
# android-saripaar
-keep class com.mobsandgeeks.saripaar.** { *; }
-keep #com.mobsandgeeks.saripaar.annotation.ValidateUsing class * { *; }
# loading icon
-keep class com.wang.avi.** { *; }
-keep class com.wang.avi.indicators.** { *; }
This is the error that shows
Optimizing (pass 1/5)...
> Task :app:transformClassesAndResourcesWithProguardForRelease
Unexpected error while evaluating instruction:
Class = [kotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$classes$1]
Method = [invoke(Lkotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$FindClassRequest;)Lkotlin/reflect/jvm/internal/impl/descriptors/ClassDescriptor;]
Instruction = [311] ificmpne +113 (target=424)
Exception = [java.lang.IllegalArgumentException] (Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue])
Unexpected error while performing partial evaluation:
Class = [kotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$classes$1]
Method = [invoke(Lkotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$FindClassRequest;)Lkotlin/reflect/jvm/internal/impl/descriptors/ClassDescriptor;]
Exception = [java.lang.IllegalArgumentException] (Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue])
Warning: Exception while processing task java.io.IOException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue]
Thread(Tasks limiter_2): destruction
> Task :app:transformClassesAndResourcesWithProguardForRelease FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
> Job failed, see logs for details
The stacktrack debug shows pretty much the same
19:04:39.831 [ERROR] [system.err] Unexpected error while evaluating instruction:
19:04:39.831 [ERROR] [system.err] Class = [kotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$classes$1]
19:04:39.832 [ERROR] [system.err] Method = [invoke(Lkotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$FindClassRequest;)Lkotlin/reflect/jvm/internal/impl/descriptors/ClassDescriptor;]
19:04:39.833 [ERROR] [system.err] Instruction = [311] ificmpne +113 (target=424)
19:04:39.833 [ERROR] [system.err] Exception = [java.lang.IllegalArgumentException] (Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue])
19:04:39.834 [ERROR] [system.err] Unexpected error while performing partial evaluation:
19:04:39.834 [ERROR] [system.err] Class = [kotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$classes$1]
19:04:39.834 [ERROR] [system.err] Method = [invoke(Lkotlin/reflect/jvm/internal/impl/load/java/lazy/descriptors/LazyJavaPackageScope$FindClassRequest;)Lkotlin/reflect/jvm/internal/impl/descriptors/ClassDescriptor;]
19:04:39.834 [ERROR] [system.err] Exception = [java.lang.IllegalArgumentException] (Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue])
19:04:40.541 [QUIET] [system.out] Warning: Exception while processing task java.io.IOException: java.lang.RuntimeException: java.lang.IllegalArgumentException: Value "a" is not an integer value [proguard.evaluation.value.UnknownReferenceValue]
19:04:40.550 [QUIET] [system.out] Thread(Tasks limiter_1): destruction
19:03:00.186 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger]
19:03:00.186 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :app:transformClassesAndResourcesWithProguardForRelease
19:04:40.550 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Execute transform for :app:transformClassesAndResourcesWithProguardForRelease'
19:04:40.551 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Build operation 'Execute transform for :app:transformClassesAndResourcesWithProguardForRelease' completed
19:04:40.551 [DEBUG] [org.gradle.api.internal.tasks.execution.DefaultTaskFingerprinter] Fingerprinting property otherFileOutputs.otherFileOutput1 (Output) for task ':app:transformClassesAndResourcesWithProguardForRelease'
19:04:40.551 [DEBUG] [org.gradle.api.internal.tasks.execution.DefaultTaskFingerprinter] Fingerprinting property otherFileOutputs.otherFileOutput2 (Output) for task ':app:transformClassesAndResourcesWithProguardForRelease'
19:04:40.570 [DEBUG] [org.gradle.api.internal.tasks.execution.DefaultTaskFingerprinter] Fingerprinting property otherFileOutputs.otherFileOutput3 (Output) for task ':app:transformClassesAndResourcesWithProguardForRelease'
19:04:40.579 [DEBUG] [org.gradle.api.internal.tasks.execution.DefaultTaskFingerprinter] Fingerprinting property streamOutputFolder (Output) for task ':app:transformClassesAndResourcesWithProguardForRelease'
19:04:40.579 [DEBUG] [org.gradle.internal.execution.steps.CacheStep] Not storing result of task ':app:transformClassesAndResourcesWithProguardForRelease' in cache because the execution failed
19:04:40.580 [DEBUG] [org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter] Removed task artifact state for {} from context.
19:04:40.581 [DEBUG] [org.gradle.internal.operations.DefaultBuildOperationExecutor] Completing Build operation 'Task :app:transformClassesAndResourcesWithProguardForRelease'
19:04:40.583 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger]
19:04:40.583 [LIFECYCLE] [class org.gradle.internal.buildevents.TaskExecutionLogger] > Task :app:transformClassesAndResourcesWithProguardForRelease FAILED

Unable to access companion object used in kotlin class after enabling proguard

I have a Kotlin class with some variables in companion object. After enabling proguard, The variables are not getting accessed.
class Test{
......
companion object {
const val USER_NAME = "user_name"
.....
}
.....
}
Proguard rules include:-
-keep class kotlin.** { *; }
-keep class kotlin.Metadata { *; }
-dontwarn kotlin.**
-keepclassmembers class **$WhenMappings {
<fields>;
}
-keepclassmembers class kotlin.Metadata {
public <fields>;
public <methods>;
}
-keepclassmembers class * {
static final % *;
static final java.lang.String *;
}
Problem resolved using #Keep before companion object
class Test{
......
#Keep companion object {
const val USER_NAME = "user_name"
.....
}
.....
}
The accepted answer uses the #Keep annotation. If that works for you, then great, but in our case it didn't make a difference.
For anyone else in the same boat, here's the solution that worked for us (add to your proguard rules):
-keepclassmembers class com.example.some.package.SomeClass {
public static ** Companion;
}
After many struggles with it the thing that worked best for me is:
-keep class <class>$Companion { *; }
no annotations needed.
I faced a similar problem, there was a function in the companion object that was not being accessed after enabling proguard.
#Keep didn't work for me, after a lot of struggle. I found the solution.
Just add #JvmStatic annotation to the function inside the companion object.
And you are good to go.