Home > Enterprise >  How to define which so file to use in gradle packaging options
How to define which so file to use in gradle packaging options

Time:11-02

I'm trying to build my android application which contains two libraries. In one library, a newer version of ffmpeg is being used. In another library, a dependency of that library is using an older version of ffmpeg. Trying to use pickFirst in the package options picks the WRONG libary. Is there ANY possible way to fix this, or is this just a limitation of Gradle?

Here is the error I am getting

Execution failed for task ':app:mergeDebugNativeLibs'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.MergeNativeLibsTask$MergeNativeLibsTaskWorkAction
   > 2 files found with path 'lib/arm64-v8a/libavcodec.so' from inputs:

CodePudding user response:

You can exclude the library from the dependency that has the "wrong" version.

dependencies {
    implementation("some-library") {
        exclude(group = "com.example.imgtools", module = "native")
    }
}

CodePudding user response:

I was actually able to solve this issue myself.

Essentially, what I needed to do was to add a task to my gradle file that would intercept the merge libs task before it runs and delete the unwanted libraries:

tasks.whenTaskAdded((tas -> {
    if (tas.name.contains("mergeDebugNativeLibs")) {
        tasks.named("mergeDebugNativeLibs") {it
            doFirst {
                java.nio.file.Path notNeededDirectory = it.externalLibNativeLibs
                    .getFiles()
                    .stream()
                    .filter(file -> file.toString().contains("Metadata"))
                    .findAny()
                    .orElse(null)
                    .toPath();
                Files.walk(notNeededDirectory).forEach(file -> {
                    if (file.toString().contains("libav") || file.toString().contains("libsw")) {
                        Files.delete(file);
                    }
                });
            }
        }
    }
    if (tas.name.contains("mergeReleaseNativeLibs")) {
        tasks.named("mergeReleaseNativeLibs") {it
            doFirst {
                java.nio.file.Path notNeededDirectory = it.externalLibNativeLibs
                        .getFiles()
                        .stream()
                        .filter(file -> file.toString().contains("Metadata"))
                        .findAny()
                        .orElse(null)
                        .toPath();
                Files.walk(notNeededDirectory).forEach(file -> {
                    if (file.toString().contains("libav") || file.toString().contains("libsw")) {
                        Files.delete(file);
                    }
                });
            }
        }
    }
}))

In this case, the unwanted libraries are in the FFmpegMetadataMediaRetriever folder/library.

  • Related