インストールされたパッケージのためのlibフォルダのパスを取得する方法
-
25-09-2019 - |
質問
共有ライブラリの.soファイルはAPKファイル内のlib / armeabiに置かれます。
私は/データ/データ/ application_package / libに
に抽出しますインストール後のlibsを読んだことがありますどのように私は、実行時に自分のアプリケーションでは、このディレクトリへの正確なパスを得ることができますか? このディレクトリは、アプリケーションによって読み取り可能ですか?または唯一executeableのアクセスが許可されていますか? それが読み取り可能な場合は - ?これはコピー保護されたアプリケーションのために、まだ本当です。
解決
あなたはとの正確なパスを取得することができます
String libraryPath = getContext().getApplicationInfo().dataDir + "/lib";
ディレクトリとそのファイルは、アプリケーションによって読み出し可能です。
は、UNIXのパーミッションがrwxr-x--x
に設定されています。とそうアプリケーション
同じグループには、ファイルを読むことができます。
他のヒント
APIレベルで追加されました9
getContext().getApplicationInfo().nativeLibraryDir;
String libpath = getApplicationInfo().nativeLibraryDir;
クラスを使用:をインポートandroid.content.pm.ApplicationInfo; の
そして、あなたはネイティブの活動や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";
は、より良い互換性のために、次の関数を使用します
@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";
}
たぶん、デバイスのサポート異なるCPU_ABIs、それはすべてのサブのlibディレクトリが含まれているnativeRootLibraryDirを取得する方が良いですので、
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";
}
所属していません StackOverflow