How To Exclude Specific Resources from an AAR Depedency?

前端 未结 4 1365
清歌不尽
清歌不尽 2020-12-03 01:04

Is there a reasonably simple way for a module\'s build.gradle file to indicate that certain files from a dependency should be excluded? I am specifically intere

相关标签:
4条回答
  • 2020-12-03 01:31

    EDIT:

    Wrote advanced gradle task for you:

    final List<String> exclusions = [];
    
    Dependency.metaClass.exclude = { String[] currentExclusions ->
        currentExclusions.each {
            exclusions.add("${getGroup()}/${getName()}/${getVersion()}/${it}")
        }
        return thisObject
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile ('com.android.support:appcompat-v7:20.+')
        debugCompile ('com.squareup.leakcanary:leakcanary-android:1.3.1')
                .exclude("res/values-v21/values-v21.xml")
        releaseCompile ('com.squareup.leakcanary:leakcanary-android-no-op:1.3.1')
    }
    
    tasks.create("excludeTask") << {
        exclusions.each {
            File file = file("${buildDir}/intermediates/exploded-aar/${it}")
            println("Excluding file " + file)
            if (file.exists()) {
                file.delete();
            }
        }
    }
    
    tasks.whenTaskAdded({
        if (it.name.matches(/^process.*Resources$/)) {
            it.dependsOn excludeTask
        }
    })
    

    Now you can use method .exclude() on each dependency, providing into list of paths, you want to exclude from specified dependency. Also, you can stack the .exclude() method calls.

    0 讨论(0)
  • 2020-12-03 01:32

    I believe you can solve this problem more elegantly using the PackagingOptions facility of the Android Gradle Plugin DSL.

    I was able to use this myself to exclude some native libraries I didn't need introduced by an AAR in my project.

    android {
        ...
        packagingOptions {
            exclude '/lib/armeabi-v7a/<file_to_exclude>'
        }
    }
    

    For the case outlined in the question, I believe this would work:

    android {
        ...
        packagingOptions {
            exclude '/res/values-v21/<file_to_exclude>'
        }
    }
    
    0 讨论(0)
  • 2020-12-03 01:45

    Yes, you can use Proguard

    buildTypes {
        release {
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
            'proguard-rules.pro'
        }
    debug {
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
            'proguard-rules.pro'
        }
    }
    
    0 讨论(0)
  • 2020-12-03 01:51

    Try compileOnly keyword to mark the resource is used for compile only.

    dependencies {
          compileOnly fileTree(include: ['*.jar'], dir: 'libs')
    }
    
    0 讨论(0)
提交回复
热议问题