yareally/Java7-on-Android
Folders and files
| Name | Name | Last commit date | |
|---|---|---|---|
Repository files navigation
Until finalized/merged, detailed instructions in markdown for Intellij IDEA 13 can be found here: https://github.com/yareally/Java7-on-Android/tree/Intellij-13 Java 7 features are now officially supported by Google as of build-tools version 19: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1.7 Prefer Scala? See my guide for using Scala with Intellij IDEA without additional plugins or external building: https://github.com/yareally/android-scala-intellij-no-sbt-plugin Miss not having operator overloading like C#? Try https://github.com/amelentev/java-oo. You'll need to do the fixes located in the following link for any Intellij IDEA based IDE: amelentev/java-oo#8 (comment) Using (features) Java 7 on Android. 1) Clone this project. 2) Add java.lang.* from this project as a base package to whatever project to get auto resource close working. This will override the internal Throwable java class so it uses this one instead. 3) Add cc.io.* from this project to whatever project to get auto resource close working. This just wraps the Input streams. 4) Set your project to use the Java 7 language level and the Android SDK to build off of JDK 7. 5) If you happen to run into issues, I probably have your solution somewhere below just from personal experience. It took much more time to get things initially play nice than it did anything else. --Frequently Asked Questions-- Q) So what works from Java 7? A) All the syntactic sugar stuff and also "try with resources." (see below) 1) String Switches 2) Try with resources (a.k.a "using/with", auto close) 3) Multiple exception caught in one catch block 4) Integer (and binary) literals for readability (e.g. 100_000_000 or 20_000 or 0b0111_0000) 5) Type inference on collections (a.k.a. "diamonds") 6) Java 8 Lambdas are also now possible on Android via https://github.com/orfjackal/retrolambda. Though I haven't found an easy to add it to the normal Intellij build process without using Ant, Maven or Gradle (Intellij IDEA 13). Modding the Android plugin would work, but with Intellij IDEA 13 nearing completion soon, not sure it's worth it. Q) What doesn't work? 1) java.nio.* (needs native backend code for that and this was just a weekend project so far) 2) Threading and multiple processor enhancements (fork/join). Don't how nicely that would play with Android if it possible. 3) File change notifications (watch/notify file api stuff). Not sure about this and a sandboxed app model. 4) invokedynamic (not possible anyways due to dalvik restrictions, unless one wants to extend dalvik too) Q) Does this really work on stock, unrooted/unmodified Android? A) Yup, because it's either using syntactic sugar features (which does not modify the bytecode) or it overrides the existing Android libraries. Q) Help! I'm getting some error related to java: cannot find method addSuppressed(java.lang.Throwable). How do I fix this? A) You probably allowed your IDE auto-formatter to reformat the java.lang.* files or the cc.io.* files you included from above or manually pasted the files into your IDE instead of importing/cloning them (which also triggered auto format). Modifying these files (even accidentally) is asking for trouble as such things can make the project revert back to using the internal Android Throwable instead of the project version. You can fix this by pasting the original file outside of your IDE or reclone the source again. I recommend setting the project java.lang.* package and wherever you put the cc.io.* package to be read only to prevent any accidental mishaps. The main issue is removing the full paths on things like like java.lang.AutoClosable really. Dalvik has troubles finding it if you make it a relative path. Alternatively, you can make java.lang.* and cc.io.* their own module/sub-project in your IDE and place the dependency for it in your main project above the Android API/Java JDK dependencies. Q) If I figure out how to get any other features working, may I commit them back to your repository? A) Sure, just ask me. This was just a simple (weekend) attempt to see what was possible and by no means a massive project. I'd love to see more contributions and any corrections made. Q) Can I include this as a jar library? A) Maybe. Intellij IDEA 13 has an option to allow "core libraries" to be overrided now, but have not tried it yet. Though without that, Android (dx.jar) will complain about overriding system classes if you do and short of modifying the dalvik compiler sources (as adding options to dx.sh/dx.bat does not work), there is no easy way around it. Well, actually there sort of is, but it requires building with Ant (see next question) directly or perhaps Maven as you can get around the issue with that. Intellij though won't like it if you are using the default method of building if you try to include these sources as a jar. See: https://code.google.com/p/android-rcs-ims-stack/issues/detail?id=42 http://code.google.com/p/dalvik/wiki/JavaxPackages https://groups.google.com/forum/?fromgroups=#!topic/android-developers/msDX8ogYrd0 http://stackoverflow.com/questions/2680827/conversion-to-dalvik-format-failed-with-error-1-on-external-jar http://stackoverflow.com/questions/3284407/conversion-to-dalvik-format-failed-with-error-1-with-javax-net-socketfactory-cla http://code.google.com/p/maven-android-plugin/issues/detail?id=214 for more details. Q) So, how does it work with ant? A) see: https://github.com/yareally/Java7-on-Android/blob/master/building_with_ant Q) Can you get Java 8 working? A) Lambdas work if one uses https://github.com/orfjackal/retrolambda. Nothing else does as far as I know yet. JDK8 is a total rewrite and backporting much more complex than switching the SDK and pulling a class + interface. It's possible, just something longer term. If you really need more programming features than mentioned here, I reccomend trying Scala or Kotlin on Android. Q) Won't this affect the entire Android System and not just my app? A) Since every Android app is sandboxed, only your code is affected. Q) Do you have example code showing things working? A) Yes, see TestActivity. Tested and working on Android 2.2+. Q) Will this work for other resource classes (like ObjectInputStream)? A) I'm sure it will, I just didn't include them. See the implemented InputStreams as a template. I had to wrap them versus extending due to IDE complaints I was getting. Moving them to a seperate module might solve that though. Q) Why doesn't Google implement these features themselves if they work? A) You would have to ask Google as all I can do is speculate. Q) Will this work on Eclipse/Netbeans/etc? A) Probably, but I don't really know how Android works on Eclipse/Netbeans, so you're on your own for that. Ant works, but requires some modifying of the build.xml to override some of the compilation time stuff.