ndk
ndk copied to clipboard
Debug flag is enabled for release build type.
Description
The toolchain file provided for CMake (found in build/cmake/android.toolchain.cmake) enables the debug flags -g in Release mode.
Excerpt of the toolchain file:
# Generic flags.
list(APPEND ANDROID_COMPILER_FLAGS
-g
...)
# ...
# Debug and release flags.
list(APPEND ANDROID_COMPILER_FLAGS_DEBUG
-O0)
It seems that the -g should be set for ANDROID_COMPILER_FLAGS_DEBUG, not ANDROID_COMPILER_FLAGS.
A workaround is to set CMAKE_CXX_FLAGS_RELEASE to -g0 when configuring the build directory.
Environment Details
Not all of these will be relevant to every bug, but please provide as much information as you can.
- NDK Version: Pkg.Revision = 13.0.3315539
- Build sytem: CMake
- Host OS: Linux Debian Jessie
- Compiler: Clang
- ABI: arm64-v8a
I'm guessing this was done for the same reason we do this in ndk-build: we always build a debugable binary but strip the debug info for the APK. I've sent an email to the Studio folks to find out if this is something that we can just fix, or if that's going to break Studio's debugging features (I don't know if they actually use Debug/Release modes).
Yeah, sounds like Studio is expecting this. If you're using cmake via gradle, gradle will strip the debug info when you package the app. If you're not using gradle, you'll need to add that logic to your packaging tools.
Closed for now, but if there's enough demand for this (I assume the case here is people using cmake and custom packaging tools rather than gradle), it should probably be re-opened and we can look in to getting Studio to do something different.
Thanks for the reply! I understand that Studio is expecting compilation with debug flags even in release mode, but this is counter-intuitive in general, and goes against the default behavior one expects when using CMake in particular. This is actually why I filed this bug.
By default, CMake provides four build types: Debug, MinSizeRel, Release, and RelWithDebInfo. The behavior you describe (a release crafted with debug information) pertains with the RelWithDebInfo build type. I understand the specific needs for Studio, and I definitely understand the need/desires to change the default flags set by CMake, but I believe that the build flags set by the toolchain file should be aligned with the meaning of the build type.
If you're not using gradle, you'll need to add that logic to your packaging tools.
The Gradle behavior (stripping the debug symbols afterward) is a specific case, as is how Studio uses CMake. I fail to understand why I would need to do extra work to address a behavior that should not even happen when compiling a release.
I've followed up on the email I sent earlier. All makes sense to me, but unfortunately it isn't my decision.
Alright, looks like Studio is going to start using RelWithDebInfo in a future release (I think it's too late for 2.3 at this point). They've opened a bug for their side of things: https://code.google.com/p/android/issues/detail?id=229391
Reopening this for tracking the toolchain file fix. I think we can't actually change anything until their fix lands or we'll end up breaking their debugging, so this is going in the unplanned bucket for now.
Hi, thanks a lot for the feedback. Nice to know it will be considered.
Perfect.
On our project, we recently moved to native build setup via gradle using externalnativebuild and cmake and noticed bloated native library sizes in our aar's. But the final apk had stripped out minified native library.
In full agreement with the description of the issue here, and appreciate the android studio team considering the request. 👍
I am amazed that this is still an open issue 3 years later. Can we get this fixed please, it doesn't sound very difficult if Android Studio is now using RelWithDebInfo... although reading the linked issue it appears that it is not yet, sorry.
no, the problem is that Studio currently requires this. that's bug https://code.google.com/p/android/issues/detail?id=229391 and until/unless that's fixed, there's nothing we can do in the NDK.
This is not a bug IMHO, and working as intended, so should be closed. Release version has nothing to do whatever debug symbols are present or not. If you really want to change this behavior just set CMAKE_CXX_FLAGS_RELEASE manually.
Turn off some optimizations:
- Release:
-O3 -DNDEBUG - Debug:
-O0 -g - RelWithDebInfo:
-O2 -g -DNDEBUG - MinSizeRel:
-Os -DNDEBUG
Breaking expected behavior, we think it's Release, but it's not:
if (CMAKE_BUILD_TYPE STREQUAL "Release")
set(C_FLAGS "${C_FLAGS} -Os -fvisibility=hidden -fvisibility-inlines-hidden")
set(LINKER_FLAGS "${LINKER_FLAGS} -Wl,-exclude-libs,ALL -Wl,--gc-sections -Wl,--strip-all")
else ()
set(C_FLAGS "${C_FLAGS} -O0")
endif ()
Looks like https://issuetracker.google.com/37128614 was fixed so now we need to wait for that version of AGP to release and decide on a compatibility window (whatever NDK we change this in will be incompatible with all current AGP versions).
I expect when AGP does a release build, cmake does a release build, when AGP does a debug build, cmake does a debug build and the so files in the apk include debug symbols to get a clear crash report at runtime.
I think this should be the default behavior, it is intuitive.
Is there any way to truly build for RELEASE in CMAKE_BUILD_TYPE when using Android Studio?
Not the topic of the thread. Assuming you've already tried passing -DCMAKE_RELEASE_TYPE=Release and it didn't work, you'll have to ask Android Studio (http://b.android.com). Generally speaking we know less about Android Studio than all of you do.