Skip to content

jni代码翻译

cheyiliu edited this page Dec 9, 2015 · 1 revision

java

	 String path = null;
	 PackageManager packageManager = sContext.getPackageManager();
	 String packageName = sContext.getPackageName();
	 try {
	 ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_SHARED_LIBRARY_FILES);
	 path = applicationInfo.publicSourceDir;
	 } catch (NameNotFoundException e) {
	 }
	 return path;

c


void f(JNIEnv* env, jobject appContext) {
	if (env == NULL || appContext == NULL) {
		return;
	}

	// PackageManager packageManager = sContext.getPackageManager();
	jclass class_Context;
	class_Context = env->GetObjectClass(appContext);
	if (class_Context == NULL)
		goto end;

	jmethodID method_getPackageManager;
	method_getPackageManager = env->GetMethodID(class_Context,
			"getPackageManager", "()Landroid/content/pm/PackageManager;");
	if (method_getPackageManager == NULL)
		goto end;

	jobject object_PackageManager;
	object_PackageManager = env->CallObjectMethod(appContext,
			method_getPackageManager);
	if (object_PackageManager == NULL)
		goto end;

	// String packageName = sContext.getPackageName();
	jmethodID method_getPackageName;
	method_getPackageName = env->GetMethodID(class_Context, "getPackageName",
			"()Ljava/lang/String;");
	if (method_getPackageName == NULL)
		goto end;

	jstring jstr_packageName;
	jstr_packageName = (jstring) (env->CallObjectMethod(appContext,
			method_getPackageName));
	if (jstr_packageName == NULL)
		goto end;

	// ApplicationInfo applicationInfo = packageManager.getApplicationInfo(packageName, PackageManager.GET_SHARED_LIBRARY_FILES);
	jclass class_PackageManager;
	class_PackageManager = env->GetObjectClass(object_PackageManager);
	if (class_PackageManager == NULL)
		goto end;

	jmethodID method_getApplicationInfo;
	method_getApplicationInfo = env->GetMethodID(class_PackageManager,
			"getApplicationInfo",
			"(Ljava/lang/String;I)Landroid/content/pm/ApplicationInfo;");
	if (method_getApplicationInfo == NULL)
		goto end;

	jobject object_ApplicationInfo;
	object_ApplicationInfo = env->CallObjectMethod(object_PackageManager,
			method_getApplicationInfo, jstr_packageName, 1024);
	if (object_ApplicationInfo == NULL)
		goto end;

	// path = applicationInfo.publicSourceDir;
	jclass class_ApplicationInfo;
	class_ApplicationInfo = env->GetObjectClass(object_ApplicationInfo);
	if (class_ApplicationInfo == NULL)
		goto end;

	jfieldID field_publicSourceDir;
	field_publicSourceDir = env->GetFieldID(class_ApplicationInfo,
			"publicSourceDir", "Ljava/lang/String;");
	if (field_publicSourceDir == NULL)
		goto end;

	jstring jstr_publicSourceDir;
	jstr_publicSourceDir = (jstring) env->GetObjectField(object_ApplicationInfo,
			field_publicSourceDir);
	if (jstr_publicSourceDir == NULL)
		goto end;

	const char* cstr_publicSourceDir;
	cstr_publicSourceDir = env->GetStringUTFChars(jstr_publicSourceDir, 0);
	LOGI("--->publicSourceDir=%s", cstr_publicSourceDir);
	int size;
	size = strlen(cstr_publicSourceDir) + 1;
	mApkPath = (char*) MALLOC(size);
	memset(mApkPath, 0, size);
	strcpy(mApkPath, cstr_publicSourceDir);
	LOGI("--->publicSourceDir=%s", mApkPath);

	// get apk's so paths

	// exception handle
	end: jboolean flag = env->ExceptionCheck();
	if (flag) {
		env->ExceptionClear();
	}
}
Clone this wiki locally