如何排除从一个的gradle的Android .AAR库归档文件 [英] How to exclude a file from an .AAR Android library archive with gradle
问题描述
我试图创建使用Android Studio和gradle这个库的Android项目的AAR文件。
I am trying to create an AAR file for a library Android project using Android Studio and gradle.
我想从这个特定的存档文件夹和文件排除,但我不能找到一个可行的解决方案。
I want to exclude from this archive specific folders and files but I cannot find a working solution.
该项目有2个味道。
app/
|--libs/
|--src/
|--flavour1/
| |--java/
| |--a1/
| | |--class_File1.java
|--flavour2/
| |--java/
| |--a1/
| | |--class_File1.java
|--main/
|--java/
| |--...
|--res/
| |--raw/
| | |--comments.txt
| |--...
|--AndroidManifest.xml
和我使用的build.gradle文件,像这样的
and I use a build.gradle file like this one
apply plugin: 'com.android.library'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
}
packagingOptions {
exclude 'assets'
exclude '**/comments.txt'
}
sourceSets {
flavour1 {
resources {
exclude '**/comments.txt'
}
}
flavour2 {
}
}
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
flavour1 {
packagingOptions {
exclude 'assets'
exclude 'res/raw/comments.txt'
}
}
flavour2 {
}
}
}
dependencies {
compile 'com.google.android.gms:play-services:+'
}
一般而言,我已经尝试了很多。我曾尝试packagingOptions,排斥sourceSets,但似乎没有任何工作。我试图达到不包括例如在.AAR归档文件comments.txt和文件夹资产,这是一个可选的文件夹。我通过重命名它ZIP和寻找到这些文件包括每次检查.AAR文件。
Generally speaking I have experimented a lot. I have tried packagingOptions, exclusion in sourceSets but nothing seems to work. What i am trying to achieve is not include for example in the .AAR archive the file comments.txt and the folder "assets" which is an optional folder. I examine each time the .AAR file by renaming it to zip and looking into the files included.
我也看着这里的文档:<一href=\"http://simpligility.github.io/android-maven-plugin/aar-mojo.html#configurations\">http://simpligility.github.io/android-maven-plugin/aar-mojo.html#configurations在那里,也许配置可能是一个解决方案,但我不知道如何使用它,或者如何前进。任何帮助将AP preciated。
I have also looked into the documentation here: http://simpligility.github.io/android-maven-plugin/aar-mojo.html#configurations where maybe configurations could be a solution but I am not sure how to use it or how to move forward. Any help will be appreciated.
推荐答案
认为这是涉及到的这个问题。一般来说,你可以尝试用:
Think it is related to this issue. Generally you could try with:
- 源组排除
- 包装选项
- AAPT选项
我不能让其中一方,虽然工作。
I couldn't get either of them to work though.
这是说,你可以做一些丑陋的黑客就像这样:
That said, you could make some ugly hacks like this:
def filterVariant = { variantToFilter, filterTask->
def vv = android.sourceSets."${variantToFilter}".res.srcDirs
println "${variantToFilter} --> ${vv*.toString()}"
def variantRes = android.sourceSets."${variantToFilter}".res
variantRes.srcDirs.each{ resDir->
def filterOutput = "${buildDir}/res-filter"
if (resDir.toString().contains(filterOutput)) {
return
}
println "begin filter ${resDir} to ${filterOutput}/${variantToFilter}"
filterTask.from fileTree(dir: resDir, exclude: '**/comment.txt')
filterTask.into "${filterOutput}/${variantToFilter}"
variantRes.srcDirs = ["${filterOutput}/${variantToFilter}"]
}
}
project.task('filterMainResources', type: Copy) {
filterVariant 'main', it
}
android.libraryVariants.all{ variant ->
project.task("filter${variant.name}Resources", type: Copy) { filterTask ->
filterVariant "${variant.name}", filterTask
filterTask.dependsOn "filterMainResources"
}
variant.mergeResources.dependsOn("filter${variant.name}Resources")
}
这篇关于如何排除从一个的gradle的Android .AAR库归档文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!