在Android Studio中调试动态加载的本机库? [英] Debug a dynamically loaded native library in Android Studio?

查看:681
本文介绍了在Android Studio中调试动态加载的本机库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的项目:

   - 项目
- app
- src ... / cpp /
- src ... / java /
- 其他模块...

cpp代码使用build.gradle中的cmake构建:

  externalNativeBuild {
cmake {
pathCMakeLists.txt
}
}

  buildTypes {
...
调试{
可调试真
jniDebuggable true
externalNativeBuild {
cmake {
arguments-DCMAKE_BUILD_TYPE = Debug
abiFiltersarmeabi-v7a,armeabi,x86
}




productFlavors {
...
实验{
externalNativeBuild {
cmake {
个目标sqlite_gcd_func
}
}
}

$ / code>

其中CMakeLists.txt为:

  cmake_minimum_required(VERSION 3.6)

add_library(#指定库的名称。
sqlite_gcd_func
#将库设置为共享库。
MODULE
#提供源文件的相对路径。
src / Experimental / cpp / GreatCircleDistance.cpp)
#指定本地头文件的路径。
include_directories(src / Experimental / cpp / include /)

生成的.so库通过SQL加载到SQLite中作为扩展:

 选择load_extension('libsqlite_gcd_func',null)

之后,库中定义的函数可用于SQL查询。



这一切工作。



什么不工作调试;在CPP代码中设置的断点不会触发。



我假设这是因为代码在运行时动态加载;我也尝试使用java直接加载模块:

  java.lang.System.loadLibrary(sqlite_gcd_func); 

但是折点依然不起作用。

<我对lldb的了解很少;我假设我需要告诉它加载的模块(通过lldb中的'图像列表'可见)是一个已知的模块,但不知道如何做到这一点(如果这是问题)。



在告诉gradle / lldb / Android Studio如何调试这段代码的任何帮助将非常感谢!



编辑:



我用相似的设置创建了一个更简单的项目,它是可调试的。在不可调试的版本中,当我进入lldb并运行'图像列表'时,出现的.so如下所示:

  C:\ Users \ ME\.lldb\module_cache\remote-android\.cache\8D1C60AA-E947-56CA-CBA5-0AA7A46B955E-73E37532\libname.so 

(即看起来像是从AVD复制的版本)。



然而,在我可以调试的版本中,它显示在:

  C:\ ... \ project\app\build\intermediates\cmake\debug\obj\x86\libname.so 

即因为某些原因,lldb似乎不使用本地应用程序版本。



>任何想法会导致什么?

解决方案

事实证明,这是由于AS中的一个旧bug。显然在过去,IML文件中的native-android-gradle部分中的SELECTED_BUILD_VARIANT可能会与实际选择的风格不一致。这导致无法加载.so文件。



一旦更正,此值似乎仍然保持最新,下面是讨论的链接(现已解决)的bug:



https ://issuetracker.google.com/issues/37114841



据我所知,项目同步 may 也可以修复这个问题(它不适合我)。


I have a project that looks like this:

- project
    - app
       - src.../cpp/
       - src.../java/
    - other modules...

The cpp code is built using cmake in build.gradle:

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

and

buildTypes {
   ...
    debug {
        debuggable true
        jniDebuggable true
        externalNativeBuild {
            cmake {
                arguments "-DCMAKE_BUILD_TYPE=Debug"
                abiFilters "armeabi-v7a", "armeabi", "x86"
            }
        }
    }
}

productFlavors {
    ...
    experimental {
        externalNativeBuild {
            cmake {
                targets "sqlite_gcd_func"
            }
        }
    }
}

where CMakeLists.txt is:

cmake_minimum_required(VERSION 3.6)

add_library( # Specifies the name of the library.
         sqlite_gcd_func
         # Sets the library as a shared library.
         MODULE
         # Provides a relative path to your source file(s).
         src/Experimental/cpp/GreatCircleDistance.cpp )
# Specifies a path to native header files.
include_directories(src/Experimental/cpp/include/)

The generated .so library is loaded into SQLite as an extension via SQL:

Select load_extension('libsqlite_gcd_func', null)

after which the function defined in the library is available to SQL queries.

This all works.

What does not work s debugging; breakpoints set in the CPP code do not trigger.

I assume this is because the code is dynamically loaded at run time; I have also tried directly loading the module using java via:

java.lang.System.loadLibrary("sqlite_gcd_func");

But break points still do not work.

My knowledge of lldb is minimal; I assume I need to tell it that the loaded module (visible via 'image list' in lldb) is a known module, but have no idea how to do so (if that is even the problem).

Any assistance in telling gradle/lldb/Android Studio how to debug this code would be much appreciated!

EDIT:

I've created a much simpler project with similar settings and it is debuggable. In the non-debugable one, when I go into lldb and run 'image list', the .so in question shows up as in:

C:\Users\ME\.lldb\module_cache\remote-android\.cache\8D1C60AA-E947-56CA-CBA5-0AA7A46B955E-73E37532\libname.so

(ie. what looks like a version copied off the AVD).

Whereas in the one that I can debug, it shows up in:

C:\...\project\app\build\intermediates\cmake\debug\obj\x86\libname.so

ie. the actual library in the build area.

lldb seems not to use the local app version for some reason.

Any idea what would cause that?

解决方案

It turns out this is due to an old bug in AS. Apparently in the past the "SELECTED_BUILD_VARIANT" in the "native-android-gradle" section in the IML file could become out of step with the actual selected flavour. This resulted in the failure to load the .so files.

Once corrected, this value seems to remain up to date now, and the following is a link to the discussion of the (now-fixed) bug:

https://issuetracker.google.com/issues/37114841

From what I can tell, a project sync may also fix the problem (it didn't for me).

这篇关于在Android Studio中调试动态加载的本机库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆