androidandroid-ndkcmakendk-build

Android Studio 2.2.3 cannot find <vector>


Configuration

Operating system: Ubuntu 16.04

Android Studio Version: 2.2.3

Key code

~/project/build.gradle:

buildscript {
      repositories {
          jcenter()
      }
      dependencies {
          classpath 'com.android.tools.build:gradle:2.2.3'
      }
  }
  ...

~/project/library/build.gradle:

apply plugin: 'com.android.library'

android {
    ...

    defaultConfig {
        ...

        externalNativeBuild {
            cmake {
                arguments '-DANDROID_TOOLCHAIN=clang',
                        '-DANDROID_STL=gnustl_static'
                     // I also tried '-DANDROID_STL=gnustl_shared', result is same
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
        }
    }

    ...
}

~/project/app/build.gradle:

apply plugin: 'com.android.application'

android {
    ...
}

dependencies {
    ...
    compile project(':library')
    ...
}

~/project/settings.gradle

include ':app', ':library'

~/project/library/src/main/cpp/test.h:

#include <vector>   // vector is red, says "cannot find vector"
#include <stdint.h>

class test {

private:
    std::vector<int32_t> array;  // std is red, says "can't resolve container std"
};

Description of problem

In file ~/project/library/src/main/cpp/test.h, vector in #include <vector> is red. When I move the mouse over it, it will popup a window which says Cannot find 'vector'. std in std::vector<int32_t> is also red. I move the mouse over it and it says Can't resolve container 'std'.

This project has no compile error. It can be compiled successfully and installed on my phone. The problem is it has a syntax error. I don't have code completion on some variables, like array in test.h.

Some information I find

I imported project teapots which contains #include <vector>. Android Studio did resolve vector and std symbol for this project.

My question

What did I do wrong? Why does project teapots not have this problem? What's the key difference between my project and teapots that makes the latter one able to resolve vector and std but mine not?


Update

I created a new project in Android Studio. In step 1 of the new project wizard, I checked include C++ Support. In the last step, I kept the default Toolchain Default for C++ Standard option. After I clicked the finish button, Android Studio automatically created the file native-lib.cpp for me. I added code #include <vector> in line 3 in this file. This time Android Studio resolved symbol vector correctly. Then I created a new module library mylibrary for this project, and in there I added a cpp folder and test.h and it also resolved the symbol vector(mylibrary module's build.gradle had been correctly added CMake stuff by me). But my problem is I need to add vector to my existing project, and my existing project can't resolve the symbol vector.


Solution

  • I figured it out. I didn't include the C++ file test.h in CMakeLists.txt. Once I included it into CMakeLists.txt add_library parentheses and synced project, the red syntax error is gone.