【问题标题】:specify .so files in build.gradle file android studio在 build.gradle 文件 android studio 中指定 .so 文件
【发布时间】:2017-07-05 14:47:50
【问题描述】:

这是我的目录结构

src -main

    -java

    -jniLibs

            -armeabi

                     -lib1.so
                     -lib2.so

需要了解应该在构建 gradle 文件中编写哪些代码才能将这些文件包含在构建中。 现在,我收到了找不到 .so 文件的错误。

  java.lang.UnsatisfiedLinkError: nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libFPEComponent.so"

关注solution by H.Brooks

我必须在这里添加

My Git repository:

我的 bUild.gradle 文件

 apply plugin: 'com.android.application'

android {
    productFlavors {
        arm {
            ndk {
                abiFilters "armeabi-v7a", "armeabi"
            }
        }

    }

    sourceSets
            {
                main
                        {

                            jniLibs.srcDirs = ['src/main/jnilibs']

                        }

//Another code

            }

    compileSdkVersion 26
    buildToolsVersion '26.0.0'
    defaultConfig {
        applicationId "com.goambee.biometricziqitza"
        minSdkVersion 15
        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'
        }

    }
    splits {

        // Configures multiple APKs based on ABI.
        abi {

            // Enables building multiple APKs per ABI.
            enable true

            // By default all ABIs are included, so use reset() and include to specify that we only
            // want APKs for x86, armeabi-v7a, and mips.

            // Resets the list of ABIs that Gradle should create APKs for to none.
            reset()

            // Specifies a list of ABIs that Gradle should create APKs for.
            include "armeabi-v7a"

            // Specifies that we do not want to also generate a universal APK that includes all ABIs.
            universalApk false
        }
    }
}

dependencies {

    compile fileTree(include: ['*.jar'], dir: 'libs')
//    compile fileTree(dir: "$buildDir/native-libs", include: 'native-libs.jar')


    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
}

//task native-libstiveLibsToJar(type: Jar, description: 'create a jar archive of the native libs') {
//    destinationDir file("$buildDir/native-libs")
//    baseName 'native-libs'
//    from fileTree(dir: 'src/main/jnilibs', include: '**/*.so')
//    into 'lib/'
//}
//
//tasks.withType(JavaCompile)
//        {
//            compileTask -> compileTask.dependsOn(nativeLibsToJar)
//        }

这里我已经包含了源集块,我也测试过没有那个块。 所以仍然 build 无法访问 .so 文件。

【问题讨论】:

  • 您是在尝试构建库还是想直接在您的应用中运行 .so 文件?
  • 使用那些 .so 文件
  • 如果你使用 .so 文件,你不必在 gradle 中做任何事情。你可以直接从文件夹中使用它
  • 您是否使用 android NDK 构建 .so 文件?
  • 好吧,我没有在 build.gradle 中编写任何代码,正如我展示的结构,那些 .so 文件在 src/main/jniLibs 中,但它不起作用,因为它失败并出现错误“无法找到 some.so 文件”。我该怎么办?

标签: android android-studio java-native-interface build.gradle .so


【解决方案1】:

如果你想使用它,你可以像这样在你的类中调用它:

static {
    System.loadLibrary("libSONAME");
}

在您的gradle 中添加以下内容:

jniLibs.srcDirs = ['libs']

编辑:

在 Android Studio 中添加 .so 库

  1. 在“src/main/”中创建文件夹“jniLibs”
  2. 将所有 .so 库放入“src/main/jniLibs”文件夹中
  3. 文件夹结构看起来像,
    |--应用程序:
    |--|--src:
    |--|--|--主要
    |--|--|--|--jniLibs
    |--|--|--|--|--armeabi
    |--|--|--|--|--|--.so 文件
    |--|--|--|--|--x86
    |--|--|--|--|--|--.so 文件
  4. 无需额外的代码,只需同步您的项目并运行您的 申请。

参考

https://github.com/commonsguy/sqlcipher-gradle/tree/master/src/main


如果您只有 .so,不确定您究竟拥有哪些文件?


请看下图:

这是我项目中的“项目/库”,这是加载/添加 .so 文件的方式:

我这样编译项目:

compile project(':videokit')

【讨论】:

  • 不太可能。错误消息表明 loadLibrary 失败,而不是本地方法无法解析
  • @H.Brooks,当这段代码运行时。我把它作为我的 MainActivity 中的一个方法,仍然给出同样的错误。
  • @H.Brooks,在 -'catch (UnsatisfiedLinkError e) { System.out.println("UnsatisfiedLinkError in static block"); e.printStackTrace(); }'
  • 您能告诉我您是如何尝试实现和使用 .so 文件的吗?
  • 我该怎么做,我应该编辑原始问题并添加图片,还是有办法在评论中发送图片。
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2016-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 2020-10-09
相关资源
最近更新 更多