Android javax.annotation.processing Package missing - android

I would like to do some annotation processing based on the example in the following link: http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm.
However, I would like to implement this in my Android project, and it seems I cannot use the package with the android platform. Do I need to add an external jar or is there something I'm missing?
Thanks.

The javax.annotation.processing package is not included in Android. You don't need to include this inside your compiled app module. You just need this dependency for your compiler/processor module during build time. Therefore, inside your app's build.gradle you should add the following after including your annotation processor:
provided 'org.glassfish:javax.annotation:10.0-b28'

There is a much easier way if you use Android Studio / Gradle.
I found the following looking through the answers of the question How do I add javax.annotation.Generated to Java SE 5?:
For a Gradle build the dependency should be
compile 'org.glassfish:javax.annotation:10.0-b28'

The javax.annotation.processing package is not included in Android, but the Android VM team describes how to include extra javax packages here - it might be applicable to your question.

implementation 'org.glassfish:javax.annotation:10.0-b28'
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
Add these two dependencies in your build.gradle file, this will work.

compileOnly 'org.glassfish:javax.annotation:10.0-b28'

Simply adding compile 'javax.annotation:javax.annotation-api:1.2' should do the trick.
see here for all available versions: https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api

for kotlin DSL you need to use compileOnly("org.glassfish:javax.annotation:10.0-b28")
for groovy DSL you need to use compileOnly 'org.glassfish:javax.annotation:10.0-b28'

I also faced same issue.
So generally we will face this issue when our jdk version will be 8+ .
So for jdk8+ you just need to add below dependency in your build.gradle(:app) file.
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
It will work like a charm. Please give it a try!

I find a method in butterknife. Add this in build.gradle:
compile files(org.gradle.internal.jvm.Jvm.current().getJre().homeDir.getAbsolutePath()+'/lib/rt.jar')

Related

Duplicate class kotlin classes kotlin version 1.3.70

Error while adding MoshiPack Library in Kotlin latest version 1.3.70 to gradle.build application
Moshi pack
implementation 'com.daveanthonythomas.moshipack:moshipack:1.0.1'
Error Message
Duplicate class kotlin.reflect.KClasses found in modules jetified-kotlin-reflect-1.1.1.jar (org.jetbrains.kotlin:kotlin-reflect:1.1.1) and jetified-kotlin-stdlib-1.3.70.jar (org.jetbrains.kotlin:kotlin-stdlib:1.3.70)
Any suggestions how to solve this issue or any other library I can use in Kotlin so I can use Message Pack.
Thanks in advance
Try to add this to your dependencies:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
and make sure you specified your Android NDK location under File>Project Structure...>SDK Location
Starting Kotlin 1.3.70 some basic useful members on KClass included in Kotlin standard library (they were in a kotlin-reflect before).
See "Working with KClass" in https://blog.jetbrains.com/kotlin/2020/03/kotlin-1-3-70-released/
In your case MoshiPack adds a kotlin-reflect library that conflicts with standard library.
You should exclude transitive dependency to resolve the conflict.
If you use KotlinDSL, in build.gradle.kts:
implementation ("com.daveanthonythomas.moshipack:moshipack:1.0.1") {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-reflect")
}
If you use Groovy, in build.gradle:
implementation ('com.daveanthonythomas.moshipack:moshipack:1.0.1') {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}
I tried this and it worked
implementation "org.jetbrains.kotlin:kotlin-reflect:1.4.10"
i think the only way to solve it , to go back to kotlin version 1.3.61 , so remove 1.3.70 and use 1.3.61
So I finally figured it out and here's how :-
So the main problem is you have injected 2 dependency of same class. Here he used 2 dependency for Kotlin which conflict in runtime to fix that you have to check which dependency is duplicated. (It is most case scenario. It can be with any other dependency i.e. Hilt)
Go to File > Project structure > Dependencies
Check which dependency are repeating. In this case it will have (androidx.core:core:1.8.0) and (androidx.core:core:+)
as you can see there are 2 dependency with same classes version 1.8.0 will have all the class which core:+ will have and this is causing a error.
Now delete (androidx.core:core:+) and hit Apply and sync project.
Now you should be good to go. here is structure after changing dependency.
Note:- This method will show all the android dependency which you might be not included but you will see all the dependency which any app has. Please remove the dependency who are you familiar with do not remove any dependency without any proper knowledge.

androidx.lifecycle.DefaultLifecycleObserver not found

I am having a problem where my build cannot find one of the androidx classes, specifically:
androidx.lifecycle.DefaultLifecycleObserver
Here are my gradle deps:
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0"
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0"
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0"
I don't have any other problems getting androidx classes, just this one.
I have tried to invalidate caches in android studio and restart with no luck. Also a build clean as well as a gradle sync.
I verified here:
https://developer.android.com/jetpack/androidx/migrate
The mapping of that class from the support library to androidx:
android.arch.lifecycle.DefaultLifecycleObserver ->
androidx.lifecycle.DefaultLifecycleObserver
Any ideas on why this is happening?
I think that's because you're missing one dependency:
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
Get the latest version of lifecycle-common-java8 here
See the documentation:
If you use Java 8 Language, then observe events with
DefaultLifecycleObserver. To include it you should add
"androidx.lifecycle:lifecycle-common-java8:" to your
build.gradle file.

AsyncTask.java cannot resolve symbol for Nullable, Mainthread, WorkerThread

I'm pretty new to android studio, but i got this error and i can't run my project. I saw a lot of answers which suggest to add the support-annotations dependency, but I'm using appcompat so i don't need that. Any thoughts?
Do you have the Android Support Library?
To do it add this to your build.gradle file of the "app" module:
dependencies {
...
implementation "com.android.support:support-annotations:27.1.0"
}
Then sync the project.
Sometimes you can do this automatically by pressing Alt+Enter over the error message and selecting the solution.
You need to fetch the libraries that contain the imports, in your build gradle file of your module add the line inside dependencies tag:
api 'com.android.support:support-annotations:X.X.X'
or
implementation 'com.android.support:support-annotations:X.X.X'
Where X.X.X is the version of the rest of your support libraries.
You should read this article that explains how to implement this with more detail.

How to get a compile error if a JAR dependency is missing?

I have an Android project with the following dependencies:
-- Android App
---> MySDK.Jar
------> 'org.apache.commons:commons-lang3:3.5'
This is MySDK.jar that has a dependency on commons-lang3.
I'm working on Android Studio and I'm thus using Gradle.
Here is my problem:
I have shared "MySDK.Jar" to someone and he has built his own Android App on top of it.
It works but we have seen that the compiler doesn't notice the missing dependency on 'org.apache.commons:commons-lang3:3.5'. At run-time there will be a crash if the code using 'org.apache.commons:commons-lang3:3.5' is called. One may not notice the problem if he doesn't call the code using this library.
I know that we can solve this issue by adding the following line to Android App build.gradle file:
compile 'org.apache.commons:commons-lang3:3.5'
I'm wondering if there is a way to get a compile error indicating such missing dependencies? It is indeed better to see the dependency problem at compilation time rather than at runtime.
What are the recommended good practices for this?
Thanks!
commons-lang3 is a transitive dependency of Android App. As such, it is often not needed for compilation - there are exceptions, especially regarding multiple levels of inheritance. So at compile time you (usually) do not know whether you miss a transitive dependency that you need at runtime.
This is where Gradle comes in. Gradle can (as Maven) resolve dependencies transitively from a Maven repository (as MavenCentral). If you put MySDK into a Maven repository (like Nexus or Artifactory, which have open source versions), everyone using MySDK will automatically draw commons-lang3 so you will not miss anything at runtime.
If you are just adding the jar file in your project you can't warning about the missing dependencies.
To do it you have to publish the jar file in a maven repo.
In this way you have a pom file which describes the dependencies that gradle has to download.
Provide a method like MySDK.init() int your MySDK.jar,call a method whe is belong to org.apache.commons:commons-lang3:3.5' in the MySDK.init() method, then put init() into onCreate() of your Application,
Another way is,putorg.apache.commons:commons-lang3:3.5 into MySDK.jar,
Hope it helps you :)

Do I need Gradle dependency for RecyclerView?

I forgot to include this in Gradle -
compile 'com.android.support:recyclerview-v7:23.+'
But my project works fine without this record.
So, why do I need to write it in Gradle?
Can I use only appcompat?
compile 'com.android.support:appcompat-v7:23.2.0'
com.android.support:appcompat-v7 does not depend on com.android.support:recyclerview-v7, but com.android.support:design does.
There's no harm in including this dependency one more time in your build.gradle, but it's not necessary since design includes it.

Categories

Resources