简体   繁体   中英

Native code debug is not working in Android Studio 3

I've tried all the ways found on StackOverflow and still facing the issue.

I've created a demo Android project with native support, added a library to it and moved all native code into the library.

Now I'm unable to stop on breakpoints in native code, native debugger became active only after a SEGFAULT crash.

I've added defaultPublishConfig "debug" into mylibrary build.gradle and debuggable true to app build.fradle . There was enough for native debugging earlier. But it is not working since Android Studio upgrade.

Here are the full build.gradle files

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. So set targets , rebuild and check again.

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. I also delete build folder, as it contains .so files in intermediate directory, but it's optional.

Android Studio does not clean libraries in the test device. They are overwritten basically, but clear them manually depending on needs. Files are in /data/app/(package name)/lib/(cpu arch.)/.
NB: Synchronize menu of device file explorer does not synchronize properly under lib or (cpu arch.) directory. To synchronize, select /data or /data/app and choose Synchronize.

NB.1 If targets is omitted, Android Studio seems to build no targets. The built output is in (project)/app/build/intermediates/cmake/(flavor)/obj/(cpu architecture). 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. 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. It was around AS3.0, as I remember.

NB.4 My environment is

  • Android Stuidio 3.2.1
  • classpath 'com.android.tools.build:gradle:3.2.1'
  • gradle-4.7-all
  • CMake: default(3.6.4111459)

I know there are newer versions for Android Studio, Gradle and CMake but they are buggy, so I chose current environment. 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). 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.

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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