ndk 怎么寻找不是根目录下的c文件
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
准确来说NDK搞的C/C++代码也并非纯纯的C/C++代码,即使现在NDK的库已经支持可以一句JAVA也不写就能跑起来,这些程序还是受 JVM的管理的,所以也不用太在意什么C调JAVA的事情了。
关键性的代码贴上分享下:
void
android_main(struct android_app* state)
{
struct stOsContextEngine engine;
// Make sure glue isn't stripped.
app_dummy();
memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;
state->onInputEvent = engine_handle_input;
engine.m_app = state;
// Prepare to monitor accelerometer
engine.m_sensorManager = ASensorManager_getInstance();
engine.m_accelerometerSensor = ASensorManager_getDefaultSensor(
engine.m_sensorManager, ASENSOR_TYPE_ACCELEROMETER);
engine.m_sensorEventQueue = ASensorManager_createEventQueue(
engine.m_sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);
// 获取AAssetManager,这个类其实是调用JAVA的类,这里通过NDK入口的传入参数获取。作用是管理资源文件的
AAssetManager* pAssetManager = state->activity->assetManager;
HResult hres = CPeckerFileIOBase::SetPrivateAPKManager(pAssetManager);
__android_log_print(LOGINFO_INFO, "android_main", "CPeckerFileIOBase::SetPrivateAPKManager result = %d",hres);
// 获取ANativeActivity,这个类其实也是调用JAVA的类,这里通过NDK入口的传入参数获取。作用是窗口Activity的
ANativeActivity* pActivity = state->activity;
// 通过Activity里面getPackageCodePath的方法获取程序运行路径,通过JNI用C++搞JAVA
JNIEnv* env = pActivity->env;
jclass clazz = env->GetObjectClass(pActivity->clazz);
jmethodID methodID = env->GetMethodID(clazz, "getPackageCodePath", "()Ljava/lang/String;");
jobject result = env->CallObjectMethod(pActivity->clazz, methodID);
ConstStringChars strPath;
jboolean isCopy;
strPath.m_pStrBuf = env->GetStringUTFChars((jstring)result, &isCopy);
strPath.m_nStrLen = env->GetStringLength((jstring)result);
__android_log_print(LOGINFO_INFO, "android_main", "Code Path =%d %s",strPath.m_nStrLen,strPath.m_pStrBuf);
CPeckerFileIOBase::SetInstallFilePath(strPath);
// 记得搞完后要清理下哦
env->ReleaseStringUTFChars((jstring)result,strPath.m_pStrBuf);
关键性的代码贴上分享下:
void
android_main(struct android_app* state)
{
struct stOsContextEngine engine;
// Make sure glue isn't stripped.
app_dummy();
memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;
state->onInputEvent = engine_handle_input;
engine.m_app = state;
// Prepare to monitor accelerometer
engine.m_sensorManager = ASensorManager_getInstance();
engine.m_accelerometerSensor = ASensorManager_getDefaultSensor(
engine.m_sensorManager, ASENSOR_TYPE_ACCELEROMETER);
engine.m_sensorEventQueue = ASensorManager_createEventQueue(
engine.m_sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);
// 获取AAssetManager,这个类其实是调用JAVA的类,这里通过NDK入口的传入参数获取。作用是管理资源文件的
AAssetManager* pAssetManager = state->activity->assetManager;
HResult hres = CPeckerFileIOBase::SetPrivateAPKManager(pAssetManager);
__android_log_print(LOGINFO_INFO, "android_main", "CPeckerFileIOBase::SetPrivateAPKManager result = %d",hres);
// 获取ANativeActivity,这个类其实也是调用JAVA的类,这里通过NDK入口的传入参数获取。作用是窗口Activity的
ANativeActivity* pActivity = state->activity;
// 通过Activity里面getPackageCodePath的方法获取程序运行路径,通过JNI用C++搞JAVA
JNIEnv* env = pActivity->env;
jclass clazz = env->GetObjectClass(pActivity->clazz);
jmethodID methodID = env->GetMethodID(clazz, "getPackageCodePath", "()Ljava/lang/String;");
jobject result = env->CallObjectMethod(pActivity->clazz, methodID);
ConstStringChars strPath;
jboolean isCopy;
strPath.m_pStrBuf = env->GetStringUTFChars((jstring)result, &isCopy);
strPath.m_nStrLen = env->GetStringLength((jstring)result);
__android_log_print(LOGINFO_INFO, "android_main", "Code Path =%d %s",strPath.m_nStrLen,strPath.m_pStrBuf);
CPeckerFileIOBase::SetInstallFilePath(strPath);
// 记得搞完后要清理下哦
env->ReleaseStringUTFChars((jstring)result,strPath.m_pStrBuf);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询