问题描述
我将 Android Studio 与 Google Drive 一起使用,这会使隐藏的系统文件可见(例如 Icon
和 desktop.ini
),从而导致错误,如.我不想运行脚本来删除这些文件,而是指示 gradle 2.14 忽略它们.
I am using Android Studio with Google Drive, which makes hidden system files visible (such as Icon
and desktop.ini
), causing errors, as described here. Rather than run scripts to delete these files, I'd like to instruct gradle 2.14 to ignore them.
为了弄清楚如何排除文件,我在目录 \app\src\main\res\values
和 \app 中创建了名为excludeme.txt"的文件\src\main\java\com\hfad\stopwatch\
.我尝试对项目中的两个 build.gradle
文件进行各种修改,但没有成功.没有成功排除文件.
In an attempt to figure out how to exclude files, I created files named "excludeme.txt" in the directories \app\src\main\res\values
and in \app\src\main\java\com\hfad\stopwatch\
. I unsuccessfully tried various modifications to the two build.gradle
files in my project. None successfully excluded the file.
具体来说,我对 Module: app
的 build.gradle
文件进行了以下修改,根据 这个问题:
Specifically, I made the following modification to the build.gradle
file for Module: app
, per this question:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'], excludes: ['**/excludeme.txt'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
}
我还根据 :
I also added this to the android
section of the same build.gradle
file, per this question:
aaptOptions {
ignoreAssetsPattern "!excludeme.txt"
}
我还补充了:
sourceSets {
main {
java {
srcDir "src" [I also tried w/o this line]
exclude "**/excludeme.txt"
}
}
}
当我选择 Build >Clean Project
,触发了构建,我得到了这个输出,这表明资源文件夹中的文件没有被忽略:
When I selected Build > Clean Project
, which triggered a build, I got this output, which shows that the file in the resources folder was not ignored:
Information:Gradle tasks [clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources]
C:\Users\Ellen\GoogleDrive\Ellen-CS115\AndroidStudioProjects\Stopwatch\app\src\main\res\values\excludeme.txt
Error:(1, 1) Error: Premature end of file.
Error:Execution failed for task ':app:mergeDebugResources'.
> C:\Users\Ellen\GoogleDrive\Ellen-CS115\AndroidStudioProjects\Stopwatch\app\src\main\res\values\excludeme.txt:1:1: Error: Premature end of file.
如果我去掉感叹号,结果是一样的:
The results were the same if I removed the exclamation point:
aaptOptions {
ignoreAssetsPattern "!excludeme.txt"
}
如何在构建 Android 项目时正确指示 gradle 忽略包含特定模式的文件?
How do I properly instruct gradle to ignore files containing a certain pattern when building Android projects?
更新 1
根据下面 Stefan 的建议,我将以下内容添加到 Module: app
的 build.gradle
文件中:
Per Stefan's suggestion below, I added the following to the build.gradle
file for Module: app
:
android {
[omitted]
sourceSets.main.res.exclude '**/whatever.txt'
}
我在 res/layout
目录中添加了一个名为 whatever.txt
的文件,构建失败:
I added a file named whatever.txt
in the res/layout
directory, and the build failed with:
Error:Execution failed for task ':app:mergeDebugResources'.
> C:\Users\Ellen\GoogleDrive\Ellen-CS115\AndroidStudioProjects\Starbuzz\app\src\main\res\layout\whatever.txt: Error: The file name must end with .xml
更新 2
根据 Nathan 的建议,我补充说:
Per Nathan's suggestion, I added:
packagingOptions {
exclude '**/whatever.txt'
exclude 'src/main/res/layout/whatever.txt'
}
到 android
部分中的 build.gradle (Module: app)
.没有改善:
to build.gradle (Module: app)
in the android
section. No improvement:
Information:Gradle tasks [:app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources]
/Users/spertus/GoogleDrive/Ellen-CS115/AndroidStudioProjects/MacTest/app/src/main/res/layout/whatever.txt
Error:Error: The file name must end with .xml
Error:Execution failed for task ':app:mergeDebugResources'.
> /Users/spertus/GoogleDrive/Ellen-CS115/AndroidStudioProjects/MacTest/app/src/main/res/layout/whatever.txt: Error: The file name must end with .xml
Information:BUILD FAILED
(最后一次构建是在 Mac 上构建的,与其他构建在 Windows 上不同.)
(This last build was on a Mac, unlike the others, which were on Windows.)
推荐答案
看起来 gradle 最近的变化改变了 sourceSets 的 DSL,所以你现在需要在过滤器字段上指定 exclude :
Looks like a recent change in gradle changed the DSL for sourceSets, so you now need to specify the exclude on the filter field:
android {
sourceSets.main.res.filter.exclude '**/whatever.txt'
}
这篇关于如何从 Android Studio gradle 构建中排除某些文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!