使用C ++和NativeActivity的类中获得一个Android APK的名字 [英] Obtaining the name of an Android APK using C++ and the NativeActivity class

查看:459
本文介绍了使用C ++和NativeActivity的类中获得一个Android APK的名字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写使用NDK和NativeActivity的Andr​​oid应用程序。我的应用程序依赖于对于作为资产的第三方code几位。目前,我正在试图提取这些资产,同时保持文件夹结构不变。

I'm writing an Android app using the NDK and NativeActivity. My app depends on a few bits of third party code that are shipped as assets. Currently I'm working on trying to extract those assets while keeping the folder structure intact.

我已经使用AssetManager尝试,但要保持文件夹结构不变,似乎有这样将大量的code参与,对于一个简单的任务,如我所提到的。因为我已经转而将目光投向尝试实施治疗APK为ZIP文件,并提取其内容的方式。但是,这需要我找到了APK的确切路径。

I've tried using the AssetManager, but to keep the folder structure intact it seemed like there would a huge amount of code involved, for a simple task such as what I've mentioned. I've since switched focus to try to implement treating the APK as a ZIP file and extract its contents that way. But that requires I find the exact path to the APK.

在一个正常的Andr​​oid应用中,应当使用getPackage codePATH,但这是附接到上下文类的抽象方法。我的问题是如何在不使用正常的活动我得到的APK的确切路径?

In a normal Android app one would use getPackageCodePath, but this is an abstract method attached to the Context class. My question is how do I obtain the exact path to the APK when not using a normal Activity?

我也打过电话通过JNI getPackage codePATH,但坠毁的帐户不能够找对方法的应用程序。

Also I tried calling getPackageCodePath via JNI, but that crashed the app on account of not being able to find the method.

编辑: 这甚至可能?

推荐答案

实际上,我是能够调用 getPackage codePATH 通过JNI,并得到它的工作。下面code置于 android_main 的NDK R7天然活性样品中的顶级记录正确的路径和不崩溃:

I was actually able to call getPackageCodePath via JNI and get it to work. The following code put at the top of android_main in the native-activity sample in NDK r7 logs the correct path and doesn't crash:

void android_main(struct android_app* state) {
    struct engine engine;

    ANativeActivity* activity = state->activity;
    JNIEnv* env = activity->env;

    jclass clazz = (*env)->GetObjectClass(env, activity->clazz);
    jmethodID methodID = (*env)->GetMethodID(env, clazz, "getPackageCodePath", "()Ljava/lang/String;");
    jobject result = (*env)->CallObjectMethod(env, activity->clazz, methodID);

    const char* str;
    jboolean isCopy;
    str = (*env)->GetStringUTFChars(env, (jstring)result, &isCopy);
    LOGI("Looked up package code path: %s", str);

    ...
}

我觉得这可能不是一个很好的解决方案,但。有两件事情令我担心:

I feel like this might not be a great solution, though. There are two things that worry me:

  1. 线程安全性 - 有大约只能使用 ENV ANativeActivity 成员在主Java线程一个丑陋的警告,如果我理解正确的事情,这code是会得到在当地活动的线程中运行。
  2. ANativeActivity clazz所成员似乎名不副实,实际上在Java <$ C $的实例C> NativeActivity的而不是类对象。否则,此code是行不通的。我真的很讨厌依靠的东西,显然是名不副实的这个样子。
  1. Thread safety - there's an ugly warning about only using the env member of ANativeActivity in the main Java thread, and if I understand things correctly, this code is going to get run in the native activity's thread.
  2. ANativeActivity's clazz member appears to be misnamed and is actually the instance of the Java NativeActivity instead of the class object. Otherwise this code wouldn't work. I really hate relying on something that is obviously misnamed like this.

除此之外,它的工作原理,我实际上是关于使用它自己来尝试提取出资产使用libzip的.apk文件,进入数据目录。

Aside from that, it works, and I'm actually about to use it myself to try to extract the assets out of the .apk using libzip and into the data directory.

这篇关于使用C ++和NativeActivity的类中获得一个Android APK的名字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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