grpc / grpc-java

The Java gRPC implementation. HTTP/2 based RPC
https://grpc.io/docs/languages/java/
Apache License 2.0
11.48k stars 3.85k forks source link

getAlpnSelectedProtocol android 15 changes #11625

Open VEZE opened 1 month ago

VEZE commented 1 month ago

This method marked as max-target-q on android 15 chagelog. Lcom/android/org/conscrypt/OpenSSLSocketImpl->getAlpnSelectedProtocol()[B,core-platform-api,max-target-q

Method may fall under the category of APIs that are conditionally blocked based on the target API level of the app. Specifically, if app targets a version higher than Android 10 (API level 29), we might encounter restrictions accessing this method due to non-SDK interface policies.

What version of gRPC-Java are you using?

1.66.0

What is your environment?

Android

What did you expect to see?

Nothing in strictMode logs.

What did you see instead?

Lcom/android/org/conscrypt/OpenSSLSocketImpl;->getAlpnSelectedProtocol()[B io.grpc.okhttp.internal.OptionalMethod.getPublicMethod (OptionalMethod.java:178)

Steps to reproduce the bug

Enabling strictMode with non sdk api violations logging.

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
            .detectNonSdkApiUsage()
            .penaltyLog()
            .build());
kannanjgithub commented 1 month ago

In the android documentation for "Expected behavior when restricted non-SDK interfaces are accessed" it says "NoSuchMethodError" will be thrown for "Reflection using Class.getDeclaredMethod(), Class.getMethod()", which is handled and ignored in gRPC okhttp code. So it should be fine.

ejona86 commented 6 days ago

But if GET_ALPN_SELECTED_PROTOCOL fails then it only tried NPN next, which should fail too. I think the question is why wasn't the [Java 9 ALPN API](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/javax/net/ssl/SSLSocket.html#getApplicationProtocol()) used, or why did it fail. I see it was used on an earlier line, so it failed? OptionalMethod only does reflection when actually invoked, so it seems it was used.

The "Java 9 ALPN APIs" are only available on Android 10+, so if you targeted a newer version but your minSdk is lower and you were testing on an older device, that could be a problem. That would mean there is no ALPN API available at all.

@VEZE, what version of Android were you running on when you noticed this issue?

VEZE commented 6 days ago

@ejona86 Android 15

kannanjgithub commented 6 days ago

Do you find this log viz

Socket unsupported for getApplicationProtocol, will try old methods

with FINER logging enabled? Or alternatively,

Failed to find Android 10.0+ APIs

from here (which would be surprising).