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
Related
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.
When I updated my project to androidx then I've got following error
I also searched but can't find my answer, although project works fine
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.example.villagefoodsecrets", appContext.getPackageName());
}
}
At this point, #RunWith(AndroidJUnit4.class) shows that it is deprecated.
And Context appContext = InstrumentationRegistry.getTargetContext();
.getTargetContext; is in red format looks like not existed. So what can I do here - should I just ignore this?
To use non-deprecated classes, add below in build.gradle (app module level)
androidTestImplementation ‘androidx.test.ext:junit:1.1.1’
Then replace below deprecated imports in ExampleInstrumentedTest class
import androidx.test.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;
with
import androidx.test.platform.app.InstrumentationRegistry ;
import androidx.test.ext.junit.runners.AndroidJUnit4;
and to get a context use below:
for Java
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
For Kotlin
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
wish this helps you
cannot instantiate viewModelProviders in activity
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
public class MainActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(this, new Observer <List<Note>> () {
#Override
public void onChanged(#Nullable List <Note> notes) {
//update RecyclerView
Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_SHORT).show();
}
});
}
}
As you are using Android x make sure you implement those dependencies in your build.gradle file
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
annotationProcessor "androidx.lifecycle:compiler:$lifecycle_version"
Then make sure you indicate those properties in your gradle.properties
android.useAndroidX=true
android.enableJetifier=true
Finally you should check if you import AppCompatActivity from Androidx not support
import androidx.appcompat.app.AppCompatActivity;
Besides Amine's answer i think it's worth checking if you are using the proper viewmodel provider.
ViewModelProvider does not have .of()... However, ViewModelProviders notice the s in the end has the method .of().
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"
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);
}
}