0

我使用的版本信息:</p>

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

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

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;
}

制作framework.jar

我已确保 frameworks/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静态方法?我该怎么做才能使自己的方法起作用?

请帮我。非常感谢!

4

0 回答 0