Home > Blockchain >  Find cached files of unpacked aar files in gradle script
Find cached files of unpacked aar files in gradle script

Time:11-06

I have an android project where a library is included (aar). Gradle will unpack and store the files in the gradle cache (.gradle\caches\transforms-2). I need a gradle script where I can retrieve a file from this cache and move it into my project. Does anybody know how this can be done?

The moving part is working. I just need to get the correct path

tasks.register('moveFile', Copy) {
  from "${path_to_build_cache}/path/to/folder/file.conf"
  into "${project.rootDir.path}/path/to/folder/"
}

EDIT

I have now tried the solution posted by @tim_yates. The problem is that I am now receiving the error CustomMessageMissingMethodException: Could not find method from() for arguments [ZIP 'C:\Users\nthemmer\.gradle\caches\modules-2\files-2.1\path\to\aar on task ':my-project:movefiles' of type org.gradle.api.DefaultTask It seems that the aar file is read correctly but it has not only one file. Any help appreciated :)

EDIT

The solution posted my tim_yates worked for me! It is still all a little magic to me still be I am getting there. Thanks again :)

CodePudding user response:

Instead of searching the cache, you should be able to use Gradle to find it for you...

Here's an example:

configurations {
    aar
}

dependencies {
    aar('com.mikhaellopez:circularimageview:4.3.0@aar')
}

tasks.register('moveFile', Copy) {
    from(zipTree(configurations.aar.singleFile)) {
        include "res/values/values.xml"
    }
    into project.layout.buildDirectory.dir('here')
}

That copies the file into ./build/here/res/values/values.xml

Edit

So there's probably multiple ways of doing this, but here's one.

Define a configuration that we will use for the single dependency you want a file from, and make compileClasspath extend from it (so the dependency ends up back in the compile classpath where it was previously)

configurations {
    aar
    compileClasspath.extendsFrom aar
}

Then in the dependencies where you reference the aar, you should be able to use aar instead of compileClasspath

dependencies {
    aar('com.mikhaellopez:circularimageview:4.3.0@aar')
}

Then you can use the moveFile task from above, and there will just be a single file

Not 100% sure what you have currently, so not sure how this fits in, but it should give you a good direction.

Here's the full build file which runs on Gradle 7.2 and uses the circularimageview arr off maven central as the test subject

plugins {
    id('java')
}

repositories {
    mavenCentral()
}

configurations {
    aar
    compileClasspath.extendsFrom aar
}

dependencies {
    aar('com.mikhaellopez:circularimageview:4.3.0@aar')
}

tasks.register('moveFile', Copy) {
    from(zipTree(configurations.aar.singleFile)) {
        include "res/values/values.xml"
    }
    into project.layout.buildDirectory.dir('here')
}
  • Related