简体   繁体   English

如何使用Gradle从.AAR Android库存档中排除文件

[英]How to exclude a file from an .AAR Android library archive with gradle

I am trying to create an AAR file for a library Android project using Android Studio and gradle. 我正在尝试使用Android Studio和Gradle为图书馆的Android项目创建AAR文件。

I want to exclude from this archive specific folders and files but I cannot find a working solution. 我想从此存档中排除特定的文件夹和文件,但找不到有效的解决方案。

The project has 2 flavours. 该项目有2种口味。

app/
|--libs/
|--src/
   |--flavour1/
   |  |--java/
   |     |--a1/
   |     |  |--class_File1.java
   |--flavour2/
   |  |--java/
   |     |--a1/
   |     |  |--class_File1.java
   |--main/
      |--java/
      |  |--...
      |--res/
      |  |--raw/
      |  |  |--comments.txt
      |  |--...
      |--AndroidManifest.xml

and I use a build.gradle file like this one 我使用这样的build.gradle文件

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:+'
}

Generally speaking, I have experimented a lot. 一般来说,我已经做了很多实验。 I have tried packagingOptions, exclusion in sourceSets but nothing seems to work. 我试过了PackagingOptions,排除在sourceSets中,但似乎没有任何效果。 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. 我要实现的目标不包括在.AAR档案中,例如comments.txt文件和“ assets”文件夹(这是一个可选文件夹)。 I examine each time the .AAR file by renaming it to zip and looking into the files included. 我每次通过将.AAR文件重命名为zip并查看其中包含的文件来进行检查。

I have also looked into the documentation here , 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: 通常,您可以尝试:

  • Source set excludes 来源集不包括
  • Packaging options 包装选项
  • Aapt options 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")
    }

I can't try it right now, but this would probably be my approach: 我现在无法尝试,但这可能是我的方法:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        aidl.srcDirs = ['src']
        resources.srcDirs = ['src']           
        renderscript.srcDirs = ['src']
        java.srcDirs = ['src']
        res.srcDirs = ['res']
    }

    flavor1 {
        java.srcDirs = ['src/main/java', 'src/flavor1/java']         
        res.srcDirs = ['src/flavor1/res'] //this differs from your structure
        assets.srcDirs = []
    }

    flavor2 {
        java.srcDirs = ['src/main/java', 'src/flavor2/java']
        res.srcDirs = ['src/flavor2/res'] //this differs from your structure
        assets.srcDirs = ['src/main/res/raw']
    }
}

Again, I couldn't try it, so this could be complete nonsense. 同样,我无法尝试,所以这可能完全是胡说。 The idea was to not exclude stuff, but only include the files/dirs you do want in you flavor. 想法是不排除内容,而仅包含您想要的风味文​​件/目录。 This may will lead to duplication of files/folders per flavor! 这可能会导致每种口味的文件/文件夹重复!

As sources I would recommend: Gralde Plugin User Guide , the answer from Xavier Ducrohet and also this Gradle + Android, want to overwrite /assets with custom folder in buildFlavor . 作为来源,我建议: Gralde插件用户指南Xavier Ducrohet答案以及这个Gradle + Android,都希望使用buildFlavor中的自定义文件夹覆盖/ assets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM