android - native 代码调试在 Android Studio 3 中不起作用

标签 android android-studio android-debug

我已经尝试了 StackOverflow 上找到的所有方法,但仍然面临这个问题。

我创建了一个具有 native 支持的演示 Android 项目,向其中添加了一个库,并将所有 native 代码移至该库中。

现在我无法在 native 代码中的断点处停止, native 调试器仅在 SEGFAULT 崩溃后才变为 Activity 状态。

我已添加defaultPublishConfig "debug"进入我的图书馆 build.gradledebuggable true到应用程序build.fradle 。之前的 native 调试已经足够了。但自从Android Studio升级后就无法使用了。

这里是完整的build.gradle文件

应用程序

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.raistlin.myapplication"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation project(path: ':mylibrary')

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

我的图书馆

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28



    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        defaultPublishConfig "debug"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        externalNativeBuild {
            cmake {
                arguments "-DANDROID_TOOLCHAIN=clang"
                cppFlags "-fexceptions", "-std=c++11", "-DJSONCPP_NO_LOCALE_SUPPORT"
                version "3.10.2"
            }
        }
    }

    buildTypes {
        debug {
            debuggable true
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

最佳答案

首先,检查是否为库选择了调试变体。

由于您的 build.gradle 有一个设置,路径“src/main/cpp/CMakeLists.txt”,我认为它不会构建。因此,设置目标,重建并再次检查。

如果断点在构建后不起作用,旧的垃圾可能会留在构建缓存中并导致问题。在资源管理器中打开项目目录并手动删除构建缓存(.externalNativeBuild 文件夹)并再次构建项目。我还删除了构建文件夹,因为它在中间目录中包含 .so 文件,但它是可选的。

Android Studio 不会清理测试设备中的库。它们基本上都会被覆盖,但根据需要手动清除它们。文件位于/data/app/(包名)/lib/(cpu arch.)/.
注意:设备文件资源管理器的同步菜单在 lib 或 (cpu arch.) 目录下无法正确同步。要同步,请选择/data 或/data/app,然后选择同步。

NB.1 如果省略 targets,Android Studio 似乎不会构建任何目标。构建的输出位于 (project)/app/build/intermediates/cmake/(flavor)/obj/(cpu Architecture) 中。如果它似乎在没有任何目标的情况下工作,请检查设备上的文件。他们混淆了测试结果。

NB.2 debuggable true 用于发布版本以启用调试。无需将其设置为调试构建,因为可调试标志已设置为默认值。

NB.3 似乎与版本相关,但 Android Studio 中的 Gradle 无法正确清理 .externalNativeBuild 树,即使调用 clean 或重建也是如此,并且会混淆 native 代码构建配置。我记得当时是 AS3.0 左右。

NB.4 我的环境是

  • Android Studio 3.2.1
  • 类路径“com.android.tools.build:gradle:3.2.1”
  • gradle-4.7-全部
  • CMake:默认(3.6.4111459)

我知道 Android Studio、Gradle 和 CMake 有更新的版本,但它们有错误,所以我选择了当前环境。据我所知,Android Studio 3.3、gradle:3.3.0、gradle-4.10.1-都在 VCS(git) 中存在严重错误。有时,编辑器中会显示错误的文件内容,并且构建会失败。将 CMake 版本设置为 3.10.x(对我来说是 3.10.2)似乎也有问题。

这是我的项目的一个副本作为示例,对原始项目进行了部分修改,但可能有效。 我已经在 Android Studio 3.2.1 中检查了库工作中的断点。

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 23
        targetSdkVersion 28
        versionCode 1
        versionName "0.0.1"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        consumerProguardFiles 'proguard-rules.pro'
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                arguments "-DANDROID_STL=c++_static"
                targets "sample"
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

更新
targets这里提到了Guide - Link Gradle to your native library - Specify optional configurations .

关于android - native 代码调试在 Android Studio 3 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54237535/

相关文章:

android - 日本字符在 Android TextView 和按钮中显示奇怪的间距

java - 成功将元素解析到列表后,SAX 解析器 endDocument 中的空列表

android - addView() 不显示 fragment 中的按钮

android - 具有特定格式的深度链接

android - Android Studio 中测试支持库的来源

android - Logcat 是 "spammed",导致 "Too much output to process"

android - 谷歌播放服务 : how to handle peers network disconnection?

android - 如何为 Deep Link Android App 触发调试启动附加调试器?

android - 如何像在 android 4.2 中一样在 android 4.3 中启用跟踪

android - findPreference(java.lang.CharSequence) 已弃用