# String assertion in android build

**URL:** <https://forum.juce.com/t/string-assertion-in-android-build/34164>\
**Category:** General JUCE discussion\
**Created:** [June 30, 2019, 4:37pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164 "2019-06-30T16:37:31Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![karbon-l-forms](https://avatars.discourse-cdn.com/v4/letter/k/6a8cbe/32.png) [@karbon-l-forms](https://forum.juce.com/u/karbon-l-forms)\
**Post date:** [June 30, 2019, 4:37pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/1 "2019-06-30T16:37:31Z")

</div>

I’m trying to compile my app with a lower minSdkVersion. (from 24 down to 19, to support an older device).

I got it to build eventually after android studio lost the plot with shedloads of unresolved symbols…

APK installs but app errors out and does not run. So I ran it thru the debugger…

It’s hitting a JUCE String assertion

```
    "If you get an assertion here, then you're trying to create a string from 8-bit data
    that contains values greater than 127. These can NOT be correctly converted to unicode
    because there's no way for the String class to know what encoding was used to
    create them. The source data could be UTF-8, ASCII or one of many local code-pages.

    To get around this problem, you must be more explicit when you pass an ambiguous 8-bit
    string to the String class - so for example if your source data is actually UTF-8,
    you'd call String (CharPointer_UTF8 ("my utf8 string..")), and it would be able to
    correctly convert the multi-byte characters to unicode. It's *highly* recommended that
    you use UTF-8 with escape characters in your source code to represent extended characters,
    because there's no other way to represent these strings in a way that isn't dependent on
    the compiler, source code editor and platform.

    Note that the Projucer has a handy string literal generator utility that will convert
    any unicode string to a valid C++ string literal, creating ascii escape sequences that will
    work in any compiler."

```

Fair enough, but the triggering call is not from my code. It’s from juce\_android\_JNIHelpers.cpp line 109…

```
    String path (cls->getClassPath());

```

If I throw in a cheeky wee cast on that line…

```
 String path ((CharPointer_UTF8)cls->getClassPath());

```

then the assertion is not hit until later on, same JNIHelpers file, line 158…

```
      LocalRef<jstring> classNameAndPackage (javaString (String (classPath).replaceCharacter (L'/', L'.')));

```

Now I don’t feel that following the debugger and casting every string that throws is particularly clever! I don’t want to be messing about with JUCE code. Don’t even know if that cast is correct thing.

Any ideas? (Ideally I’d rather not be targeting sdk 19 / 4.4.1 but I don’t even know if that’s the issue. )

---

<div class="post-metadata">

**Author:** ![Pca](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@Pca](https://forum.juce.com/u/Pca)\
**Post date:** [November 5, 2019, 2:26pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/2 "2019-11-05T14:26:48Z")

</div>

I have exactly the same issue. Did you find a workaround?

[Update]  
I just find out that JUCE\_PUSH\_NOTIFICATIONS\_ACTIVITY is defined to something really weird in CMakeList.txt.

Any idea about possible cause?

_add\_definitions("-DJUCE\_ANDROID=1" “-DJUCE\_ANDROID\_API\_VERSION=16” “-DJUCE\_PUSH\_NOTIFICATIONS=1” “ **-DJUCE\_PUSH\_NOTIFICATIONS\_ACTIVITY=“潣⽭潲楬樯捵⽥畊散捁楴楶祴�ɭ”** ” “-DJUCER\_ANDROIDSTUDIO\_7F0E4A25=1” “-DJUCE\_APP\_VERSION=1.2.054” “-DJUCE\_APP\_VERSION\_HEX=0x10236”)_

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [November 5, 2019, 4:28pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/3 "2019-11-05T16:28:29Z")

</div>

> [@Pca](#):
>
> Any idea about possible cause?

Trying to work it out by going backwards:

- [`JUCE_PUSH_NOTIFICATIONS_ACTIVITY` is set to the output of `getJNIActivityClassName`](https://github.com/WeAreROLI/JUCE/blob/5.4.5/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h#L1445)
- [`getJNIActivityClassName` calls `getActivityClassString`](https://github.com/WeAreROLI/JUCE/blob/5.4.5/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h#L1220)
- `getActivityClassString` returns either
  - [`androidCustomActivityClass.get().toString()`](https://github.com/WeAreROLI/JUCE/blob/5.4.5/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h#L1205)
  - [`"com.roli.juce.JuceActivity"`](https://github.com/WeAreROLI/JUCE/blob/5.4.5/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h#L86)
  - [`"android.app.Activity"`](https://github.com/WeAreROLI/JUCE/blob/5.4.5/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h#L1210)

Do you have anything specified in the “Custom Android Activity” field of the Android exporter in Projucer?

---

<div class="post-metadata">

**Author:** ![Pca](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@Pca](https://forum.juce.com/u/Pca)\
**Post date:** [November 5, 2019, 4:34pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/4 "2019-11-05T16:34:33Z")

</div>

No, I just have the empty default value. But I ran the Producer in Visual Studio to trace the issue, and I discovered that the following line is not working properly (juce\_ProjectExport\_Android.h, line 1145)

defines.set (“JUCE\_PUSH\_NOTIFICATIONS\_ACTIVITY”, String::formatted(""%s"", getJNIActivityClassName().toUTF8()));

getJNIActivityClassName returns the right text, but the result of String::formatted is the chinese character string you can see in my previous message.

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [November 5, 2019, 10:39pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/5 "2019-11-05T22:39:37Z")

</div>

`String::formatted` is quite broken (because not actually cross-platform) and its documentation says not to use it:

> <https://github.com/WeAreROLI/JUCE/blob/5.4.5/modules/juce_core/text/juce_String.h#L894-L906>

@ed95 @jules @t0m Could you please consider (again) eliminating this broken function? Could you at least please make sure that it is not used in Projucer and any other JUCE code that should be cross-platform? Thanks!

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [November 5, 2019, 10:47pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/6 "2019-11-05T22:47:30Z")

</div>

@Pca you can apply the following diff to fix Projucer:

```auto
--- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h
+++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h
@@ -1442,7 +1442,7 @@ private:
         if (arePushNotificationsEnabled())
         {
             defines.set ("JUCE_PUSH_NOTIFICATIONS", "1");
- defines.set ("JUCE_PUSH_NOTIFICATIONS_ACTIVITY", String::formatted("\"%s\"", getJNIActivityClassName().toUTF8()));
+ defines.set ("JUCE_PUSH_NOTIFICATIONS_ACTIVITY", getJNIActivityClassName().quoted());
         }

         if (androidInAppBillingPermission.get())

```

---

<div class="post-metadata">

**Author:** ![Pca](https://avatars.discourse-cdn.com/v4/letter/p/7ba0ec/32.png) [@Pca](https://forum.juce.com/u/Pca)\
**Post date:** [November 5, 2019, 10:58pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/7 "2019-11-05T22:58:34Z")

</div>

Yep I reached the same conclusion when I saw this format string, and already fixed it the same way in my local copy 😃

Thanks! 👍

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 6, 2019, 8:45am UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/8 "2019-11-06T08:45:18Z")

</div>

Ouch! Thanks for the heads-up, yes, we’ll definitely fix that very nasty bit of code!

Like the comment says, I’d love to get rid of that method but last time I tried, the moaning was unbearable!

---

<div class="post-metadata">

**Author:** ![bobbybalow](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@bobbybalow](https://forum.juce.com/u/bobbybalow)\
**Post date:** [November 17, 2019, 9:31pm UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/9 "2019-11-17T21:31:03Z")

</div>

Thank you for sharing this info! I’m very new to JUCE and had a quick question if you have a sec. Where exactly does this bit of code go to fix the bug? Thanks so much in advance-

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [November 18, 2019, 5:32am UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/10 "2019-11-18T05:32:52Z")

</div>

This bit of code goes in `extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h`. Once you’ve made the change, you need to re-build Projucer using one of the available sets of build files under `extras/Projucer/Builds`.

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [November 18, 2019, 6:47am UTC](https://forum.juce.com/t/string-assertion-in-android-build/34164/11 "2019-11-18T06:47:03Z")

</div>

Or just pull the develop branch! This was added in [77b19b9](https://github.com/WeAreROLI/JUCE/commit/74b19b94b0b7f62e9bab8404a6d70aee5e54f4cf).
