Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android P API Compatibility #1387

Open
rantianhua opened this issue Sep 18, 2018 · 8 comments
Open

Android P API Compatibility #1387

rantianhua opened this issue Sep 18, 2018 · 8 comments

Comments

@rantianhua
Copy link

I installed my app on Android P, there was a dialog showing "Detected problems with API compatibility (visit g.co/dev/appcompat for more info)" every time i launched my app. I enabled the new StrictMode feature and I got the stack trace:

09-18 14:42:15.389 27929-27929/com.xxx.xxx D/StrictMode: StrictMode policy violation: android.os.strictmode.NonSdkApiUsedViolation: Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe;
        at android.os.StrictMode.lambda$static$1(StrictMode.java:428)
        at android.os.-$$Lambda$StrictMode$lu9ekkHJ2HMz0jd3F8K8MnhenxQ.accept(Unknown Source:2)
        at java.lang.Class.getDeclaredField(Native Method)
        at com.google.gson.internal.UnsafeAllocator.create(UnsafeAllocator.java:41)
        at com.google.gson.internal.ConstructorConstructor$14.<init>(ConstructorConstructor.java:221)
        at com.google.gson.internal.ConstructorConstructor.newUnsafeAllocator(ConstructorConstructor.java:220)
        at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:96)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:101)
        at com.google.gson.Gson.getAdapter(Gson.java:457)
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53)
        at com.google.gson.Gson.getAdapter(Gson.java:457)
        at retrofit2.converter.gson.GsonConverterFactory.responseBodyConverter(GsonConverterFactory.java:63)
        at retrofit2.Retrofit.nextResponseBodyConverter(Retrofit.java:330)
        at retrofit2.Retrofit.responseBodyConverter(Retrofit.java:313)
        at retrofit2.ServiceMethod$Builder.createResponseConverter(ServiceMethod.java:739)
        at retrofit2.ServiceMethod$Builder.build(ServiceMethod.java:172)
        at retrofit2.Retrofit.loadServiceMethod(Retrofit.java:170)
        at retrofit2.Retrofit$1.invoke(Retrofit.java:147)
        at java.lang.reflect.Proxy.invoke(Proxy.java:1006)

So there is any solution or workaround not to reflect system non-open api?

@hamilyon
Copy link

#1305

@ChristopheVersieux
Copy link

I can confirm this is in latest version 2.8.5

This warning also appears in Google Play Developer console under compatibility issues.

@davidoakley
Copy link

This is still in the latest version 2.8.6, and is still appearing as a warning on the Google Play Developer console.

@mwshubham
Copy link

+1

@ravinderparihar
Copy link

Add androidx multidex in build.gradle it's working for me
implementation 'androidx.multidex:multidex:2.0.1'

@Marcono1234
Copy link
Collaborator

Probably related to #445

@zipswich
Copy link

I have the following:

    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'com.google.code.gson:gson:2.8.6'

I get:

StrictMode policy violation: 
android.os.strictmode.NonSdkApiUsedViolation: Lsun/misc/Unsafe;->theUnsafe:Lsun/misc/Unsafe;
        at android.os.StrictMode.lambda$static$1(StrictMode.java:407)
        at android.os.-$$Lambda$StrictMode$lu9ekkHJ2HMz0jd3F8K8MnhenxQ.accept(Unknown Source:2)
        at java.lang.Class.getDeclaredField(Native Method)
        at com.google.gson.internal.UnsafeAllocator.create(UnsafeAllocator.java:41)
        at com.google.gson.internal.ConstructorConstructor$14.<init>(ConstructorConstructor.java:221)
        at com.google.gson.internal.ConstructorConstructor.newUnsafeAllocator(ConstructorConstructor.java:220)
        at com.google.gson.internal.ConstructorConstructor.get(ConstructorConstructor.java:96)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:101)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.internal.bind.MapTypeAdapterFactory.getKeyAdapter(MapTypeAdapterFactory.java:142)
        at com.google.gson.internal.bind.MapTypeAdapterFactory.create(MapTypeAdapterFactory.java:125)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:117)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:166)
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102)
        at com.google.gson.Gson.getAdapter(Gson.java:458)
        at com.google.gson.Gson.fromJson(Gson.java:931)
        at com.google.gson.Gson.fromJson(Gson.java:870)

@Marcono1234
Copy link
Collaborator

Do you intentionally rely on Gson creating instances of classes without default constructor1?

  • If yes: Then I am not sure if there is any solution to this because Gson relies on internal JDK / Android methods to create instances of such classes. Though any hints for solutions to this are welcome.
  • If no: Could you please try the method GsonBuilder.disableJdkUnsafe()? And for any failing classes either:
    • Add a default constructor
    • If it is a non-static inner class make the class static

Footnotes

  1. default constructor = constructor without parameters

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants