利用Android studio 調試任意已有c++工程

記錄: Android studio 3.1.2 

NDK r16

SDK 28 Level

 

首先如果你的原有工程是CMake 編譯的 那麼沒問題,直接上 : 工程右鍵菜單: Linker C++ project  選擇原有CMakeList 文件. 

然後工程配置需要修改,默認AS會編譯所有交叉編譯的平臺

 

這裏太慢配置一個常用的平臺:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.example.chijing.myapplication"
        minSdkVersion 21
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
               //'-LIBS="-lsupc++ -lstdc++'
                cppFlags "-frtti -fexceptions -std=c++14"
                arguments
                        '-DANDROID_STL=c++_shared ARCH=arm  -ABI_TYPE=armeabi-v7a'

                ndk {
                    //moduleName "jary"         //生成的so名字
                    abiFilters "armeabi-v7a"  //輸出指定三種abi體系結構下的so庫。
                    stl "c++_shared"    //打開.c 的 debug (此句是打開的debug的關鍵)
                }
            }
        }

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

                }
    }
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
    externalNativeBuild {
        cmake {
            path 'D:/source/kernel/CMakeLists.txt'
            //LIBS="-lsupc++ -lstdc++"

            //ANDROID_CPP_FEATURES="rtti;exceptions"
            //ANDROID_STL="c++_shared"



        }
    }
}
dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.+'

    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

 

配置完成後即可build完成.然後即可調試, 需要lldb插件

另外一個很重要的地方,要添加符號文件給lldb,不然是無法進入C++代碼的.

如下

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章