It is impossible to add support for C ++ 11th version in Android studio 2.0. There is a need to use the thread and mutex libraries. I can not create an object of type thread constantly gets an error:

Error:(15, 5) error: 'thread' was not declared in this scope 

build.grade app file

 apply plugin: 'com.android.model.application' model { android { compileSdkVersion = 23 buildToolsVersion = "23.0.2" defaultConfig.with { applicationId = "com.example.a1.enetndktest" minSdkVersion.apiLevel = 11 targetSdkVersion.apiLevel = 23 versionCode = 1 versionName = "1.0" } } android.ndk { moduleName = "EnetTest" stl = "gnustl_shared" cppFlags.add("-std=c++11") cppFlags.add("-Wall") cppFlags.add("-g") cppFlags.add("-libstdc++") cppFlags.add("-lsupc++") cppFlags.add("-pthread") cppFlags.add("-frtti") cppFlags.add("-fexceptions") cppFlags.add("-DANDROID") ldLibs.addAll(["android", "dl", "atomic", "log", "EGL", "GLESv2", "z"]) } android.buildTypes { release { minifyEnabled = false proguardFiles.add(file("proguard-rules.txt")) } } android.productFlavors { create("arm") { ndk.with{ abiFilters.add("armeabi") } } create("armv7") { ndk.with { abiFilters.add("armeabi-v7a") } } } // Our workaround compileOptions.with { sourceCompatibility = JavaVersion.VERSION_1_7 targetCompatibility = JavaVersion.VERSION_1_7 } } dependencies { compile fileTree(dir: "libs", include: [$/*.jar/$]) compile "com.android.support:appcompat-v7:23.1.1" } 

build.grade project file

 buildscript { repositories { jcenter() } dependencies { classpath "com.android.tools.build:gradle-experimental:0.4.0" // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } 

C ++ code file

  #include <jni.h> #include "TestThreadClass.h" JNIEXPORT jint JNICALL Java_clib_ClibInt_enetinit(JNIEnv *env, jobject obj) { TestThreadClass t; t.threadFunction(); return 10; } 

Header file TestThreadClass.h

  #ifndef ENETNDKTEST2_TESTTHREADCLASS_H #define ENETNDKTEST2_TESTTHREADCLASS_H #include <thread> using namespace std; class TestThreadClass { public: void threadFunction(); }; #endif //ENETNDKTEST2_TESTTHREADCLASS_H 

TestThreadClass.cpp file

 #include "TestThreadClass.h" void threadFunction1() { int i = 0; while (i < 100000) { i++; } } void TestThreadClass::threadFunction(){ thread t(threadFunction1); t.join(); } 

Calling C ++ functions from java

 package clib; public class ClibInt { static { System.loadLibrary("EnetTest"); } public native int enetinit(); } 
  • And where does the information come from, that there should be exactly thread ** .h **? - D-side
  • In the standard C ++ 11 there is such a library which header file thread.h)) if you include all libraries without h there will be an error - Misha_Pafos
  • In the standard library C ++ 11 there is no such header file. But thread is. . - D-side
  • check #include <thread> nothing has changed)) - Misha_Pafos
  • This is normal. So, an invalid header file is just one of many problems you have. - D-side

1 answer 1

Replace compiler with clang with c ++ _ static runtime:

 toolchain = "clang" stl = 'c++_static' 

Or, download the old version of NDK (version 11c) from the main site: NDK Downloads

In NDK version 12, coming along with android studio, there are some problems with std :: thread and gnustl

Here is an example of a gradle project file with std :: thread: github.com/googlesamples/android-ndk/choreographer-30fps/app/build.gradle

  • Error: Unable to load class 'org.gradle.nativeplatform.internal.DefaultBuildType_Decorated'. This is a nightmare ((everything is much simpler in the eclipse - Misha_Pafos
  • Created a new project. I downloaded NDK 11c and made a gradle-file as in the example and it worked. Thank you very much)) - Misha_Pafos