programing

Android 스튜디오가 소스 폴더를 인식하지 못함

nasanasas 2020. 11. 14. 10:18
반응형

Android 스튜디오가 소스 폴더를 인식하지 못함


표준 Android Studio 디렉터리 구조를 사용하고 있으며 다른 빌드 유형을 만들었습니다.

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

모든 것이 잘 컴파일되지만 AS는 모든 소스 폴더를 인식하지 못합니다. 하에서 만 폴더 maindebug원으로 표시된 아래 폴더 previewrelease사실상 정규 폴더로서 표시되는 폴더는 이러한 에러 검사는 없다

여기에 이미지 설명 입력

.iml 파일을 확인했는데 sourceFolder 태그가 추가되지 않았습니다.

프로젝트 iml 파일을 수동으로 편집하면 다음 줄을 추가합니다.

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

잘 작동하는 것 같습니다.

여기에 이미지 설명 입력

... 내 gradle 파일과 동기화 할 때까지-위의 줄을 제거합니다.

이것은 gradle 플러그인의 버그입니까, 아니면 내가 잘못하고 있습니까?


빌드 변형 목록에서 전환해야합니다. 그러면 AS가 적절한 소스 세트를 선택합니다. 빌드 변형


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.


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


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

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

참고 URL : https://stackoverflow.com/questions/22068731/android-studio-doesnt-recognise-source-folders

반응형