Problems upgrading Android Studio to Java 1.9 for Roboelectric - android

I am trying to use Robolectric 4.3.1 for testing
I get the error: java.lang.IllegalArgumentException: failed to configure com.example.loginTest: Package targetSdkVersion=30 > maxSdkVersion=29
So because of that I downloaded JDK 1.9 and changed the JDK location in the "Project Structure" dialog. I also changed my app/build.gradle file:
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_9
targetCompatibility JavaVersion.VERSION_1_9
}
...
When I do that, then nothing compiles at all. It's almost as if the Android SDK is not working:
I get errors such as:
class file for android.view.ViewGroup not found
package android.content does not exist
package android.widget does not exist
etc.

Related

error: package android.view does not exist

I am getting this error in view bindings file generated by Android studio. I have added
buildFeatures {
viewBinding true
}
to the app build.gradle file.
In your build.gradle(:app) file,
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Try changing the java version.
I got the same error when using VERSION_11 but the VERSION_1_8 fixed the issue.
Android Studio don' t update Gradle to version 7.
Open project Structure, you can find in File, set in Project:
Android Gradle Plugin Version: 7.0.0
and
Gradle Version: 7.0.2
Compile now and all will work fine.
Updating the android studio to the latest version fixed the issue for me.

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.

android opennlp build error: cannot access Path class file for java.nio.file.Path not found

I am trying to use apache opennlp library for my android project. But build fails. As instructed in some SO posts I have checked my javac version which is 1.8.0. I have also added following compile options
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Then build error asked me to enable jack, which I did. But then I got the error mentioned in title. From a SO post I got to learn that not all Java packages are included in Android. So how do I use opennlp in my android project? Are there any other NLP libraries which are easier to integrate/use in Android?
try following
android{
jackOptions {
enabled true
}
}
add this in gradle project level.

Android Studio: Referencing a "java-6-compiled-jar" causes UNEXPECTED TOP-LEVEL EXCEPTION

Whenever I put my jar file that was compiled using language level 6 or 7 in the Android Studio's 'libs' folder and try to deploy the application to the device, I get the following exception:
Unexpected Error
Local path doesn't exist. Local path doesn't exist. The project may need to be synced with Gradle files.
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version
Strangely, if I remove my jar and put another jar file (say joda-time-x.jar) there, everything works just fine.
Also
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
in the build.gradle does not help.

Android switch to gradle doesn't compile JRE7 code features

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" />

Categories

Resources