简体   繁体   English

使用JDK中的类而不是android SDK(ADK)

[英]Use class from the JDK instead of the android SDK (ADK)

Is there a possibility to use a class from the JDK (1.7) instead of the Android SDK (ADK, API 23)? 是否有可能使用JDK(1.7)中的类而不是Android SDK(ADK,API 23)?

A class I need to work with exists in the Android SDK (API 23) and in the JDK (1.7). 我需要使用的类存在于Android SDK(API 23) JDK(1.7)中。 By default the class from the ADK is used. 默认情况下,使用ADK中的类。 However I want to used the class from the JDK. 但是我想使用JDK中的类。

In my case I want to use the DatatypeFactory.newInstance(factoryClassName, classLoader) which is placed in the package javax.xml.datatype in the JDK and the ADK. 在我的情况下,我想使用DatatypeFactory.newInstance(factoryClassName, classLoader) ,它放在JDK和ADK中的包javax.xml.datatype中。 Due to differences in the implementation i would like to use the class from the JDK instead of the ADK. 由于实现上的差异,我想使用JDK中的类而不是ADK。

How can I use the class from the JDK instead of the class form the ADK? 如何使用JDK中的类而不是ADK中的类?

You cannot do that. 你不能这样做。 Not by simply switching the import statements anyway. 不过只是简单地切换import语句。 Your code running on Android as its destination platform will not have access to Java SDK classes, therefore the code will fail to load the classes or it will load the Android version of the class with the exact same fully qualified name (which is your case). 您在Android上作为其目标平台运行的代码将无法访问Java SDK类,因此代码将无法加载类,或者它将使用完全相同的完全限定名称加载类的Android版本(这是您的情况) 。

If you want to use that specific implementation from Java SDK, you will have to make it available at the destination platform. 如果要使用Java SDK中的特定实现,则必须在目标平台上使用它。

You have 2 options, but neither is good: 你有两个选择,但两者都不好:

Use Java SDK as library 使用Java SDK作为库

By bringing rt.jar (~50MB) into your project. rt.jar (~50MB)带入您的项目。 However that would create many potential problems, as Android SDK and Java SDK mirror some packages and classes and you could end up loading the wrong versions. 然而,这会产生许多潜在的问题,因为Android SDK和Java SDK镜像了一些包和类,最终可能会加载错误的版本。

or 要么

Bring the source in 带来源

Second option - only slightly better - is to bring the source of that class into your project, only if all its dependencies can be met by Android SDK, or could be brought in as source too. 第二个选项 - 只是略好一点 - 是将该类的源代码引入您的项目,只有当Android SDK可以满足其所有依赖项时,或者也可以将其作为源代码引入。 This would be painful... 这会很痛苦......

The correct solution however is to 然而,正确的解决方案是

Embrace the ecosystem 拥抱生态系统

I don't know what are your specific requirements, but I would wager that Android's implementation of this class is quite close to Java one. 我不知道你的具体要求是什么,但我敢打赌Android的这个类的实现非常接近Java。 If you cannot achieve something by using Android SDK, try asking about that specific problem here, or search for 3rd party libraries which could achieve, what you need. 如果您无法通过Android SDK实现某些目标,请尝试在此处询问该特定问题,或者搜索可以实现您需要的第三方库。 Perhaps the solution is easier than you think. 也许解决方案比你想象的要容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM