Skip to content

Commit

Permalink
Fix the crash issue on the Android platform caused by an invalid cont…
Browse files Browse the repository at this point in the history
…ext.
  • Loading branch information
bofeng-song committed Dec 13, 2024
1 parent a70ecb1 commit 2f049c0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 13 deletions.
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
4 changes: 2 additions & 2 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 *javaGameActivity = cc::JniHelper::getContext();

// Retrieve class information
jclass context = env->FindClass("android/content/Context");
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 *javaGameActivity = cc::JniHelper::getContext();

// Retrieve class information
jclass context = env->FindClass("android/content/Context");
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);
}
}
19 changes: 16 additions & 3 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 @@ -202,6 +207,14 @@ jobject JniHelper::getActivity() {
return sContext;
}

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
bool JniHelper::setClassLoaderFrom(jobject contextInstance) {
if (!JniHelper::classloader) {
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

0 comments on commit 2f049c0

Please sign in to comment.