简体   繁体   English

本机代码调试在Android Studio 3中不起作用

[英]Native code debug is not working in Android Studio 3

I've tried all the ways found on StackOverflow and still facing the issue. 我已经尝试了在StackOverflow上找到的所有方法,但仍然面临该问题。

I've created a demo Android project with native support, added a library to it and moved all native code into the library. 我创建了一个具有本地支持的演示Android项目,向其中添加了一个库,并将所有本机代码移至该库中。

Now I'm unable to stop on breakpoints in native code, native debugger became active only after a SEGFAULT crash. 现在,我无法在本机代码中的断点处停止,本机调试器仅在SEGFAULT崩溃后才处于活动状态。

I've added defaultPublishConfig "debug" into mylibrary build.gradle and debuggable true to app build.fradle . 我已经将defaultPublishConfig "debug"添加到库build.gradle并且debuggable true到应用程序build.fradle There was enough for native debugging earlier. 早期的本机调试已足够。 But it is not working since Android Studio upgrade. 但是自从Android Studio升级以来,它不起作用。

Here are the full build.gradle files 这是完整的build.gradle文件

app 应用

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'
}

mylibrary 我的图书馆

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'
}

First, check debug variant is selected for library. 首先,为库选择检查调试变量。

As your build.gradle has a setting, path "src/main/cpp/CMakeLists.txt" , it won't build I think. 由于您的build.gradle具有设置path "src/main/cpp/CMakeLists.txt" ,所以我认为它不会构建。 So set targets , rebuild and check again. 因此,设置targets ,重建并再次检查。

If breakpoints doesn't work after build, old garbage may be left in the build cache and causes problems. 如果在构建后断点不起作用,则旧的垃圾可能会留在构建缓存中并引起问题。 Open project directory in explorer and delete build cache (.externalNativeBuild folder) manually and build the project again. 在资源管理器中打开项目目录,然后手动删除构建缓存(.externalNativeBuild文件夹),然后再次构建项目。 I also delete build folder, as it contains .so files in intermediate directory, but it's optional. 我还删除了build文件夹,因为它在中间目录中包含.so文件,但这是可选的。

Android Studio does not clean libraries in the test device. Android Studio不会清除测试设备中的库。 They are overwritten basically, but clear them manually depending on needs. 它们基本上被覆盖,但是根据需要手动清除它们。 Files are in /data/app/(package name)/lib/(cpu arch.)/. 文件位于/ data / app /(程序包名称)/ lib /(cpu架构)/中。
NB: Synchronize menu of device file explorer does not synchronize properly under lib or (cpu arch.) directory. 注意:设备文件资源管理器的同步菜单在lib或(cpu档案)目录下无法正确同步。 To synchronize, select /data or /data/app and choose Synchronize. 要进行同步,请选择/ data或/ data / app,然后选择同步。

NB.1 If targets is omitted, Android Studio seems to build no targets. NB.1如果省略了targets ,则Android Studio似乎没有建立目标。 The built output is in (project)/app/build/intermediates/cmake/(flavor)/obj/(cpu architecture). 生成的输出位于(project)/ app / build / intermediates / cmake /(flavor)/ obj /(cpu体系结构)中。 If it seems to work without any targets, check files on device. 如果似乎没有任何目标,则检查设备上的文件。 They confuse test results. 他们混淆了测试结果。

NB.2 debuggable true is for release build to enable debugging. NB.2 debuggable true用于发布版本以启用调试。 No need to set it for debug build, as debuggable flag is set as default. 无需将其设置为调试版本,因为可调试标志设置为默认值。

NB.3 Seems version dependent but Gradle in Android Studio does not clean .externalNativeBuild tree properly even if clean or rebuild is called, and confuses native code build configs. NB.3似乎依赖版本,但即使调用clean或rebuild,Android Studio中的Gradle也无法正确清理.externalNativeBuild树,并混淆了本机代码生成配置。 It was around AS3.0, as I remember. 我记得当时是在AS3.0左右。

NB.4 My environment is NB.4我的环境是

  • Android Stuidio 3.2.1 Android Stuidio 3.2.1
  • classpath 'com.android.tools.build:gradle:3.2.1' 类路径'com.android.tools.build:gradle:3.2.1'
  • gradle-4.7-all gradle这个-4.7-所有
  • CMake: default(3.6.4111459) CMake:默认(3.6.4111459)

I know there are newer versions for Android Studio, Gradle and CMake but they are buggy, so I chose current environment. 我知道有适用于Android Studio,Gradle和CMake的较新版本,但它们存在问题,因此我选择了当前环境。 As far as I've experienced, Android Studio 3.3, gradle:3.3.0, gradle-4.10.1-all have severe bug in VCS(git). 据我所知,Android Studio 3.3,gradle:3.3.0,gradle-4.10.1-在VCS(git)中均存在严重的错误。 Wrong file contents are shown in editor and build fails, sometimes. 错误的文件内容显示在编辑器中,有时会生成失败。 Setting CMake version to 3.10.x (3.10.2 for me) also seems buggy. 将CMake版本设置为3.10.x(对我而言是3.10.2)似乎也有问题。

Here's a copy from my project as a sample, partially modified from original one but may work. 这是我项目的一个副本,作为示例,对原始副本进行了部分修改,但可以使用。 I've checked breakpoints in a library work in Android Studio 3.2.1. 我已经在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'
}

Update 更新
targets is mentioned here Guide - Link Gradle to your native library - Specify optional configurations . 此处提到了目标。 指南-将Gradle链接到您的本机库-指定可选配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM