13

I been trying to get write platform code for Flutter to get a background service started. In here, I used a minimal example with no actual work done to show that the application simply won't run. The actual flutter code is not modified at all.

MainActivity.java

public class MainActivity extends FlutterActivity {

  Intent i = new Intent(this, MainService.class);

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

MainService.java

public class MainService extends IntentService {
  public MainService() {
    super("MainService");
  }

  @Override
  protected void onHandleIntent(Intent Intent) {
  }
}

AndroidManifest.xml

 <service android:enabled="true"
android:name=".MainService"></service>

The buildVersion is >27 and Manifest file has the service tag added accordingly.

Compiling and running with flutter run -v will show the following message:

..
[ +121 ms] Starting: Intent { act=android.intent.action.RUN flg=0x20000000 cmp=com.example.hello/.MainActivity (has extras)}
[   +1 ms] Waiting for observatory port to be available...

And the installation is stuck.

Is there a workaround about this? If this is an actual bug in Flutter's implementation, does it also mean there is no way to run a Flutter application in the background?

4
  • 1
    Please post your MainActivity class which extends FlutterActivity Commented Dec 18, 2018 at 10:28
  • Updated with the full class body.
    – Carrein
    Commented Dec 18, 2018 at 13:54
  • Are you behind a proxy (do you have a proxy configured for Internet access)? Commented Dec 18, 2018 at 13:59
  • No. I have tried running two new projects on different PCs with different connections and it is still stuck.
    – Carrein
    Commented Dec 18, 2018 at 14:02

11 Answers 11

5

I had this problem and eventually tracked it down to the fact that I'd changed the package name of my project to com.test.MyAppName in the AndroidManifest. This is not a valid Dart package name. You can verify this by trying to create a new project using

flutter create --org com.test MyAppName

This results in the error message:

"MyAppName" is not a valid Dart package name.

From the Pubspec format description:

DO use lowercase_with_underscores for package names.

Package names should be all lowercase, with underscores to separate words, just_like_this. Use only basic Latin letters and Arabic digits: [a-z0-9_]. Also, make sure the name is a valid Dart identifier -- that it doesn't start with digits and isn't a reserved word.

The solution for me was to recreate my project with the desired values by running

flutter create --org com.test my_app_name

and copying across the lib folder, pubspec file, any assets, etc. This should fix the problem. Note that this results in the kotlin folder (android/app/main/kotlin) having subfolders set up correctly based on the domain that you specified, e.g. kotlin/com/test/ for com.test

4

I had the same problem. In my case flutter clean fixed the issue. The cause of problem for me was a rename in Android package. After renaming the package you might need to clean the flutter old build files.

4

If you're experiencing this and you use Firebase, try downloading and inserting a new copy of your "google-services.json" file from your Firebase console. Additionally, be sure you follow all of the steps listed in the Add Firebase to your Flutter App documentation.

If you have enabled Crashlytics for it as well, be sure you have enabled it in your Firebase console and have followed the other instructions listed in the Get Started with Firebase Crashlytics documentation.

I'm not exactly sure which one of these fixed my issue, but nothing else worked until I checked all steps listed in those two docs.

4

First, check if the two names are the same:

  • the package name in android/app/src/mainAndroidManifest.xml
  • the package name in android/app/main/kotlin/MainActivity.kt

Then, check that the iOS bundle name is similar to the package name. For example:

  • iOS bundle name: com.example.myAppName
  • Package name: com.example.my_app_name

Finally, check that the name of the Firebase iOS application bundle ID is the same as the iOS bundle name.

In Firebase:

In Xcode:

3

Another potential cause for this issue is the package specified in your MainActivity.kt is in the wrong format, it should also use the same valid Dart package name, e.g.

package com.test.my_app_name

instead of

package com.test.MyAppName

1
  • After three hours of searching and trying random solutions, finally your answer solved my problem. Thank you so much.
    – pyprism
    Commented Dec 6, 2021 at 11:54
1

Another way to recreate this issue is to change the application's android:name attribute in the AndroidManifest from the default value of io.flutter.app.FlutterApplication to something else (in my example com.test.my_app_name). The purpose of this attribute is detailed in the comments in the standard AndroidManifest created by flutter:

 <!-- io.flutter.app.FlutterApplication is an android.app.Application
      that calls FlutterMain.startInitialization(this); in its onCreate
      method. In most cases you can leave this as-is, but you if you want 
      to provide additional functionality it is fine to subclass or
      reimplement FlutterApplication and put your custom class here. -->

So do not change that value unless it is to use your own custom class - this is not just a label.

0

Following @david-conlisk 's answer, it's also possible that you:

  • renamed the Kotlin package without updating the package line;

  • copy-pasted the MainActivity code from another class/project, but forgot to update the package line

All these cases could lead to the same hang and crash.

IMHO, Android Studio should shift away from the current text-file editing hell, and provide an all-in-one project configuration UI similar to Xcode or VisualStudio. The need for editing multiple Gradle config files, and Android's own property pages leaves too much room for mistakes.

0

One of the root cause of this issue is bugs in the ADB command, where some app related process is still running in the background even though the ADB showing app doesn't exist. The workaround of this behavior is to run uninstall from ADB:

adb  -s  <deviceID> uninstall <com.example.appname>
0

I have faced the same problem and fixed it by including all supporting platforms in my app level build file like this enter image description here

5
  • where is that file? give more details pls
    – Rajesh
    Commented Jun 22, 2021 at 7:09
  • @RajeshJr. In Android/app/build.gradle if I remember it correctly Commented Jul 8, 2021 at 22:13
  • Tried But It crashed the app
    – Rajesh
    Commented Jul 10, 2021 at 8:26
  • @RajeshJr. not sure about what's wrong with that, this worked for me didn't need to change anything else Commented Jul 12, 2021 at 20:42
  • pardon, the issue is not same. I tried your solution for another issue that is why it failed. Please watch this video youtube.com/watch?v=S63BWnOj-Oc
    – Rajesh
    Commented Jul 14, 2021 at 4:41
0

check what is added recently in pubspec.yaml file,

  1. try removing that and run the app again.

  2. check for any other code to be added with respect to the new thing added in pubspec.yaml

0

The same problem which I faced was solved by:

running a command inside the sdk platform-tools in cmd

adb uninstall <package - name>

Command line argument to be initiated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.