How to use Realm database with Java 8 features - java

I am new to using Realm database for Android.
I modified my gradle files to include the Jack toolchain so that I could use Java 8 language features. I also modified the gradle files to install the Realm plugin. When I synced the project gradle files, I received the following error: Error:Could not find property 'options' on task ':app:compileDebugJavaWithJack'. The two modifications work fine on their own, but for some reason I cannot have both at the same time.
I would very much appreciate help on this matter.

It is not possible to use Jack compiler with Realm at the moment, because Jack does not support bytecode manipulation (Javassist / Transform API).
In order to use lambdas, it's easier for you to use Retrolambda instead for the time being.
buildscript {
//...
dependencies {
classpath "io.realm:realm-gradle-plugin:1.1.0"
classpath 'me.tatarka:gradle-retrolambda:3.2.5'
}
}
And
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'realm-android'
android {
...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

Unfortunately Jack compiler and Realm can't play together right now. Please follow that topic. That one is also useful.

Related

how to fix Android Annotations 4.6.0 compile with android Studio 3.5 and 3.5.0 gradle

I updated Android studio to 3.5 but there is a problem when I use android annotations
Gradle may disable incremental compilation as the following annotation processors are not incremental: jetified-androidannotations-4.6.0.jar (org.androidannotations:androidannotations:4.6.0).
Consider setting the experimental feature flag android.enableSeparateAnnotationProcessing=true in the gradle.properties file to run annotation processing in a separate task and make compilation incremental
I've put android.enableSeparateAnnotationProcessing=true in the gradle.properties file
but it's say that
INFO: The option setting 'android.enableSeparateAnnotationProcessing=true' is experimental and unsupported.
The current default is 'false'.
Could not find the AndroidManifest.xml file
I had almost the same problem and couldn't build my app after updating android studio and gradle to 3.5 but according to this answer I added this to my defaultConfig{} in app gradle and problem solved!
javaCompileOptions {
annotationProcessorOptions {
arguments = [
"androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString()
]
}
}
There is 2 way of fixing this issue
Method One - Use Snapshot version
Current Annotation lib version is 4.6, to fix these error temporary you can use a Snapshot version
add the following URL in Project-level Gradle
buildscript {
repositories {
maven { url = 'https://oss.sonatype.org/content/repositories/snapshots' }
}
Method Two - Use javaCompileOptions
Add following code in gradle DSL
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments = ["resourcePackageName": android.defaultConfig.applicationId]
arguments = ["androidManifestFile": "$projectDir/src/main/AndroidManifest.xml".toString()]
}
}
}
Hope it will work :)
AndroidAnnotations does not support Gradle incremental annotation processing, yet. You can still build your app, it only means that incremental compilation time is not as fast as it could be. There is an issue for tracking the implementation of incremental processing.
I suggest not to set android.enableSeparateAnnotationProcessing flag, as it is experimental, can cause other issues, and will slow down your clean builds.

Java Import build.gradle file for minecraft mod

I have been following this tutorial - https://mcforge.readthedocs.io/en/latest/gettingstarted/
- and I am stuck on this section - Launch IDEA and choose to open/import the build.gradle file, using the default gradle wrapper choice. While you wait for this process to finish, you can open the gradle panel, which will get filled with the gradle tasks once importing is completed.
How do I import the build.gradle file? what is the build.gradle file? what does it do? I am new to coding, any help is appreciated. thx
Launch IDEA and select "File" → "New" → "Project from Existing Sources"
Select build.gradle file from the unpacked archive from the site you've provided
Check wrapper settings on the next screen. Leave the defaults.
Wait till IDEA builds the projects and makes indexes.
Happy hacking!
build.gradle is basically a build configuration file. It describes the way a piece of software is made. Like: where is the source code, what are the project's dependencies, where to get and how to link them, how to test and so on.
Speaking about particular build.gradle from forge-mdk:
buildscript {
repositories {
jcenter()
maven { url = "https://files.minecraftforge.net/maven" }
}
dependencies {
classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
}
}
apply plugin: 'net.minecraftforge.gradle.forge'
This part applies net.minecraftforge.gradle.forge plugin that, I guess, is used to build Minecraft mods. As this is a third-party plugin buildscript block adds a repository (https://files.minecraftforge.net/maven) where it can be downloaded.
version = "1.0"
group = "com.yourname.modid" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "modid"
This part describes the result ("artifact") of the projects. It has version 1.0, name modid and will be published (if published) under com.yourname.modid group. This is a Maven related vocabulary. I guess, you'll need to replace this values with your own.
sourceCompatibility = targetCompatibility = '1.8' // Need this here so eclipse task generates correctly.
compileJava {
sourceCompatibility = targetCompatibility = '1.8'
}
Here you state that the projects is built with Java 8
minecraft {
version = "1.12.2-14.23.5.2775"
runDir = "run"
mappings = "snapshot_20171003"
}
Here you configure net.minecraftforge.gradle.forge plugin that you've added previously. Basically, any plugin can expose it's own configuration block and you'll need to read the docs to know what do the values mean.
dependencies {
…
}
The project has no dependencies yet, thus empty dependencies block
processResources {
// this will ensure that this task is redone when the versions change.
inputs.property "version", project.version
inputs.property "mcversion", project.minecraft.version
// replace stuff in mcmod.info, nothing else
from(sourceSets.main.resources.srcDirs) {
include 'mcmod.info'
// replace version and mcversion
expand 'version':project.version, 'mcversion':project.minecraft.version
}
// copy everything else except the mcmod.info
from(sourceSets.main.resources.srcDirs) {
exclude 'mcmod.info'
}
}
Here you configure built-int processResources task that… processes resources. As you see, things are self-descriptive in Gradle. Tasks are Java classes that has documentation. For example, here are the docs for ProcessResources. One more link for DSL reference
Hope this answer will get you some info to start with!

Compile Java 8 Android library to be consumed in Java 7

Let's say I am making an Android library (to be distributed as a .aar) and I want to use Java 8 language features in my internal library code. Basic stuff like lambdas, etc.
In my experience, if I then assemble and publish this library any developer that relies up on it will get errors in build unless they add this to their build.gradle:
android {
// ...
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
Is there any way to compile my library so that consumers don't have to do this? Assume that the public API surface of the library does not contain any Java 8 specific features.

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.

Android: Dex cannot parse version 52 byte code

I just switched to Android Studio 2.1 and this error showed up when trying to compile an app the was previously working:
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.
I had already updated the main project's gradle.build file to force Java 1.7 code generation:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.0'
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
}
I had also updated the module gradle.build as follows to set the java version:
android {
compileSdkVersion 19
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.abc.def"
minSdkVersion 19
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
The submodule being built with Maven. In the pom.xml file I have also tried to force 1.7 code generation.
I understand that I am using an assembly artifact, which incorporates subordinate modules, but i have not changed any of the subordinate modules and the resulting .jar file for the module ran fine last time I compiled.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId> <!-- maven-compiler-plugin -->
<version>2.6</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
My question:
Is this an Android Studio 2.1 problem? Have others seen it?
Assuming this is my error and since the error message gives no help in finding the bad module, are there any recommendations on finding the V52 code? I can't just omit the libraries without breaking large amount of code. Can one inspect a .jar file to find the code revision?
just use java 1.8 with Android Studio 3.0+ and set following works for me:
it seems need the latest build tools
classpath 'com.android.tools.build:gradle:3.0.0'
and
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
...
//jackOptions { // DEPRECATED
//enabled true
//}
}
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
If you have a module with a java library that is not Android-specific, this should work: apply plugin:'java'
Put it at the top of the build.gradle file, then rebuild.
apply plugin: 'java'
apply plugin: 'jacoco'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.11'
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
If you use org.jetbrains:annotation:15 and retrolambda plugin then remove line compile org.jetbrains:annotations:15.0 from your build.gradle and the error will disappear. It works for me.
Possibly, some of your dependencies was compiled with Java 8, not for Android especially. Try to switch that dependencies to older version. I don't exactly know which library you should downgrade, because you haven't attached a list of dependencies of your main module.
For example: I had the same problem. After hours of searching, I've found that library org.codehaus.httpcache4j.uribuilder:2.0.0 requires Java 8, as of github. So, when i've switched to 1.1.0, project has been successfully builded and deployed.
Try to add to main build.gradle in section allprojects
tasks.withType(JavaCompile) {
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
}
or add this in dependencies
sourceCompatibility = 1.7
targetCompatibility = 1.7
in all modules manually
I was able to solve this issue by adding the following lines:
jackOptions {
enabled true
}
to defaultConfig in build.gradle file.
You can follow the guidelines for Java 8 on the link -
https://developer.android.com/guide/platform/j8-jack.html
I had the same problem with the greendao-generator dependency. I mistakenly added that dependency into my build.gradle (compile 'org.greenrobot:greendao-generator:3.1.0') and AndroidStudio showed me the same error message.
Probably it's because that module has been compiled with Java 8.
So I removed that dependency from my build.gradle and all compiled happily :)
I solved this problem as belowed :
apply plugin: 'java'
sourceCompatibility = 1.7
targetCompatibility = 1.7
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
Switching off Instant Run in Android Studio 2.2 with Gradle plugin 2.2.2 fixed it for me. Switching back to an older version of the Gradle plugin (such as 2.2.0) also fixed it, but that's less desirable imho.
This happened to me with Android Studio 2.3.3. The solution I found was to delete the build folder and then Rebuild Project. It was as simple as that.
I also faced same error in Android 2.3.3, after adding few JAR depencies. Problem was due to the depenecy io.netty:netty-all:4.1.16.Final. This 4.1.16 version JAR was compiled with Java 1.8 and all others was generated with Java 1.7.
This get resolved, after including older version of netty(which was generated with Java 1.7) in my build.gradle file.
compile 'io.netty:netty-all:4.1.5.Final'
I have come across this issue when trying to upgrade to auto-value v 1.5 in Android Studio v 2.3.3. Auto-value 1.5 will presumably be compatible with AS 3 (It requires an updated java compiler)
For now auto-value 1.4.1 works.
I face the same issue with Reactive Location APIs Library for Android and RxJava 2.Just update build.gradle to 3.0.1 and reduce the Reactive Location APIs Library for Android and RxJava 2 library version from 1.0.4 to 1.0.3
It works fine in my case.
I have come across this issue when trying to import a jar compiled by jdk 1.8 in Android Studio 3.0. I tried all above solution, but none work. so, I asked the developer of that jar to re-compile it with jdk 1.7, and then it work well, not come across this issue again.
If possible for you:
Upgrade android tools to: classpath 'com.android.tools.build:gradle:3.0.0'
buildToolsVersion "26.0.2"
This should take care of the issue.
Reference: https://developer.android.com/studio/write/java8-support

Categories

Resources