Android 应用无法安装

Android app cannot install

在 AIDE 中为 Android 开发应用程序时,我遇到了这个错误。该应用程序将成功编译但不会安装,报告此错误:

Could not run the App directly as root. Consider disabling direct running in the settings.

WARNING: linker: app_process has text relocations. This is wasting memory and is a security risk. Please fix.
pkg: /storage/sdcard/AppProjects/MyProgram/bin/MyProgram.apk
Failure [INSTALL_FAILED_DEXOPT]
exit with 0

我研究了可能导致此问题的原因,主要遇到了 "certificate error, try resigning the package" 和 "setting a permission twice in the manifest" 等原因,其中 none 有效。

我找到问题所在了。它在一些看起来非常像这样的代码中:

public class Builder<T extends Base> {
    private final List<Def1> subDefs1 = new ArrayList<>();
    private final List<Def2> subDefs2 = new ArrayList<>();

    public Builder<T> add(final Collection<Def1> ds) {
        subDefs1.addAll(ds);
        return this;
    }

    public Builder<T> add(final Collection<Def2> ds) {
        subDefs2.addAll(ds);
        return this;
    }
}

interface Base {}

final class Def1 implements Base {}

final class Def2 implements Base {}

我有这些 add 方法,它们都采用某种 Collection。这个问题一定与 Java 乏善可陈的泛型和 dexing 过程有关,我猜...

您的问题:Java认为您定义了两个具有相同签名的方法。

Java 方法签名定义:https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

method declarations have six components, in order:

1.Modifiers—such as public, private, and others you will learn about later.

2.The return type—the data type of the value returned by the method, or void if the method does not return a value.

3.The method name—the rules for field names apply to method names as well, but the convention is a little different.

4.The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.

  1. An exception list—to be discussed later.
  2. The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

正如您在上面看到的,泛型 类 的规范不是 java 方法签名的一部分。因此 java 检测到两个具有相同签名的添加方法。