1

I am building my app in debug mode, and I notice some errors saying "parent failed to evaluate: no location, value may have been optimized out". Therefore, I try to add "-O0" in my module build.gradle like this:

externalNativeBuild {
    cmake {
        cppFlags "-O0 -frtti -fexceptions -std=c++11 -DANDROID_ARM_NEON=TRUE -mfloat-abi=softfp "
        abiFilters "armeabi-v7a"
    }
}

But still, the same error shows up after adding "-O0". May I ask how to disable compiler optimization properly? My android Studio version is 2.3.3, my sdk tool version is 26.0.2 and my ndk version is 15.1.4

Michael
  • 57,169
  • 9
  • 80
  • 125
Edmosphere
  • 11
  • 1
  • 3
  • 1
    You may find it useful to check how the actual compilation command looks like, see https://stackoverflow.com/a/43442227/192373. – Alex Cohn Aug 07 '17 at 08:52

2 Answers2

3

If you want to disable optimization for release build, you can force Debug for C/C++ only:

android {
  defaultConfig {
    externalNativeBuild {
      cmake {
        arguments '-DCMAKE_BUILD_TYPE:STRING=Debug'
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
3

You can override the build flags by adding the following to your CMakeLists.txt:

# Debug flags
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0")

# Release flags
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Ofast")

To verify this has worked check build output in:

app/.externalNativeBuild/cmake/<buildconfig>/<architecture>/build.ninja

Look for a line starting with FLAGS. This doesn't actually replace the existing compiler flags it just appends your flags and these take precedence.

The default flags are inherited from $ANDROID_NDK/build/cmake/android.toolchain.cmake so you could edit that file directly, however, if you update your NDK these changes will be overwritten.

donturner
  • 17,867
  • 8
  • 59
  • 81