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
Related
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.
I have checked all answers on the stack. But nothing helps. Might be problem in Kotlin + DI
So I got an exception that Eventbus for some reason cant initialize itself in presenter class. By debugger I see that method of initializing executed proper way from onCreate. I use the same code as I use in other classes (Java classes) and other works correctly. Could you please give an advice where might be an issue
Error
Caused by: org.greenrobot.eventbus.EventBusException: Subscriber class com.myapp.mvp.ui.myprofile.MyProfileActivity and its super classes have no public methods with the #Subscribe annotation
at org.greenrobot.eventbus.SubscriberMethodFinder.findSubscriberMethods(SubscriberMethodFinder.java:67)
at org.greenrobot.eventbus.EventBus.register(EventBus.java:140)
at com.myapp.mvp.ui.myprofile.MyProfileActivity.onCreate(MyProfileActivity.kt:67)
at android.app.Activity.performCreate(Activity.java:7981)
at android.app.Activity.performCreate(Activity.java:7970)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
My presenter class
import com.arellomobile.mvp.InjectViewState
import com.arellomobile.mvp.MvpPresenter
import com.myapp.UserProfileItemsList
import com.myapp.eventbus.VisitsEvent
import com.myapp.eventbus.UserEvent
import com.myapp.models.UserDescriptionModel
import com.myapp.mvp.model.interactor.myprofile.MyProfileInteractor
import com.myapp.utils.ActionsCountInfoCallback
import com.myapp.utils.UserCallback
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import java.util.ArrayList
import javax.inject.Inject
#InjectViewState
class MyProfilePresenter #Inject constructor(private val interactor: MyProfileInteractor) : MvpPresenter<MyProfileView>() {
private val userDescriptionList = ArrayList<UserDescriptionModel>()
override fun onFirstViewAttach() {
super.onFirstViewAttach()
setAllCurrentUserInfo()
setActionsCount()
}
fun setAllCurrentUserInfo() {
interactor.getAllCurrentUserInfo(UserCallback{ fsUser ->
viewState.setUserData(fsUser.name, fsUser.age, fsUser.country, fsUser.image)
userDescriptionList.addAll(UserProfileItemsList.initData(fsUser))
viewState.setList(userDescriptionList)
EventBus.getDefault().post(UserEvent(fsUser))
})
}
private fun setActionsCount() {
interactor.getActionsCountInfo(
ActionsCountInfoCallback{ visits, likes -> viewState.setActionsCount(visits, likes) })
}
#Subscribe
private fun updateActionsCount(event: VisitsEvent){
viewState.setActionsCount(event.getmVisits(), event.getmLikes())
}
fun registerSubscribers() {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this)
}
}
fun unsubscribe(){
EventBus.getDefault().unregister(this)
}
}
MyProfileActivity and its super classes have no public methods with the #Subscribe annotation
Emphasis mine. Remove the private from your function here:
#Subscribe
private fun updateActionsCount(event: VisitsEvent)
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"))
}
}
I've been using following Kotlin extension to add my lifecycle observers:
fun Lifecycle.addObserverUntilDestroy(observer: LifecycleObserver) {
addObserver(observer)
addObserver(object : LifecycleObserver {
#OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
fun onDestroy() {
removeObserver(this)
removeObserver(observer)
}
})
}
The idea was to automatically remove the observer when the Lifecycle is being destroyed.
So I can call it in Activity's onPostCreate (in this case a Java code but it is the same when calling from Kotlin):
ArchitectureComponentExtensions.addObserverUntilDestroy(getLifecycle(), myViewModel);
And have this code in my ViewModel (a custom one, not the on from Architecture Components):
#OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
public void onViewDestroyed() {
// This is not being called anymore
}
But this stopped working after migrating from:
import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleObserver
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.OnLifecycleEvent
to:
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
Any ideas why this is not working anymore?
I'm trying to do some test with Koin, but I'm getting the "Unresolved reference:KoinTest' howeber, seems that is doing the imports correctly, because I can't see any error in the code. Is only when I try to run the test.
I've tried to clean and rebuild the project and reinit android studio, but still the problem
TEST CLASS
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.koin.core.context.startKoin
import org.koin.core.context.stopKoin
import org.koin.test.KoinTest
import salva.perez.cabify.di.applicationModule
import org.koin.test.inject
class VoucherPresenterTest : KoinTest {
private val presenter: VoucherContract.Presenter by inject()
#Before
fun before() {
startKoin {
modules(applicationModule)
}
}
#After
fun after() {
stopKoin()
}
#Test
fun testInitViewCorrectly() {
...
}
}
GRADLE
implementation 'org.koin:koin-android:2.0.1'
testImplementation 'org.koin:koin-test:2.0.1'
Consider using androidTestImplementation if you want to use it in your androidTest and debugImplementation if you want to use it in unitTest as well.
Sorry for being so late(just 2 years late) but if there is someone that is still looking for this solution.. You have to put manually the import -> import org.koin.test.inject , to Android recognize the KoinTest interface.