【问题标题】:compileOnly two similar libraries to produce one APKcompileOnly 两个相似的库来生成一个 APK
【发布时间】:2019-07-09 06:55:14
【问题描述】:

我有两个罐子,left.jarright.jar。它们相似但不相同。

left.jar:

package com.mydomain.config

public class PkgConfig {
    public static int getID() {
        throw new RuntimeException();
    }

    public static String getLeftValue() {
        throw new RuntimeException();
    }

    // the rest of the methods are the same with right.jar
}

right.jar:

package com.mydomain.config

public class PkgConfig {
    public static int getID() {
        throw new RuntimeException();
    }

    public static String getRightValue() {
        throw new RuntimeException();
    }

    // the rest of the methods are the same with left.jar
}

在我的活动中,我这样做:

public void myMethod() {
    if(com.mydomain.config.PkgConfig.getID() > 1000) {
        mDeviceValue = com.mydomain.config.PkgConfig.getLeftValue();
    }
    else {
        mDeviceValue = com.mydomain.config.PkgConfig.getRightValue();
    }
}

这就是我声明依赖项的方式:

compileOnly files('libs/left.jar')
compileOnly files('libs/right.jar')

这个设置给我一个错误提示

错误:找不到符号方法getRightValue()

我的目标是从中创建一个 APK。我知道我可以使用sourcesets 并这样做:

leftCompileOnly files('libs/left.jar')
rightCompileOnly files('libs/right.jar') 

但它会创建两个 APK。有没有办法解决这个问题?

【问题讨论】:

  • 你检查过这个SO Question吗?一些答案建议编写自定义类加载器。

标签: java android gradle


【解决方案1】:

在同一个 APK 中不可能有两个具有相同完全限定名称的类。所以,要么:

  • 将两个 JAR 替换为一个 JAR 和一个类,或者

  • 重命名其中一个类,或

  • 将其中一个类移动到不同的包中

【讨论】:

  • 顺便说一下@CommonsWare,我使用的是compileOnly,因为实际的类在运行时由加载apk的特定设备加载。我有左设备和右设备。 left.jar 用于左设备,而 right.jar 用于右设备。你坚持你的答案吗?
  • @user1506104:您可能有一个存根PkgConfig 用于编译,它同时具有左右方法。这基本上就是android.jar 文件的工作方式。然后希望在运行时,ART(或 Dalvik)将能够匹配实际实现中存在的方法,只要您不对不存在的方法进行编码(例如,尝试在正确的设备)。显然,这适用于 Android SDK,但我不知道在特定于 Android SDK 的构建过程中可能有多少魔法不适用于您自己的代码。
猜你喜欢
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 1970-01-01
  • 2020-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多