In an Android app, I added this code to onCreate()
Closeable sss = new Socket();
if (!(sss instanceof Closeable)) {
throw new Runtim
For future readers, I want to clarify something that was assumed to be evident by everyone in this discussion:
How could I assign Closeable sss = new Socket();
if Socket
is not Closeable
?
The API requirements specified in AndroidManifest.xml are:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
and the device reports
android.os.Build.VERSION.SDK_INT=16
The code was compiled for API level 19, this is why it was successfully compiled. But it was run on API level 16, and this is why it failed.
In other words, the compile-time check was performed for API19 on one computer (the desktop PC) while the run-time check was performed for API16 on another computer (the handheld device).
Socket is not itself closeable, but both of its streams are. And closing either stream will close the Socket
This issue occurs on Android API levels prior to 19. On affected versions, Socket does not implement Closeable.
Sources: