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

Crashes on API < 24 with new AGP that is needed for API 33 #242

Open
polivmi1 opened this issue Aug 25, 2022 · 8 comments
Open

Crashes on API < 24 with new AGP that is needed for API 33 #242

polivmi1 opened this issue Aug 25, 2022 · 8 comments

Comments

@polivmi1
Copy link

Linking to work done in #225

For some reason, AGP included these dependencies also for lower versions, but the newest build system removes them and we shouldn't call them on API < 24:

@TakahikoKawasaki this PR doesn't seem to fix the issue as
`private static void initialize() throws Exception
{
// Constructor which represents javax.net.ssl.SNIHostName(String).
// The class is available since Java 1.8 / Android API Level 24 (Android 7.0)
sSNIHostNameConstructor = Misc.getConstructor(
"javax.net.ssl.SNIHostName", new Class<?>[] { String.class });

    // Method which represents javax.net.ssl.SSLParameters.setServerNames(List<SNIServerName>).
    // The method is available since Java 1.8 / Android API Level 24 (Android 7.0)
    sSetServerNamesMethod = Misc.getMethod(
            "javax.net.ssl.SSLParameters", "setServerNames", new Class<?>[] { List.class });
}`

is still called. This causes a crash on the newest AGP used with Android 33 - 7.3.x

java.lang.NoClassDefFoundError at javax.net.ssl.SNIServerName.<clinit>(Unknown Source) at java.lang.Class.classForName(Native Method) at java.lang.Class.forName(Class.java:308) at java.lang.Class.forName(Class.java:272)

and NoClassDefFoundError is an Error and not an exception

https://developer.android.com/reference/javax/net/ssl/SNIServerName was added with API 24

A fix is to either catch the error, or better prevent this code to run on API < 24

@andrew-ld
Copy link

//Android versions below Nougat (Version 7/SDK 24) require setHostname() to be invoked on the socket object

@polivmi1
Copy link
Author

@andrew-ld this is true, but because

is called in the initialization and that method isn't available for < 24, it will throw a NoClassDefFoundError which isn't caught. So my suggestion is to either catch this or better remove the code for API < 24

@andrew-ld
Copy link

andrew-ld commented Sep 19, 2022

there is a try catch in static initialization

static
{
try
{
initialize();
}
catch (Exception e)
{
e.printStackTrace();
}
}

@polivmi1
Copy link
Author

@andrew-ld as I wrote in the first message, that catch is useless, because NoClassDefFoundError is an Error and not an exception, so it will not catch it.

@andrew-ld
Copy link

I missed the point, you are right, it should be handled directly throwable to avoid future problems.

@polivmi1
Copy link
Author

@andrew-ld will you be able to add it or should I create a PR? It looks like the repository is dead...

@yandeqing
Copy link

yandeqing commented Jan 30, 2023

Linking to work done in #225

I download the source project add it to my project then skip the "initialize" method on API < 24

like

    private static void initialize() throws Exception {
        if (Build.VERSION.SDK_INT < 24) {
            Log.e("SNIHelper", "The class is available since Java 1.8 / Android API Level 24 (Android 7.0)");
            return;
        }
        // Constructor which represents javax.net.ssl.SNIHostName(String).
        // The class is available since Java 1.8 / Android API Level 24 (Android 7.0)
        sSNIHostNameConstructor = Misc.getConstructor(
                "javax.net.ssl.SNIHostName", new Class<?>[]{String.class});

        // Method which represents javax.net.ssl.SSLParameters.setServerNames(List<SNIServerName>).
        // The method is available since Java 1.8 / Android API Level 24 (Android 7.0)
        sSetServerNamesMethod = Misc.getMethod(
                "javax.net.ssl.SSLParameters", "setServerNames", new Class<?>[]{List.class});
    }

@yandeqing
Copy link

yandeqing commented Jan 30, 2023

there is a try catch in static initialization

static
{
try
{
initialize();
}
catch (Exception e)
{
e.printStackTrace();
}
}

static 
{ 
    try 
    { 
       if (Build.VERSION.SDK_INT < 24) {
           return;
       }
        initialize(); 
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
    } 
} 

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

No branches or pull requests

3 participants