添加在Android中缺失的标准Java类。

huangapple go评论70阅读模式
英文:

Adding standard java classes that are missing in Android

问题

我正在使用一个Java安全提供程序,该提供程序会从javax.security.auth.login包中引发FailedLoginException异常。
由于我在我的Android应用程序中使用了这些提供程序,我遇到了一个问题,即Android的javax.security.auth.login包没有这样的类,它只有LoginException,该类是其他异常的基类。因此,每当抛出FailedLoginException异常时,我的应用程序都会抛出NoClassDefFound错误。

是否有一种方法可以将这个缺失的标准Java类添加到我的Android应用程序中,或者绕过这个错误?
对于任何帮助,我都表示感谢!

英文:

I am using a Java Security Provider which throws FailedLoginException from the javax.security.auth.login package.
As i am using these provider in my Android App i get the problem that Androids javax.security.auth.login package doesn't have such class, it only has the LoginException, which is the base class for the other ones. Because of that my application is throwing NoClassDefFound whenever the FailedLoginException is thrown.

Is there a way to add this missing standard Java class to my Android App, or to workaround that Error?

Any help is appreciated!

答案1

得分: 1

> 有办法将这个缺失的标准Java类添加到我的Android应用中吗,或者有方法绕过这个错误?

据我所知,答案是否定的。

我怀疑我们能给你的最好建议是:

> 不要用这种方式进行用户身份验证!

Android不支持所有 javax.security.**,特别是它不支持JAAS身份验证和授权框架,这似乎是你正在尝试使用的东西。

参考:

虽然从理论上讲可能(可能)将JAAS移植到Android,但我没有找到任何证据表明有人在这样做方面取得了成功。

英文:

> Is there a way to add this missing standard Java class to my Android App, or to workaround that Error?

AFAIK, No and No.

I suspect that the best advice we can give you is:

> Don't do your user authentication this way!

Android does not support all of javax.security.**, and in particular it doesn't support the JAAS authentication and authorization framework, which is what you are apparently trying to use.

See:

While it might be possible (in theory) to port JAAS to Android, I haven't found any evidence that anyone has succeeded in doing this.

答案2

得分: 0

NoClassDefFoundError在Java中与ClassNotFoundException不同,它是在Java虚拟机在运行时无法找到编译时可用的特定类时发生的。参见:3种解决Java中java.lang.NoClassDefFoundError的方法

在Android Studio项目资源管理器中检查您的rt.jarExternal Libraries -> <1.8> -> rt.jar -> javax -> security -> auth-> login ->。您可以看到存在FailedLoginException

现在检查 External Libraries -> <Android API xx Platform> -> android.jar -> javax -> security -> auth-> login ->。您会发现FailedLoginException不存在。

这意味着您的库是通过rt.jar进行编译的,现在与android.jar一起使用。另一方面,您不能将rt.jar与Android应用程序一起使用(并且在某种程度上与android.jar重叠)。因为它是桌面JRE的运行时库,不适用于Android。参见:Android Studio - android.jar和rt.jar冲突

现在,为了解决您的问题,您可以获取外部库的源代码并进行重写。参见:如何在Android应用程序中使用来自rt.jar的类?

<s>我建议自己实现该类并添加一些调试功能,以避免其他依赖类的实现。</s>

另一种不推荐的方法是从这里下载一个jar包并将其添加到您的项目中。

英文:

NoClassDefFoundError in Java is different from ClassNotFoundException and comes when Java Virtual Machine is not able to find a particular class at runtime which was available at compile time. See: 3 ways to solve java.lang.NoClassDefFoundError in Java J2EE

Check rt.jar in your Android studio project explorer : External Libraries -> <1.8> -> rt.jar -> javax -> security -> auth-> login -> . You can see FailedLoginException exist.

Now check External Libraries -> <Android API xx Platform> -> android.jar -> javax -> security -> auth-> login -> . You can see FailedLoginException Not exist.

It means your library compiled by rt.jar and now used with android.jar. In other side,
you cannot use rt.jar with an Android application (and somewhat overlaps with android.jar). because it is the runtime library for the desktop JRE, not Android. See : Android Studio - android.jar and rt.jar conflicts

Now to solve your problem you can Get the source code to the external library and rewrite it. See How to use classes from rt.jar in an android application?

<s>I suggest to implement that class yourself with some debug function to avoid other dependent class implementation.</s>

Another way which is not recommended is downloading a jar from here and adding it to your project.

答案3

得分: -1

当开发新的Android操作系统时,会附带一系列的Java类,这些类为需要这些类来正常运行的应用程序提供支持。让我们以Time API为例。它是在Android API级别26中添加的,因此使用此库的应用程序将在较低的API级别(如Marshmallow,API 23)上崩溃。

这就是混淆(desugaring)发挥作用的时候。它允许较低的API级别使用新的Java库。我们将看到它是如何实现的,但在此之前,让我们启动Android Studio进行快速演示,以使事情更加清晰。

要解决这个问题,我们需要按照以下步骤在项目中启用混淆。

  1. 将Android Gradle插件设置为4.0或更高版本。
  2. 在编译选项中,启用coreLibraryDesugaringEnabled标志,并将Java目标和源兼容性设置为Java 8。
  3. 在编译选项中,启用coreLibraryDesugaringEnabled标志,并将Java目标和源兼容性设置为Java 8。
  4. 启用multidex(适用于支持API级别20或更低的项目)。
  5. 添加混淆依赖。
<!-- 开始代码片段 -->
<dependency>
    coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.5'
</dependency>
<!-- 结束代码片段 -->
英文:

When any new Android OS is developed, it is shipped with a bunch of Java classes that provide support to the apps requiring those classes to function. Let’s consider the Time API in this case. It was added to the Android API level 26 and hence, the apps using this library will crash on lower API levels such as Marshmallow (API 23).

Here’s when desugaring comes into the picture. It allows lower API levels to work with new Java libraries. We’ll see how it does the trick but before that, let’s fire up Android Studio for a quick demo to make things more clear.

To fix this, we need to enable desugaring in our project using the following steps.

  1. Set Android gradle plugin to 4.0 or higher.
  2. In compile options, enable coreLibraryDesugaringEnabled flag and set Java target & source compatibilities to Java 8.
  3. In compile options, enable coreLibraryDesugaringEnabled flag and set Java target & source compatibilities to Java 8.
  4. Enable multidex (for projects supporting API level 20 or below)
  5. Add the desugaring dependency.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-xhtml -->

   coreLibraryDesugaring &#39;com.android.tools:desugar_jdk_libs:1.0.5&#39;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年8月19日 21:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63488098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定