【问题标题】:JNI shared library seem to contain symbols in release buildJNI 共享库似乎在发布版本中包含符号
【发布时间】:2015-08-18 20:53:53
【问题描述】:

我是安卓原生开发的新手。目前正在将一个库移植到安卓平台并与应用程序集成。

我通过ndk-build 命令行工具构建库。在发布配置中我指定了NDK_DEBUG=0 选项,实际上我可以在源C/C++ 文件中看到它们确实是在发布配置中编译的(定义了NDEBUG)。

但是,生成的 .so 文件似乎包含调试符号。使用编辑器打开文件,我可以看到库未导出的所有内部符号(变量和函数名称),因此它们显然不应该出现在发布版本中。我的意思是,这个库文件 .so 将包含在应用程序的 apk 文件中并分发给客户端,世界上没有任何理由让用户得到这个带有符号的文件。

那么,我该如何摆脱它们呢?我应该在ndk-build 命令行或Android.mk 文件中指定一个选项吗?

【问题讨论】:

    标签: android java-native-interface native


    【解决方案1】:

    您应该在Application.mk 文件中使用APP_OPTIM := release 以及在AndroidManifest.xml 中使用android:debuggable="false"

    APP_OPTIM
        This optional variable can be defined to either 'release' or
        'debug'. This is used to alter the optimization level when
        building your application's modules.
    
        A 'release' mode is the default, and will generate highly
        optimized binaries. The 'debug' mode will generate un-optimized
        binaries which are much easier to debug.
    
        Note that if your application is debuggable (i.e. if your manifest
        sets the android:debuggable attribute to "true" in its <application>
        tag), the default will be 'debug' instead of 'release'. This can
        be overridden by setting APP_OPTIM to 'release'.
    Note that it is possible to debug both 'release' and 'debug'
    binaries, but the 'release' builds tend to provide less information
    during debugging sessions: some variables are optimized out and
    can't be inspected, code re-ordering can make stepping through
    the code difficult, stack traces may not be reliable, etc...
    

    顺便说一句,如果你仔细看看 ndk-build 实际在做什么,你应该会看到一个 strip 命令,我认为它负责删除这些符号。

    【讨论】:

    • 嗯,谢谢你的回答。我已经弄清楚了问题所在: (1) - 默认情况下,所有函数和全局变量都被 GCC 视为“可见”(即可导出)。为了改变这一点,我添加了一个选项-fvisibility=hidden。 (2) 当我尝试使用-flto 选项构建代码时,我最终破坏了优化。为什么它会阻止优化而不是改进它们 - 长篇大论:)
    猜你喜欢
    • 2018-06-14
    • 2014-11-03
    • 1970-01-01
    • 2016-09-26
    • 2020-04-13
    • 2021-08-05
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    相关资源
    最近更新 更多