Android 无法打开调试器端口:java.net.SocketException“套接字已关闭”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23615523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:19:12  来源:igfitidea点击:

Unable to open debugger port : java.net.SocketException “Socket closed”

androiddebuggingandroid-studioandroid-gradle-plugin

提问by craned

I'm using Android Studio, with multiple flavors using Gradle, each with a Debug and Release type, organized as described here, on the bottom half.When I try to start the debugger, I get this error:

我正在使用 Android Studio,使用 Gradle 有多种风格,每种风格都有一个 Debug 和 Release 类型,按照此处的描述组织,在下半部分。当我尝试启动调试器时,出现此错误:

Error running androidRecover [installAppDebug]: Unable to open debugger port :     java.net.SocketException "Socket closed

I'm also unable to attach the debugger to my device once it's running (it only displays the name of my phone, not the app).

我也无法在调试器运行后将其连接到我的设备(它只显示我的手机名称,而不显示应用程序的名称)。

All 3 flavors install on my phone just fine. I just can't get it to let me debug them. I also tested attaching the debugger on a Nexus tablet, and I got the same result.

所有 3 种口味都安装在我的手机上就好了。我只是无法让我调试它们。我还在 Nexus 平板电脑上测试了连接调试器,我得到了相同的结果。

It's not Gradle specifically as a whole because I can run other Gradle-based apps and attach the debugger just fine so I wonder if it's something with how I've setup my Gradle project and settings.

它不是专门作为一个整体的 Gradle,因为我可以运行其他基于 Gradle 的应用程序并很好地附加调试器,所以我想知道它是否与我如何设置我的 Gradle 项目和设置有关。

Here's my build.gradle:

这是我的 build.gradle:

apply plugin: 'android'
apply from: 'signing.gradle'

android {
    compileSdkVersion 19
    buildToolsVersion '19.0.3'

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile
                     ('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

    productFlavors {
        flav1 {
            packageName "com.ex.flav1"
            versionCode 32
            versionName "1.0.5"
            signingConfig signingConfigs.flav1
        }

        flav2 {
            packageName "com.ex.flav2"
            versionCode 33
            versionName "1.0.6"
            signingConfig signingConfigs.flav2
        }

        flav3 {
            packageName "com.ex.flav3"
            versionCode 27
            versionName "1.0.0"
            signingConfig signingConfigs.flav3
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:19.+'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/httpmime-4.2.5.jar')
}

I really have no idea what else to try. Android Studio is completely up-to-date. I've restarted Android Studio, my phone, and my computer.

我真的不知道还有什么可以尝试的。Android Studio 是完全最新的。我已经重新启动了 Android Studio、我的手机和我的电脑。

Also, last week I was having this problem, but it was a specific socket that was blocked, from trying to run the emulator and my phone at the same time. I also noticed I had multiple Gradle processes running, because it wasn't killing them on its own, which I often had to kill Android Studio to kill them. Once that was fixed, it was working off and on.

另外,上周我遇到了这个问题,但它是一个特定的套接字被阻止,试图同时运行模拟器和我的手机。我还注意到我有多个 Gradle 进程在运行,因为它不会自行杀死它们,我经常不得不杀死 Android Studio 来杀死它们。一旦解决了这个问题,它就会断断续续地工作。

Let me know if you need any other info.

如果您需要任何其他信息,请告诉我。

Thanks, Devin

谢谢,德文

EditI finally know why @hasanaydogar's answer works and why it probably would have solved my problem if we had known it then. See my 2nd comment on it to know why, but in short, you have to select in that dropdown the name that matches your app's root directory.

编辑我终于知道为什么@hasanaydogar 的答案有效,以及为什么如果我们当时知道它可能会解决我的问题。请参阅我对它的第二条评论以了解原因,但简而言之,您必须在该下拉列表中选择与您的应用程序根目录匹配的名称。

回答by hasanaydogar

Just Click the button (left side the RUN button).

只需单击按钮(左侧的 RUN 按钮)。

enter image description here

在此处输入图片说明

Select Android. Then Run.

选择安卓。然后运行。

It will connect to your device.

它将连接到您的设备。

And dont forget to change build variant

并且不要忘记更改构建变体

enter image description here

在此处输入图片说明

回答by craned

I finally understand why I was getting that error so I'm going to explain how I Debugnow. Note that I use Gradle(build multiple apk's using the same code), which might influence some how you use the third part of this answer.

我终于明白为什么我会收到那个错误,所以我现在要解释我是怎么做的Debug。请注意,我使用Gradle(使用相同的代码构建多个 apk),这可能会影响您使用本答案第三部分的方式。

For these to work, in the dropdown next to the debug (icon in #1) and run buttons in the top toolbar, you have to have the one selected with the following icon next to it because that's the name of the root directory of your app where all your code lives:

为了使这些工作,在调试(#1 中的图标)和顶部工具栏中的运行按钮旁边的下拉菜单中,您必须选择旁边带有以下图标的那个,因为这是根目录的名称您所有代码所在的应用程序:

enter image description here

在此处输入图片说明

  1. To start debugging from the beginning, I run the app in Debugmode from the start, using this button in your toolbar:
  1. 为了从头开始调试,我Debug从一开始就以模式运行应用程序,使用工具栏中的这个按钮:

enter image description here!

在此处输入图片说明!

  1. To attach the debugger to the app when it's already running as @scottyab mentioned, I click the Attach Debuggerbutton in your toolbar:
  1. 要在应用程序已经按照@scottyab 提到的方式运行时将调试器附加到应用程序,我单击Attach Debugger工具栏中的按钮:

enter image description here

在此处输入图片说明

  1. To run the release version of my app in debug mode, I've started changing my strings in the Debugversion of strings.xmlin the file path myApp/src/appNameDebug(verses appNameRelease)/res/values/strings.xml, more easily seen here, on the bottom half. When I say change, I really mean that I have two versions of all the strings (3 in my case) necessary to change from using the debug server to using the release server. It might not be completely kosher, but it takes about 5 seconds to go the file, and hold down Cmd+/ and uncomment and comment all of the appropriate lines.
  1. 为了在调试模式下运行我的应用程序的发布版本,我已经开始在文件路径中的Debug版本中更改我的字符串,在这里更容易看到,在下半部分。当我说更改时,我的意思是我有两个版本的所有字符串(在我的例子中是 3 个),从使用调试服务器更改为使用发布服务器。它可能不完全符合 kosher,但大约需要 5 秒钟才能访问该文件,并按住 Cmd+/ 并取消注释和注释所有适当的行。strings.xmlmyApp/src/appNameDebug(verses appNameRelease)/res/values/strings.xml

My Releaseversion is just there for when I'm ready to build an apk for release.

我的Release版本只在我准备好构建 apk 以供发布时使用。

Doing things in this way has eliminated that error popping up anymore. I think the Releaseversion is just not made for debugging, and I haven't found an easy way to turn the debug flags on when in Releasemode.

以这种方式做事已经消除了再次出现的错误。我认为该Release版本不是为调试而制作的,而且我还没有找到一种在Release模式下打开调试标志的简单方法。

回答by Nick

I managed to get rid of this problem by killing & restart the adb process,hope this would help :]

我设法通过杀死并重新启动 adb 进程来解决这个问题,希望这会有所帮助:]

回答by Eric Lin

I have solved this question with reference to the following SO Answer

我已经参考以下SO Answer解决了这个问题

The "Select Run/Debug Configuration" button in android studio 2.3.2

android studio 2.3.2 中的“选择运行/调试配置”按钮

enter image description here

在此处输入图片说明

Change the Debug type to Native

将调试类型更改为本机

enter image description here

在此处输入图片说明

回答by scottyab

I managed to get this working by attaching the debugger after a build see Unable to open debugger port : java.net.SocketException "Socket closed"

我设法通过在构建后附加调试器来使其工作,请参阅无法打开调试器端口:java.net.SocketException“Socket closed”