在框架中调用自创建的静态方法时出现NoSuchMethodError

时间:2019-08-22 01:38:25

标签: android frameworks robolectric

我正在使用的版本信息:

Android studio 3.4.2
gradle 5.1.1
SDK 28
JAVA 8
JUnit 4.12
robolectric 4.3

我在frameworks / base / core / java / android / app / DownloadManager.java中创建一个新的静态方法“ isDownloadAlertEnabled”。

public static boolean isDownloadAlertEnabled() {
    boolean isEnabled = true;
    String downloadAlert = SystemProperties.get("persist.downloadalert.enabled");
    if (downloadAlert == null || downloadAlert.isEmpty()) {
        String gms = SystemProperties.get("ro.com.google.gmsversion");
        if (gms == null || gms.isEmpty()) {
            isEnabled = true;
        } else {
            isEnabled = false;
        }
    } else {
        isEnabled = downloadAlert.equals("true");
    }
    if (DEBUG) Log.d(TAG, "isDownloadAlertEnabled : " + isEnabled);
    return isEnabled;
}

make framework.jar

我确保在框架/base/api/current.txt和out / target / common / obj / PACKAGING / public_api.txt中已经存在方法“ isDownloadAlertEnabled”

将jar放入app / libs

添加为库

现在我想将新方法称为“ isDownloadAlertEnabled

@RunWith(RobolectricTestRunner.class)
public class test3 {
    @Test
    public void test333() {
        System.out.println("test333 isDownloadAlertEnabled:" + DownloadManager.isDownloadAlertEnabled());
    }
}

但结果失败:运行时出现NoSuchMethodError

[Robolectric] com.android.providers.downloads.test3.test333: sdk=28; resources=BINARY
Called loadFromPath(/system/framework/framework-res.apk, true); mode=binary sdk=28

java.lang.NoSuchMethodError: android.app.DownloadManager.isDownloadAlertEnabled()Z

at com.android.providers.downloads.test3.test333(test3.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

当我使用另一个静态方法“ getRecommendedMaxBytesOverMobile”进行测试时,这是SDK28最初使用的方法。结果就是成功。

为什么不能调用自创建的静态方法,却可以调用原始的SDK静态方法? 我该怎么做才能使自己的方法起作用?

请帮助我。非常感谢!

0 个答案:

没有答案