ZitchCode / SecureHttpClient

Cross-platform HttpClientHandler with TLS1.2 and Certificate Pinning
MIT License
33 stars 6 forks source link

[android] java.lang.NoSuchFieldError when nugets that reference Square.OkIO as Com.Airbnb.Android.Lottie are used #13

Closed gsgou closed 3 years ago

gsgou commented 3 years ago

App targets Android 11 var caCertEncoded = ResourceHelpers.GetString("my_ca.der"); var caCert = Encoding.ASCII.GetBytes(caCertEncoded); var innerHandler = new SecureHttpClientHandler(null); innerHandler.UseProxy = false; innerHandler.SetTrustedRoots(caCert);

First i receive this error:

Java.Lang.NoClassDefFoundError: Failed resolution of: Lkotlin/jvm/internal/Intrinsics; ---> Java.Lang.ClassNotFoundException: Didn't find class "kotlin.jvm.internal.Intrinsics" on path: DexPathList[[zip file "/data/app/packagename-MIedcx8jioN_ktfV9OYQ6g==/base.apk"],nativeLibraryDirectories=[/data/app/packagename-MIedcx8jioN_ktfV9OYQ6g==/lib/arm64, /data/app/packagename-MIedcx8jioN_ktfV9OYQ6g==/base.apk!/lib/arm64-v8a, /system/lib64, /system/product/lib64]] --- End of inner exception stack trace --- at Java.Interop.JniEnvironment+StaticMethods.CallStaticObjectMethod (Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue args) [0x0006e] in <94b04e1756104d1fa1d144932ae2720c>:0 at Java.Interop.JniPeerMembers+JniStaticMethods.InvokeObjectMethod (System.String encodedMember, Java.Interop.JniArgumentValue parameters) [0x00018] in <94b04e1756104d1fa1d144932ae2720c>:0 at Square.OkHttp3.MediaType.Parse (System.String obj) [0x00029] in <1cd2f9ca08484661a451714e3f29dd79>:0 at SecureHttpClient.SecureHttpClientHandler.SendAsync (System.Net.Http.HttpRequestMessage request...

It went away by adding Xamarin.Kotlin.StdLib package. Then i received the following. I haven't researched further sofar on this.

java.lang.NoSuchFieldError: No field Companion of type Lokio/Options$Companion; in class Lokio/Options; or its superclasses (declaration of 'okio.Options' appears in /data/app/packagename-PoqnLdcyHFiqMwthixxBUA==/base.apk) at okhttp3.internal.Util.(Util.kt:71) at okhttp3.internal.Util.checkOffsetAndCount(Util.kt:97) at okhttp3.RequestBody$Companion.create(RequestBody.kt:145) at okhttp3.RequestBody$Companion.create$default(RequestBody.kt:143) at okhttp3.RequestBody.create(Unknown Source:8) at mono.java.lang.RunnableImplementor.n_run(Native Method) at mono.java.lang.RunnableImplementor.run(RunnableImplementor.java:30) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:224) at android.app.ActivityThread.main(ActivityThread.java:7562) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)

Downgrading to 1.14.1 which uses Square.OkHttp3 (>= 3.14.4) fixed this specific issue that rises with Square.OkHttp3 v4 having moved its implementation to Kotlin. Do i miss some configuration in order to upgrade?

tranb3r commented 3 years ago

For the first error, yes I confirm that you need to add the Xamarin.Kotlin.StdLib package to your application. The reason why you need it is because the dependency is not transitive (yet).

It would be very easy to fix and I've opened a ticket a few months ago in xamarin.components, but it still needs to be fixed: https://github.com/xamarin/XamarinComponents/issues/1017

tranb3r commented 3 years ago

For the second error, I don't know. There is a unit test similar to your code, so it should work. Could you please share a repro project so I can check your code ?

gsgou commented 3 years ago

Tks @tranb3r for your feedback. I ll have to create a new project with the API call over weekend. I ll share it over the next days so you can take a look.

gsgou commented 3 years ago

@tranb3r finally i could reproduce the issue. It seems simply adding the nuget Com.Airbnb.Xamarin.Forms.Lottie (which includes a jar) in the project causes various java.lang.NoSuchFieldError errors. Might also happen with other packages. Its reproduced here: https://github.com/gsgou/securehttpclient_issue_13 I appreciate if you can take a look on it.

gsgou commented 3 years ago

Hi @tranb3r will you have the chance to take a look this week on it?

tranb3r commented 3 years ago

Hi @tranb3r will you have the chance to take a look this week on it?

@gsgou Sure, I'll have a look this week.

gsgou commented 3 years ago

Thanks for the heads-up.

tranb3r commented 3 years ago

Com.Airbnb.Xamarin.Forms.Lottie 4.0.9 references Com.Airbnb.Android.Lottie 3.6.1, which references Square.OkIO (>= 1.17.4). SecureHttpClient 1.18.1 references Xamarin.SquareUp.Okio (>= 2.10.0).

Unfortunately, Xamarin.SquareUp.Okio and Square.OkIO are two different packages, because of this issue: https://github.com/xamarin/XamarinComponents/issues/1010

I suspect the issue you're facing is because jar from both packages are in conflict.

@gsgou Before I investigate more, could you please confirm if you're having an issue with some other package (that do not reference Square.OkIO), or if this is specific to Lottie ?

gsgou commented 3 years ago

@tranb3r i can confirm other packages that do not reference Square.OkIO have no issues. By removing Lottie the conflict went away. Thanks for mentioning the issue with the XamarinComponents repo.

tranb3r commented 3 years ago

@gsgou OK ! So until the official square.okio is updated in xamarincomponents, there is nothing we can do. Let's keep this issue open... and pray ;)

gsgou commented 3 years ago

@tranb3r a workaround was to explicitly decalre in Android project : <PackageReference Include="Square.OkIO" Version="2.8.0" />

Seems there are 52 nugets currently using Square.OkIO https://www.nuget.org/packages/Square.OkIO/#used-by I don't have idea whats different and required in Xamarin.Square.OkIO regarding the bindings and versioning but if there are no blocking reasons maybe using the more popular Square.OkIO would help avoid similar conflicts with other nugets.

tranb3r commented 3 years ago

You're right, SecureHttpClient 1.18.1 uses OkHttp 4.9.1, which requires OkIO >= 2.8.0. So the workaround you propose should work. And we could use Square.OkIO instead of Xamarin.SquareUp.OkIO.

However, starting from OkHttp 4.10, it will require OkIO >= 2.9.0. So unless there is an update in XamarinComponents, it won't be able to upgrade OkHttp in SecureHttpClient if I switch back to Square.OkIO.

gsgou commented 3 years ago

Thank you i understand you need to support Xamarin.SquareUp.OkIO due to slow updates in XamarinComponents currently to keep the project up to date https://square.github.io/okhttp/changelog/.

Let's pray for better times with XamarinComponents :) Tks for your effort on this cool project!

tranb3r commented 3 years ago

Xamarin Components have been updated, so I've updated SecureHttpClient to use again official square bindings. The fix is in SecureHttpClient 1.18.3.

gsgou commented 3 years ago

Thank you for the heads-up. Xamarin.Kotlin.StdLib is also not needed explicitly anymore.

tranb3r commented 3 years ago

Thank you for the heads-up. Xamarin.Kotlin.StdLib is also not needed explicitly anymore.

Yes. The buildtransitive was added to the kotlin package. I've removed the dependency in the test projects.