I happened to notice NetworkOnMainThreadException was thrown on my Android device (Google Pixel; Android 8.0). The call stack looks as follows:
=== case 1:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at com.smartdevicelink.transport.TCPTransport.sendBytesOverTransport
at com.smartdevicelink.transport.SdlTransport.sendBytes
at com.smartdevicelink.SdlConnection.SdlConnection.onProtocolMessageBytesToSend
at com.smartdevicelink.protocol.AbstractProtocol.handlePacketToSend
at com.smartdevicelink.protocol.WiProProtocol.StartProtocolService
at com.smartdevicelink.SdlConnection.SdlConnection.startService
=== case 2:
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at com.smartdevicelink.transport.TCPTransport.sendBytesOverTransport
at com.smartdevicelink.transport.SdlTransport.sendBytes
at com.smartdevicelink.SdlConnection.SdlConnection.onProtocolMessageBytesToSend
at com.smartdevicelink.protocol.AbstractProtocol.handlePacketToSend
at com.smartdevicelink.protocol.WiProProtocol.EndProtocolService
at com.smartdevicelink.SdlConnection.SdlConnection.closeConnection
at com.smartdevicelink.SdlConnection.SdlConnection.unregisterSession
at com.smartdevicelink.SdlConnection.SdlSession.close
at com.smartdevicelink.proxy.SdlProxyBase.cleanProxy
at com.smartdevicelink.proxy.SdlProxyBase.dispose
While NetworkOnMainThreadException may be specific to TCPTransport as you see the call stack above, we should use worker thread for any communication with HU regardless of the transport.
This issue can happen if a) SdlProxyBase.startService() or .dispose() are called in main thread, and b) StrictMode is turned on the device.
This class has been deprecated and will be removed in the next major release (October, 2020). Therefore, we do not plan on creating a fix for this issue.
I happened to notice NetworkOnMainThreadException was thrown on my Android device (Google Pixel; Android 8.0). The call stack looks as follows: === case 1: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at com.smartdevicelink.transport.TCPTransport.sendBytesOverTransport at com.smartdevicelink.transport.SdlTransport.sendBytes at com.smartdevicelink.SdlConnection.SdlConnection.onProtocolMessageBytesToSend at com.smartdevicelink.protocol.AbstractProtocol.handlePacketToSend at com.smartdevicelink.protocol.WiProProtocol.StartProtocolService at com.smartdevicelink.SdlConnection.SdlConnection.startService
=== case 2: android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448) at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:108) at java.net.SocketOutputStream.write(SocketOutputStream.java:153) at com.smartdevicelink.transport.TCPTransport.sendBytesOverTransport at com.smartdevicelink.transport.SdlTransport.sendBytes at com.smartdevicelink.SdlConnection.SdlConnection.onProtocolMessageBytesToSend at com.smartdevicelink.protocol.AbstractProtocol.handlePacketToSend at com.smartdevicelink.protocol.WiProProtocol.EndProtocolService at com.smartdevicelink.SdlConnection.SdlConnection.closeConnection at com.smartdevicelink.SdlConnection.SdlConnection.unregisterSession at com.smartdevicelink.SdlConnection.SdlSession.close at com.smartdevicelink.proxy.SdlProxyBase.cleanProxy at com.smartdevicelink.proxy.SdlProxyBase.dispose
While NetworkOnMainThreadException may be specific to TCPTransport as you see the call stack above, we should use worker thread for any communication with HU regardless of the transport. This issue can happen if a) SdlProxyBase.startService() or .dispose() are called in main thread, and b) StrictMode is turned on the device.