diff --git a/modules/juce_core/native/juce_JNIHelpers_android.cpp b/modules/juce_core/native/juce_JNIHelpers_android.cpp index 3b2c2a0b99..810b88ba44 100644 --- a/modules/juce_core/native/juce_JNIHelpers_android.cpp +++ b/modules/juce_core/native/juce_JNIHelpers_android.cpp @@ -377,7 +377,6 @@ void JNIClassBase::initialise (JNIEnv* env, jobject context) if (classRef == nullptr) classRef = GlobalRefImpl { LocalRef { env->FindClass (classPath) } }; - jassert (classRef != nullptr); initialiseFields (env); } } diff --git a/modules/juce_core/native/juce_JNIHelpers_android.h b/modules/juce_core/native/juce_JNIHelpers_android.h index 5735cbbad9..e19d8b7280 100644 --- a/modules/juce_core/native/juce_JNIHelpers_android.h +++ b/modules/juce_core/native/juce_JNIHelpers_android.h @@ -246,7 +246,8 @@ public: JNIClassBase (const char* classPath, int minSDK, const uint8* byteCode, size_t byteCodeSize); virtual ~JNIClassBase(); - operator jclass() const noexcept { return classRef; } + jclass getJclass() const { return classRef; } + operator jclass() const noexcept { return getJclass(); } static void initialiseAllClasses (JNIEnv*, jobject context); static void releaseAllClasses (JNIEnv*); @@ -293,7 +294,7 @@ template constexpr auto numBytes (const T (&) [N]) { retu #define DECLARE_JNI_FIELD(fieldID, stringName, signature) jfieldID fieldID; #define DECLARE_JNI_CALLBACK(fieldID, stringName, signature) -#define DECLARE_JNI_CLASS_WITH_BYTECODE(CppClassName, javaPath, minSDK, byteCodeData) \ +#define DECLARE_OPTIONAL_JNI_CLASS_WITH_BYTECODE(CppClassName, javaPath, minSDK, byteCodeData, allowFailure) \ static_assert (minSDK >= 24, "There's no need to supply a min SDK lower than JUCE's minimum requirement"); \ class CppClassName ## _Class : public JNIClassBase \ { \ @@ -302,6 +303,19 @@ template constexpr auto numBytes (const T (&) [N]) { retu \ void initialiseFields (JNIEnv* env) \ { \ + if constexpr (allowFailure) \ + { \ + if (getJclass() == nullptr) \ + { \ + env->ExceptionClear(); \ + return; \ + } \ + } \ + else \ + { \ + jassert (getJclass() != nullptr); \ + } \ + \ Array callbacks; \ JNI_CLASS_MEMBERS (CREATE_JNI_METHOD, CREATE_JNI_STATICMETHOD, CREATE_JNI_FIELD, CREATE_JNI_STATICFIELD, CREATE_JNI_CALLBACK); \ resolveCallbacks (env, callbacks); \ @@ -311,6 +325,9 @@ template constexpr auto numBytes (const T (&) [N]) { retu }; \ static inline const CppClassName ## _Class CppClassName; +#define DECLARE_JNI_CLASS_WITH_BYTECODE(CppClassName, javaPath, minSDK, byteCodeData) \ + DECLARE_OPTIONAL_JNI_CLASS_WITH_BYTECODE (CppClassName, javaPath, minSDK, byteCodeData, false) \ + //============================================================================== #define DECLARE_JNI_CLASS_WITH_MIN_SDK(CppClassName, javaPath, minSDK) \ DECLARE_JNI_CLASS_WITH_BYTECODE (CppClassName, javaPath, minSDK, nullptr)