简体   繁体   English

如何找出可在大量Android手机上安全使用的Java最低版本?

[英]How can I find out the minimum version of Java that is safe to use across a large number of Android phones?

I recently found out at work we're limiting our Java target to 1.1, because there are supposedly still Android phones that only support up to that version. 我最近发现在工作中,我们将Java目标限制为1.1,因为据说仍然有Android手机仅支持该版本。 I've been trying to Google which versions of Java are installed on the different version of Android (unless they do get updated, then what is the minimum version that's included?), and have had no such luck in my search. 我一直在尝试Google在不同版本的Android上安装了哪些Java版本(除非它们得到更新,然后包括的最低版本是多少?),并且在搜索中没有这样的运气。

So, I'm trying to find out what is realistically the best minimum target for Java on Android. 因此,我试图找出实际上是Android上Java的最佳最低目标的方法。 Know of any good sources detailing the versions of Java used across Android devices? 是否知道详细说明跨Android设备使用的Java版本的任何好的资源?

[Edit] For clarification, we're using the Java SDK to build a jar, then packaging it into the apk. [编辑]为了澄清起见,我们使用Java SDK来构建一个jar,然后将其包装到apk中。 So I do need to know a specific Java SDK target, not the Android API level. 因此,我确实需要了解特定的Java SDK目标,而不是Android API级别。

You can support all major versions of Android (2.1 and above, possibly even lower) with Java 1.6. 您可以使用Java 1.6支持所有主要版本的Android(2.1及更高版本,甚至可能更低)。

Infact, as long as you compile with anything but 1.7 you'll be fine, though compiling with 1.5 and below gives a bunch of errors with @Override annotations. 实际上,只要您使用1.7以外的任何东西进行编译,就可以了,尽管使用1.5及以下版本进行编译会产生带有@Override批注的错误。 1.6 is your safest bet. 1.6是您最安全的选择。 I've used it in all my apps so far, and haven't found a single device that was incompatible because of my Java version. 到目前为止,我已经在所有应用程序中使用了它,但是还没有找到因为我的Java版本而不兼容的单个设备。

First don't confuse Java with Android. 首先不要将Java与Android混淆。 The Java-RE and the Android system itself are different. Java-RE和Android系统本身是不同的。

If you talk about Android versions, you should support API level 8 at a minimum and target API level 17. That should give your access to 90% and more of all Android devices. 如果您谈论的是Android版本,则应至少支持API级别8和目标API级别17。这应该使您可以访问90%或更多的所有Android设备。

The rest Raghav already wrote. 其余的拉格夫已经写过了。

For Java as a compiler itself: You can use JavaSDK 7 without any issue but you shouldn't use a compile setting above 1.6. 对于Java作为本身的编译器:可以毫无问题地使用JavaSDK 7,但不应使用1.6以上的编译设置。 Using 1.1 is way outdated! 使用1.1已经过时了!

Update: It's nowadays Java 7 without try-with-resource and without some newer methods & classes. 更新:如今的Java 7没有try-with-resource,也没有一些较新的方法和类。 Your IDE will complain if you use something you shouldn't. 如果您不应该使用某些东西,您的IDE将会抱怨。

The Android tools know Java 7 and are capable of transforming it into Android bytecode that runs on old devices. Android工具了解Java 7,并且能够将其转换为可在旧设备上运行的Android字节码。

One can also use the full set of Java 7 features (ie including try-with-resources) for apps targeting Android 4.4 and up. 对于面向Android 4.4及更高版本的应用程序,也可以使用全套Java 7功能(即,包括尝试资源)。


Downloading and Building Android itself requires 下载和构建 Android本身需要

JDK 6 if you wish to build Gingerbread or newer; 如果您想构建Gingerbread或更高版本,请使用JDK 6; JDK 5 for Froyo or older. 适用于Froyo或更旧版本的JDK 5。

Building older versions with JDK 6 works in my experience since the class format generated by the Java compiler is still compatible with the class format of JDK 5. 根据我的经验,使用JDK 6构建较早版本是可行的,因为Java编译器生成的类格式仍然与JDK 5的类格式兼容。

The same applies to Apps. 同样适用于Apps。 Android's internal Java implementation is not the same as the JDK so it has no comparable version numbers. Android的内部Java实现与JDK不同,因此没有可比较的版本号。 But for example String#isEmpty() that was added in JDK 6 says Added in API level 9 in Android's documentation which is exactly the version after aforementioned Froyo. 但是例如,在JDK 6中 Added in API level 9 String#isEmpty()表示Added in API level 9Android文档的 Added in API level 9Added in API level 9该代码,该版本正是上述Froyo之后的版本。

Eclipse / Android Lint will also mark usage of those methods as error if your App has a lower minimum version than 9. 如果您的应用的最低版本低于9,则Eclipse / Android Lint还将这些方法的使用标记为错误。

Using JDK 6 to build any app should work perfectly fine. 使用JDK 6来构建任何应用程序都可以正常工作。 Your code must not use core Java methods that did not exist on older Android versions just like any other method in the Android framework. 您的代码不得使用旧版Android版本中不存在的核心Java方法,就像Android框架中的任何其他方法一样。 It is also no problem to use @Override annotations for implemented interfaces since that annotation is not included in the .class file (see RetentionPolicy.SOURCE ). @Override批注用于已实现的接口也没有问题,因为该批注不包含在.class文件中(请参阅RetentionPolicy.SOURCE )。

What actually matters in the end is that Android's dex compiler that turns all the .class files into an Android .dex file actually understands the class format and can produce a valid dex file. 最终真正重要的是,Android的dex编译器将所有.class文件转换为Android .dex文件,实际上可以理解类格式并可以生成有效的dex文件。 The dex format is the one has to be understood by whatever old Android phone you have and I'd assume that the dex compiler makes sure that it produces a valid format for any version of Android. dex格式是您所拥有的任何旧Android手机都必须理解的格式,并且我认为dex编译器可确保为任何版本的Android生成有效格式。 (Here is AFAIK the problem with JDK 7 since the class format has changed) (这是AFAIK,因为类格式已更改,因此JDK 7存在问题)

I'd suggest that you use Java 6 as compile target to build any of your apps. 我建议您使用Java 6作为编译目标来构建任何应用程序。 That results in the most recent version of .class file that the dex compiler understands. 这样就生成了dex编译器可以理解的.class文件的最新版本。 Older versions do work but you might get warnings like the following 较旧的版本可以使用,但您可能会收到类似以下的警告

[dx] warning: Ignoring InnerClasses attribute for an anonymous inner class
[dx] (com.bubblesoft.org.apache.commons.logging.impl.LogFactoryImpl$1) that doesn't come with an
[dx] associated EnclosingMethod attribute. This class was probably produced by a
[dx] compiler that did not target the modern .class file format. The recommended
[dx] solution is to recompile the class from source, using an up-to-date compiler
[dx] and without specifying any "-target" type options. The consequence of ignoring
[dx] this warning is that reflective operations on this class will incorrectly
[dx] indicate that it is *not* an inner class.

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

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