【问题标题】:Android studio can not find java.math.BigInteger some methods in projectAndroid studio在项目中找不到java.math.BigInteger的一些方法
【发布时间】:2017-07-21 22:00:31
【问题描述】:

我在 Intellij Idea 中使用 kotlin 实现了一个项目。这是没有错误的工作。这是我的 Intellij 项目照片,没有错误

但是当我在 Android Studio 中实现这个项目时,我得到了错误,因为工作室在 BigInteger 类中找不到这个方法

longValueExact()

Android Studio 项目照片:

我的计算机中有一个 JDK (v 1.8)。但在 Itellij 项目中,此方法有效,而在 android studio 中无效。

我在 android studio 中打开 java.math.BigInteger 类并看到 longValueExact() 方法:

kotlin 类:

open class AuthKey(val key: ByteArray) {

constructor(key: ByteBuffer) : this(key.array())

init {
    if (key.size != 256)
        throw RuntimeException("AuthKey must be 256 Bytes found ${key.size} bytes")
}

// see https://core.telegram.org/mtproto/description#key-identifier
// SHA1 generates 20 bytes long hash, authKeyId is the lower 8 bytes
val keyId = CryptoUtils.substring(CryptoUtils.SHA1(key), 12, 8)

fun getKeyIdAsLong() = BigInteger(keyId).longValueExact()
}

class TempAuthKey(key: ByteArray, val expiresAt: Int) : AuthKey(key)

这意味着我们在 java 1.8 中有这个类,我们在 android studio 中添加了这个。和 android studio 用外部库中的所有方法加载这个类。但是我们不能在项目类中使用这些方法!

谁能帮帮我?

【问题讨论】:

  • 请添加代码而不是附加图像。

标签: java-8


【解决方案1】:

Android 有自己的java.whatever 类实现,并且还可以有不同的接口,尤其是在最近的 Java 版本中添加的东西时。在https://developer.android.com/reference/java/math/BigInteger.html 你可以看到即使是最新的 Android API (25) 也没有这个方法。因此,如果您设法编译了程序,它将无法在设备上实际运行。

<1.8> 外部库不用于 Android 项目,如果您查看模块依赖项,您不应该看到它。


更新 - 此方法和其他“精确值”方法已在 API 31 中添加到 Android。

【讨论】:

  • 有什么方法可以添加这些类吗?
  • 我不这么认为。您可以查看 IDEA 中方法的来源,如果可以使用可用的方法(对于 longValueExact 是这样),您可以自己实现等效方法。
  • @AlexeyRomanov 所以即使使用目标级别 8,android 也无法实现所有 java 8 API?
  • @Xan 不,它没有。 Android 中可用的 API 不会也不能依赖于您设置的语言级别,它们依赖于compileSdkVersion。请参阅stackoverflow.com/questions/26694108/… 了解更多信息。
猜你喜欢
  • 2017-02-22
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 1970-01-01
  • 2022-10-24
  • 2015-12-17
  • 1970-01-01
相关资源
最近更新 更多