2

Lets say I have an Android application built using an Android Studio project that relies on a core android class; android.text.Selection. One of the functions, removeSelection, is flagged up as a cause for crashes. I suspect that is due to 3rd party keyboards. I would like to modify that function to try and work around the issue, changing from;

/**
 * Remove the selection or cursor, if any, from the text.
 */
public static final void removeSelection(Spannable text) {
    text.removeSpan(SELECTION_START, Spanned.SPAN_INTERMEDIATE);
    text.removeSpan(SELECTION_END);
    removeMemory(text);
}

to;

/**
 * Remove the selection or cursor, if any, from the text.
 */
public static final void removeSelection(Spannable text) {
    Log.i("Test", "Method replaced.");
    setSelection(text, 0, 0);
}

I can get the source for the class; https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/text/Selection.java. I also know that you can modify the class path in Java to use a modified version of a class; https://www.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/. The class itself is located in the main android.jar dependencies.

How can I tweak this method, include it in my project and make my application prefer the modified version?

JimmyDeemo
  • 313
  • 1
  • 15
  • I think what I want is to Monkey Patch the `android.jar` file; https://stackoverflow.com/a/42141003/807965. I'm not sure how to slot a `.jar` in before that. – JimmyDeemo Sep 03 '18 at 13:36
  • I'm looking into this possible solution; https://stackoverflow.com/a/41767723/807965. However, this comment is discouraging; https://stackoverflow.com/questions/46314161/use-custom-android-bluetooth-instead-of-one-present-in-default-sdk-android-jar#comment79590912_46314161. – JimmyDeemo Sep 04 '18 at 12:57

0 Answers0