Cannot find symbol class AutoValueGson_AutoValueGsonFactory - android

I hope you can help me, I'm trying to implement autovalue in my android project, but the code is not been generated.
I added in app/build.gradle:
provided 'com.google.auto.value:auto-value:1.5.3'
annotationProcessor 'com.google.auto.value:auto-value:1.5.3'
annotationProcessor 'com.ryanharter.auto.value:auto-value-gson:0.7.0'
compile 'com.ryanharter.auto.value:auto-value-gson-annotations:0.7.0'
In android studio 3.0 default settings in compiler > annotation processors I checked Enable annotation processing, and selected Obtain processors from project classpath.
I created AutoValueGsonFactory class like this:
import com.google.gson.TypeAdapterFactory;
import com.ryanharter.auto.value.gson.GsonTypeAdapterFactory;
#GsonTypeAdapterFactory
public abstract class AutoValueGsonFactory implements TypeAdapterFactory {
// Static factory method to access the package
// private generated implementation
public static TypeAdapterFactory create() {
return new AutoValueGson_AutoValueGsonFactory();
}
}
Then I click on Build > Rebuild Project
But is not working, throw this error:
Error:(16, 20) error: cannot find symbol class AutoValueGson_AutoValueGsonFactory
What I'm doing wrong or what I'm missing?

Create one model class and then rebuild the project.
The Error will be gone by now :)
Below is a sample model class created using ROBOPOJO plugin in android studio
import com.google.auto.value.AutoValue;
import com.google.gson.TypeAdapter;
import com.google.gson.annotations.SerializedName;
import com.google.gson.Gson;
import com.intellicar.finder.Data;
#AutoValue
public abstract class LoginData{
#SerializedName("msg")
public abstract String msg();
#SerializedName("data")
public abstract Data data();
#SerializedName("err")
public abstract Object err();
#SerializedName("status")
public abstract String status();
public static TypeAdapter<LoginData> typeAdapter(Gson gson) {
return new AutoValue_LoginData.GsonTypeAdapter(gson);
}
}

Related

Cannot resolve symbol DaggerAppComponent

There is my GitHub project - https://github.com/alekseytimoshchenko/MVVM_FLOW that I haven't used a lot of time and today I decided to check it out as a result I faced some compile/Gradle/build issues, so I needed to update some dependency and other things. Eventually looks like everything is ok, however, I got a compile
error: cannot find symbol
import com.example.krokosha.quizyourself.di.components.DaggerAppComponent;
This likely happens due to some specific dependencies or their version, I went through a few answers here on SO, but looks like everything is correct, but still, this class is not generated.
If it is suitable you are welcome to take a look at the project by the ref provided above, otherwise, I'll put some code here
There are my grale dependencies
...
//Dependency Injection
api "com.google.dagger:dagger:2.35.1"
annotationProcessor "com.google.dagger:dagger-compiler:2.28.3"
api "com.google.dagger:dagger-android:2.35.1"
api "com.google.dagger:dagger-android-support:2.28.3" // if you use the support libraries
annotationProcessor "com.google.dagger:dagger-android-processor:2.28.3"
...
and there is my class where I got an error:
package com.example.krokosha.quizyourself.di;
import android.content.Context;
import com.example.krokosha.quizyourself.di.components.AppComponent;
import com.example.krokosha.quizyourself.di.components.BaseComponent;
import com.example.krokosha.quizyourself.di.components.BaseComponentBuilder;
import com.example.krokosha.quizyourself.di.components.DaggerAppComponent;
import com.example.krokosha.quizyourself.di.moduls.AppModule;
import com.example.krokosha.quizyourself.di.moduls.BaseModule;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;
import javax.inject.Provider;
public class ComponentsHolder
{
private final Context context;
#Inject
Map<Class<?>, Provider<BaseComponentBuilder>> builders;
private Map<Class<?>, BaseComponent> components;
private AppComponent appComponent;
public ComponentsHolder(Context context)
{
this.context = context;
}
public ComponentsHolder init()
{
appComponent = DaggerAppComponent.builder().appModule(new AppModule(context)).build();
appComponent.injectComponentsHolder(this);
components = new HashMap<>();
return this;
}
public BaseComponent getBaseComponent(Class<?> cls)
{
return getBaseComponent(cls, null);
}
public BaseComponent getBaseComponent(Class<?> cls, BaseModule module)
{
BaseComponent component = components.get(cls);
if (component == null)
{
BaseComponentBuilder builder = builders.get(cls).get();
if (module != null)
{
builder.module(module);
}
component = builder.build();
components.put(cls, component);
}
return component;
}
public void releaseBaseComponent(Class<?> cls)
{
components.put(cls, null);
}
}
What is a possible problem here?
DaggerAppComponent is the file Dagger generates as an implementation of your AppComponent interface. If DaggerAppComponent is not being generated, it usually means that Dagger has emitted an error that you can read further up in your compiler log. If you see one of those errors, please edit it into your question.
Also, note that you're mismatching your Dagger versions: your libraries are 2.35.1 but your annotation processor is 2.28.3. In some cases this mismatch can cause a compilation failure, though it's more typical for it to cause the generated DaggerAppComponent file to fail to compile within the generated code. (The generated code calls the dagger libraries at runtime, so if the versions don't match the generated code might call into a class or method that doesn't exist.) Because DaggerAppComponent is missing entirely, it's less likely that the version skew is the root cause of your error here, but in any case you should always have all your Dagger versions consistent: api/implementation/annotationProcessor and between dagger and dagger.android.

How to import WorkManager class

I have this:
import android.arch.*;
public class ScrollingActivity extends AppCompatActivity {
private PeriodicWorkRequest notificationWorkSingle;
private WorkManager mWorkManager;
}
I love how not a single tutorial shows us which class to import
How to import PeriodicWorkRequest and WorkManager?
Of course:
import androidx.work.WorkManager;
import androidx.work.PeriodicWorkRequest;
and use this in build.gradle
dependencies {
implementation "android.arch.work:work-runtime:1.0.0-beta02"
}
and
dependencies {
classpath "android.arch.work:work-runtime:1.0.0-beta02"
}
A small addition to the answer by Alexander Mills:
Here's the link to the official docs on how to add Architecture Components to your project ->
Adding Components to your Project

mvvm dagger 2 ViewModelInjector

there, i need help, im a .NET developer, but now i want to try android.
i downloaded a project from here : https://github.com/gahfy/MVVMPosts
im having problems with the BaseViewModel.kt.
i know that DaggerViewMOdelInjector is created by dagger 2 with my interface ViewModelInjector, but it is not created and i cannot build the project, please help me !!
package net.gahfy.mvvmposts.base
import android.arch.lifecycle.ViewModel
import net.gahfy.mvvmposts.injection.component.DaggerViewModelInjector
import net.gahfy.mvvmposts.injection.component.ViewModelInjector
import net.gahfy.mvvmposts.injection.module.NetworkModule
import net.gahfy.mvvmposts.ui.post.PostListViewModel
import net.gahfy.mvvmposts.ui.post.PostViewModel
abstract class BaseViewModel:ViewModel(){
private val injector: ViewModelInjector = DaggerViewModelInjector
.builder()
.networkModule(NetworkModule)
.build()
init {
inject()
}
/**
* Injects the required dependencies
*/
private fun inject() {
when (this) {
is PostListViewModel -> injector.inject(this)
is PostViewModel -> injector.inject(this)
}
}
}
update like:-
implementation "com.google.dagger:dagger:$dagger2_version"
kapt "com.google.dagger:dagger-compiler:$dagger2_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger2_version"
// compileOnly "org.glassfish:javax.annotation:3.1.1"

AutoValue Gson Extension not creating AutoValueGsonTypeAdapterFactory

I'm trying to use AutoValueGsonTypeAdapterFactory but the class is never generated. Here is my implementation.
#AutoValue
public abstract class Foo {
public abstract String bar();
#SerializedName("Baz") abstract String baz();
public abstract int quux();
public static TypeAdapter<Foo> typeAdapter(Gson gson) {
return new AutoValue_Foo.GsonTypeAdapter(gson);
}
}
//Gson
compile "com.google.code.gson:gson:2.8.0"
//AutoValue
apt "com.google.auto.value:auto-value:1.3"
apt "com.ryanharter.auto.value:auto-value-gson:0.4.5"
provided "com.ryanharter.auto.value:auto-value-gson:0.4.5"
The typeAdapter method has a warning "never used" and AutoValueGsonTypeAdapterFactory is never generated
Seems that the AutoValueGsonTypeAdapterFactory is autogenerated until version 0.4.0.
Now the suggested approach is to create a single implementation of TypeAdapterFactory for all of your auto-value-gson classes annotated with #GsonTypeAdapterFactory. Read the auto-value-gson docs.
AutoValue: Gson Extension now requires that you annotate a class with #GsonTypeAdapterFactory since the auto generating solution didn't support multiple module projects.
You can find the details in the documentation.

Dagger 2: No implementation generated for component interface

I have created a demo Android Lib project and used dagger 2.0 with the following steps:
Added the following jars to /libs folder:
dagger-2.0.jar
dagger-compiler-2.0.jar
dagger-producers-2.0-beta.jar
guava-18.0.jar
javawriter-2.5.1.jar
javax.annotation-api-1.2.jar
javax.inject-1.jar
Project -> Properties -> Java Compiler -> Annotation Processing (Enabled annotation processing)
Project -> Properties -> Java Compiler -> Annotation Processing - Factory path: Added all the above mentioned jars.
Created the following classes:
public class Car {
private Engine engine;
#Inject
public Car(Engine engine) {
this.engine = engine;
}
public String carDetails(){
String engineName = this.engine.getName();
int engineNumber = this.engine.getNumber();
return "This car has the following details: \n" + engineName + "----" + engineNumber;
}
}
public interface Engine {
public String getName();
public int getNumber();
}
public class Toyota implements Engine{
#Override
public String getName() {
return "This is toyota engine";
}
#Override
public int getNumber() {
return 1234567890;
}
}
#Component(modules = EngineModule.class)
public interface EngineComponent {
void inject();
}
#Module
public class EngineModule {
public EngineModule(DemoApplication demoApplication) {
}
#Provides
Engine provideEngine(){
return new Toyota();
}
}
But inside /.apt-generated folder there are only two files:
Car_Factory.java EngineModule_ProvideEngineFactory.java
DaggerEngineComponent.java is not there for me to build the component.
Could someone please help?
I'm guessing the annotation processor is encountering an error and Eclipse is not showing you the log. If you have log output in the Output view, you may want to paste that into the question.
Specifically, I think it's erroring out on void inject(), which isn't a format descibed in the #Component docs. Those docs describe three types of methods:
Parameterless factory methods that return an injectable type Dagger creates and injects, like Engine createEngine(), or
Single-parameter void methods that receive an instance created elsewhere and apply method and field injection, like void injectEngine(Engine) or Engine injectEngine(Engine).
Subcomponent-returning methods that combine your Component's bindings with those from another module.
Because your void inject() doesn't match any of those formats, Dagger is likely erroring out and refusing to create a DaggerEngineComponent.

Categories

Resources