Include .java files as java source files in Androi

2020-06-09 11:16发布

问题:

Today after update android studio my source folder in project marks as below("J" inside a red circle mark). How can I fix it? I tried clear caches, re-import project, use gradlew clean build, but it doesn't help.

build.gradle

apply plugin: 'android'
apply plugin: 'android-apt'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    lintOptions {
        abortOnError false
    }
}

def AAVersion = '3.0.1'
dependencies {
   ......
}

apt {
    arguments {
     ....
    }
}

回答1:

I found some magical solution while playing with this

There are two possible solution you can try to make it correct (tricky one)

1.Replace this in your build.gradle file

java.srcDirs = ['src']   to   java.srcDirs = ['src/com']

2.Create a subdirectory inside src name it whatever you want, I prefer name it java like

src > java

move all your java package in to it and change the

java.srcDirs = ['src']   to   java.srcDirs = ['src/java']

These are the tricky solutions. It can be considered as a bug in Android Studio that it is not considering the java source set if it is the root of src directory but compiles fine.

After doing all the necessary changes sync your project with gradle and your yellow directory will be magically turned into blue.

Note : Even you have them in root there will be no effect on compilation as per my experience. Only Studio will not mark them as java sourcesets in IDE.



回答2:

  1. Open Project Structure in Android Studio(Cmd+;)
  2. Under modules Header over the left of the window, look onto right side of it , You will get to see three tabs one of which is Sources ,Select it.
  3. There, you will find the structure of your project which you have imported or might have created by self.
  4. Go for the src folder of it and wait for the sources link above that dialog with many others to get avail to apply onto it,grab it.
  5. Apply and OK
  6. Problem Solved.


回答3:

I solved it just closing the project from android studio and remove the project from recent project. Re-import the project, it worked. If any one facing for android project he/she can try this. Thanks



回答4:

This is bug https://code.google.com/p/android/issues/detail?id=66758 and should be fixed in Android Studio 0.5.1, which is out now.



回答5:

  1. Open build.gradle file (one in module, not in project)
  2. As you open it you will see "This folder does not belong to a Gradle project. Make sure it is registered in settings.gradle." message, and "Add now..." link in upper-right corner
  3. Click "Add now" and Gradle will do its job.


回答6:

If you are facing this issue after maybe switching branch or after taking pull. Then you can fix it by going in to File>Invalidate Caches/restart.