Android studio无法识别源文件夹

时间:2021-12-02 00:28:21

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

我正在使用标准的Android Studio目录结构,我创建了不同的构建类型:

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

一切都编译好,但AS不识别所有的源文件夹。只有main和debug下的文件夹被标记为source,预览和发布下的文件夹显示为普通文件夹实际上这些文件夹中没有错误检查

Android studio无法识别源文件夹

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

我检查了.iml文件,并没有添加sourceFolder标签。

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

如果我手动编辑项目iml文件添加行:

 <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.

它似乎工作正常。

Android studio无法识别源文件夹

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

...直到我与我的gradle文件同步 - 这会删除上面的行。

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

这是gradle插件中的错误,还是我做错了什么?

4 个解决方案

#1


116  

You have to switch it in the build variants list, then AS will pick up the appropriate source sets. Android studio无法识别源文件夹

您必须在构建变体列表中切换它,然后AS将选择适当的源集。

#2


3  

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

首先,尝试重新导入项目。删除所有构建目录,.iml文件和.idea文件夹。然后导入项目。

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

如果这不起作用,那么你可以尝试这个“强迫它”。查看Bernd Bergler的回复。请注意,这是一个黑客攻击,理想情况下不是必需的

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部分之外的build.gradle文件中即可。来源描述:

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.

Android Studio会自动从gradle构建文件生成.iml项目文件。此任务编辑Android Studio项目文件app.iml并添加测试目录。每当Android Studio重新扫描gradle文件时,更改都会丢失,但在此之后它会运行一个构建并且任务被挂钩,因此这一切都很好。这个版本有一些调整,例如将新任务添加到正常的构建周期中有点不同,并且优雅地处理缺少.iml文件。

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.

这对我来说有一定的作用:IDE现在将其识别为src树,但不希望将其与任何其他src树链接。

#3


2  

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

在我的情况下,只有File - > Invalidate Caches / Restart帮助了我,所以如果上面的解决方案对你不起作用 - 试试这个。

#4


1  

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

将其添加到模块的build.gradle文件中:

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

#1


116  

You have to switch it in the build variants list, then AS will pick up the appropriate source sets. Android studio无法识别源文件夹

您必须在构建变体列表中切换它,然后AS将选择适当的源集。

#2


3  

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

首先,尝试重新导入项目。删除所有构建目录,.iml文件和.idea文件夹。然后导入项目。

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

如果这不起作用,那么你可以尝试这个“强迫它”。查看Bernd Bergler的回复。请注意,这是一个黑客攻击,理想情况下不是必需的

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部分之外的build.gradle文件中即可。来源描述:

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.

Android Studio会自动从gradle构建文件生成.iml项目文件。此任务编辑Android Studio项目文件app.iml并添加测试目录。每当Android Studio重新扫描gradle文件时,更改都会丢失,但在此之后它会运行一个构建并且任务被挂钩,因此这一切都很好。这个版本有一些调整,例如将新任务添加到正常的构建周期中有点不同,并且优雅地处理缺少.iml文件。

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.

这对我来说有一定的作用:IDE现在将其识别为src树,但不希望将其与任何其他src树链接。

#3


2  

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

在我的情况下,只有File - > Invalidate Caches / Restart帮助了我,所以如果上面的解决方案对你不起作用 - 试试这个。

#4


1  

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

将其添加到模块的build.gradle文件中:

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