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

andreasv

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.

The project has 2 flavours.

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

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. 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.

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.

Samuil Yanovski

Think it is related to this issue. Generally you could try with:

  • Source set excludes
  • Packaging options
  • Aapt options

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")
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Android Archive Library (aar) vs standard jar

How to exclude a jar from gradle

How to exclude a jar from gradle

In an Android Gradle build, how to exclude dependencies from an included jar file?

How to set name of AAR output from Gradle

How to exclude file from resources using Gradle and Android Studio?

Gradle: Exclude file from Android assets folder

How to publish aar file to Apache Archiva with Gradle

How Do I Remove A File From An Archive Using Gradle

Exclude native library in Gradle Android build

Can gradle resolve dependencies from just a Android .aar file?

Wrapping native libraries (SO files) into an Android Archive (aar file)

Gradle: how to include dependencies from repositories to output aar file

How to exclude sound clips from music library in Android app

How to get .AAR file from gradle dependency?

How to exclude files from aar with Gradle dynamically?

Export Android Library as AAR file

How to download a aar file from a private gitlab repo via gradle

How to build CHAQUOPY Android library (.AAR or .JAR file) in Android Studio

android gradle:how to exclude existing translation/locale from apk?

How to build AAR from Android Library?

How to import an .aar library file in a library module and export as single library?

Android Studio how to package single AAR from multiple library projects?

How to exclude file when fixing a zip archive

Android Studio - Gradle dependencies - How to Exclude `bolts` from `facebook`?

How to create *.aar file from "existing" android studio project ( Not creating new android library)?

Uploading Android aar library to bintray from TravisCI

How to use Unity exported Android library as an AAR?

How to choose only .aar file from file manager in android