The standard TeX algorithm doesn't penalize the length of the last line, so doesn't solve this. The implementation in Android, based on Minikin[1], applies some penalty to the last line by default, as it's especially unpleasant on mobile. It also has a "balanced" setting[2] which applies the full penalty, as that's often more aesthetically pleasing in UI contexts, though it would not be a good choice for a running text, as it would result in paragraphs of uneven width.
[1]: https://raphlinus.github.io/text/2022/11/08/minikin.html
[2]: https://developer.android.com/reference/android/widget/TextV...