Question

Les fichiers de bibliothèques partagées sont placés dans lib / armeabi dans un fichier apk.

J'ai lu après l'installation du libs s'extrait à / data / data / application_package / lib

Comment puis-je obtenir le chemin exact de ce répertoire dans ma demande au moment de l'exécution? Est-ce répertoire lisible par l'application? Ou est seulement l'accès executeable permis? Si elle est lisible - Est-ce toujours vrai pour les applications protégé contre la copie

Était-ce utile?

La solution

Vous pouvez obtenir le chemin exact avec:

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

Le répertoire et les fichiers sont lisibles par l'application.

Les permissions unix sont mis à rwxr-x--x. Donc, avec des applications le même groupe peut lire les fichiers.

Autres conseils

Ajouté au niveau de l'API 9

getContext().getApplicationInfo().nativeLibraryDir;

String libpath = getApplicationInfo().nativeLibraryDir;

Class: import android.content.pm.ApplicationInfo;

Et si vous utilisez une activité native et 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);
String libraryPath = context.getFilesDir().getParentFile().getPath() + "/lib";

Pour une meilleure compatibilité, utilisez la fonction suivante:

@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";
}

Peut-être un support de périphérique différent CPU_ABIs, il est donc préférable d'obtenir nativeRootLibraryDir qui contiennent tous les sous-répertoires lib:

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";
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top