20

I am trying to use

std::function

But the compiler throws an error

Error:(50, 10) error: no type named 'function' in namespace 'std'

I have tried to modify the build.gradle file

externalNativeBuild {
    ndkBuild {
        path "src/main/jni/Android.mk"
    }
    cmake {
        cppFlags "-std=c++11"
        arguments "-DANDROID_STL=gnustl_static"
        path 'src/main/jni/CMakeLists.txt'
    }
}

But it doesn't accept arguments other than path and throws the following errors

Error:(28, 0) Could not find method arguments() for arguments [-DANDROID_STL=gnustl_static] on object of type com.android.build.gradle.internal.dsl.CmakeOptions.

Please help me to be able to use

std::function

UPDATE

Thanks to @Alex Cohn I was able to configure flags and arguments, and now my file looks like that

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '26.0.2'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11"
                arguments "-DANDROID_STL=gnustl_static"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        /*   ndkBuild {
               path "src/main/jni/Android.mk"
           }*/
        cmake {
            // cppFlags "-std=c++11"
            //arguments "-DANDROID_STL=gnustl_static"
            path 'src/main/jni/CMakeLists.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-annotations:25.0.1'
}
  • Do you have `#include ` into your cpp source? – vre Nov 18 '17 at 19:37
  • You might further add the `-DCMAKE_CXX_STANDARD=11` to your arguments settings. – vre Nov 18 '17 at 19:46
  • @vre, yeah I have tried, adding arguments doesn't work, it won't accept any arguments at all, can I set this arguments in CMAke file itself ? –  Nov 18 '17 at 19:49
  • Btw. there is a superfluous comma at the end of the arguments line. I guess that prevents those other options to be recognized. You use the comma only for separating different arguments. – vre Nov 18 '17 at 19:50
  • @vre, you rigth however this is just a typo, I have accidently typed it while inserting the code –  Nov 18 '17 at 19:55

1 Answers1

48

There are two entirely different blocks in build.gradle that are named externalNativeBuild (thanks, Google).

One is under android top-level block, and it specifies the path to native build script, cmake or ndkBuild (but not both).

The other can be used to specify, from gradle, some parameters for the native build. This block is usually a child of the defaultConfig block, but can also appear for flavors (see an example).

These externalNativeBuild blocks can also have cmake or ndkBuild children, but only the child that matches the path setting (see above) is relevant, the other is silently ignored.

The bottom line, split your block in two, and put each one in correct place in build.gradle hierarchy.

leonheess
  • 16,068
  • 14
  • 77
  • 112
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • 3
    Very valuable information !! Thank you I will try to solve the problem now, at least I am able to set flags now. –  Nov 18 '17 at 20:40
  • 7
    I don't know what I would have done without your answer !! It works !! NOW IT WORKS !! I wonder where I can find the information provided by you ? I have read all official docs several times ! thanks, Google –  Nov 18 '17 at 20:42
  • *sad android noises* – CCJ May 28 '20 at 19:46