Ell*_*tus 14 gradle android-studio build.gradle android-gradle-plugin
我使用过Android Studio与谷歌驱动器,这使得隐藏的系统文件可见(如Icon和desktop.ini),导致错误,如所描述这里.我不想运行脚本来删除这些文件,而是指示gradle 2.14忽略它们.
为了弄清楚如何排除文件,我在目录\app\src\main\res\values和中创建了名为"excludeme.txt"的文件\app\src\main\java\com\hfad\stopwatch\.我没有成功尝试对build.gradle项目中的两个文件进行各种修改.没有成功排除该文件.
具体来说,我根据这个问题对build.gradle文件进行了以下修改:Module: app
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'
}
Run Code Online (Sandbox Code Playgroud)
根据这个问题,我还将此添加到android同一build.gradle文件的部分:
aaptOptions {
ignoreAssetsPattern "!excludeme.txt"
}
Run Code Online (Sandbox Code Playgroud)
我还补充说:
sourceSets {
main {
java {
srcDir "src" [I also tried w/o this line]
exclude "**/excludeme.txt"
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我选择了Build > Clean Project触发构建的时候,我得到了这个输出,这表明资源文件夹中的文件没有被忽略:
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.
Run Code Online (Sandbox Code Playgroud)
如果删除感叹号,结果是一样的:
aaptOptions {
ignoreAssetsPattern "!excludeme.txt"
}
Run Code Online (Sandbox Code Playgroud)
在构建Android项目时,如何正确指示gradle忽略包含特定模式的文件?
更新1
根据Per Stefan的建议,我在build.gradle文件中添加了以下内容Module: app:
android {
[omitted]
sourceSets.main.res.exclude '**/whatever.txt'
}
Run Code Online (Sandbox Code Playgroud)
我添加了一个whatever.txt在res/layout目录中命名的文件,并且构建失败了:
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
Run Code Online (Sandbox Code Playgroud)
更新2
根据内森的建议,我补充道:
packagingOptions {
exclude '**/whatever.txt'
exclude 'src/main/res/layout/whatever.txt'
}
Run Code Online (Sandbox Code Playgroud)
要build.gradle (Module: app)在android一节.没有得到改善:
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
Run Code Online (Sandbox Code Playgroud)
(最后一个版本是在Mac上,与Windows上的其他版本不同.)
看起来 gradle 最近的变化改变了 sourceSets 的 DSL,所以你现在需要在过滤器字段上指定排除:
android {
sourceSets.main.res.filter.exclude '**/whatever.txt'
}
Run Code Online (Sandbox Code Playgroud)
Android 插件有自己的 SourceSets 概念:http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure
所以你可能想做类似的事情
android {
sourceSets.main.res.exclude '**/whatever.txt'
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12621 次 |
| 最近记录: |