Gidiyo
Gidiyo

Reputation: 411

Enable Exception C++

I am trying to make APP native code for Android. The Native code is in cplusplus. Whenever I try to make, the following error appears.

H236Plus.cpp:135: error: exception handling disabled, use -fexceptions to enable

How do I use -fexceptions to enable exception handling, and where do i use it?

Upvotes: 26

Views: 22956

Answers (8)

lnstadrum
lnstadrum

Reputation: 576

I solved this problem by adding cFlags "-fexceptions" into ndk section of build.gradle script in app or lib folder, like this:

ndk {
    ...
    cFlags "-fexceptions"
}

Update: with a newer Gradle plugin, it goes into externalNativeBuild/cmake section as follows:

android {
    compileSdkVersion 29

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29

        externalNativeBuild {
            cmake {
                cppFlags.addAll(["-std=c++11", "-fexceptions", ...])
                ...
            }
        }
    }

...

Upvotes: 1

c z
c z

Reputation: 8987

Just a note for anyone who has started with one of the NDK examples.

They tend to set -fno-exceptions in their CMakeLists.txt, you need to remove it:

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -Wall -Werror -fno-exceptions -frtti") 

You might want to check whether you want -Werror while you're at it.

Upvotes: 1

Pravin Divraniya
Pravin Divraniya

Reputation: 4374

Refer this answer by @Tundebabzy if you are using ndk-build build system.

For CMake build system

add the following to your module-level build.gradle file:

android {
  ...
  defaultConfig {
    ...
    externalNativeBuild {

      // For ndk-build, instead use ndkBuild {}
      cmake {
        // Enables exception-handling support.
        cppFlags "-fexceptions"
      }
    }
  }
}
...

For more information See this Link.

Upvotes: 4

Someone Somewhere
Someone Somewhere

Reputation: 23787

with the latest version of Android Studio this is what my build.gradle looks like:

model {
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.2"

        buildTypes {
            release {
                minifyEnabled false
                shrinkResources false
                proguardFiles.add(file("proguard-rules.txt"))
                signingConfig = $("android.signingConfigs.release")
            }
        }

        defaultConfig {
            applicationId "my.android.app"
            minSdkVersion.apiLevel 16
            targetSdkVersion.apiLevel 23
            versionCode 29
            versionName "my.latest.version"
        }

        ndk {
            moduleName "jni-utils"
            ldLibs.add("log")
            cppFlags.add("-std=c++11")
            cppFlags.add("-fexceptions")
            stl "gnustl_static"
        }
    }
    android.signingConfigs {
        create("release") {
            storeFile "C:\\Android\\Git\\MyProject\\keystore\\keystoreCommon"
            storePassword "put you password here"
            keyAlias "put your alias here"
            keyPassword "put your password here"
        }
    }
}

Upvotes: 2

Tundebabzy
Tundebabzy

Reputation: 879

It depends on what runtime you are using. If you are not using system runtime and are building with ndk-build, you add any of these to your Android.mk file:

  • LOCAL_CPP_FEATURES += exceptions (Recommended)
  • LOCAL_CPPFLAGS += -fexceptions

Also, you can add the following line to your Application.mk file:

  • APP_CPPFLAGS += -fexceptions

There's more information in docs/CPLUSPLUS-SUPPORT.html in your NDK folder

Upvotes: 33

ognian
ognian

Reputation: 11541

You need to build with CrystaX's custom NDK. It has full libstdc++, RTTI and exceptions support. It's generally the best tool for Android development I know.

Upvotes: 6

DumbCoder
DumbCoder

Reputation: 5766

In the compiler flags add -fexception in your Makefile.

Upvotes: 5

EricSchaefer
EricSchaefer

Reputation: 26350

-fexception is a compiler switch. How you use it depends on your compiler setup. What compiler are you using? IDE? build tool?

Upvotes: 5

Related Questions