Reputation: 21
I just got a new laptop and after installing flutter I tried to run it using USB debugging but it keeps showing this error
Launching lib\main.dart on 123456ABC in debug mode...
Exception in thread "main" java.net.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed
because connected host has failed to respond
at java.base/sun.nio.ch.NioSocketImpl.implRead(NioSocketImpl.java:325)
at java.base/sun.nio.ch.NioSocketImpl.read(NioSocketImpl.java:350)
at java.base/sun.nio.ch.NioSocketImpl$1.read(NioSocketImpl.java:803)
at java.base/java.net.Socket$SocketInputStream.read(Socket.java:966)
at java.base/sun.security.ssl.SSLSocketInputRecord.read(SSLSocketInputRecord.java:484)
at java.base/sun.security.ssl.SSLSocketInputRecord.readHeader(SSLSocketInputRecord.java:478)
at java.base/sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:160)
at java.base/sun.security.ssl.SSLTransport.decode(SSLTransport.java:111)
at java.base/sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1510)
at java.base/sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1425)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:455)
at java.base/sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:426)
at java.base/sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:580)
at java.base/sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:187)
at java.base/sun.net.www.protocol.http.HttpURLConnection.followRedirect0(HttpURLConnection.java:2902)
at java.base/sun.net.www.protocol.http.HttpURLConnection.followRedirect(HttpURLConnection.java:2811)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1927)
at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1589)
at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:224)
at org.gradle.wrapper.Download.downloadInternal(Download.java:58)
at org.gradle.wrapper.Download.download(Download.java:44)
at org.gradle.wrapper.Install$1.call(Install.java:61)
at org.gradle.wrapper.Install$1.call(Install.java:48)
at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:65)
at org.gradle.wrapper.Install.createDist(Install.java:48)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:128)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Running Gradle task 'assembleDebug'... 55.7s
Exception: Gradle task assembleDebug failed with exit code 1
I ran flutter doctor and it shows no problem with my setup
[√] Flutter (Channel stable, 3.13.9, on Microsoft Windows [Version 10], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.7.6)
[√] Android Studio (version 2022.2)
[√] VS Code (version 1.79.2)
[√] Connected device (4 available)
[√] Network resources
• No issues found!
I have used flutter multiple times on other systems but I have not had this problem. Also saw a similar question but it was not really centered on flutter and so were the replies so I'm asking one centered on flutter
Upvotes: 2
Views: 4425
Reputation: 1
After months of troubleshooting, sleepless nights, countless open tabs, and help from ChatGPT, I finally found a solution to this messed up Exception error in Flutter for Android emulators! Although fixing it led to a new (and thankfully simpler) issue, this initial hurdle was by far the most challenging. Here's the full process I followed to resolve it: Step 1: Run Flutter Doctor Start by running flutter doctor to diagnose your environment and identify any missing components or issues. This command helps you understand which dependencies need to be installed or configured.
Step 2: Install cmdline-tools One of the key steps was to install Android’s cmdline-tools, as it was missing from the SDK.
C:\Users\YourUsername\AppData\Local\Android\Sdk\cmdline-tools\latest 3. Ensure that the bin directory within cmdline-tools is added to your system’s Path environment variable: makefile
C:\Users\YourUsername\AppData\Local\Android\Sdk\cmdline-tools\latest\bin Step 3: Accept Android Licenses After setting up cmdline-tools, navigate to the bin directory and accept the Android SDK licenses. Use the command: bash flutter doctor --android-licenses This step ensures that all necessary Android licenses are accepted, which is critical for running Android emulators and building apps. Step 4: Configure Java Ensure your Java Development Kit (JDK) version is compatible with both Flutter and Android:
java -version This should return Java 17. Step 5: Run Flutter Again After setting up cmdline-tools, accepting licenses, and configuring Java, try running your Flutter code on the Android emulator. With everything correctly set up, the emulator should run without the original connection error.
The major Issue Im facing currently is this: ERROR: JAVA_HOME is set to an invalid directory: =C:\Program Files\Java\jdk-22
Please set the JAVA_HOME variable in your environment to match the location of your Java installation. Running Gradle task 'assembleDebug'... 243ms Error: Gradle task assembleDebug failed with exit code 1
And I've tried everything
Upvotes: 0
Reputation: 11
This is what I did: I went to the android folder and opened the gradle-wrapper.properties
file. Then I found the particular version the flutter is trying to use, then I went to open a android project in Android Studio and change the project gradle version to that of the flutter and I let Android Studio download the gradle and I reopened my flutter project. It downloaded some files and everything works.
Sometimes Android Studio might also be misbehaving by not downloading the gradle. Just set your preferred gradle then go to gradle-wrapper.properties
in Android Studio project then delete the backslash from the distribution URL. (Just make sure the URL is correct.) Then invalidate cache and restart. That should work and sometimes Android Studio might not be able to download the lower version gradle that fluter might be using. Just copy the URL of the current Android Studio gradle, which should have been downloaded at that time, copy the URL and paste it in the flutter project gradle-wrapper.properties
on the distribution URL. It will download necessary file and it should work.
It took days to solve this. Hope it helps.
Upvotes: 0
Reputation: 467
You should keep enable developer mode on your device. You can enable it following bellow steps.
Go to your device settings its mean you'r android mobile.
Go to About phone and you can see build number in there.
You can press it 5 times and now you can enable your developer option.
Follow this article by using this link you canegot more details about it.
Upvotes: 1
Reputation: 39
I found the solution to this problem after 2 days of tries, i found out that the Link to the Gradle website in the GradleWrapper is not working, so i only changed it from https to http and then i manually downloaded a Gradle-8.5.bin.zip and added it to the folder inside dist in the actual .gradle folder in C://, then i close the project and opened again. That's all! Happy coding
Upvotes: 0