Android studio does not recognize generated custom class - android

My problem is strictly connected with IDE. I have a custom class generator and its working correctly (Classes are in gen folder after build project). Unfortunately, Android Studio cannot resolve those classes in my project. But the funniest part is: When I hardcode correct import and use this class (even when compilator turns this part to red) and run a project... it works.
I tried clean project / rebuild / reset Android Studio... nothing helped. Any ideas how to solve this?

Solution:
In my build.gradle I had to add this generated directory into source set - in my case:
android {
...
sourceSets {
main.java.srcDirs += 'build/generated/source/apt'
}
}

I hade same problem after editing gradle.build module level,
after many searching i found out that a removed
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
and after re add every thing works fine

Related

Android: Can't import class from another module

I have an Android app module, an Android library module :foo, and a pure java module :bar. In :foo, I have a Foo class, and in :bar, I have a Bar class.
I would like to reference Foo from my Bar class. When I hover over the Foo variable, Android Studio gives me the following suggestion:
Add dependency on module 'MyApplication.foo.main'
When I do as Android Studio suggests, it adds a dependency in build.gradle for the :bar module. After adding the suggested code, this is what my build.gradle looks like:
build.gradle (:bar)
plugins {
id 'java-library'
}
java {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
dependencies {
implementation project(path: ':foo')
}
Except there's one small problem; Foo still can't be resolved! Does anyone know how I can import it?
Here is what my project structure looks like:
So it turns out you cannot add an Android module as a dependency to a pure java module, which I guess makes sense.
Android Studio IDE was just suggesting something that cannot be done, which is misleading.

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.

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 Lambda expressions are not allowed at this language level

I am working on a chat application. I got a demo app from github, the chat app that is working. In some classes they used lambda expressions but it's working fine, but when I copy those code mine is giving this error " Lambda expressions are not allowed at this language level ". Some people said that android studio does not support lambda expressions but the demo app is working on my phone.
in build.gradle there should be
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
And you should probably use RetroLambda. Look at the demo project's build.gradle file
In their Android project they are using RetroLambda -
a plugin that allows lambdas in lower versions of Java,
which includes Android (see the plugin at the top --> https://github.com/NaikSoftware/StompProtocolAndroid/blob/master/example-client/build.gradle)
Follow the setup here: https://github.com/evant/gradle-retrolambda

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