如何修补和替换核心android API类?

时间:2018-09-03 13:17:45

标签: java android android-studio selection monkeypatching

让我们说我有一个使用依赖于核心android类的Android Studio项目构建的Android应用程序; android.text.Selection。其中一个功能removeSelection被标记为崩溃的原因。我怀疑这是由于第三方键盘造成的。我想修改该功能以尝试解决此问题,从;更改为;

/**
 * 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);
}

至;

/**
 * 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);
}

我可以得到课程的资料; https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/text/Selection.java。我也知道您可以在Java中修改类路径以使用类的修改版本。 https://www.sourceallies.com/2010/02/replacing-and-patching-java-application-and-core-classes/。该类本身位于主要的android.jar依赖项中。

如何调整此方法,将其包含在项目中,并使我的应用程序更喜欢修改后的版本?

0 个答案:

没有答案