Jonathan Lindgren
Jonathan Lindgren

Reputation: 1262

Android Studio: This file not part of the project, but the project builds successfully

I have a strange problem that suddenly appeared in android studio. I created a new cpp file, and included it in Android.mk. Then I synced the project. However, android studio still complains that the file is not part of the project and that I need to sync, BUT the whole project builds successfully.

Likewise, if I remove one of the other older files from Android.mk that it did not complain about, and resyncs and then tries to build the project, as expected the build fails, but android studio does NOT complain that that file is not part of the project anymore.

So somehow, suddenly the android studio editor is not able to correctly identify which files have been synced and are part of the project, but during compilation everything works as expected.

Does anyone know how to fix this annoying problem? I have tried clean project, invalidate caches/restart as well as updating android studio without luck (AS version 3.4).

Upvotes: 12

Views: 10919

Answers (11)

allan
allan

Reputation: 51

  1. close androidStudo and rm .idea/, restart;
  2. invalidate Caches;
  3. annotate the reference native lib in settings.gradle & app's build.gradle, sync, then reopen it and sync again.

Upvotes: 0

Max Klint
Max Klint

Reputation: 886

I solved a similar issue by deleting the .idea folder and restarting Android Studio.

Upvotes: 0

Haomin
Haomin

Reputation: 1559

Invalidate cache and restart and you should be good. However, make sure you checked the Clear file system cache and local history checkbox.

Upvotes: 0

Alexp
Alexp

Reputation: 529

For those struggling with this for me I made a small change to CMakeLists.txt (I altered the version required). This forced the CMake to regenerate, and all missing files were added. This is quicker than the other options listed here.

This applied to NDK 21, so YMMV!

Upvotes: 0

I fixed with update 'com.android.tools.build:gradle' in the artic fox version 2020.3.1

Upvotes: 0

Sdghasemi
Sdghasemi

Reputation: 5608

I used Build > Refresh Linked C++ Projects menu and it worked.

Menu Preview in Android Studio

Upvotes: 18

gregko
gregko

Reputation: 5822

Had the same problem with Android Studio 4.0.1 and the latest gradle at this time (6.1.1?). The problem went away after I exited Android Studio and deleted .gradle and .idea folders in the project main directory, plus deleted .cxx and build directories in the affected module directory. I'm not sure which really helped, but most probably deleting .grade and/or .idea

Upvotes: 1

Bulwinkel
Bulwinkel

Reputation: 2331

Problem Environment

  • Android Studio 3.5 RC 2
  • gradle-4.10-all
  • com.android.tools.build:gradle:3.2.1

Solution

Update to:
- gradle-5.5.1-all
- com.android.tools.build:gradle:3.4.2

Steps

From the project root run (note this has to be done first):

./gradlew wrapper --gradle-version 5.5.1 --distribution-type all

In root build.gradle file:

buildscript {
    //...
    dependencies {
        classpath("com.android.tools.build:gradle:3.4.2")
        //...
    }
}

Upvotes: 4

fredzzt
fredzzt

Reputation: 133

fix this by update my 'com.android.tools.build:gradle'

Upvotes: 1

alijandro
alijandro

Reputation: 12167

I have similar problem, it could be the compatibility issue of gradle version and gradle plugin version, because my solution is replacing the old configuration

// build.gradle
classpath 'com.android.tools.build:gradle:3.2.1'
...
// gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

with the following new one by changing gradle version from 4.6 to 4.10.1.

// build.gradle
classpath 'com.android.tools.build:gradle:3.2.1'
...
// gradle-wrapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip

After that, Android studio can index and track my new cpp files in project.

Upvotes: 2

user7340499
user7340499

Reputation:

I had a similar problem. Like yourself, I have tried everything.

  • Invalidate and Restart: Doesn't work
  • Manual deleting folders: .gradle .idea .ndkbuild etc. doesn't work
  • Clean, Rebuild, Link C++ Files: Doesn't work

One thing that kind of helped me was: I changed the NDK version. I compiled, then got a compilation error (didn't matter because it was the wrong version of NDK anyway), then I reverted to the original NDK. This appeared to solve the problem, however, it got back again.

My solution was to reset Android Studio to factory settings. If you are on Linux, you can start by deleting these folders:

rm -rf ~/.android

rm -rf ~/.AndroidStudio3.4

Then you download and run your Android Studio and not import anything from anywhere.

I suspect the problem was caused by one of the plugins I've installed.

It may be a good idea to backup those two folders from time to time and reload them from there if necessary.

EDIT: It seems that my problem persisted after the above solution after adding more .cpp files. After seeing that, I searched where that popup came from. It follows that "This file is not part of project..." popup is pushed from ndk-build. (Class name: NewCppSourceNotificationProvider - StaleCppProjectNotificationPanel). What I tried, and what worked so far; I used Android Studio 3.5 Canary13 with NDK version r19c (Stable version). I hope this helps you.

Upvotes: 4

Related Questions