We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 1815

Summary: SDL_AndroidGetActivity() does not work.
Product: SDL Reporter: Pallav Nawani <pallavnawani>
Component: mainAssignee: Gabriel Jacobo <gabomdq>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: gabomdq, philipp.wiesemann
Version: HG 2.0   
Hardware: All   
OS: Android (All)   

Description Pallav Nawani 2013-04-20 02:19:55 UTC
The function returns a local jobject, which goes out of scope as soon as function returns, thanks to LocalReferenceHolder refs(__FUNCTION__);

This code works:

AAssetManager *IcOSAndroidGetAssetManager()
{

	SDL_Log("%s(): Attempting to obtain JNI pointer", __FUNCTION__);
	JNIEnv *aEnv = (JNIEnv *)SDL_AndroidGetJNIEnv();
	assert(aEnv);

	SDL_Log("%s(): Got JNI", __FUNCTION__);

	jclass aSdlActivityClass = aEnv->FindClass("org/libsdl/app/SDLActivity");
	SDL_Log("%s(): Got SDL Class (%x)", __FUNCTION__, aSdlActivityClass);

    jmethodID aStaticMid = aEnv->GetStaticMethodID(aSdlActivityClass, "getContext", "()Landroid/content/Context;");
    jobject aContext =  aEnv->CallStaticObjectMethod(aSdlActivityClass, aStaticMid);

	//jobject aContext = (jobject)SDL_AndroidGetActivity();
	SDL_Log("%s(): Got Activity (%x)", __FUNCTION__, aContext);

	jclass aSdlClass = aEnv->FindClass("android/content/Context");
	SDL_Log("%s(): Got Class (%x)", __FUNCTION__, aSdlClass);

	jmethodID aJavaMethodID = aEnv->GetMethodID(aSdlClass, "getAssets", "()Landroid/content/res/AssetManager;");
	SDL_Log("%s(): Got Method ID (%x)", __FUNCTION__, aJavaMethodID);
    jobject aJavaAssetManager = aEnv->CallObjectMethod(aContext, aJavaMethodID);
	SDL_Log("%s(): Got Asset Manager (%x)", __FUNCTION__, aJavaAssetManager);

	return AAssetManager_fromJava(aEnv, aJavaAssetManager);
}

Uncomment the commented line to make it fail.
Comment 1 Gabriel Jacobo 2013-04-20 11:20:12 UTC
Good find!
Comment 2 Gabriel Jacobo 2013-04-22 10:21:16 UTC
Fixed in http://hg.libsdl.org/SDL/rev/0cb47cc139d3

You should remember to delete the local reference manually!
Comment 3 Philipp Wiesemann 2013-04-22 15:53:31 UTC
(In reply to comment #2)
> Fixed in http://hg.libsdl.org/SDL/rev/0cb47cc139d3
> 
> You should remember to delete the local reference manually!

Maybe it would be useful to add this warning in the SDL_system.h file because SDL_AndroidGetActivity() is meant for library users and most of them will not look into the implementation itself (the function is not used internally).