Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define which so file to use in gradle packaging options

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:
like image 325
Bernard Allotey Avatar asked Oct 20 '25 12:10

Bernard Allotey


1 Answers

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.

like image 95
Bernard Allotey Avatar answered Oct 22 '25 02:10

Bernard Allotey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!