Android use gradle build with "minifyEnabled true" meet an error, - android

this is My Error infoļ¼š
Unexpected error while performing partial evaluation:
Class = [org/apache/log4j/chainsaw/Main]
Method = [<init>()V]
Exception = [java.lang.IllegalArgumentException] (Can't find common super class of [java/lang/NoClassDefFoundError] (with 5 known super classes) and [org/apache/log4j/chainsaw/LoadXMLAction] (with 1 known super classes))
Exception while processing task
java.lang.IllegalArgumentException: Can't find common super class of [java/lang/NoClassDefFoundError] (with 5 known super classes) and [org/apache/log4j/chainsaw/LoadXMLAction] (with 1 known super classes)
at proguard.evaluation.value.TypedReferenceValue.findCommonClass(TypedReferenceValue.java:441)
at proguard.evaluation.value.TypedReferenceValue.generalize(TypedReferenceValue.java:277)
at proguard.evaluation.value.TypedReferenceValue.generalize(TypedReferenceValue.java:201)
at proguard.evaluation.value.ReferenceValue.generalize(ReferenceValue.java:298)
at proguard.evaluation.Variables.generalize(Variables.java:136)
at proguard.evaluation.TracedVariables.generalize(TracedVariables.java:118)
at proguard.optimize.evaluation.PartialEvaluator.evaluateSingleInstructionBlock(PartialEvaluator.java:690)
at proguard.optimize.evaluation.PartialEvaluator.evaluateInstructionBlock(PartialEvaluator.java:594)
at proguard.optimize.evaluation.PartialEvaluator.visitExceptionInfo(PartialEvaluator.java:1079)
at proguard.classfile.visitor.ExceptionHandlerFilter.visitExceptionInfo(ExceptionHandlerFilter.java:67)
at proguard.classfile.attribute.CodeAttribute.exceptionsAccept(CodeAttribute.java:186)
at proguard.optimize.evaluation.PartialEvaluator.evaluateExceptionHandlers(PartialEvaluator.java:1019)
at proguard.optimize.evaluation.PartialEvaluator.evaluateInstructionBlockAndExceptionHandlers(PartialEvaluator.java:574)
at proguard.optimize.evaluation.PartialEvaluator.visitCodeAttribute0(PartialEvaluator.java:271)
at proguard.optimize.evaluation.PartialEvaluator.visitCodeAttribute(PartialEvaluator.java:188)
at proguard.classfile.attribute.CodeAttribute.accept(CodeAttribute.java:101)
at proguard.classfile.ProgramMethod.attributesAccept(ProgramMethod.java:81)
at proguard.classfile.attribute.visitor.AllAttributeVisitor.visitProgramMember(AllAttributeVisitor.java:95)
at proguard.classfile.util.SimplifiedVisitor.visitProgramMethod(SimplifiedVisitor.java:92)
at proguard.classfile.ProgramMethod.accept(ProgramMethod.java:73)
at proguard.classfile.ProgramClass.methodsAccept(ProgramClass.java:516)
at proguard.classfile.visitor.AllMethodVisitor.visitProgramClass(AllMethodVisitor.java:47)
at proguard.classfile.visitor.ClassAccessFilter.visitProgramClass(ClassAccessFilter.java:67)
at proguard.classfile.ProgramClass.accept(ProgramClass.java:358)
at proguard.classfile.ClassPool.classesAccept(ClassPool.java:124)
at proguard.optimize.Optimizer.execute(Optimizer.java:462)
at proguard.ProGuard.optimize(ProGuard.java:328)
at proguard.ProGuard.execute(ProGuard.java:127)
at proguard.gradle.ProGuardTask.proguard(ProGuardTask.java:1074)
at com.android.build.gradle.tasks.AndroidProGuardTask.doMinification(AndroidProGuardTask.java:139)
at com.android.build.gradle.tasks.AndroidProGuardTask$1.run(AndroidProGuardTask.java:115)
at com.android.builder.tasks.Job.runTask(Job.java:48)
at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:227)
at java.lang.Thread.run(Thread.java:745)
:mainAPlication:shrinkReleaseMultiDexComponents FAILED
if I set
minifyEnabled true
this error comes,
if I set
minifyEnabled flase
my proguardtask works fine,
this is my proguard.cfg File:
-optimizationpasses 1
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-dontshrink
-verbose
-microedition
-ignorewarnings
-keepattributes InnerClasses,Signature
-keepattributes *Annotation*
So ,I don't know how this error happen, this looks because minifyEnabled work error ,
does any body meet the same question.
when I add this line in proguard.cfg ,My build works
-dontoptimize
But I don't know which reason cause the error

It was likely an external library
I dug this up
http://sohu.io/questions/1918042/proguard-cant-find-any-super-classes
and sure enough along these lines I have a similar scenario:
Proguard fails to build due to compiling a library that contains Log4J. Get The owner of that library to remove log4j and invocations to it is the approach I am going to take. In particular for producation as proguard really should be used in a production/release scenario...

Related

okhttp3 minifyEnabled causes crash on Android 4.1.2

This code in okhttp:3.12.13 works fine with minifyEnabled false
private static boolean supportsAlpn() {
if (Security.getProvider("GMSCore_OpenSSL") != null) {
return true;
} else {
try {
Class.forName("android.net.Network"); //NoClassDefFoundError here if minifyEnabled true
return true;
} catch (ClassNotFoundException ignored) { }
}
return false;
}
but with minifyEnabled true the following error occurs on Android 4.1.2
Caused by: java.lang.ExceptionInInitializerError
at okhttp3.OkHttpClient.newSslSocketFactory(OkHttpClient.java:292)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:258)
at okhttp3.OkHttpClient.<init>(OkHttpClient.java:231)
Caused by: java.lang.ExceptionInInitializerError
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:217)
at java.lang.Class.forName(Class.java:172)
at okhttp3.internal.platform.AndroidPlatform.supportsAlpn(AndroidPlatform.java:219)
at okhttp3.internal.platform.AndroidPlatform.buildIfSupported(AndroidPlatform.java:262)
at okhttp3.internal.platform.Platform.findAndroidPlatform(Platform.java:246)
at okhttp3.internal.platform.Platform.findPlatform(Platform.java:202)
at okhttp3.internal.platform.Platform.<clinit>(Platform.java:78)
Caused by: java.lang.NoClassDefFoundError
at android.net.Network.<clinit>(Unknown Source)
What lines should be added to Proguard to avoid the crash?
Edit:
Adding rules 1 and 2 didn't help.
These rules worked, but apk size gets increased by 0.6 MB.
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
I'm wondering if it is possible to fix the issue without keeping all okhttp3 classes in the apk.
Just get rules from below files/links
https://raw.githubusercontent.com/square/okhttp/master/okhttp/src/jvmMain/resources/META-INF/proguard/okhttp3.pro
https://github.com/square/okio/blob/master/okio/src/jvmMain/resources/META-INF/proguard/okio.pro
Refer
https://square.github.io/okhttp/features/r8_proguard/
I experienced an identical problem on Android 4.1.1 with OkHttp 3.12.12 but the suggest fix to proguard rules did not correct my issue.
Interesting to note that the NoClassDefFoundError appears to be coming from a copy of android.net.Network that is bundled in the APK by r8 but contains the following unusual code (disassembled with apktool):
.class public synthetic Landroid/net/Network;
.super Ljava/lang/Object;
# interfaces
.implements Landroid/os/Parcelable;
# direct methods
.method static synthetic constructor <clinit>()V
.locals 1
new-instance v0, Ljava/lang/NoClassDefFoundError;
invoke-direct {v0}, Ljava/lang/NoClassDefFoundError;-><init>()V
throw v0
.end method
This is why the NoClassDefFoundError has no message and originates from clinit (static initializer).
This is a fairly wild guess but perhaps r8 generates this code internally for the minify phase and then accidentally materializes it into the APK? So it appears this is likely some kind of bug in r8.
Regardless the fix that actually worked was to not use Android Gradle plugin 7.3.X and instead revert to AGP 7.2.2. AGP 7.4.0-beta04 also appears to work. With these other version of AGP the unsual android.net.Network class does not appear in the APK.

Jackson serialisation fails by throwing KotlinReflectionInternalError when serializing model with empty list and minifyEnabled true

I have minifyEnabled true in my build.gradle file and let's say I have a model
data class SomeModel(
val numberList: List<Int>
)
It get's serialized properly when numberList variable have some value's
SomeModel(listOf(1, 2, 3))
but the serialization fails if the content of numberList is empty
SomeModel(listOf()) // or SomeModel(emptyList())
Above both cases work fine if I change code to minifyEnabled false
I serialize like this
jsonMapper.writeValueAsString(someModel)
My Jackson's JsonMapper looks like this
JsonMapper().apply {
enable(MapperFeature.INFER_PROPERTY_MUTATORS)
enable(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
registerModule(KotlinModule())
}
I even have added the class in proguard-rules.pro file
-keep class org.package.SomeModel {
<fields>;
}
# tried this also
-keep class org.package.SomeModel {
*;
}
The error log are as follows
kotlin.reflect.jvm.internal.KotlinReflectionInternalError: No accessors or field is found for property val kotlin.collections.EmptyList.serialVersionUID: kotlin.Long
at kotlin.reflect.jvm.internal.KPropertyImplKt.computeCallerForAccessor(KPropertyImpl.kt:240)
at kotlin.reflect.jvm.internal.KPropertyImplKt.access$computeCallerForAccessor(KPropertyImpl.kt:1)
at kotlin.reflect.jvm.internal.KPropertyImpl$Getter$caller$2.invoke(KPropertyImpl.kt:156)
at kotlin.reflect.jvm.internal.KPropertyImpl$Getter$caller$2.invoke(KPropertyImpl.kt:147)
at kotlin.reflect.jvm.internal.ReflectProperties$LazyVal.invoke(ReflectProperties.java:62)
at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:31)
at kotlin.reflect.jvm.internal.KPropertyImpl$Getter.getCaller(Unknown Source:7)
at kotlin.reflect.jvm.ReflectJvmMapping.getJavaMethod(ReflectJvmMapping.kt:63)
at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector.getCorrespondingGetter(KotlinAnnotationIntrospector.kt:145)
at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector.hasRequiredMarker(KotlinAnnotationIntrospector.kt:110)
at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector.access$hasRequiredMarker(KotlinAnnotationIntrospector.kt:23)
at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector$hasRequiredMarker$hasRequired$1.invoke(KotlinAnnotationIntrospector.kt:40)
at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector$hasRequiredMarker$hasRequired$1.invoke(KotlinAnnotationIntrospector.kt:23)
at com.fasterxml.jackson.module.kotlin.ReflectionCache.javaMemberIsRequired(ReflectionCache.kt:56)
at com.fasterxml.jackson.module.kotlin.KotlinAnnotationIntrospector.hasRequiredMarker(KotlinAnnotationIntrospector.kt:33)
at com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair.hasRequiredMarker(AnnotationIntrospectorPair.java:318)
at com.fasterxml.jackson.databind.introspect.AnnotationIntrospectorPair.hasRequiredMarker(AnnotationIntrospectorPair.java:318)
at com.fasterxml.jackson.databind.introspect.POJOPropertyBuilder.getMetadata(POJOPropertyBuilder.java:229)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._anyIndexed(POJOPropertiesCollector.java:1197)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._sortProperties(POJOPropertiesCollector.java:1099)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collectAll(POJOPropertiesCollector.java:425)
at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getJsonValueAccessor(POJOPropertiesCollector.java:227)
at com.fasterxml.jackson.databind.introspect.BasicBeanDescription.findJsonValueAccessor(BasicBeanDescription.java:258)
at com.fasterxml.jackson.databind.ser.BasicSerializerFactory.findSerializerByAnnotations(BasicSerializerFactory.java:391)
at com.fasterxml.jackson.databind.ser.BasicSerializerFactory.buildCollectionSerializer(BasicSerializerFactory.java:705)
at com.fasterxml.jackson.databind.ser.BasicSerializerFactory.buildContainerSerializer(BasicSerializerFactory.java:647)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:200)
at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:169)
at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:1473)
at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1441)
at com.fasterxml.jackson.databind.SerializerProvider.findPrimaryPropertySerializer(SerializerProvider.java:651)
at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddPrimarySerializer(PropertySerializerMap.java:72)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter._findAndAddDynamic(BeanPropertyWriter.java:895)
E/AndroidRuntime: at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:706)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:770)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:178)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4485)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3740)
at org.nvest.BiTool.test1(BiTool.kt:173)
at org.bharti.axa.ui.HomeScreenActivity$onCreate$4.onClick(HomeScreenActivity.kt:92)
at android.view.View.performClick(View.java:7509)
at android.view.View.performClickInternal(View.java:7486)
at android.view.View.access$3600(View.java:841)
at android.view.View$PerformClick.run(View.java:28709)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:236)
at android.app.ActivityThread.main(ActivityThread.java:8060)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:656)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:967)
The error indicates that No accessors or field is found for property val kotlin.collections.EmptyList.serialVersionUID: kotlin.Long
So kotlin.reflect knows about EmptyList.serialVersionUID but it can't find it for Jackson to serialize the empty list. For it to know about EmptyList.serialVersionUID while it's not actually accessible likely means that it was in the kotlin.Metadata of kotlin.collections.EmptyList but not in the class itself.
Since this happens only with minifyEnabled = true, it appears that r8 stripped kotlin.collections.EmptyList.serialVersionUID but didn't actually update the kotlin.Metadata of kotlin.collections.EmptyList to avoid incorrectly indicating that the field is still there.
This is likely a bug with r8, and r8 is packaged with the android gradle plugin, so updating the android gradle plugin to the latest stable release should resolve the issue. If it doesn't, I recommend filing an r8 bug in the Google Issue Tracker.

Proguard doesnt preserve the line numbers and method names in stacktrace

Here are few lines from proguard-rules.pro
-keepattributes *Annotation*
-keepattributes Signature
-keepattributes InnerClasses,EnclosingMethod
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception
-dontwarn org.apache.http.**
Logcat output (error line number is listed as 1133, while my source file is 100 lines longer)
09-04 16:11:46.698 3827-5280/com.XX.main E/AndroidRuntime: FATAL EXCEPTION: IntentService[ActivityRecognizedTracker]
Process: com.XX.main, PID: 3827
java.lang.NullPointerException: Attempt to read from field 'double com.XX.trips.Trip.a' on a null object reference
at com.XX.ActivityRecognizedTracker.onHandleIntent(SourceFile:1133)
I am preserving the line numbers and source file attributes, but stack trace is still obfuscated. What am I doing wrong?
AFAIK it's not possible to obfuscate the code and have original stacktraces. So if you want to see original method and class names in the stacktrace, you have to add -dontobfuscate rule.
But you don't really need the original stacktrace.
You are using -keepattributes SourceFile,LineNumberTable and that enables you to unambiguously retrace the stacktrace. Just don't forget to keep the generated mapping.txt file.
Moreover if you remove -renamesourcefileattribute SourceFile you'll see the original file names in the parentheses. The line number is already there, so you should be able to figure out without retracing where the exception actually happened.

How to decode ProGuard's obfuscated code precisely?

I am using ProGuard in my application and problem is when users report some problem to my console and I can't decode it precisely because of "Unknown source".
Here is example of stacktrace:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.my.package.j.a(Unknown Source)
at com.a.a.c.c.j(Unknown Source)
at com.a.a.c.c.b(Unknown Source)
at com.a.a.c.e.run(Unknown Source)
at java.lang.Thread.run(Thread.java:856)
Then I am using this code to decode it:
./retrace.sh -verbose mapping.txt stacktrace.txt > out.txt
And here is output:
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.my.package.MyFragment$10.void output(int,java.lang.String)(Unknown Source)
at com.stericson.RootTools.execution.Shell.void readOutput()(Unknown Source)
at com.stericson.RootTools.execution.Shell.void closeCustomShell()(Unknown Source)
com.stericson.RootTools.execution.Shell startShell(int)
void access$200(com.stericson.RootTools.execution.Shell)
at com.stericson.RootTools.execution.Shell$2.void run()(Unknown Source)
at java.lang.Thread.run(Thread.java:856)
It only shows name of Fragment when error occurred, but I also need exact line and method.
Your question has actually two parts.
1) Why are you missing the line information?
You are removing the line information during obfuscation. You need the following rules in your proguard.cfg
-renamesourcefileattribute MyApplication
-keepattributes SourceFile,LineNumberTable
Find details for retracing line numbers here: http://proguard.sourceforge.net/manual/retrace/examples.html#with
2) Why is it missing some method/class name, in your example
com.my.package.MyFragment$10.void
This is because $10 is most likely an anonymous class declaration which will be treated differently during compiling and subsequent obfuscation. First easy solution is of course to get rid of the anonymous declaration and declare it somewhere. Another solution would be to add the following line again to your proguard.cfg
-keepattributes EnclosingMethod
This of course will again not remove some information and will reduce your obfuscation.

ProGuard not working as expected after Android Studio update

Since I updated Android Studio to version 1.2.1.1 I have the following problem:
Whenever I build a release version / build variant of my app, I get a NoClassDefFoundError on the Adjust library I have included in the project as a library module.
The stracktrace:
java.lang.NoClassDefFoundError: com.adjust.sdk.AdjustConfig
at de.myapp.GlobalApp.prepareAdjust(GlobalApp.java:111)
at de.myapp.GlobalApp.onCreate(GlobalApp.java:71)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
at android.app.ActivityThread.access$1300(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Corresponding part of my code:
AdjustConfig config = new AdjustConfig(this, someString, otherString);
When I turn off Proguard with minifyEnabled false; in my build.gradle, the error is gone.
My proguard-rules.pro looks like this:
-keepattributes **
-keep class !android.support.v7.internal.view.menu.**,** {*;}
-dontpreverify
-dontoptimize
-dontshrink
-dontwarn **
These Proguard rules might look a bit strange because they do but one thing: obfuscate classes in the android.support.v7.internal.view.menu package.
This procedure is a workaround for a a known issue of the Android Support library on Samsung devices.
Even more confusingly, the NoClassDefFoundError only occurs only devices running Android < 5.0.
Any ideas on what the reason could be or how to fix this?
The documentation says:
If you are using Proguard, add these lines to your Proguard file:
-keep class com.adjust.sdk.** { *; }
-keep class com.google.android.gms.common.** { *; }
-keep class com.google.android.gms.ads.identifier.** { *; }

Categories

Resources