Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native prebuilt shared library in Android Studio with experimental Gradle plugin

I am trying to add a native prebuilt shared library to my project in Android Studio. I am using the gradle-experimental:0.6.0-alpha5. However, whenever I try to add the prebuilt shared library to my application model, I get the following error:

Error:Cause: org.gradle.api.internal.PolymorphicDomainObjectContainerConfigureDelegate

The library is added into the application model how it is described by the Google Gradle Experimental Guide:

repositories {
    prebuilt(PrebuiltLibraries) {
        binaries.withType(SharedLibraryBinary) {
            sharedLibraryFile = file("/path_to_libs/${targetPlatform.getName()}/shared_lib.so")
        }
    }
}

android.sources {
    main {
        jniLibs {
            dependencies {
                library "shared_lib"
            }
        }
    }
}

The crucial line is library "shared_lib". There is no error if I uncomment this line.

Since this is not working, I have also tried to use the guide from ph0b.com. They are using a different syntax for adding native shared libraries (I just left out the headers since I do not have a single directory including all headers):

repositories {
    libs(PrebuiltLibraries) {
        shared_lib {
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = file("/path_to_libs/${targetPlatform.getName()}/shared_lib.so")
            }
        }
    }
}

android.sources {
    main {
        jni {
            dependencies {
                library "shared_lib" linkage "shared" 
            }
        }
    }
}

Nevertheless, this does not work as well. Android Studio does not copy my shared_lib to the apk file. Hence, I always get the following error:

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader [...] couldn't find "shared_lib.so"

Can anyone tell me how I can include native prebuild library into my project? I am using buildToolsVersion = '22.0.1' and compileSdkVersion = 22 as build parameters.

like image 402
budderick Avatar asked Feb 24 '16 16:02

budderick


1 Answers

This one worked for me (0.6.0-beta6).

repositories {
    prebuilt(PrebuiltLibraries) {
        YourLib {
            binaries.withType(SharedLibraryBinary) {
                sharedLibraryFile = file("src/main/libs/armeabi-v7a/libYourLib.so")
            }
        }
    }
}

android.sources {
    main {
        jniLibs {
            dependencies {
                library "YourLib"
            }
        }
    }
}

Looks like they just forgot to mention the "YourLib {}" part around "binaries.withType".

like image 92
zhur Avatar answered Nov 04 '22 14:11

zhur