How to get Android Studio main SourceSet with Gradle API?

会有一股神秘感。 提交于 2019-12-05 16:46:16

First,the code doesn't work because it's only for the java project with java gradle plugin. So I googled and found something work around. Here is my code.

project.afterEvaluate {
    ...
    def sets;
    if (Utils.is140orAbove()) {
        sets = project.android.sourceSets;
    } else {
        sets = project.android.sourceSetsContainer;
    }
    sets.all { AndroidSourceSet sourceSet ->
        if(sourceSet.name.startsWith("main"))
            for(File file:sourceSet.java.getSrcDirs())
                if(file.exists()) {
                    mainSource = file;
                    break;
                }
    }
    ...
}

My code is not beautiful.Waiting for better answer.

android.sourceSets.main.java.getSrcDirs()[0].path

This works for me in Android Studio 3.1 and later

project.android.sourceSets.main

works. Note that it has type AndroidSourceSet, not SourceSet. It has basically the same API, though, with Android-relevant additions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!