Android Studio 无法识别源文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22068731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:29:39  来源:igfitidea点击:

Android studio doesn't recognise source folders

androidgradleandroid-studio

提问by imbryk

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 mainand debugare marked as source, folders under previewand releaseare displayed as normal folders In effect there is no error checking in those folders

一切都编译正常,但 AS 无法识别所有源文件夹。只有文件夹下maindebug标记为源,文件夹下previewrelease显示为普通文件夹 实际上,这些文件夹中没有错误检查

enter image description here

enter image description here

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.

它似乎工作正常。

enter image description here

enter image description here

...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 插件中的错误,还是我做错了什么?

回答by Ladios Jonquil

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

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

回答by Sababado

First, try re-importing the project. Delete all of your build directories, .imlfiles and the .ideafolder. 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 的回复。请注意,这是一个 hack,理想情况下没有必要

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.gradlefile outside of the androidsection. Description from this source:

只需将其放在build.gradleandroid部分之外的文件中即可。 来自这个来源的描述

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 树链接。

回答by Anton Ryabyh

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

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

回答by Makotosan

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

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

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