okhttp3 minifyEnabled causes crash on Android 4.1.2 - android

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.

Related

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.

Android - App with obfuscated AAR library is crashing when minifyEnabled true causes java.lang.ExceptionInInitializerError

I have an android app that uses CSRMesh Library as an AAR module. The AAR itself is obfuscated. My app works fine when minifyEnabled is false in build.gradle but when I set it to true the app is crashing.
The following code is from a custom MeshLibraryManager class for making API calls to the library.
private void enableBluetooth() {
try {
mCurrentChannel = MeshChannel.BLUETOOTH;
isChannelReady = false;
mBtBearer = new BluetoothBearer(this, mContext.get());
if (mService != null) {
mService.setBearer(mBtBearer); // crash occurs in this line
mService.setBluetoothBearerEnabled(mBtBearer);
}
EventBus.getDefault().post(new MeshSystemEvent(MeshSystemEvent.SystemEvent.CHANNEL_NOT_READY));
} catch (Exception e) {
e.printStackTrace();
}
}
In this code block, mService is an instance of the MeshService class from the library.
I have added a comment in the line where the app crashes. If I comment out that line and the line below it the app doesn't crash but then I'm not able to perform the required Bluetooth operations.
This is the complete stack trace of the crash:
2021-05-14 16:21:48.994 27372-27372/com.example.myApp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myApp, PID: 27372
java.lang.ExceptionInInitializerError
at com.csr.internal.mesh_le.g0.a(SourceFile:70)
at com.csr.internal.mesh_le.f.l(SourceFile:269)
at com.csr.csrmesh2.MeshService.y(SourceFile:309)
at com.example.scanner.api.MeshLibraryManager.v(SourceFile:336)
at com.example.scanner.api.MeshLibraryManager.o(SourceFile:59)
at com.example.scanner.api.MeshLibraryManager$b.onServiceConnected(SourceFile:595)
at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1738)
at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1770)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:201)
at android.app.ActivityThread.main(ActivityThread.java:6820)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:922)
I have tried adding proguard rules for the required classes whichever I could figure from code and stack trace but nothing works.
Please suggest how can I fix this.
You must ensure that the following line will be there in your proguard rules. This will tell your application to ensure that the SpongyCastle encryption libraries can be used.
-keep class org.spongycastle.jcajce.provider.asymmetric.** { *; }

VerifyError: Rejecting class android.support.g.b because it failed compile-time verification

This is similar to Rejecting class because it failed compile-time verification Android and java.lang.VerifyError: Verifier rejected class on Lollipop when using release APK but the origin of my problem is still unknown and all the proposed solutions do not help.
Manually installing a signed release apk with MultiDex support and ProGuard enabled throws this exception on start:
475-475/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: ..., PID: 475
java.lang.VerifyError: Verifier rejected class android.support.g.b due to bad method void android.support.g.b.<init>() (declaration of 'android.support.g.b' appears in /data/app/...-2/base.apk)
at ...MyApp.attachBaseContext(Unknown Source)
at android.app.Application.attach(Application.java:181)
This will not happen with -dontoptimize. Current minimal optimization setting:
-optimizations "code/*"
I get the same error without -optimizations or with less restrictive options. Adding -keep class android.* wont help either. I get no error with "code/removal/*,code/simplification/*", but this ignores -assumenosideeffects
android.support.g.b is the obfuscated android.support.multidex.MultiDex class itself, and the error is caused when the overridden attachBaseContext(Context) is called:
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
Decoding this method (the default constructor) with the byte code viewer results in:
private b() { // <init> //()V
return
}
It would be useful to disable optimization by class specification.
Alternative: Knowing the minimum -optimizations option that considers -assumenosideeffects
Do not use member wildcards with -assumenosideeffects because this will "spill over" to Object for other classes. See https://sourceforge.net/p/proguard/bugs/716/#98d4
Similar problems may come up for any rules with interfaces. Example:
-assumenosideeffects class * implements Interface {
public <init>();
public Object interfacemethod(); # May concern methods with same signature in other Objects
}

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

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...

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