Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the crash issue on the Android platform caused by an invalid context. #18036

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion native/cocos/platform/android/AndroidPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class GameInputProxy {
}
ABORT_IF(_jniEnv != nullptr)

Paddleboat_init(_jniEnv, platform->_app->activity->javaGameActivity);
Paddleboat_init(_jniEnv, cc::JniHelper::getContext());
Paddleboat_setControllerStatusCallback(gameControllerStatusCallback, this);
// This is needed to allow controller events through to us.
// By default, only touch-screen events are passed through, to match the
Expand Down
8 changes: 4 additions & 4 deletions native/cocos/platform/android/adpf_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool ADPFManager::initializePowerManager() {
#endif

JNIEnv *env = cc::JniHelper::getEnv();
auto *javaGameActivity = cc::JniHelper::getActivity();
auto *applicationContext = cc::JniHelper::getContext();

// Retrieve class information
jclass context = env->FindClass("android/content/Context");
Expand All @@ -117,7 +117,7 @@ bool ADPFManager::initializePowerManager() {
// Get the method 'getSystemService' and call it
jmethodID mid_getss = env->GetMethodID(
context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject obj_power_service = env->CallObjectMethod(javaGameActivity, mid_getss, str_svc);
jobject obj_power_service = env->CallObjectMethod(applicationContext, mid_getss, str_svc);

// Add global reference to the power service object.
obj_power_service_ = env->NewGlobalRef(obj_power_service);
Expand Down Expand Up @@ -189,7 +189,7 @@ bool ADPFManager::initializePerformanceHintManager() {
return true;
#else
JNIEnv *env = cc::JniHelper::getEnv();
auto *javaGameActivity = cc::JniHelper::getActivity();
auto *applicationContext = cc::JniHelper::getContext();

// Retrieve class information
jclass context = env->FindClass("android/content/Context");
Expand All @@ -203,7 +203,7 @@ bool ADPFManager::initializePerformanceHintManager() {
jmethodID mid_getss = env->GetMethodID(
context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;");
jobject obj_perfhint_service = env->CallObjectMethod(
javaGameActivity, mid_getss, str_svc);
applicationContext, mid_getss, str_svc);

// Add global reference to the power service object.
obj_perfhint_service_ = env->NewGlobalRef(obj_perfhint_service);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package com.cocos.lib;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -58,19 +59,19 @@ public class CocosActivity extends GameActivity {



private native void onCreateNative();
private native void onCreateNative(Context applicationContext);

@Override
protected void onCreate(Bundle savedInstanceState) {
onLoadNativeLibraries();
onCreateNative();
onCreateNative(this.getApplicationContext());

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);

// GlobalObject.init should be initialized at first.
GlobalObject.init(this, this);
GlobalObject.init(this.getApplicationContext(), this);

CocosHelper.registerBatteryLevelReceiver(this);
CocosHelper.init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,11 @@ public void run() {

host = domain.startsWith("www.") ? domain.substring(4) : domain;
if (fileLen > 0) {
SharedPreferences sharedPreferences = GlobalObject.getContext().getSharedPreferences("breakpointDownloadSupport", Context.MODE_PRIVATE);
Context ctx = GlobalObject.getContext();
if (ctx == null) {
return;
}
SharedPreferences sharedPreferences = ctx.getSharedPreferences("breakpointDownloadSupport", Context.MODE_PRIVATE);
if (sharedPreferences.contains(host) && sharedPreferences.getBoolean(host, false)) {
downloadStart = fileLen;
} else {
Expand Down Expand Up @@ -246,6 +250,9 @@ public void onResponse(Call call, Response response) throws IOException {

// save breakpointDownloadSupport Data to SharedPreferences storage
Context context = GlobalObject.getContext();
if (context == null) {
return;
}
SharedPreferences sharedPreferences = context.getSharedPreferences("breakpointDownloadSupport", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
long total = response.body().contentLength() + downloadStart;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public static int createWebView() {
GlobalObject.runOnUiThread(new Runnable() {
@Override
public void run() {
CocosWebView webView = new CocosWebView(GlobalObject.getContext(), index);
CocosWebView webView = new CocosWebView(GlobalObject.getActivity(), index);
FrameLayout.LayoutParams lParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
Expand Down
5 changes: 3 additions & 2 deletions native/cocos/platform/android/jni/JniCocosEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ void android_main(struct android_app *app) {
}

//NOLINTNEXTLINE
JNIEXPORT void JNICALL Java_com_cocos_lib_CocosActivity_onCreateNative(JNIEnv *env, jobject activity) {
cc::JniHelper::init(env, activity);
JNIEXPORT void JNICALL Java_com_cocos_lib_CocosActivity_onCreateNative(JNIEnv *env, jobject activity, jobject context) {
cc::JniHelper::init(env, context);
cc::JniHelper::setActivity(activity);
}
}
21 changes: 17 additions & 4 deletions native/cocos/platform/java/jni/JniHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jmethodID JniHelper::loadclassMethodMethodId = nullptr;
jobject JniHelper::classloader = nullptr;
std::function<void()> JniHelper::classloaderCallback = nullptr;
jobject JniHelper::sContext = nullptr;
jobject JniHelper::sActivity = nullptr;
JavaVM *JniHelper::sJavaVM = nullptr;

JavaVM *JniHelper::getJavaVM() {
Expand All @@ -139,9 +140,13 @@ void JniHelper::init(JNIEnv *env, jobject context) {

void JniHelper::onDestroy() {
if (JniHelper::sJavaVM) {
if (JniHelper::sContext) {
cc::JniHelper::getEnv()->DeleteGlobalRef(JniHelper::sContext);
JniHelper::sContext = nullptr;
if (sContext) {
cc::JniHelper::getEnv()->DeleteGlobalRef(sContext);
sContext = nullptr;
}
if (sActivity) {
cc::JniHelper::getEnv()->DeleteGlobalRef(sActivity);
sActivity = nullptr;
}
LOGD("JniHelper::onDestroy");
}
Expand Down Expand Up @@ -199,7 +204,15 @@ jobject JniHelper::getContext() {
jobject JniHelper::getActivity() {
// TODO(cjh): In normal mode, sContext is Activity itself, but in surface-less mode, we need to
// returns nullptr.
return sContext;
return sActivity;
}

void JniHelper::setActivity(jobject activity) {
if (sActivity) {
cc::JniHelper::getEnv()->DeleteGlobalRef(sActivity);
sActivity = nullptr;
}
sActivity = cc::JniHelper::getEnv()->NewGlobalRef(activity);
}

#if CC_PLATFORM == CC_PLATFORM_OHOS
Expand Down
2 changes: 2 additions & 0 deletions native/cocos/platform/java/jni/JniHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class CC_DLL JniHelper {
static JavaVM *getJavaVM();
static JNIEnv *getEnv();
static jobject getActivity();
static void setActivity(jobject activity);
static jobject getContext();

static void init(JNIEnv *env, jobject context);
Expand Down Expand Up @@ -394,6 +395,7 @@ class CC_DLL JniHelper {

private:
static jobject sContext;
static jobject sActivity;
static JavaVM *sJavaVM;

static JNIEnv *cacheEnv();
Expand Down
Loading