Android原生库消失,一旦应用程序被安装 [英] Android native libs disappearing once app is installed

查看:384
本文介绍了Android原生库消失,一旦应用程序被安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

切换到Android Studio之后,我开始看到可怕的。

After switching to Android Studio, I began seeing the dreaded

java.lang.UnsatisfiedLinkError: dlopen failed: library
'/data/app-lib/com.myapp.test-1/libmylib.so' not found 

错误。当我拆开APK,我可以看到libmylib.so与所有其他本地库(libmyotherlib.so和libtest.so)的lib / armeabi文件夹下,这样的包装应该不是问题...沿我决定根我的测试设备,并检查了我的应用程序的文件夹下的/数据/应用-lib,其中其机库应安装后的实际内容 - 我发现,我的应用程序的本地库中的一个(libmylib.so)的应用程序后失踪被安装在设备上。 libmylib.so和libmyotherlib.so文件放在src /主/ jniLibs pre-建.so文件,并libtest.so是从的src / main / JNI test.c的编译。

error. When I unpack the apk, I can see libmylib.so along with all the other native libraries (libmyotherlib.so and libtest.so) under the lib/armeabi folder, so packaging shouldn't be the problem... I decided to root my test device and check out the actual contents of my app's folder under /data/app-lib, where its native libraries should be after install -- I found that one of my app's native libraries (libmylib.so) was missing after the app is installed on a device. libmylib.so and libmyotherlib.so are pre-built .so files placed in src/main/jniLibs, and libtest.so is compiled from test.c in src/main/jni.

在我切换到Android公寓这只是开始;我已经验证从同一个code在Eclipse ADT内置的APK拥有所有需要的库present下/data/app-lib/com.myapp.test-1后安装。

This only started after I switched to Android Studio; I've verified that apks built from the same code in Eclipse ADT have all the required libraries present under /data/app-lib/com.myapp.test-1 after installation.

针对Android Studio生成相关的build.gradle:

Relevant build.gradle for Android Studio build:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.myapp.test"
        minSdkVersion 17
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"

        ndk{

            moduleName "test"//testing ndk integration
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'  
        }
    }

}

repositories {
    // You can also use jcenter if you prefer
    mavenCentral()
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'

    //android support libs etc.
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:support-v13:23.1.0'

}

相关gradle.properties文件

Relevant gradle.properties file

android.useDeprecatedNdk=true

根据的src / main / JNI我只有test.c的

Under src/main/jni I have only test.c

#include <jni.h>

int main(){

    return 0;
}

和下的src / main / jniLibs / armeabi我有

and under src/main/jniLibs/armeabi I have

libmylib.so
libmyotherlib.so

Android.mk为Eclipse ADT构建相关的:

Relevant Android.mk for Eclipse ADT build:

include $(CLEAR_VARS)
LOCAL_MODULE    := test
LOCAL_SRC_FILES += test.c
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := myotherlib
LOCAL_SRC_FILES :=  $(TARGET_ARCH_ABI)/libmyotherlib.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES :=  $(TARGET_ARCH_ABI)/libmylib.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

相关/数据/应用程序安装后,APP-lib中的内容由Android Studio构建:

Relevant /data/app-lib contents after installing app built by Android Studio:

root@SOLONE SL-K40:/ # ls /data/app-lib/com.myapp.test-1               
libmyotherlib.so
libtest.so

相关/数据/应用程序安装后,APP-lib中的内容通过Eclipse的ADT建:

Relevant /data/app-lib contents after installing app built by Eclipse ADT:

root@SOLONE SL-K40:/ # ls /data/app-lib/com.myapp.test-1               
libmylib.so
libmyotherlib.so
libtest.so

我意外地发现,通过添加

I accidentally found that by adding

sourceSets {
        main {
            jni.srcDirs = []
 }
}

我的build.gradle我能得到libmylib.so在安装后再次现身,但有在我的项目的任何NDK源$ C ​​$ C此precludes。

to my build.gradle I can get libmylib.so to show up again after installation, but this precludes having any NDK source code in my project.

第一个问题:
任何想法可能是怎么回事?难道说MYLIB实际上编译比armeabi和Android的ABI另一种是抛弃它,因为它的实际ABI没有它排在APK文件夹里面的匹配(我没有源$ C ​​$ C为MYLIB) ?我的问题听起来类似于人讨论<一个href=\"http://stackoverflow.com/questions/29897408/i-need-loadlibrary-two-so-library-files-on-android-studio-v1-1-0\">here,但是那个人似乎只看到一个共享库在决赛中安装的应用程序;我看到所有,但我的共享库之一。

First Question: Any idea what might be going on here? Could it be that mylib was actually compiled for an abi other than armeabi and Android is discarding it because its actual abi doesn't match the folder it came in inside the apk (I don't have the source code for mylib)? My problem sounds similar to one discussed here, but that person seemed to see only one shared library in the final installed app; I'm seeing all but one of my shared libraries.

第二个问题:
什么是包括pre-内置.so文件在Android工作室创建当前正确的方法是什么? 线索 <一个href=\"http://stackoverflow.com/questions/25724999/how-to-add-$p$pbuilt-so-library-in-android-studio-0-8-6\">around 似乎是由Android的Studio版本差异很大(我使用的是Android工作室1.5,摇篮2.4版本,Android插件版本1.3.0)是否还有需要重定向jniLibs.srcDir变量的src / main /库?

Second Question: What is the current correct way to include pre-built .so files in an Android Studio build? Clues around the 'net seem to vary wildly by Android Studio version (I'm using Android Studio 1.5, Gradle version 2.4, Android plugin version 1.3.0) Is there still a need to redirect the jniLibs.srcDir variable to src/main/libs?

推荐答案

是的,最好的方法是定义的 jniLibs.srcDir ,这样所有prebuilt库可以从那里复制。

Yes, the best approach is to define jniLibs.srcDir such that all prebuilt libraries can be copied from there.

是的,ABI是问题的最可能的来源。如果prebuilt图书馆建于 armeabi 但是设备(如今天的绝大多数设备)支持的 armeabi-V7A ,然后安装程序会高高兴兴地复制< STRONG> ... V7A 您不可─prebuilt库的版本为 /data/app-lib/com.myapp.test-1 ,然后加载程序稍后会抱怨说<强> libmylib.so 丢失。

Yes, the ABI is the most probable source of the problem. If the prebuilt library was built for armeabi but the device (as the vast majority of devices today) supports armeabi-v7a, then the installer will happily copy the …v7a versions of your not-prebuilt libraries to /data/app-lib/com.myapp.test-1, and the loader will later complain that libmylib.so is missing.

您应该指示An​​droid工作室只有构建您只需ABI。如果你没有做什么在Eclipse构建特殊的,那么这是最有可能的 armeabi

You should instruct Android Studio to only build one ABI. If you did not do something special in Eclipse build, then this is most likely armeabi.

指示结合的方式取决于插件的gradle的版本。

The way to instruct AS depends on the version of gradle plugin.

对于'com.android.tools.build:gradle:1.5.0插件,我使用类似

android {
    defaultConfig.ndk {
        …
        abiFilter 'armeabi'
    }

    splits {
        abi {
            enable true
            reset()
            include 'armeabi'
        }
    }
}

对于 ... gradle这个实验性:0.2.0 ,我用

model {
    android.ndk {
        …
        abiFilters += 'armeabi'
    }
}

我没有必要启用在分裂的实验的插件,所以我不会误导你对语法的变更。

I didn't have a need to enable splits on the experimental plugin, so I will not mislead you about the syntax changes there.

这篇关于Android原生库消失,一旦应用程序被安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆