Dagger 2 Component class not generated - java

i'm currently learn dagger2 but i have problem is daggercomponent class not generated and i can't know the reason
implementation 'com.google.dagger:dagger-android:2.27'
implementation 'com.google.dagger:dagger-android-support:2.27'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.27'
public class Nugets {
#Inject
public Nugets() {
}
}
public class Water {
#Inject
public Water() {
}
}
public class Coffee {
Nugets nugets;
Water water;
#Inject
public Coffee(Nugets nugets, Water water) {
this.nugets = nugets;
this.water = water;
}
}
#Component
public interface CoffeeComponent {
Coffee getCoffee();
}
when i tried to to find DaggerCoffeeComponent class i didn't find it.
note i rebuild project

You have to implementation dagger instead of dagger-android. Don't forget to make project or rebuild
implementation 'com.google.dagger:dagger:2.27'
annotationProcessor 'com.google.dagger:dagger-compiler:2.27'

Question you asked i encountered many times, And the lucky thing is i know how to solve it.
here are simple methods you can follow to solve it.
You can clean/rebuild project to solve it.
You can invalidate/cache restart your android studio to solve it.
You can delete cache folder where you installed your android studio.but you have to make sure your android studio closed. Now you can restart it, your problem is gone.
You can rename dagger component and rebuild your project to generate class. This solve your issue.
Here are some quick methods,you can try to solve it.

If someone still faced this problem, just use those dependencies below it works for me :
implementation 'com.google.dagger:dagger-android:2.44.2'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
annotationProcessor 'com.google.dagger:dagger-compiler:2.20'
try always to use the latest version of the dagger, use this link to get the latest version: https://github.com/google/dagger/releases

Related

IntelliJ does not recognize fillStateContainer, getDefaultState(), or getPlacementHorizontalFacing() Forge 1.16.5

I am making a custom two block long model called "littleguys:operating_table" and I watched tutorials to make it face the direction I want when it is placed. I made a custom OperatingTable class here:
package com.soliid.littleguys.blocks;
import net.minecraft.block.*;
import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.StateContainer;
import net.minecraftforge.common.ToolType;
public class OperatingTable extends HorizontalBlock
{
public OperatingTable()
{
super(AbstractBlock.Properties.of(Material.STONE)
.harvestLevel(1)
.harvestTool(ToolType.PICKAXE)
.sound(SoundType.STONE)
.requiresCorrectToolForDrops()
.strength(3.5f, 4.0f)
);
}
#Override
protected void fillStateContainer (StateContainer.Builder<Block, BlockState> builder)
{
builder.add(FACING);
}
#Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
return this.getDefaultState().with(FACING, context.getPlacementHorizontalFacing().getOpposite());
}
}
The #Override gives me an error reading
Method does not override method from its superclass, getDefaultState() gives me Cannot resolve method 'getDefaultState' in 'OperatingTable', and getPlacementHorizontalFacing() gives me Cannot resolve method 'getPlacementHorizontalFacing' in 'BlockItemUseContext'.
I now realized that these methods are not in OperatingTables's superclasses (HorizontalBlock and Block) but I want to know which methods are now used instead. There are no errors in the registry of RegistryObject<Block> OPERATING_TABLE or RegistryObject<Item> OPERATING_TABLE_ITEM.
This class is not complete but I cannot continue until I resolve the error.
There are several possibilities, but most cases are cache issue.
Please try invalidate cache and restart IntelliJ.
If issue persist, then sync Gradle project manually.
In build.gradle, on line 34, mappings is defined as channel: 'official', version: '1.16.5'. This should be changed to channel: 'snapshot', version: '[snapshot version]'. The version I used was '20210309-1.16.5'. Then, rebuild the gradle in Terminal using gradlew genIntellijRuns.

How to use Room library?

In my application i want use database and for this i used Room library.
I added this dependencies :
implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'
But when added Database class show me show below error in build tab :
cannot access Publisher
My Database class :
#Database(entities = {TitlesEntity.class},version = 1,exportSchema = false)
public abstract class TitlesDatabase extends RoomDatabase {
public abstract TitlesDao titlesDao();
}
How can i fix it?
You are using obsolete dependencies; for Android Jetpack use:
annotationProcessor "androidx.room:room-compiler:2.2.2"
testImplementation "androidx.room:room-testing:2.2.2"
implementation "androidx.room:room-runtime:2.2.2"
The "cannot access Publisher" likely comes from Android Architecture and/or it's RxJava bindings. Beside that, the question is too broad to provide an accurate answer - and there are enough tutorials.

Kotlin's JvmDefault - still need to declare the method ?

I am building my Spring Boot 1.5 + Kotlin 1.2.41 project into a jar. One of the interfaces in the jar has the #JvmDefault and it compiles fine with the flag (if I remove the flag, it fails).
Now, I am trying to use this interface in another java project, in which I define the Kotlin project as a dependency.
In one implementing class, I don't override the default method. Intellij seems to be OK with it, as it doesn't complain. However, when I compile with Maven, I get :
[ERROR] attempting to assign weaker access privileges; was public
If I implement the method (with some dummy implementation), then it compiles... but it defeats the purpose of the default interface.
Any idea what could be wrong ?
When opening the Kotlin interface code from the java project, here's the decompiled code I see :
public interface CrawlerOutput {
#kotlin.jvm.JvmDefault public open fun finalize(): kotlin.Unit { /* compiled code */ }
public abstract fun output(analyzedRepository: com.myCompany.Repository): kotlin.Unit
}
My java code implementing the interface :
public class CsvOutput implements CrawlerOutput {
#Override
public void output(Repository repository) throws IOException {
log.info("own output is receiving some data !");
}
/**
* IF I REMOVE BELOW METHOD, MAVEN CAN'T COMPILE IT ANYMORE,
* COMPLAINING OF WEAKER ACCESS PRIVILEGE
*/
#Override
public void finalize(){
}
}
Am I missing something ?
Thanks
Vincent
Your method name conflicts with java.lang.Object.finalize(). The error should be fixed if you choose a different method name.
Android Studio and JVM always update its versions. As a result of that some of you may experience this error message.
Inheritance from an interface with '#JvmDefault' members is only allowed with -Xjvm-default option
Don't worry . The solution is very simple. Just add below code part to the end of android block of your app level build.gradle file and sync.
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
kotlinOptions {
freeCompilerArgs += [
"-Xjvm-default=all",
]
}
}

Android room persistent: AppDatabase_Impl does not exist

My app database class
#Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract FavoritesDao favoritesDao();
public static AppDatabase getAppDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
Gradle lib:
compile "android.arch.persistence.room:runtime:+"
annotationProcessor "android.arch.persistence.room:compiler:+"
And when i ask for instance it will give this error, AppDatabase_Impl does not exist
in my application class
public class APp extends Application {
private boolean appRunning = false;
#Override
public void onCreate() {
super.onCreate();
AppDatabase.getAppDatabase(this); //--AppDatabase_Impl does not exist
}
}
For those working with Kotlin, try changing annotationProcessor to kapt in the apps build.gradle
for example:
// Extensions = ViewModel + LiveData
implementation "android.arch.lifecycle:extensions:1.1.0"
kapt "android.arch.lifecycle:compiler:1.1.0"
// Room
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
also remember to add this plugin
apply plugin: 'kotlin-kapt'
to the top of the app level build.gradle file and do a clean and rebuild (according to https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)
In Android Studio, if you get errors when you paste code or during the build process, select Build >Clean Project. Then select Build > Rebuild Project, and then build again.
UPDATE
If you have migrated to androidx
def room_version = "2.3.0" // check latest version from docs
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
UPDATE 2 (since July 2021)
def room_version = "2.3.0" // check latest version from docs
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Just use
apply plugin: 'kotlin-kapt'
in app build.gradle
And keep both in dependencies
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
EDIT
In newer version don't need to add both dependencies at a time
Just use, hope it will work.
kapt 'android.arch.persistence.room:compiler:1.1.1'
I had this error when I missed
#Database(entity="{<model.class>})
Ensure that the entity model specified in the annotation above refers to the particular model class and also ensure that the necessary annotation:
#Entity(tableName = "<table_name>" ...)
is properly defined and you'd be good
if you are using kotlin classes to implement database then
use
apply plugin: 'kotlin-kapt'
and
kapt "android.arch.persistence.room:compiler:1.1.1"
in your gradle file, it will work.
For Kotlin Developers
Use this:
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
And add apply plugin: 'kotlin-kapt' to the top of the app level build.gradle.
For Java Developers
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
It is not just about updating your dependencies. Make sure all your Room dependencies have the same version.
implementation 'android.arch.persistence.room:rxjava2:1.1.0-alpha2'
implementation 'android.arch.persistence.room:runtime:1.1.0-alpha2'
annotationProcessor "android.arch.persistence.room:compiler:1.1.0-alpha2"
In the sample snippet above, all my Room dependencies have the same version 1.1.0-alpha2
Agreed with the above answers
The solution is as below. Change annotationProcessor to kapt as below
// annotationProcessor "androidx.room:room-compiler:$room_version"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
I meet with the problem, because I forget #Dao annotation
#Dao
public interface SearchHistoryDao {
#Query("SELECT * FROM search_history")
List<SearchHistory> getAll();
#Insert
void insertAll(SearchHistory... histories);
#Delete()
void delete(SearchHistory history);
}
Room Official tutorial
make sure to add correct dependency for room in build.gradle
ext {
roomVersion = '2.1.0-alpha06'
}
// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
And below line at the top-
apply plugin: 'kotlin-kapt'
I met this problem because I have forgotten the apt dependences
implementation "android.arch.lifecycle:extensions:$archLifecycleVersion"
implementation "android.arch.persistence.room:runtime:$archRoomVersion"
annotationProcessor "android.arch.lifecycle:compiler:$archLifecycleVersion"
annotationProcessor "android.arch.persistence.room:compiler:$archRoomVersion"
after added the annotationProcessor, and rebuild it, the problem solved.
If you are using kotlin, add kotlin annotation processor plugin to app level build.gradle
plugins {
id "org.jetbrains.kotlin.kapt"
}
Also remove annotationProcessor and replace it with kapt
Instead of
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
}
Use
dependencies {
def room_version = "2.3.0"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
}
The annotationProcessor only works in java environment. The kapt takes care of both java and kotlin. If something wrong with your implementation, those plugins will show them at the compile time.
Had the same problem. Implemented the few classes and interface as officially told in a new example project created by Android Studio:
https://developer.android.com/training/data-storage/room/
All mentioned solutions above did not help, the necessary _Impl files according to my database class were not generated by Room. Finally executing gradle clean build in terminal gave me the hint that lead to the solution:
"warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation annotation processor argument OR set exportSchema to false."
I added the parameter exportSchema = false in the database class
#Database(entities = arrayOf(User::class), version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
And then it worked, found these two generated files in the app module under generatedJava:
AppDatabase_Impl
UserDao_Impl
I don't understand this behaviour as the parameter is said to be optional, see
https://stackoverflow.com/a/44645943/3258117
The question is pretty old, but I've stumbled on this today and none of the provided answers helped me. Finally I managed to resolve it by noticing that google documentation actually is still adopted to Java and not Kotlin by default, actually they have added a comment which I've ignored
For Kotlin use kapt instead of annotationProcessor
So, instead of
annotationProcessor "androidx.room:room-compiler:$room_version"
If you are developing with Kotlin, you should use:
kapt "androidx.room:room-compiler:$room_version"
In my kotlin app, I just added the following line at the top of my build.gradle file :
apply plugin: 'kotlin-kapt'
And the following line in the dependencies section:
kapt "androidx.room:room-compiler:2.2.5"
I hope it fixes your issue.
In my case, I was testing the connectivity for room database and I have put the testing class inside the directory which I have created inside the AndroidTest folder. I have moved it out of the custom directory, then it worked pretty well.
The same phenomenon occurred to me.
following
implementation "android.arch.persistence.room:runtime:1.1.1"
Adding causes another build error but tracks the cause from the log.
In my case, there was an error in the SQL implementation.
After fixing, the build was successful.
So you may want to check the implementation of the entire room library instead of looking at the crashed locals.
Use the following gradle link:
compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'
You need to create a different singleton class and get the AppDatabase from there like this:
RoomDB.java
public class RoomDB {
private static RoomDB INSTANCE;
public static AppDatabase getInstance(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();
//Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
App.java
public class App extends Application {
private boolean appRunning = false;
#Override
public void onCreate() {
super.onCreate();
RoomDB.getInstance(this); //This will provide AppDatabase Instance
}
The issue is more around the correct library that is not included in the gradle build. I had a similar issue and added the missing
testImplementation "android.arch.persistence.room:testing:$room_version
Changing the dependencies in my gradle file did'nt help me in fixing the error.I had missed this Database annotation in class where Room database was initialized which was causing this issue.
#Database(entities = [UserModel::class], version = 1)
Ensure that the entity model specified in the annotation above refers to the particular model class
For Kotlin Developers
if you checked Dao and Entity and also used Kapt and there is no problem, I guess there is a problem with your kotlin version if you are using kotlin 1.4 and above.
update Room to last version from this link.
2.3.0-alpha03 solved my problem.
For me, the Android Studio automatically updated dependencies as soon as you include any of the Room database related imports. But as per https://developer.android.com/jetpack/androidx/releases/room#declaring_dependencies you need to update few. Here is how my code-base looks like:
AppDatabase.kt
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
#Database(entities = arrayOf(MyEntity::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun myDAO(): MyDAO
companion object {
#Volatile private var instance: AppDatabase? = null
private val LOCK = Any()
operator fun invoke(context: Context)= instance ?: synchronized(LOCK){
instance ?: buildDatabase(context).also { instance = it}
}
private fun buildDatabase(context: Context) = Room.databaseBuilder(context,
AppDatabase::class.java, "db-name.db")
.build()
}
}
Update the build.gradle as specified in one of the answers:
apply plugin: 'kotlin-kapt' // this goes with other declared plugin at top
dependencies { // add/update the following in dependencies section
implementation 'androidx.room:room-runtime:2.2.3'
// annotationProcessor 'androidx.room:room-compiler:2.2.3' // remove this and use the following
kapt "androidx.room:room-compiler:2.2.3"
}
Sync the gradle and you should be good to go.
In addition to missing
annotationProcessor "android.arch.persistence.room:compiler:x.x.x"
I had also missed adding the below annotation in my class
#Entity(tableName = "mytablename")
Nothing works from above answers and I noticed that the issue persists for me when I'm using room version2.3.0 or 2.4.2. However, 2.5.0-alpha01 version works well when I applied it.
build.gradle:app
def roomVersion = '2.5.0-alpha01'
implementation "androidx.room:room-ktx:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
testImplementation "android.arch.persistence.room:testing:$roomVersion"
In my case just by changing annotationProcessor to kapt on my room-compiler dependency, did the work.
As of Jan 2023 - I faced a similar issue after refactoring my code to use ServiceLocator class.
I resolved it by going on a spree of changing room versions. It worked with 2.5.0-alpha02
version_room = "2.5.0-alpha02" <-- build.gradle (project)
//Room
implementation "androidx.room:room-runtime:$version_room"
kapt "androidx.room:room-compiler:$version_room"
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$version_room"
implementation("androidx.room:room-guava:$version_room")
I am running the following on Android Eel 2020.1.1:
version_kotlin = "1.7.21"
version_android_gradle_plugin = "4.0.1"
Reading the example here:
Room Example
I fixed this error just using the correct (I guess it is) annotationProcessorFile, as follows:
annotationProcessor "android.arch.persistence.room:compiler:<latest_version>"
Also, I upgraded to 2.2.0 either in Room Version as in Lifecycle version.
Once synchronized the graddle, I could start working with Room.
So, Good luck! And let the code be with you!
Not in the case of OP, but this also happens when you mistakenly use implementation instead of annotationProcessor like this:
implementation "android.arch.persistence.room:compiler:x.x.x"
Instead of this:
annotationProcessor "android.arch.persistence.room:compiler:x.x.x"
Just in case anyone out there should make the same mistake as I did, don't call your database class "Database" or you'll get the same error.
check this annotation above class
#Database(entities = arrayOf(Schedule::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
// ....
}

Integrating an external pure Java library and having Android classes access on it

I'm having troubles trying to get my external Java project so I can use Android classes on it as well. The library is already integrated on the Android project. For instance: I have several model classes on it that I would want to implement Parcelable so they can be seriallized accordingly, but none of the Android classes are available on them.
Clarification I only did this in order to try to solve the issue
So far I've only tried:
Changing and matching the external library's package:
Package name in Android
com.domain.androidproject
Library's package originally
com.domain.libproject
Changed to:
com.dommain.androidproject.libproject
But no luck so far. I imported the library as a Gradle external project vía:
compile project(path: ':LibProject')
Thank you for your help.
You'll have to define a binding between your pure java library and android. You could use Dependency injection to inject the models using the class signature, and then define the parcelable models inside the app (or into another project, like a plugin). Or you could achieve the same using generics. keep in mind, since the java library is already compiled, technically, you can't change it by importing it into the android project (I've seen people "rewriting" some files from a dependency and then adding them with the whole original path to fool the classpath, but that's highly risky since you are not gonna be able to interact with the rest of the dependency's code and if something changes, the thing will break).
if you have access to the pure java's library sourcecode, then modify it to use factories or providers of models. If not, extend the models, add parcelable support, and attempt to use those instead of the original model classes.
Example:
let's suppose we have a model and some functions using it:
public class myModel{
private int id;
private String name;
public void setId(int id){
this.id = id;
}
//more getters and setters
}
public interface myModelCreator<T>{
public myModel create(T toModel);
public T uncreate(myModel fromModel);
}
public static void doSomething(myModel model){
//some library operations
}
Now, in the android project:
public class myAndroidModel extends myModel implements Parcelable{
/*Implements the parcelable methods using the class accessors, or you can change the myModel members to protected.*/
}
public class myAndroidModelCreator implements myModelCreator<myAndroidModel>{
#Override
public myModel create(myAndroidModel toModel){
//create the myModel using the parcelable class.
}
#Override
public myAndroidModel uncreate(myModel fromModel){
//reverse operation.
}
}
Now, in the android project, you can use the parcelable subclass everywhere, and everytime you need to call the library, you can supply the creator interface using the parcelables as arguments.
Another alternative would be changing the library method signatures to something like this:
public static void<T extends myModel> doSomething(T model){
//some library operations
}
So you can directly consume the parcelable subclasses. But depending on your hierarchy, that may be not possible. Lastly, you could attempt to implement dependency injection into the java project using Guice and Roboguice in the android project. Since roboguice uses guice, it is possible they can interoperate, but that's a long shot.
I like Fco P.'s answer, but for the sake of completness, here is an alternative answer.
Use json to serialize objects, rather than Parcelable. You can then put your serialized json as a string extra in intent or as string in bundles.
it's faster to implement than using Parcelable, with libraries such as Google GSON or Square moshi.
it's less performant than Parcelable
Generally if you want to make use of classes in another project/library:
File -> New -> Import Module -> Navigate to the directory of an old project/Library -> Ok
Check off the modules you want to import -> OK
Right click the app module -> Open Module Settings -> dependencies -> + -> Module -> The new Module.
Your project should then be usable in whatever project you just did that for.
Create an android library project with packagename com.domain.libproject
Copy all the sources in src folder.
Update jar dependencies in build.gradle and after that you can make your class in the library parcelable.
Let me know if any issues.
Best regds

Categories