More than one file was found with OS independent path 'lib/x86/libusb.so'
Asked Answered
M

12

46

I am using libusb in my android application. When I am trying to build libusb native library then I get below error message, *.so files generated.

Error:Execution failed for task ':app:transformNativeLibsWithMergeJniLibsForDebug'. More than one file was found with OS independent path 'lib/x86/libusb.so'

enter image description here

build.gradle

import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.0"
    defaultConfig {
        applicationId "com.williams.libusbpoc"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }

    sourceSets.main {
        jniLibs.srcDir 'src/main/libs'
        jni.srcDirs = [] //disable automatic ndk-build call
    }

    // call regular ndk-build(.cmd) script from app directory
    task ndkBuild(type: Exec) {
        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
            commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
        } else {
            commandLine 'ndk-build', '-C', file('src/main').absolutePath
        }
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation ('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.0.0-beta2'
    testImplementation 'junit:junit:4.12'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    compile "org.jetbrains.anko:anko-appcompat-v7-commons:$anko_version"
}

I am on windows machine. Does anyone know what could be the issue ?

Means answered 6/7, 2017 at 16:13 Comment(0)
M
29

I removed jniLibs.srcDir 'src/main/libs' code inside sourceSets.main block. It was creating *.so files twice.

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = [] //disable automatic ndk-build call
}
Means answered 7/7, 2017 at 4:36 Comment(3)
Did not work for me: Error:(25, 1) A problem occurred evaluating project ':app'. > Cannot cast object 'src/main/jni/lib' with class 'java.lang.String' to class 'java.lang.Iterable'Riobard
Thanks it worked for me with Android Studio 4.0.1 and NDK version 21.3.6528147Kristiekristien
it seems in old gradle version srcDirs is mandatory if you want to pack so into apkInterlocutrix
N
112

I was having this issue in my React-Native Bridge project after I added AAR files of 3rd party SDK. And I was linking the Bridge into my Main React-native application.

Solution (May differ for you):

Add this in app/build.gradle the Main React-Native application:

android {
    // ...
    packagingOptions {
        pickFirst '**/*.so'
    }
}
  • Test the Build on React-Native Bridge project after adding the AAR libraries.
  • Clean the React-Native Bridge project
  • Clean the React-Native application project
  • Remove node_modules and re-install the bridge package into the project.
  • Run the application.

I faced another issue related to this (If you include AAR into library project that's not being linked to main application)

https://mcmap.net/q/372909/-task-app-transformnativelibswithmergejnilibsfordebug-failed

Neldanelia answered 29/10, 2019 at 6:26 Comment(4)
also worked for a non react problem, thanksOfficer
after it I am unable to assemble Debug apk. Means When I assembles debug. its generate apk which is not a debug apkMichikomichon
worked for flutter-android studio too.Heurlin
For react native 0.67 this solution worked, for 0.68 it didn'tBarriebarrientos
M
29

I removed jniLibs.srcDir 'src/main/libs' code inside sourceSets.main block. It was creating *.so files twice.

sourceSets.main {
    jniLibs.srcDir 'src/main/libs'
    jni.srcDirs = [] //disable automatic ndk-build call
}
Means answered 7/7, 2017 at 4:36 Comment(3)
Did not work for me: Error:(25, 1) A problem occurred evaluating project ':app'. > Cannot cast object 'src/main/jni/lib' with class 'java.lang.String' to class 'java.lang.Iterable'Riobard
Thanks it worked for me with Android Studio 4.0.1 and NDK version 21.3.6528147Kristiekristien
it seems in old gradle version srcDirs is mandatory if you want to pack so into apkInterlocutrix
S
21

you can use like this:

add the following code into build.gradle ,

packagingOptions {
    pickFirst 'lib/armeabi-v7a/your_name.so'
    pickFirst 'lib/arm64-v8a/your_name.so'
    pickFirst 'lib/x86/your_name.so'
    pickFirst 'lib/x86_64/your_name.so'
}

this pickFirst that means : if more than one path matches the first-pick, only the first found will be selected. please click Get more information

Satire answered 5/6, 2019 at 2:54 Comment(0)
S
10

In case of react-native add android/app/build.gradle file into andorid : {.....} section this :

packagingOptions {
    pickFirst 'lib/x86/libc++_shared.so'
    pickFirst 'lib/x86_64/libjsc.so'
    pickFirst 'lib/arm64-v8a/libjsc.so'
    pickFirst 'lib/arm64-v8a/libc++_shared.so'
    pickFirst 'lib/x86_64/libc++_shared.so'
    pickFirst 'lib/armeabi-v7a/libc++_shared.so'
}

then use "gradlew clean"

Spool answered 12/5, 2021 at 19:18 Comment(2)
Thanks! This helped a lot with FFMPEG.Evin
add this line too: pickFirst 'lib/x86_64/libavformat.so'Barriebarrientos
P
5

I've seen a similar error running my app after migration to Android Studio 3.0. A build clean solved the issue.

Proptosis answered 7/11, 2017 at 21:11 Comment(0)
H
5

in my case adding these fixed the issue to build.gradle app level module

packagingOptions {
    pickFirst 'lib/armeabi-v7a/libblasV8.so'
    pickFirst 'lib/mips/librsjni.so'
    pickFirst 'lib/x86/libblasV8.so'
    pickFirst 'lib/mips/libRSSupport.so'
    pickFirst 'lib/mips/libblasV8.so'
}

but in your case library names could be different so replace them with your library names

Hecklau answered 17/6, 2019 at 1:16 Comment(0)
M
2

I was seeing this error after adding react-native-pdf to my project. FAHID's solution worked for me.

Note that in later versions of Gradle, pickFirst() is deprecated in favor of resources.pickFirsts() or jniLibs.pickFirsts.add().

Moony answered 30/3, 2022 at 13:37 Comment(0)
E
2

If you are using react native version below 0.70.5 then you should implement this, Add this code in android/build.gradle file

repositories {
   exclusiveContent {
       // We get React Native's Android binaries exclusively through npm,
       // from a local Maven repo inside node_modules/react-native/.
       // (The use of exclusiveContent prevents looking elsewhere like Maven Central
       // and potentially getting a wrong version.)
       filter {
           includeGroup "com.facebook.react"
       }
       forRepository {
           maven {
               url "$rootDir/../node_modules/react-native/android"
           }
       }
    }
    // ...
  }
}

It worked for me.

If Your gradle version 6.1 or below, you must use a different workaround as gradle 6.1 does not support exclusiveContent. You can use this,

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    configurations.all {
        resolutionStrategy {
            // Remove this override in 0.65+, as a proper fix is included in react-native itself.
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }
Enteron answered 22/12, 2022 at 7:7 Comment(0)
T
1

Manually clean the project. Perform the following in the project directory:

rm -rf .idea .gradle */build */.cxx
rm -rf ~/.gradle

This will clean up all intermediate files and also clear the gradle cache.

Terrorism answered 1/6, 2021 at 11:27 Comment(1)
clean build folder fixedWald
R
0

I just want to add to Nonos answer that I think I received this issue after running ndk-build within my app/jni directory, and then running ./gradlew installDebug from the top-level directory of my Android NDK project. So doing an ndk-build clean in app/jni before doing another Gradle build did indeed solve the problem.

Riffe answered 3/12, 2018 at 6:30 Comment(0)
T
0

N Sharma solutions is good.

But in case your conflict is with an external module that you added as a library in your project and you are not able to edit its code as it is readonly you can follow below steps:

I was facing same issue with ffmpeg library after merging two Android projects as one project.

Actually issue was arriving due to two different versions of ffmpeg library but they were loaded with same names in memory. One library was placed in JNiLibs while other was inside another library used as module. I was not able to modify the code of module as it was readonly so I renamed the one used in my own code to ffmpegCamera and loaded it in memory with same name.

System.loadLibrary("ffmpegCamera");

This resolved the issue and now both versions of libraries are loading well as separate name and process id in memory.

Terrenceterrene answered 9/3, 2020 at 12:15 Comment(1)
Where do I execute this command?Monaghan
V
0

faced this issue in react-native "react-native": "0.64.2"
facing this issue because of recent react native updates, old builds are failing

i have done the fallowing to run the build successfully & this solution works after most of the issue caused in recent update.

  1. in android/build.gradle (not the project level) change the followings
  • add kotlin version in ext of buildScripts {... kotlin_version = '1.6.10'}

  • in dependecies add the classpath
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    add this line in between ending of buildscript and beginning of allprojects

  • add this line in the first beginning of all projects

    configurations.all {
      resolutionStrategy {
          force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
      }
    }
    
Vining answered 24/11, 2022 at 8:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.