Android switch to gradle doesn't compile JRE7 code features - android

I moved my android app over to Android Studio without switching to Gradle. Now I want to move to Gradle. The app compiles in Android Studio before switching to Gradle, but now that I have Gradle all set up, it won't compile the String Switch Statements or the diamond operators. The error I am getting is
Gradle: error: strings in switch are not supported in -source 1.6
(use -source 7 or higher to enable strings in switch)
I have made sure that I am running on JRE 7 by printing the
System.getProperty("java.version")
in a task. The output is
1.7.0_25
What confuses me most is the discrepancy between "-source 1.6" and "use -source 7". But I know that both of these are names for Java sdk's so maybe the titles are just being mixed up.
Is there a Gradle setting I need to set? or is this not possible in Gradle? If not it is confusing why it works without Gradle.
It should be noted that the without Gradle version of my project runs the default Android Studio build. I didn't write an ant script or maven script for building it. One of those may be the way it is being built, but I don't have any project specific files for them. Just the Android Studio .iml files.
UPDATE
I tried adding the following to the build.gradle android{} section
compileOptions {
sourceCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
targetCompatibility = org.gradle.api.JavaVersion.VERSION_1_7
}
but the .class files failed to build and it weren't included in the apk. See the "Android Projects Need Libraries Compiled with Java 1.6" section on this post

You can upgrade an existing Android app/library module to Java 7 by adding the following in the android section of your build.gradle:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
For a Java (non-android) module, you should add the following to build.gradle just after apply plugin: 'java':
sourceCompatibility = 1.7
targetCompatibility = 1.7
For both types of modules, you will need to manually change the language level of your project in File -> Project Structure -> Project (or you can manually edit the config in .idea/misc.xml from JDK_1_6 to JDK_1_7).
You might also need to add the following to .idea/compiler.xml, in the <component name="CompilerConfiguration"> block (but see how you get on without it first):
<bytecodeTargetLevel target="1.7" />

Related

Error: lambda expressions are not supported in -source 1.7 (use -source 8 or higher to enable lambda expressions)

I am getting this error message while building the Cordova Android app.
Error:(159, 66) error: lambda expressions are not supported in -source
1.7 (use -source 8 or higher to enable lambda expressions)
The following compileOptions has been set on my build.gradle (module android) and build.gradle (module CordovaLib) as you can see here:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
How to resolve this problem?
The error stems from your app module. You have to set up Java 8 for each separate module. Go to File > Project Structure and update it from the Properties tab for that module or make sure you have added the compileOptions snippet to the correct build.gradle file of your app module.
If there are further modules, make sure they are set up for Java 8, before using Java 8 functionality.
For full reference, see the official Android Java 8 Support Page.

Error:Jack is required to support java 8 language features. Either enable Jack or remove sourceCompatibility JavaVersion.VERSION_1_8

As per mentioned in developer.android website
"The Jack toolchain is deprecated, as per this announcement. If your
project depends on Jack, you should migrate to using Java 8 support
built into Android Studio’s default toolchain."
I need to use lambda expressions so I have disabled the jack options, but when I compile my code, I get the following error
Error:Jack is required to support Java 8 language features. Either enable Jack or remove sourceCompatibility JavaVersion.VERSION_1_8.
Below is the screenshot of my build.gradle file
Check you version of com.android.tools.build:gradle.
I had the same error. My solution:
set com.android.tools.build:gradle:3.0.0-alpha6
update kotlin to 1.1.3-2
add to repositories google()
I'm using AS 3.0 Canary 6.
You should do the following:
Update your Android Studio to version 3.0.1.
Upgrade your gradle to 'com.android.tools.build:gradle:3.0.1'
Make sure you buildToolsVersion is at least 26.0.2.
After that you can remove Jack options and use supported Java 8 features.

Dagger 2 components not generated when using Jack

When I enable the Jack compiler in Android Studio 2.2 the Dagger 2 component is not generated. Can Dagger 2 be used with Jack? If so, how would I go about configuring my application?
From my application's build.gradle:
jackOptions {
enabled true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
I sunk like 2 days into figuring this out. So I'm circling back to posting the findings here in case it saves someone time:
This is caused by a bug in Jack that prevents classpaths from working properly. It has to do with Jack running "in-process" (in the same JVM as the gradle daemon). Setting android.defaultConfig.jackOptions.jackInProcess to false does get beyond the "Preconditions" error but it causes other problems (2 JVMs that hog system resources) & bugs that break the build in other (worse) ways.
For now, the best solution seems to be:
Wait for the 2.3 release of the Android gradle plugin, which already has the fix for this.
Downgrade Dagger to v2.2, in the meantime.
It's the highest version that seems to avoid the Guava conflict with Jack.
EDIT: update 1/14/2017:
I ran into several OTHER problems with Jack and got so tired of it that I switched to retrolambda and kicked myself for not doing this earlier! Right now, Jack simply seems to cause more problems than it solves. Just add the lines with a plus and delete the lines with a minus and you can return to Dagger 2.8 while waiting for Jack to get it's act together!
+plugins {
+ id "me.tatarka.retrolambda" version "3.4.0"
+}
apply plugin: 'com.android.application'
+apply plugin: 'me.tatarka.retrolambda'
- jackOptions {
- enabled true
- }
For even faster retrolambda builds, add org.gradle.jvmargs=-Xmx4608M to your gradle.properties file so that dexing can happen in-process. Now, I'm on Dagger 2.8 and my clean builds are only 12 seconds, GOOD RIDDANCE, JACK!
The documentation page on Jack and Jill has instructions specific to annotation processors "to be applied at compile time but not to be included in your APK", advising the use of the annotationProcessor dependency scope. The example coincidentally mentions Dagger 2:
dependencies {
compile 'com.google.dagger:dagger:2.0'
annotationProcessor 'com.google.dagger:dagger-compiler:2.0'
}
Jack is now deprecated, see this post.
You have to upgrade your Android Studio to 3.0 preview 1, to be able to use Java 8.
If you can't upgrade it (conflict with other lib), or you want to wait for a release version, you can try this workaround solution :
Add the retrolamba lib, follow instructions here.

Is there way to use Java 8 features with Android library project?

I followed the Android Java 8 Features manual. It works well for Android application project. But when I try to use it with Android library project I get
Error:Library projects cannot enable Jack. Jack is enabled in default config.
Partial solution:
I enabled lamdba expression with Gradle Retrolambda Plugin.
I had the same issue and tried different approaches. It now works for me without using retrolambda (which produced some weird error during runtime). Also Jack is not active for the same reason you already mentioned.
There is an interesting bug post at google.com about this topic: https://code.google.com/p/android/issues/detail?id=211386
Here is my build.gradle script, I used the workaround from the bug post to fix the "MethodType not found" exception during compilation.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
}
}
apply plugin: 'com.android.library'
repositories {
mavenCentral()
}
// Java8 not fully supported in library projects yet, https://code.google.com/p/android/issues/detail?id=211386
// this is a temporary workaround to get at least lambdas compiling
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xbootclasspath/a:" + System.properties.get("java.home") + "/lib/rt.jar"
}
}
android {
compileSdkVersion 24
buildToolsVersion "24"
defaultConfig {
minSdkVersion 10
targetSdkVersion 24
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
This sure has been a long ride. I tried all possible combinations of the gradle plugin, the experimental gradle plugin + retrolambda + Jack etc, but with no luck. Until now. From Android Studio 3.0 Preview 1 or later (and, consequently, the Android Gradle plugin 3.0.0-alpha1 or later), the Jack toolchain is deprecated and replaced by some new bytecode transformation - sugaring, used in conjunction with the standard javac compiler.
With this setup I've personally (finally!!!) been successful in using Java 8 features such as lambdas in a library project.
This page has all the techy info as well as migration help etc: https://developer.android.com/studio/write/java8-support.html

Gradle Configurations not Working as Expected in New Android Build System

Environment Configuration
com.android.tools.build:gradle:0.4
gradle version 1.6
jdk 1.6 (OSX)
android build tools version 17
compile sdk version 17
The issue that I seem to be having is that I can’t seem to exclude lombok from being added to the apk. I tried to do it by creating a provided configuration like this:
configurations {
provided
}
sourceSets {
main { compileClasspath += configurations.provided }
}
and then adding the dependency like this:
dependencies {
provided ‘org.projectlombok:lombok:0.11.8′
}
But I’m still getting this error:
Error: duplicate files during packaging of APK <myapp>.apk
Path in archive: LICENSE
Origin 1: /<home>/.gradle/caches/artifacts-24/filestore/org.projectlombok/lombok/0.11.8/jar/e43ce2be16d8990568a4182c0bf996ad3ff0ba42/lombok-0.11.8.jar
Origin 2: /<home>/.gradle/caches/artifacts-24/filestore/org.sonatype.sisu.inject/cglib/2.2.1-v20090111/jar/7ce5e983fd0e6c78346f4c9cbfa39d83049dda2/cglib-2.2.1-v20090111.jar
:packageRelease FAILED
I have tried using lombok-api.jar which then causes a different issue regarding some AccessLevel annotation while performing dex.
Which suggests that its including the lombok jar file into the apk. This shouldn't be happening, any suggestions?
You can't use sourceSets because we use custom ones. You'd have to do the following:
android.applicationVariants.each { variant ->
variant.javaCompile.classpath += configurations.provided.
}
However, it should be possible to instead remove the dependency from our "package" config (which replaces the "runtime" one.) I'll look into it.

Categories

Resources