Java 7 features backported to Android 2.2+

Overview

Using Java 7 & 8 Features on Android

Feeling like you're stuck in 2006 while developing apps and libraries for Android?

Don't worry, you're not alone. However, there's a solution!

To help bring Android Java development into the modern era, I've tested and compiled a list of Java 7 and 8 features that will work on Android. Some of them, I discovered through a bit of testing and modding, while others depend on libraries maintained by others independently. Without further BS, here's a list of everything that can be used without issues on at least Android 2.2+.

Google Now Officially Supports Java 7 on Android

Starting with Android 4.4, Google has declared officially that you can use Java 7 features with build-tools version 19. Directions for how to set it up with Gradle can be found on tools.android.com.

Possible (Working) Features

Things that don't Work

  • Java 7 file API (java.nio.*) because some native code needs ported
  • File change notifications (watch/notify file api stuff)
  • Invoke Dynamic and features that rely on it (would require modifying Dalvik to support it)

Setting up on Intellij IDEA 13 (and probably Android Studio)

The new Intellij finally has a built in option to override core libraries of Android, making much of the above far easier. The instructions below take advantage of this and allow us to override the core libraries with a jar within Intellij IDEA normal build process. If using Intellij IDEA 12, you'll have to use the old fallback instructions as the directions for Intellij IDEA 13 will not work.

  1. Clone this project or grab core.jar from the libs directory.
  2. Enable overriding the Android core libraries by going to File → Settings → Compiler → Android Compilers alt text
  3. Ensure core.jar is in your project library directory like below: alt text
  4. Go to the File dropdown menu → Project Structure → Libraries and create a new library with core.jar: alt text
  5. While still in the project setup area from above, go to "Modules" and add the newly created library as a project dependency. Ensure that it is placed above the Android SDK so that it overrides the Android SDK when necessary. alt text
  6. Under the SDKs settings in the Platform Settings area, ensure you have an Android SDK set up to use at least Java 7 (Java 8 if you wish to use lambdas). Java 8 and Android are a bit quirky together in Intellij, so you may experience issues getting the Java 8 SDK to stick. alt text
  7. The final setting to change under the project settings area is under "Project." Change the project language level to 7.0 (8.0 if you are going to use lambdas). alt text
  8. Run this project's Android TestActivity to ensure everything is set up properly. If it does not run or you see syntax errors in Intellij, then ensure you settings looks similar to my screen shots.

FAQ

Does this really work on stock, unrooted/unmodified Android?

Yup, because it's either using syntactic sugar features (which does not modify the bytecode) or it overrides the existing Android libraries.

I'm having a problem unrelated to setting up the above configuration or running the provided example code.

If you're having an issue related to either, feel free to file an issue and I will try to help you out when I have a few moments. If you are having an issue pertaining to lambdas or fork-join involving possible bugs in those libraries, please contact the maintainers of each project respectfully.

Can I use Gradle with this?

I still have to test it, but it'll probably work as long as you add the jar as a dependency in Gradle instead of under Intellij's build system. Don't forget to enable the core library setting though. Gradle is also the recommended way to use Java 8 lambdas via Evant's Gradle plugin until I find a method to do it without Gradle.

Can I use Ant?

If you need to use Ant, I have a guide for that.

Won't this affect the entire Android System and not just my app?

Since every Android app is sandboxed, only your code is affected.

Do you have example code showing things working?

Yes, please see the TestActivity included in this project.

Will this work for other resource classes (like ObjectInputStream)?

I'm sure it will, I just didn't include them in the jar. See the implemented InputStreams source as a template in the jar. I had to wrap them versus extending due to IDE complaints I was getting (in IDEA 12). Moving them to a separate module might solve that though or IDEA 13 may no longer require that (need to test it).

Why doesn't Google implement these features themselves if they work?

I don't work for Google and cannot answer that any better than any other non-employee. Perhaps eventually they will add them, but it's probably more likely they will add official support for a language alternative to Java first.

Will this work on Eclipse/Netbeans/etc?

Probably, but I don't really know how Android works on Eclipse/Netbeans, so you're on your own for that. If someone wishes to document the setup process for either, let me know and I will add it here when you're done. Aside from that, Ant works outside of Intellij, but requires some modifying of the build.xml to override some of the compilation time stuff.

You might also like...
A super fast build tool for Android, an alternative to Instant Run
A super fast build tool for Android, an alternative to Instant Run

Freeline Freeline is a super fast build tool for Android and an alternative to Instant Run. Caching reusable class files and resource indices, it enab

Command-line tool to count per-package methods in Android .dex files

dex-method-counts Simple tool to output per-package method counts in an Android DEX executable grouped by package, to aid in getting under the 65,536

View Inspection Toolbar for Android Development
View Inspection Toolbar for Android Development

View Inspector Plugin View inspection toolbar for android development. Features Boundary show outlines show margins show paddings Layer Scalpel featur

Make Android screenshots of scrollable screen content
Make Android screenshots of scrollable screen content

scrollscreenshot Make Android screenshots of scrollable screen content - brought to you by PGS Software SA This tool makes a number of screenshots, sc

🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.
🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.

🇨🇳 中文 / 🇯🇵 日本語 / 🇬🇧 English 🍼 Debug Bottle An Android debug / develop tools written using Kotlin language. All the features in Debug bottle are

[] Dissect layout traversals on Android
[] Dissect layout traversals on Android

Probe Dissect layout traversals on Android. Features Intercept View methods. onMeasure(int, int) onLayout(boolean, int, int, int, int) draw(Canvas) an

Android Library Finder
Android Library Finder

alfi Android Library Finder Search through thousands of android libraries that can help you scale your projects elegantly Usage Search for something a

Annotation based simple API flavored with AOP to handle new Android runtime permission model

Let Annotation based simple API flavoured with AOP to handle new Android runtime permission model. If you check Google's Samples about the new permiss

Combines tools for fast android app devlopment

Android - Rapid Test Driven Development Combine tools to generate most of the boilerplate code. Examples how to test different aspects of an android a

Owner
Wes Lanning
Wes Lanning
Android Resource Manager application to manage and analysis your app resources with many features like image resize, Color, Dimens and code Analysis

Android Resource Manager application to manage and analysis your app resources with many features like image resize, Color, Dimens and code Analysis

Amr Hesham 26 Nov 16, 2022
知乎 Android 团队使用的 Java 库静态代码检查工具,支持目录、.jar、.aar 格式,可通过配置文件添加检查策略

SimpleWalker 知乎 Android 团队使用的 Java 库静态代码检查工具,支持目录、.zip、.aar 格式,可通过配置文件添加检查策略。 主要用于检查 Android 应用的三方依赖是否有调用隐私接口。 使用方法 项目工程中 release/ 目录下是最新版本的可执行文件,help

zkw周柯文 87 Dec 20, 2022
An easy to use translation / localization api written in Kotlin that can be used with Java and Kotlin

KTranslate KTranslate is an easy to use TranslationAPI written in Kotlin, with currently 26 supported languages. It is very easy to use in Kotlin and

Felix Beinssen 0 Sep 2, 2022
A tool to install components of the Android SDK into a Maven repository or repository manager to use with the Android Maven Plugin, Gradle and other tools.

Maven Android SDK Deployer Original author including numerous fixes and changes: Manfred Moser [email protected] at simpligility technologies i

simpligility 1.4k Dec 27, 2022
A simple utility to remove unused resources in your Android app to lower the size of the APK. It's based on the Android lint tool output.

android-resource-remover android-resource-remover is utility that removes unused resources reported by Android Lint from your project. The goal is to

Keepsafe 1.3k Dec 16, 2022
This is a Android Studio/ IntelliJ IDEA plugin to localize your Android app, translate your string resources automactically.

#Android Localizationer This is a Android Studio/ IntelliJ IDEA plugin to localize your Android app, translate your string resources automactically. T

Wesley Lin 822 Dec 8, 2022
A tool to install components of the Android SDK into a Maven repository or repository manager to use with the Android Maven Plugin, Gradle and other tools.

Maven Android SDK Deployer Original author including numerous fixes and changes: Manfred Moser [email protected] at simpligility technologies i

simpligility 1.4k Dec 27, 2022
Automated-build-android-app-with-github-action - CI/CD Automated Build Android App Bundle / APK / Signed With Github Action

Automated Build Android With Using Github Action Project Github Action Script Us

Faisal Amir 34 Dec 19, 2022
proguard resource for Android by wechat team

AndResGuard Read this in other languages: English, 简体中文. AndResGuard is a tooling for reducing your apk size, it works like the ProGuard for Java sour

shwenzhang 8.1k Jan 9, 2023