Support JAVA-8 Jar in Android 5 - React Native - java

Solution from Android Documentation is below
android {
defaultConfig {
// Required when setting minSdkVersion to 20 or lower
multiDexEnabled true
}
compileOptions {
// Flag to enable support for the new language APIs
coreLibraryDesugaringEnabled true
// Sets Java compatibility to Java 8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.0.9'
}
This is working fine for Native android app, But this configuration is not working for React-Native app and throwing below error while building the app.
A problem occurred evaluating project ':app'.
Could not find method coreLibraryDesugaringEnabled() for arguments [true] on object of type com.android.build.gradle.internal.CompileOptions.

Half knowledge in always dangerous, and it happens here. As per the documentation above solution worked only for gradle version greater than 4. My gradle version was
classpath("com.android.tools.build:gradle:3.5.3")
I changed it to
classpath "com.android.tools.build:gradle:4.0.1"
Now it's working.

Related

Is it possible to use other JavaVersion in android studio?

I have been trying to change this part of my gradle.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Each time I find a post about how to fix the current error a new error appears.
I'm trying to include a java library in an android studio project.
The library uses JDK 14 or higher.
So I downloaded jdk-14.0.2 and updated the variable JAVA_HOME to C:\Progra~1\Java\jdk-14.0.2
(after struggling to make android studio compile with this one)
I can compile a new project just changing the JDK to use in Project structure -> JDK location.
But updating the JavaVersion to any higher number throws error: package android.os does not exist
Solved it adding this to dependencies
implementation files("${android.getSdkDirectory().getAbsolutePath()}/platforms/${android.compileSdkVersion}/android.jar")
But now I get several of these errors
com.android.tools.r8.errors.b: Unsupported class file version: 58
Others are in java.lang or other places, but all say Unsupported class file version: 58
Is there a way to make android studio properly work with a higher JavaVersion?
All posts I see say that is as simple as just change the JDK location, but it hasn't been like that.
Currently android can only work up to Java11.
I found this when gradle updated to 7.0, this version required Java11.
Seems for this you also need to have Android Studio Arctic Fox.
With this my app's gradle can have this
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 26
targetSdkVersion 30
versionCode 1
versionName "1.0"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}
}

Issue with log4j version 2.9.0 in Android

I am using log4j in one of my Android lib projects. Now I updated the log4j version from 2.3.0 to 2.9.0 and building the lib project is successful and also I was able to successfully add this lib project to my main project as a dependency. But when I run my project, I get the following error.
Error:Error converting bytecode to dex:
Cause: Dex cannot parse version 53 byte code.
This is caused by library dependencies that have been compiled using Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
...while parsing META-INF/versions/9/org/apache/logging/log4j/util/ProcessIdUtil.class
To over come the above error I made the following changes in the gradle.
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
After the above changes I got the following error while running the project.
Error:Jar transformation: class files coming from an unsupported Java
version Error:Execution failed for task
':MyApp:transformClassesWithPreJackPackagedLibrariesForDebug'.
com.android.build.api.transform.TransformException: com.android.builder.core.JackToolchain$ToolchainException: Jack compilation exception
Now I am not getting a proper solution for this problem. Even I raised this in log4j forum (https://issues.apache.org/jira/browse/LOG4J2-2038). But the provided solution doesn't explain much.
Mean while I am trying to work directly with the log4j android code from here .
This has become a blocker for me. Any help will be appreciated. Thanks.

Java 1.7 compatibility for a library in android studio

I want to use this library in my android studio project but when I add it into project using gradle it show the following error
Error:Error converting bytecode to dex: Cause: Dex cannot parse version 52 byte code. This is caused by library dependencies that have been compiled using Java 8 or above. If you are using the 'java' gradle plugin in a library submodule add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle file.
It seems it is because of java 1.7 compatablility but I have no idea how to fix it. Android studio suggests to add targetCompatibility = '1.7' sourceCompatibility = '1.7' to that submodule's build.gradle but how can I add it into gradles dependencies section?
Version 52 is actually Java 8.
The library was compiled without Java 7 compatibility, so it cannot be used if you are targeting Java 7 or lower.
I see that there's an open issue about it on their GitHub. It might be a problem with this versions only, so you might try using a previous version until they fix the issue.
You should use VERSION_1_8 instead of VERSION_1_7
Install JDK 1.8
You should add this
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
FYI
Before use 1_8 please read Use Java 8 Language
To enable Java 8 language features and Jack for your project, enter the following in your module-level build.gradle file:
android {
...
defaultConfig {
...
jackOptions {
enabled true
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Clean-Rebuild and RUN

Android Studio 2.2 and Jack are getting blocked by Avira Antivirus

So I update my project to use the new jack compiler, but for some unknown reason my AntiVir blocks the task transformClassesWithPreJackPackagedLibrariesForDebug
It works if I disable AntiVir real-time protection. I really don't want to do that and I don't want to start putting my android project in the exception list.
I am using Android Studio 2.2 and this is my build.gradle
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
minSdkVersion 21
targetSdkVersion 24
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
jackOptions {
enabled true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
My project.gradle
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
}
}
And the error when I try to run the app on my hardware device.
Error:Execution failed for task ':app:transformClassesWithPreJackPackagedLibrariesForDebug'.
> Failed to delete temporary file C:\Users\User\AppData\Local\Temp\jill-1475579265083-0.jack
While compiling disable real-time protection in your anti-virus package. It is the only workaround that is reliable at this time.
You could add your Android SDK, Android Studio and project folders to your antivirus whitelist. This way you will still be protected while building.
Guide here.
Fixed using 'com.android.tools.build:gradle:2.2.0-alpha5'
if not working than please add your Android SDK, Android Studio and project folders to your antivirus whitelist:
Guide

JDK 1.7 String switch statement is not working

[Fixed]
Thanks to #Visil below (accepted answer). I added the recommended code to build.gradle and waited after syncing gradle. The following popped up shortly after that...
Original Question
I am doing some Android programming with Android Studio 0.5.9, and I wanted to have some String switch statements. I set up everything, but the compiler is complaining that it cannot handle them.
String Switch-statements were introduced in JDK 1.7 so I am confused as to why I cannot do this.
Just to prove I am using JDK 1.7, you can check the image below...
...also, my machine has JDK 1.8 installed...
...what's up with this?
[UPDATE]
File >> Other Settings >> Default Settings
File >> Other Settings >> Default Project Structure
Build.gradle
Please check your gradle source compatibilty configuration:
With Android KitKat (buildToolsVersion 19) you can use the diamond
operator, multi-catch, strings in switches, try with resources, etc.
To do this, add the following to your build file:
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Note that you can use minSdkVersion with a value earlier than 19, for
all language features except try with resources. If you want to use
try with resources, you will need to also use a minSdkVersion of 19.
You also need to make sure that Gradle is using version 1.7 or later
of the JDK. (And version 0.6.1 or later of the Android Gradle plugin.)
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1.7

Categories

Resources