简体   繁体   中英

Android app debugs for a few seconds then just stops

I'm trying to set up a new Android app development environment at home and I've run into a problem that is a showstopper.

First, here is my environment:

  1. Windows 10
  2. Visual Studio Community 2015 with Xamarin platform installed
  3. Test device is: G Pad 7.0 LTE
  4. Android Version 5.0.2

I'm following this tutorial: MSDN tutorial

When I go to debug my test app, here is what happens.

  • Select debug from Visual Studio with my tablet as the target
  • VS deploys to the tablet
  • App starts on the tablet
  • App and debug session runs for about 5-10 seconds.
  • Debug session ends and app closes

No errors are displayed in Visual Studio and when i turn on Android logging it's a such a massive overflow of information that I don't really know where to begin searching for a problem.

Please also note that when I first set up the app, according to the tutorial, debugging on the tablet worked fine. The initial build is simply a template with a few basic controls.

In between then and when it stopped working I also added and successfully tested a Windows Phone project on a Windows Phone device.

Once I got to the section where I was modifying the project is when the problem started. I thought that perhaps a change in the code I did caused this issue so I tried another blank app and that blank app now crashes as well.

When I deploy the tutorial app to an emulator it crashes immediately but when I deploy a blank app it doesn't crash.

I also tried restoring the tablet to factory defaults and testing again. Same scenario: Blank app and tutorial app run for a few seconds, debugging stops, app closes.

Any clue as to what type of problem I'm looking at?

Solution

  1. Clean your project (Right-Click on Project and select clean)
  2. Go to your build folder. Probably in your Android project's root folder, named "obj". Delete either build you had, when you ran into this behavior (Debug or Release).

Directory Path

./AndroidProject/obj/Debug
./AndroidProject/obj/Release
  1. Restart Visual Studio
  2. Build and run again and it should work just fine, as the md5-hashes are now matching again.

I had also uninstalled the app before from the device and restarted it, I don't know whether this is required anymore, because the app is now being "reinstalled" by newer Xamarin versions anyway.

adb uninstall com.your.domain

Story and Background information

Have had the same issue with a device running Android 5.0.

Basically, the app was just stopping without any note. When I now restarted the phone, The log was extended (once) by the following line:

Device could not find component named: *com.your.domain*/{*md5hash*}.*{Activity}

It turns out that with Andrpod 5.0 a hashing mechanism was implemented:

With the 5.0 release, the default package names for Android Callable Wrappers will be based on the MD5SUM of the assembly-qualified name of the type being exported. This allows the same fully-qualified name to be provided from two different assemblies and not get a packaging error. See this for further information Xamarin.Android 5.1

So, if the md5-hash now changes from time to time, a simple Clean of your project is not enough.

I ran into this same issue and applied the same solution described above many times, which seemed to remedy this particular problem.

A possibly related fix, but definitely worthwhile note

After releasing this Android app and later releasing updates to the app, we got feedback from users that their Android Home screen shortcut to the app was disappearing after each app update. This is not good, for obvious reasons, but the fix for it was very simple as noted here:

dream-team-mobile-blog - Android app removes shortcut on update Xamarin solution

On the main activity, set a Name attribute of

<your package>.<your main activity>

like:

com.mycompany.myapp.MainActivity

For my solution, I chose to do it in code like this:

[Activity(Name = Constants.PackageName + "." + nameof(MainActivity), 
Label = Constants.AppIconTitle, Icon = "@drawable/icon", 
Theme = "@style/splashscreen", MainLauncher = true, 
LaunchMode = LaunchMode.SingleInstance, 
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

//Side Note:  I change my PackageName between Prod and SQA builds, so using
//a constant as seen above makes that change less risky to miss maintaining.
//Also, using nameof() keeps things tight and a sure match.

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

I mention it here in this post because, having this fix in place may have:

  1. definitely prevented app shortcut loss and
  2. possibly prevented the issue discussed in this post

as the MD5 hash prefix from the Main activity seems to no longer be generated.

This tweak may fix two bugs with one fix. If not, implementing a fixed main Activity Name will surely help your users keep their shortcut to your app when you release updates!

转到AndroidManifest.xml并更改您的包名称。

What fixed it for me in Visual Studio 2017 is going to :

Tools → Options → Xamarin → Android Settings

and uncheck:

Preserve Application data cache on device between deploys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM