Android Studio 2.2.3 can not find & lt; vector & gt;

advertisements

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. Move mouse over it will popup a window which says Cannot find 'vector'. std in std::vector<int32_t> is also red. Move mouse over it says Can't resolve container 'std'.

This project has no compile error. It can be compiled successfully and installed on my phone. Problem is it has 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 I did wrong? Why project teapots don't have this problem? What's the key difference between my project and teapots that makes the latter one can resolve vector and std but mine not?


Update

I created a new project in android studio. In step 1 of new project wizard, I checked include C++ Support. In last step, I kept default Toolchain Default for C++ Standard option. After I clicked finish button, android studio automatically created 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, in there I added cpp folder and test.h and it also resolved symbol vector(mylibrary module's build.gradle had been correctly added cmake stuff by me). But my problem is I need add vector to my existed project, and my existed project can't resolve symbol vector.


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