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?