Can I "use" Java 8 With Android Development Now?
Solution 1:
In Google I/O 2016, they announced that Android now supports some features of Java 8, but not all the features.
Features like
- Default and static interface methods
- Lambda expressions (also available on API level 23 and lower)
- Repeatable annotations
- Method References (also available on API level 23 and lower)
- Type Annotations (also available on API level 23 and lower)
Additionally, the following Java 8 language APIs are also available:
- Reflection and language-related APIs
java.lang.FunctionalInterfacejava.lang.annotation.Repeatablejava.lang.reflect.Method.isDefault()- and Reflection APIs associated with repeatable annotations, such as
AnnotatedElement.getAnnotationsByType(Class)
- Utility APIs
java.util.functionjava.util.stream
All what you should do is to use the new jack compiler, to do this you have just to add this to your gradle file build.gradle (Module: app):
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
References:
Solution 2:
If you've been using Java 7 to deploy Android apps then it's certain, up to this point, you haven't used any Java 8 features so I don't see how it would matter.
Follow your instructor's directions and when you do an assignment for school simply select either the JDK or Language Level in the Project Structure.
CTRL + ALT + S, select Project
You can default to the Java 8 SDK but limit it to Java 7's features for your Android apps. Or you can simply set your homework projects to the Java 8 SDK.
Going out on a limb here assuming Android Studio includes the core settings of Intellij.
Solution 3:
It's safe if you don't use the java 8 features.
although you may be keen on java 8 with retroLambda and collectionsQuery. https://github.com/evant/gradle-retrolambda and https://bitbucket.org/mart_bogdan/collectionsquery/src
This will allow you to write your code to something like the following: -
mButton.setOnClickListener( v-> doClickEvent());
mView.postDelayed( () -> someMethodToRun() , 1000);
Queryable.from(listOfObject).forEachR(r -> doProcess(r));
as opposed to the clunky
mButton.setOnClickListener( newView.OnClickListener(){
@OverridepublicvoidonClick(View v) {
doClickEvent();
}
});
to use java 8 with retrolambda just add in the gradle file.
apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda'
android {
// :// Snips the rest of configuration.// :
compileOptions {
encoding "UTF-8"
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.innahema:collections-query:0.2.9'
}
and at the project gradle.build file add the following...
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'me.tatarka:gradle-retrolambda:3.2.3'
classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'//fix lint issue// NOTE: Do not place your application dependencies here; they belong// in the individual module build.gradle files
}
}
Solution 4:
I guess you should just give it a try. On a side note: You can easily run JDK 7 and JDK 8 side by side. I don't know about Android Studio, but in Eclipse you can configure the build path.
Despite that: Are you using Java 8 specific features for your homework? If you don't use lambdas or JavaFX 8, I don't think you will need JDK 8. Sounds more like your professor just wants to use the latest version of Java (judging from your first sentence).
Post a Comment for "Can I "use" Java 8 With Android Development Now?"