Flutter Error: PluginRegistry cannot be converted to FlutterEngine - android

Trying to build my app (which has previously worked fine) after switching out a package. Now I'm getting the following:
/Users/rgb/Repositories/cadsys_member_app_flutter/android/app/src/main/java/com/example/member_app_flutter/Application.java:18: error: incompatible types: PluginRegistry cannot be converted to FlutterEngine
GeneratedPluginRegistrant.registerWith(registry);
^
/Users/rgb/Repositories/member_app_flutter/android/app/src/main/java/com/example/cadsys_member_app_flutter/MainActivity.java:13: error: cannot find symbol
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
^
symbol: class MethodChannel
location: class MainActivity
/Users/rgb/Repositories/cadsys_member_app_flutter/android/app/src/main/java/com/example/member_app_flutter/MainActivity.java:13: error: cannot find symbol
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL)
^
symbol: variable CHANNEL
location: class MainActivity
Note: /Users/rgb/Repositories/member_app_flutter/android/app/src/main/java/com/example/member_app_flutter/Application.java uses or overrides a deprecated API.
Here is my Application.java:
package net.xxx.app;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
Haven't been able to find a solution, hope someone can shed some light on this issue.

Related

Application.kt: Unresolved reference: AndroidAlarmManager

I am trying to use the android_alarm_manager_plus package to schedule background tasks in flutter. In their documentation, they have added the Application class code for JAVA, which is as follows -
public class Application extends FlutterApplication implements PluginRegistrantCallback {
#Override
public void onCreate() {
super.onCreate();
AlarmService.setPluginRegistrant(this);
}
#Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
But I'm working on a kotlin based project (without any knowledge of kotlin, I may add) and there is no documentation for this. I tried writing the kotlin counterpart to this code myself, but I'm still facing errors.
My Application.kt file -
package com.example.my_app;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.AndroidAlarmManager.AlarmService;
class Application : FlutterApplication(), PluginRegistry.PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
AlarmService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
registry?.registrarFor("GeneratedPluginRegistrant");
}
}
The errors -
Application.kt: (7, 27): Unresolved reference: AndroidAlarmManager
Application.kt: (15, 9): Unresolved reference: AlarmService
You do not need to add this Java/Kotlin code. That's for the old Android embedding. Note the "DEPRECATED" here in the title: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/android_alarm_manager_plus#flutter-android-embedding-v1-deprecated

Firebase messaging-handle background message in kotlin

I'm using firebase_messaging in my flutter application.
To handle background messages with firebase messaging in pub they suggested to create new Application.java file and replace java file name in AndroidManifest file.
In my application i'm using kotlin and i already implemented some native code in MainActivity.kt
So how to write this code in kotlin.
package io.flutter.plugins.firebasemessagingexample;
import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
public class Application extends FlutterApplication implements PluginRegistrantCallback {
#Override
public void onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
#Override
public void registerWith(PluginRegistry registry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
it is mandatory to replace MainActivity to Application in AndroidManifest file?
Here is the working background notification kotlin code:
package com.example.yourapp
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
Here is the Kotlin code for the new firebase cloud messaging version:
package id.your.app
import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingBackgroundService
// import io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin
class Application : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingBackgroundService.setPluginRegistrant(this)
}
override fun registerWith(registry: PluginRegistry?) {
// FlutterFirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebase.messaging.FlutterFirebaseMessagingPlugin"))
}
}

Dagger component: error: cannot find symbol kotlin classes

Generated component of dagger couldn't find symbol of the kotlin classes even if there is no read lines on this component.
Task :app:compileStagingDebugJavaWithJavac FAILED
\app\build\generated\source\kapt\stagingDebug\...\DaggerAppComponent.java:49: error: cannot find symbol
import com...features.Main.splash_screen.SplashScreenActivity;
^
symbol: class SplashScreenActivity
location: package com...features.Main.splash_screen
When I type the code in java it runs successfully and this issue only happens in windows. It doesn't appear in mac or linux.
interface ISplashScreen : IView {
fun gotoMainScreen()
}
#Module
abstract class SplashModule {
#Binds
abstract fun provideISplash(splashScreenActivity: SplashScreenActivity): ISplashScreen
}
class SplashScreenActivity : BaseActivity<SplashScreenPresenter>(), ISplashScreen {
#Inject
lateinit var splashScreenPresenter: SplashScreenPresenter
override fun gotoMainScreen() {
Navigator.goToMainScreen(context)
}
override fun onCreateInit(savedInstanceState: Bundle?) {
}
override fun getLayout(): Int = R.layout.activity_splash
override fun initPresenter(): SplashScreenPresenter = splashScreenPresenter
}
class SplashScreenPresenter #Inject constructor(view: ISplashScreen,
environment: PresenterEnvironment)
: BasePresenter<ISplashScreen>(view, environment) {
// TODO : load project data by API
#SuppressLint("CheckResult")
override fun startLogic(savedInstance: Bundle?) {
super.startLogic(savedInstance)
Completable.timer(3, TimeUnit.SECONDS)
.compose(getDestroyIoTransformer<Unit>())
.subscribe {
view.gotoMainScreen()
}
}
}
#Module
public abstract class ActivityModule {
#ContributesAndroidInjector(modules = SplashModule.class)
abstract SplashScreenActivity bindSplashScreenActivity();
#ContributesAndroidInjector(modules = { CartFragmentProvider.class, CategoryFragmentProvider.class})
abstract MainActivity bindMainActivity();
}
//Gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-platform-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android'
implementation "com.google.dagger:dagger:2.23.1"
implementation "com.google.dagger:dagger-android-support:2.23.1"
kapt "com.google.dagger:dagger-compiler:2.23.1"
kapt "com.google.dagger:dagger-android-processor:2.23.1"
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:49: error: cannot find symbol
import com....features.Main.splash_screen.SplashScreenActivity;
^
symbol: class SplashScreenActivity
location: package com....features.Main.splash_screen
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:51: error: cannot find symbol
import com....features.Main.splash_screen.SplashScreenPresenter;
^
symbol: class SplashScreenPresenter
location: package com....features.Main.splash_screen
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\modules\ActivityModule_BindSplashScreenActivity.java:3: error: cannot find symbol
import com....features.Main.splash_screen.SplashModule;
^
symbol: class SplashModule
location: package com....features.Main.splash_screen
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\modules\ActivityModule_BindSplashScreenActivity.java:4: error: cannot find symbol
import com....features.Main.splash_screen.SplashScreenActivity;
^
symbol: class SplashScreenActivity
location: package com....features.Main.splash_screen
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\modules\ActivityModule_BindSplashScreenActivity.java:25: error: cannot find symbol
public interface SplashScreenActivitySubcomponent extends AndroidInjector<SplashScreenActivity> {
^
symbol: class SplashScreenActivity
location: class ActivityModule_BindSplashScreenActivity
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\modules\ActivityModule_BindSplashScreenActivity.java:27: error: cannot find symbol
interface Factory extends AndroidInjector.Factory<SplashScreenActivity> {}
^
symbol: class SplashScreenActivity
location: interface SplashScreenActivitySubcomponent
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:217: error: cannot find symbol
SplashScreenActivity arg0) {
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.SplashScreenActivitySubcomponentFactory
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:224: error: cannot find symbol
private final SplashScreenActivity arg0;
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:226: error: cannot find symbol
private SplashScreenActivitySubcomponentImpl(SplashScreenActivity arg0Param) {
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:233: error: cannot find symbol
private SplashScreenPresenter getSplashScreenPresenter() {
^
symbol: class SplashScreenPresenter
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:237: error: cannot find symbol
public void inject(SplashScreenActivity arg0) {
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:240: error: cannot find symbol
private SplashScreenActivity injectSplashScreenActivity(SplashScreenActivity instance) {
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:240: error: cannot find symbol
private SplashScreenActivity injectSplashScreenActivity(SplashScreenActivity instance) {
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:14: error: cannot find symbol
public final class SplashScreenActivity_MembersInjector implements MembersInjector<SplashScreenActivity> {
^
symbol: class SplashScreenActivity
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:25: error: cannot find symbol
private final Provider<SplashScreenPresenter> splashScreenPresenterProvider;
^
symbol: class SplashScreenPresenter
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:33: error: cannot find symbol
Provider<SplashScreenPresenter> splashScreenPresenterProvider) {
^
symbol: class SplashScreenPresenter
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:48: error: cannot find symbol
Provider<SplashScreenPresenter> splashScreenPresenterProvider) {
^
symbol: class SplashScreenPresenter
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:42: error: cannot find symbol
public static MembersInjector<SplashScreenActivity> create(
^
symbol: class SplashScreenActivity
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:52: error: cannot find symbol
public void injectMembers(SplashScreenActivity instance) {
^
symbol: class SplashScreenActivity
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:61: error: cannot find symbol
public static void injectSplashScreenPresenter(SplashScreenActivity instance,
^
symbol: class SplashScreenActivity
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenActivity_MembersInjector.java:62: error: cannot find symbol
SplashScreenPresenter splashScreenPresenter) {
^
symbol: class SplashScreenPresenter
location: class SplashScreenActivity_MembersInjector
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:8: error: cannot find symbol
public final class SplashScreenPresenter_Factory implements Factory<SplashScreenPresenter> {
^
symbol: class SplashScreenPresenter
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:9: error: cannot find symbol
private final Provider<ISplashScreen> viewProvider;
^
symbol: class ISplashScreen
location: class SplashScreenPresenter_Factory
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:13: error: cannot find symbol
public SplashScreenPresenter_Factory(Provider<ISplashScreen> viewProvider,
^
symbol: class ISplashScreen
location: class SplashScreenPresenter_Factory
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:20: error: cannot find symbol
public SplashScreenPresenter get() {
^
symbol: class SplashScreenPresenter
location: class SplashScreenPresenter_Factory
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:24: error: cannot find symbol
public static SplashScreenPresenter_Factory create(Provider<ISplashScreen> viewProvider,
^
symbol: class ISplashScreen
location: class SplashScreenPresenter_Factory
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:29: error: cannot find symbol
public static SplashScreenPresenter newInstance(ISplashScreen view,
^
symbol: class ISplashScreen
location: class SplashScreenPresenter_Factory
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:29: error: cannot find symbol
public static SplashScreenPresenter newInstance(ISplashScreen view,
^
symbol: class SplashScreenPresenter
location: class SplashScreenPresenter_Factory
...\app\src\main\java\com\...\common\di\modules\ActivityModule.java:3: error: cannot find symbol
import com....features.Main.splash_screen.SplashModule;
^
symbol: class SplashModule
location: package com....features.Main.splash_screen
...\app\src\main\java\com\...\common\di\modules\ActivityModule.java:4: error: cannot find symbol
import com....features.Main.splash_screen.SplashScreenActivity;
^
symbol: class SplashScreenActivity
location: package com....features.Main.splash_screen
...\app\src\main\java\com\...\common\di\modules\ActivityModule.java:16: error: cannot find symbol
abstract SplashScreenActivity bindSplashScreenActivity();
^
symbol: class SplashScreenActivity
location: class ActivityModule
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\modules\ActivityModule_BindSplashScreenActivity.java:24: error: cannot find symbol
#Subcomponent(modules = SplashModule.class)
^
symbol: class SplashModule
location: class ActivityModule_BindSplashScreenActivity
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\modules\ActivityModule_BindSplashScreenActivity.java:20: error: cannot find symbol
#ClassKey(SplashScreenActivity.class)
^
symbol: class SplashScreenActivity
location: class ActivityModule_BindSplashScreenActivity
...\app\src\main\java\com\...\common\di\modules\ActivityModule.java:15: error: cannot find symbol
#ContributesAndroidInjector(modules = SplashModule.class)
^
symbol: class SplashModule
location: class ActivityModule
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:143: error: cannot find symbol
return MapBuilder.<Class<?>, Provider<AndroidInjector.Factory<?>>>newMapBuilder(2).put(SplashScreenActivity.class, (Provider) splashScreenActivitySubcomponentFactoryProvider).put(MainActivity.class, (Provider) mainActivitySubcomponentFactoryProvider).build();}
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:234: error: cannot find symbol
return new SplashScreenPresenter(arg0, DaggerAppComponent.this.providePresenterEnvironmentProvider.get());}
^
symbol: class SplashScreenPresenter
location: class DaggerAppComponent.SplashScreenActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\common\di\components\DaggerAppComponent.java:271: error: cannot find symbol
return MapBuilder.<Class<?>, Provider<AndroidInjector.Factory<?>>>newMapBuilder(4).put(SplashScreenActivity.class, (Provider) DaggerAppComponent.this.splashScreenActivitySubcomponentFactoryProvider).put(MainActivity.class, (Provider) DaggerAppComponent.this.mainActivitySubcomponentFactoryProvider).put(CartFragment.class, (Provider) cartFragmentSubcomponentFactoryProvider).put(CategoriesFragment.class, (Provider) categoriesFragmentSubcomponentFactoryProvider).build();}
^
symbol: class SplashScreenActivity
location: class DaggerAppComponent.MainActivitySubcomponentImpl
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:21: error: cannot find symbol
return new SplashScreenPresenter(viewProvider.get(), environmentProvider.get());
^
symbol: class SplashScreenPresenter
location: class SplashScreenPresenter_Factory
...\app\build\generated\source\kapt\stagingDebug\com\...\features\Main\splash_screen\SplashScreenPresenter_Factory.java:31: error: cannot find symbol
return new SplashScreenPresenter(view, environment);
^
symbol: class SplashScreenPresenter
location: class SplashScreenPresenter_Factory
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
39 errors
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:compileStagingDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
Get more help at https://help.gradle.org
Deprecated Gradle features were used in this build, making it incompatible with Gradle 6.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.1.1/userguide/command_line_interface.html#sec:command_line_warnings
BUILD FAILED in 23s
50 actionable tasks: 9 executed, 41 up-to-date
does your project use kapt?
If so, you can disable the cache of kapt.
Open your build.gradle file in the app module, and set the useBuildCache to false, and sync the Gradle.
Try to rebuild your code.
kapt {
useBuildCache = false
}
If everything goes fine, you can enable useBuildCache again.

react-native-file-provider giving error while gradle build

i installed react-native-file-provider and during gradle build i am getting this error. Execution failed for task
':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:5: error: cannot find symbol
import com.facebook.react.ReactApplication;
^
symbol: class ReactApplication
location: package com.facebook.react
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:8: error: cannot find symbol
import com.facebook.react.ReactNativeHost;
^
symbol: class ReactNativeHost
location: package com.facebook.react
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:16: error: cannot find symbol
public class MainApplication extends Application implements ReactApplication {
^
symbol: class ReactApplication
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:18: error: cannot find symbol
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
^
symbol: class ReactNativeHost
location: class MainApplication
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:40: error: cannot find symbol
public ReactNativeHost getReactNativeHost() {
^
symbol: class ReactNativeHost
location: class MainApplication
/../`enter code here`test2/android/app/src/main/java/com/testapp2/MainActivity.java:5: error: MainActivity is not abstract and does not override abstract method getPackages() in ReactActivity
public class MainActivity extends ReactActivity {
^
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:18: error: cannot find symbol
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
^
symbol: class ReactNativeHost
location: class MainApplication
/../test2/android/app/src/main/java/com/testapp2/MainApplication.java:39: error: method does not override or implement a method from a supertype
#Override
^
8 errors

How to inject dependencies into a Robolectric Test-class using RoboGuice

I want to start using Robolectric and RoboGuice in my Android-Apps. While I make satisfactory progress using Robolectric I am stuck using RoboGuice. I created a small Android-App for experimenting. It is only one Activity, injecting a Button and setting its OnClickListener.
In the related Test-Class I want to Inject this Activity, to be able to test the Button. I tried a lot of things I found all over the internet, but none of these worked, so I give it a go here.
Here is some code:
MainActivity.java:
package com.example.TrialApp;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import roboguice.activity.RoboActivity;
import roboguice.inject.InjectView;
public class MainActivity extends RoboActivity implements View.OnClickListener {
#InjectView(R.id.main_LoginButton) private Button loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
loginButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
SharedPreferences.Editor editor;
if (view.getId() == R.id.main_Login_Button)
Log.i("Login-Button pressed... ", "");
}
}
CustomRobolectricTestRunner.java:
package com.example.TrialApp;
import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.junit.runners.model.InitializationError;
import java.io.File;
public class CustomRobolectricTestRunner extends RobolectricTestRunner {
public CustomRobolectricTestRunner(Class testClass) throws InitializationError {
// defaults to "AndroidManifest.xml", "res" in the current directory
super(testClass, new File("TrialApp"));
}
}
MainActivity_Test.java:
package com.example.TrialApp;
import com.google.inject.Inject;
import com.xtremelabs.robolectric.Robolectric;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertNotNull;
#RunWith(CustomRobolectricTestRunner.class)
public class MainActivity_Test {
#Inject MainActivity mainActivity;
#Inject ClassWithoutAName classWithoutAName;
#Before
public void setUp() {
}
#Test
public void mainActivityShouldNotBeNull() {
assertNotNull(mainActivity);
}
#Test
public void classWithoutANameShouldNotBeNull() {
assertNotNull(classWithoutAName);
}
}
classWithoutAName is just a non-Activity-class with no content. I added just for injecting a non-Activity-class.
Running the Test-Class both tests fail giving the following errors:
java.lang.AssertionError
at com.example.TrialApp.MainActivity_Test.mainActivityShouldNotBeNull(MainActivity_Test.java:33) <8 internal calls>
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288) <16 internal calls>
and
java.lang.AssertionError
at com.example.TrialApp.MainActivity_Test.classWithoutANameShouldNotBeNull(MainActivity_Test.java:38) <8 internal calls>
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:288) <16 internal calls>
Obviously something is missing. Injecting in MainActivity works fine, and the app is running.
Why is the same pattern of injecting dependencies in a Robolectric-Testclass not working? Where is the missing link?
Thank you
After reading some more articles and threads I found a partial solution. I still haven't found a way to inject UI-elements in a testclass, but I found out, how to inject non-UI-elements in a testclass. The trick is to implement a "RobolectricTestModule" extending the AbstractModule. The bindings made in the modules declared in the roboguice.xml are not present in the test-environment, so we need to declare the bindings for the test-environment in this extra-module. In the customized Testrunner we replace the DefaultRoboModule with the new RobolectricTestModule.
ClassWithoutAName.java:
package com.example.TrialApp;
public class ClassWithoutAName {
private String string;
public ClassWithoutAName(String string) {
this.string = string;
}
public String getString() {
return string;
}
}
ClassWithoutANameProvider.java
package com.example.TrialApp.GuiceModules;
import com.example.TrialApp.ClassWithoutAName;
import com.google.inject.Provider;
public class ClassWithoutANameProvider implements Provider<ClassWithoutAName> {
#Override
public ClassWithoutAName get() {
return new ClassWithoutAName("testString");
}
}
ClassWithOutANameModule.java
package com.example.TrialApp.GuiceModules;
import com.example.TrialApp.ClassWithoutAName;
import com.google.inject.AbstractModule;
public class ClassWithOutANameModule extends AbstractModule {
#Override
protected void configure() {
bind(ClassWithoutAName.class).toProvider(ClassWithoutANameProvider.class);
}
}
RobolectricTestModule.java
package com.example.TrialApp.GuiceModules;
import com.example.TrialApp.WeirdThings;
import com.google.inject.AbstractModule;
public class RobolectricTestModule extends AbstractModule {
#Override
protected void configure() {
bind(ClassWithoutAName.class).toProvider(ClassWithoutANameProvider.class);
}
}
CustomRobolectricTestRunner.java
package com.example.TrialApp;
import android.app.Application;
import com.example.TrialApp.GuiceModules.RobolectricTestModule;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.RobolectricTestRunner;
import org.junit.runners.model.InitializationError;
import roboguice.RoboGuice;
import java.io.File;
public class CustomRobolectricTestRunner extends RobolectricTestRunner {
public CustomRobolectricTestRunner(Class testClass) throws InitializationError {
// defaults to "AndroidManifest.xml", "res" in the current directory
super(testClass, new File("TrialApp"));
}
#Override
public void prepareTest(Object test) {
Application application = (Application) Robolectric.application;
RoboGuice.setBaseApplicationInjector(application, RoboGuice.DEFAULT_STAGE,
RoboGuice.newDefaultRoboModule(application), new RobolectricTestModule());
RoboGuice.getInjector(application).injectMembers(test);
}
}
Thats it for the non-UI-Elements.

Categories

Resources