16

共享库 .so 文件放在 apk 文件中的 lib/armeabi 中。

我已阅读安装后的库被提取到 /data/data/application_package/lib

如何在运行时在我的应用程序中获取此目录的确切路径?应用程序是否可以读取此目录?还是只允许可执行访问?如果它是可读的 - 这对于受版权保护的应用程序仍然适用吗?

4

6 回答 6

42

在 API 级别 9 中添加

getContext().getApplicationInfo().nativeLibraryDir;

于 2013-05-29T06:19:40.577 回答
14

您可以通过以下方式获得确切的路径:

String libraryPath = getContext().getApplicationInfo().dataDir + "/lib";

该目录及其文件可由应用程序读取。

unix 权限设置为rwxr-x--x. 因此具有相同组的应用程序可以读取文件。

于 2011-01-09T11:23:43.807 回答
4

And if you're using a native activity and C++:

void ANativeActivity_onCreate(ANativeActivity* app, void*, size_t) {
    const jclass contextClass = app->env->GetObjectClass(app->clazz);
    const jmethodID getApplicationContextMethod =
        app->env->GetMethodID(contextClass, "getApplicationContext", "()Landroid/content/Context;");
    const jobject contextObject =
        app->env->CallObjectMethod(app->clazz, getApplicationContextMethod);
    const jmethodID getApplicationInfoMethod = app->env->GetMethodID(
        contextClass, "getApplicationInfo", "()Landroid/content/pm/ApplicationInfo;");
    const jobject applicationInfoObject =
        app->env->CallObjectMethod(contextObject, getApplicationInfoMethod);
    const jfieldID nativeLibraryDirField = app->env->GetFieldID(
        app->env->GetObjectClass(applicationInfoObject), "nativeLibraryDir", "Ljava/lang/String;");
    const jobject nativeLibraryDirObject =
        app->env->GetObjectField(applicationInfoObject, nativeLibraryDirField);
    const jmethodID getBytesMethod = app->env->GetMethodID(
        app->env->GetObjectClass(nativeLibraryDirObject), "getBytes", "(Ljava/lang/String;)[B");
    const auto bytesObject = static_cast<jbyteArray>(app->env->CallObjectMethod(
        nativeLibraryDirObject, getBytesMethod, app->env->NewStringUTF("UTF-8")));
    const size_t length = app->env->GetArrayLength(bytesObject);
    const jbyte* const bytes = app->env->GetByteArrayElements(bytesObject, nullptr);
    const std::string libDir(reinterpret_cast<const char*>(bytes), length);
于 2018-08-07T15:41:28.110 回答
2
String libpath = getApplicationInfo().nativeLibraryDir;

Class used: import android.content.pm.ApplicationInfo;

于 2018-06-25T09:37:56.933 回答
0
String libraryPath = context.getFilesDir().getParentFile().getPath() + "/lib";

为了更好的兼容性,请使用以下函数:

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public static String getLibraryDirectory(Context context) {
    int sdk_level = android.os.Build.VERSION.SDK_INT;

    if (sdk_level >= Build.VERSION_CODES.GINGERBREAD) {
        return context.getApplicationInfo().nativeLibraryDir;
    } 
    else if (sdk_level >= Build.VERSION_CODES.DONUT) {
        return context.getApplicationInfo().dataDir + "/lib";
    }

    return "/data/data/" + context.getPackageName() + "/lib";
}
于 2014-10-30T07:17:43.300 回答
0

Maybe a device support different CPU_ABIs, so it's better to get nativeRootLibraryDir which contain all sub lib directories:

public static String getNativeLibraryDirectory(Context context) {
    int sdk_level = android.os.Build.VERSION.SDK_INT;

    if (sdk_level >= Build.VERSION_CODES.GINGERBREAD) {
        try {
            String secondary = (String) ApplicationInfo.class.getField("nativeLibraryRootDir").get(context.getApplicationInfo());
            return secondary;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    else if (sdk_level >= Build.VERSION_CODES.DONUT) {
        return context.getApplicationInfo().dataDir + "/lib";
    }

    return "/data/data/" + context.getPackageName() + "/lib";
}
于 2016-05-26T07:38:59.270 回答