Android studio doesn't recognise source folders

AndroidGradleAndroid Studio

Android Problem Overview


I'm using a standard Android Studio directory structure and I created different build types:

buildTypes {
    debug {
        runProguard false
        packageNameSuffix ".debug"
        signingConfig signingConfigs.debug
    }
    preview.initWith(buildTypes.debug)
    preview {
        packageNameSuffix ".preview"
    }
    release {
        runProguard false
        signingConfig signingConfigs.release
    }
}

everything compiles fine, but AS doesnt recognize all of the source folders. Only folders under main and debug are marked as source, folders under preview and release are displayed as normal folders In effect there is no error checking in those folders

enter image description here

I checked the .iml file and sourceFolder tags were not added.

If I edit the project iml file manually adding the lines:

 <sourceFolder url="file://$MODULE_DIR$/src/preview/java" isTestSource="false" />
 <sourceFolder url="file://$MODULE_DIR$/src/preview/res" type="java-resource" />

It seems to work fine.

enter image description here

...until I sync with my gradle file - which removes the above lines.

Is this a bug in gradle plugin, or am I doing something wrong?

Android Solutions


Solution 1 - Android

You have to switch it in the build variants list, then AS will pick up the appropriate source sets. build variants

Solution 2 - Android

First, try re-importing the project. Delete all of your build directories, .iml files and the .idea folder. Then import the project.

If that doesn't work then you can try this to "force it". Checkout this response from Bernd Bergler. Note that this is a hack and ideally isn't necessary

Here's a slightly modified version of his code.

task addPreview {
    def src = ['src/preview/java']
    def file = file("app.iml")

    doLast {
        try {
            def parsedXml = (new XmlParser()).parse(file)
            def node = parsedXml.component[1].content[0]
            src.each {
                def path = 'file://$MODULE_DIR$/' + "${it}"
                def set = node.find { it.@url == path }
                if (set == null) {
                    new Node(node, 'sourceFolder', ['url': 'file://$MODULE_DIR$/' + "${it}", 'isTestSource': "false"])
                    def writer = new StringWriter()
                    new XmlNodePrinter(new PrintWriter(writer)).print(parsedXml)
                    file.text = writer.toString()
                }
            }
        } catch (FileNotFoundException e) {
            // nop, iml not found
        }
    }
}

// always do the addPreview on prebuild
gradle.projectsEvaluated {
    preBuild.dependsOn(addPreview)
}

Simply drop that in your build.gradle file outside of the android section. Description from this source:

> Android Studio automatically generates .iml project files from gradle > build files. This task edits the Android Studio project file app.iml > and adds the test directory. The changes are lost whenever Android > Studio rescans the gradle files, but right after that it runs a build > and the task is hooked into that, so it’s all good. This version has a > couple of tweaks, such as adding the new task into the normal build > cycle a bit differently, and gracefully handling the absence of the > .iml file.

This has worked to an extent for me: The IDE recognizes it as a src tree now but doesn't want to link it with any other src trees.

Solution 3 - Android

In my case only File -> Invalidate Caches / Restart have helped me, so if solutions above does not work for you - try this.

Solution 4 - Android

Add this to your module's build.gradle file:

sourceSets {
    main.java.srcDirs += 'src/preview/java'
    main.java.srcDirs += 'src/release/java'
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionimbrykView Question on Stackoverflow
Solution 1 - AndroidLadios JonquilView Answer on Stackoverflow
Solution 2 - AndroidSababadoView Answer on Stackoverflow
Solution 3 - AndroidAnton RyabyhView Answer on Stackoverflow
Solution 4 - AndroidMakotosanView Answer on Stackoverflow