void* new$JNI_OnLoad(JavaVM* vm, void *reserved) { //通过Hook获取到JavaVM,保存JavaVM DEXLOG("new$JNI_OnLoad GVM is get!"); GVM = vm; return $JNI_OnLoad(vm,reserved); }
/********************************* Android MAIN **********************************/ extern void android_main(struct android_app* state) { ///////////////////////////////// // Make sure glue isn't stripped. app_dummy(); //enable COUT overloadSTDCOUT(); ///////////////////////////////// //Init state state->onAppCmd = __android_handle_cmd; state->onInputEvent = __android_handle_input; //save app app_state=state; //attach java thread attachToAndroidThreadJava(); //init sensors initSensors(); createSensorsEventQueue(); addAccelerometer(); //////////////////////////// int ident; int events; struct android_poll_source* source; //waiting to get window while (!getIsAndroidValidDevice()){ while ((ident = ALooper_pollAll(0, NULL, &events,(void**)&source)) >= 0){ if (source != NULL) source->process(state, source); if (state->destroyRequested != 0) return; } usleep( 16 ); } //////////////////////////// //GET APK (ZIP FILE) ANativeActivity* activity = state->activity; jclass clazz = (*getEnv())->GetObjectClass(getEnv(), activity->clazz); jmethodID methodID = (*getEnv())->GetMethodID(getEnv(), clazz, "getPackageCodePath", "()Ljava/lang/String;"); jobject result = (*getEnv())->CallObjectMethod(getEnv(), activity->clazz, methodID); jboolean isCopy; path_apk= (const char*) (*getEnv())->GetStringUTFChars(getEnv(), (jstring)result, &isCopy); //////////////////////////// //INIT openAL/backend JNI_OnLoad(activity->vm,0); //no in OpenSL ES backend //////////////////////// //call main char *argv[2]; argv[0] = strdup("Easy2D"); argv[1] = NULL; int out=main(1, argv); //call atexit callatexitANDROID(); //deattach java thread deattachToAndroidThreadJava(); //disable COUT unoverloadSTDCOUT(); }
jboolean rvmLoadNativeLibrary(Env* env, const char* path, ClassLoader* classLoader) { DynamicLib** nativeLibs = NULL; if (!classLoader || classLoader->parent == NULL) { // This is the bootstrap classloader nativeLibs = &bootNativeLibs; } else if (classLoader->parent->parent == NULL && classLoader->object.clazz->classLoader == NULL) { // This is the system classloader nativeLibs = &mainNativeLibs; } else { // Unknown classloader rvmThrowUnsatisfiedLinkError(env, "Unknown classloader"); return FALSE; } char* errorMsg = NULL; DynamicLib* lib = rvmOpenDynamicLib(env, path, &errorMsg); if (!lib) { if (!rvmExceptionOccurred(env)) { rvmThrowUnsatisfiedLinkError(env, errorMsg); } return FALSE; } obtainNativeLibsLock(); if (rvmHasDynamicLib(env, lib, *nativeLibs)) { // The lib is already in nativeLibs rvmCloseDynamicLib(env, lib); releaseNativeLibsLock(); return TRUE; } jint (*JNI_OnLoad)(JavaVM*, void*) = rvmFindDynamicLibSymbol(env, lib, "JNI_OnLoad", FALSE); if (JNI_OnLoad) { // TODO: Check that JNI_OnLoad returns a supported JNI version? JNI_OnLoad(&env->vm->javaVM, NULL); if (rvmExceptionOccurred(env)) { releaseNativeLibsLock(); return FALSE; } } rvmAddDynamicLib(env, lib, nativeLibs); releaseNativeLibsLock(); return TRUE; }