locked
Unfortunately "app" has stopped RRS feed

  • Question

  • User301400 posted

    Hi,

    I am trying to install my first ever Xamarin Forms app on my Note 4 Android Phone. I have not tried to publish the app to the Play store yet. I wanted to try out the app on my phone first. But I am having a hard time (a real hard-time) making it work.

    I followed the deployment and publish documentation word for word. First off, my app works in debug mode on the simulator fine. Then I created a release build. I chose the "Bundle assemblies into Native code" on the packaging tab of the Android Options page. I did not choose Enable ProGuard because since I have a space in the default installation "Program Files", ProGuard growls at me. I tried the symlink trick but did not work for me. On the Linker tab, I have chosen Sdk and user assemblies for Linking. I have "Optimize Code" in the Build page. I have not dotfuscated my code since it is only going to be installed on my phone, for now, and I don't think I would want to reengineer the app back to my own code. So, I am good there.

    I have also created the keystore as described in the documenation. I created the Archive. All these steps went without a glitch. I now have a .apk file in the folder I chose for my Adhoc distribution. So far so good. I attached the apk file in an email and sent it to myself. I opend the email, downloaded the apk file on my mobile phone (a Samsung Note 4). It seemed to have installed without any error. I can see the icon in my apps page. But when I tap on the icon, I get a "unfortunately 'my app name' has stopped. I have no clue what is wrong. Can somebody help please?

    Babu.

    Monday, May 8, 2017 12:33 AM

Answers

  • User53115 posted

    Yes, it means you have about 8mb worth of compiled code that the linker doesn't think you need. My guess is it's probably classes that you use indirectly from NuGet package references.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 9, 2017 1:45 PM

All replies

  • User301400 posted

    Today I found out that if I create an archive without using "Linking" while building and publishing, the resultant .apk file can be installed on my android phone manually and it works. But then, the size of the apk is about 53mb where are when I use Linking, it is only 15mb. Huge difference. The problem is, again, when I use Linking, after installing the apk on my phone, the error "unfortunately the app has stopped". When I publish the app to google store, I would very much like to use the Linking as well as obfuscation. I am hoping someone can shed some light into this issue. Thanks.

    Monday, May 8, 2017 1:17 PM
  • User53115 posted

    When you turn on the linker, are you setting it to "SDK Assemblies Only" or "SDK and User Assemblies"? It's likely you're stripping out some class that is only referenced indirectly so your app crashes when it tries to use it but it isn't there.

    To find the source of the problem, I would try turning on the linker in a debug build and seeing what causes the application to crash.

    Monday, May 8, 2017 3:54 PM
  • User301400 posted

    Thanks JoeManke, I kinda figured out what is causing it but not why. When I have SDK and user assemblies in my linker, the app crashes as soon as it is launched, after installation. But if I set it to SDK assemblies only, i found that it works fine. So, I have narrowed it down to this reason. But I am not sure why it is happening. When I do both SDK and user assemblies, the apk size is about 15mb. When I do only the SDK, it is about 23mb. Does that make sense?

    Monday, May 8, 2017 8:49 PM
  • User53115 posted

    Yes, it means you have about 8mb worth of compiled code that the linker doesn't think you need. My guess is it's probably classes that you use indirectly from NuGet package references.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, May 9, 2017 1:45 PM
  • User252555 posted

    Hi all, I had started working on android services using Intent services and Broadcastreceiver via AlarmManager call in the mainactivity

    My error is Unfortunately, MyApp has stopped while reboot time so please share your idea.

    1.I call Broadcastreceiver using AlarmManager specific time 4 clock it will send SMS from to one device to other devices 2.Stop intent services -By using this method(StopSelf();) stop the services, then again set another time in AlarmManager called to Broadcast receiver. 3.Reboot time call Broadcastreceiver specific time 4 clock it

    1.I call Broadcastreceiver using AlarmManager specific time 4 clock it will send SMS from to one device to other devices Broadcast receiver.

    MainActivity.cs

    using Android.App; using Android.Widget; using Android.OS; using Android.Content; using Intentservice1.MReceiver;

    namespace Intentservice1 { [Activity(Label = "Intentservice1", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { private Handler handler; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

            // Set our view from the "main" layout resource
             SetContentView (Resource.Layout.Main);
    
            try
            {
                PellucidLog.Logfile.WriteEventLog("Class:MainActivity", "Method:MainActivity", 0, "OnCreate method Called", System.Diagnostics.TraceEventType.Information);
                Intent alarm = new Intent(this, typeof(MyReceiver));
                //bool alarmRunning = (PendingIntent.GetBroadcast(this, 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
                bool alarmRunning = (PendingIntent.GetBroadcast(this, 0, alarm, PendingIntentFlags.NoCreate) != null);
                if (alarmRunning == false)
                {
                    PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarm, 0);
                    AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService); 
                    Java.Util.Calendar firingCal = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
                    Java.Util.Calendar currentCal = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
    
                    firingCal.Set(Java.Util.Calendar.Hour, 4); // At the hour you wanna fire
                    firingCal.Set(Java.Util.Calendar.Minute, 1); // Particular minute
                    firingCal.Set(Java.Util.Calendar.Second, 0); // particular second
    
                    long intendedTime = firingCal.TimeInMillis;
                    long currentTime = currentCal.TimeInMillis;
    
                    if (intendedTime >= currentTime)
                    {
                        PellucidLog.Logfile.WriteEventLog("Class:MyReceiver", "Method:MainActivity ", 0, "current time set OnCreate method Called", System.Diagnostics.TraceEventType.Information);                                               
                        alarmManager.SetExact(AlarmType.RtcWakeup, intendedTime, pendingIntent);
                    }
                    else
                    {
                        // set from next day
                        // you might consider using calendar.add() for adding one day to the current day
                        firingCal.Add(Java.Util.Calendar.DayOfMonth, 1);
                        intendedTime = firingCal.TimeInMillis;
                        alarmManager.SetExact(AlarmType.RtcWakeup, intendedTime, pendingIntent);
                    }                    
                    PellucidLog.Logfile.WriteEventLog("Class:MyReceiver", "Method:MainActivity ", 0, " end of else OnCreate method Called", System.Diagnostics.TraceEventType.Information);
                }
            }
            catch (System.Exception ex)
            {
                Toast.MakeText(this, "Exception occurred in Oncreate of MainActivity class:" + ex.Message, ToastLength.Long).Show();
            }
        }
    }
    

    }

    2.Stop intent services -By using this method(StopSelf();) stop the services, then again set another time in AlarmManager called to Broadcast receiver.

    MyIntenService.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Text;

    using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Telephony; using Intentservice1.MReceiver;

    namespace Intentservice1.TimerServices { [Service] public class MyIntenService : IntentService { public MyIntenService() { PellucidLog.Logfile.WriteEventLog("Class:MyIntenService", "Method:MyIntenService", 0, "MyIntenService constructor called ", System.Diagnostics.TraceEventType.Information); } public override void OnCreate() { base.OnCreate(); PellucidLog.Logfile.WriteEventLog("Class:MyIntenService", "Method:OnCreate", 0, "OnCreate method called ", System.Diagnostics.TraceEventType.Information); } [return: GeneratedEnum] public override StartCommandResult OnStartCommand(Intent intent, [GeneratedEnum] StartCommandFlags flags, int startId) { PellucidLog.Logfile.WriteEventLog("Class:MyIntenService", "Method:OnStartCommand", 0, "OnStartCommand method called ", System.Diagnostics.TraceEventType.Information); return base.OnStartCommand(intent, flags, startId); } public override void OnDestroy() { PellucidLog.Logfile.WriteEventLog("Class:MyIntenService", "Method:OnDestroy", 0, "OnDestroy method called ", System.Diagnostics.TraceEventType.Information); base.OnDestroy(); } public override IBinder OnBind(Intent intent) { return base.OnBind(intent); }
    PendingIntent sentPI; String SENT = "SMS_SENT"; protected override void OnHandleIntent(Intent intent) { try { sentPI = PendingIntent.GetBroadcast(this, 0, new Intent(SENT), 0);
    SmsManager.Default.SendTextMessage("+919600605918", null, "Message send from Alarm manager to client place. ", sentPI, null); PellucidLog.Logfile.WriteEventLog("Class:MyIntenService", "Method:OnHandleIntent", 0, "Message send ", System.Diagnostics.TraceEventType.Information); StopSelf(); Intent alarm = new Intent(this, typeof(MyReceiver)); PendingIntent pendingIntent = PendingIntent.GetBroadcast(this, 0, alarm, 0); AlarmManager alarmManager = (AlarmManager)GetSystemService(Context.AlarmService);

                Java.Util.Calendar firingCal = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
                Java.Util.Calendar currentCal = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
    
                firingCal.Set(Java.Util.Calendar.Hour, 4); // At the hour you wanna fire
                firingCal.Set(Java.Util.Calendar.Minute, 5); // Particular minute
                firingCal.Set(Java.Util.Calendar.Second, 0); // particular second
    
                long intendedTime = firingCal.TimeInMillis;
                long currentTime = currentCal.TimeInMillis;
    
                if (intendedTime >= currentTime)
                {                    
                    alarmManager.SetExact(AlarmType.RtcWakeup, intendedTime, pendingIntent);
                }
                else
                {
    
                    firingCal.Add(Java.Util.Calendar.DayOfMonth, 1);
                    intendedTime = firingCal.TimeInMillis;
                    alarmManager.SetExact(AlarmType.RtcWakeup, intendedTime, pendingIntent);
                }
            }
            catch(Exception ex)
            {
                PellucidLog.Logfile.WriteEventLog("Class:MyIntenService", "Method:OnHandleIntent", 0, "Error occurred:"+ex.Message, System.Diagnostics.TraceEventType.Information);
            }
    
        }
    }
    

    }

    3.Reboot time call Broadcastreceiver specific time 4 clock it

    MyReceiver.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Text;

    using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Intentservice1.TimerServices; using Android.Util;

    namespace Intentservice1.MReceiver { [BroadcastReceiver(Enabled =true)] [IntentFilter(new[] { Android.Content.Intent.ActionBootCompleted })] public class MyReceiver : BroadcastReceiver { public override void OnReceive(Context context, Intent intent) { try { if (intent.Action != null) { if (intent.Action.Equals(Intent.ActionBootCompleted)) { PellucidLog.Logfile.WriteEventLog("Class:MyReceiver", "Method:OnReceive first ", 0, "ActionBootCompleted OnReceive method Called", System.Diagnostics.TraceEventType.Information); Intent alarm = new Intent(context, typeof(MyReceiver)); Intent alarmservices = new Intent(context, typeof(MyIntenService));

                            PendingIntent pendingIntent = PendingIntent.GetService(context, 0, alarmservices, PendingIntentFlags.CancelCurrent);
                            AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
    
                            Java.Util.Calendar firingCal = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
                            Java.Util.Calendar currentCal = Java.Util.Calendar.GetInstance(Java.Util.TimeZone.Default);
    
                            firingCal.Set(Java.Util.Calendar.Hour, 4); // At the hour you wanna fire
                            firingCal.Set(Java.Util.Calendar.Minute,10); // Particular minute
                            firingCal.Set(Java.Util.Calendar.Second, 0); // particular second
    
                            long intendedTime = firingCal.TimeInMillis;
                            long currentTime = currentCal.TimeInMillis;
                            if (intendedTime >= currentTime)
                            {
    
                                PellucidLog.Logfile.WriteEventLog("Class:MyReceiver", "Method:OnReceive second ", 0, "ActionBootCompleted current time set OnReceive method Called", System.Diagnostics.TraceEventType.Information);                               
                                alarmManager.SetExact(AlarmType.RtcWakeup, intendedTime, pendingIntent);
                            }
                            else
                            {
                                PellucidLog.Logfile.WriteEventLog("Class:MyReceiver", "Method:OnReceive second ", 0, "ActionBootCompleted later time OnReceive method Called", System.Diagnostics.TraceEventType.Information);
    
                                firingCal.Add(Java.Util.Calendar.DayOfMonth, 1);
                                intendedTime = firingCal.TimeInMillis;                               
                                alarmManager.SetExact(AlarmType.RtcWakeup, intendedTime, pendingIntent);
                            }
    
    
                        Toast.MakeText(context, "MyReceiver BroadcastReceiver called AlarmReceiver Received intent!", ToastLength.Short).Show();
                        Log.Info("Received intent!", "MyReceiver BroadcastReceiver called ");
    
                    }
                }
                else
                {
                    Toast.MakeText(context, "MyReceiver Received intent second called!", ToastLength.Short).Show();
                    Log.Info("Received intent!", "Received second");
                    PellucidLog.Logfile.WriteEventLog("Class:MyReceiver", "Method:OnReceive second", 0, "Null OnReceive method Called", System.Diagnostics.TraceEventType.Information);
                    context.StartService(new Intent(context, typeof(MyIntenService)));
                }
            }
            catch (Exception ex)
            {
                Toast.MakeText(context, "MyReceiver Received intent!" + ex.Message, ToastLength.Short).Show();
            }
        }
    }
    

    }

    Mainfest file screen shot and attached

    Log.cs

    using System; using System.Collections.Generic; using System.Linq; using System.Text;

    using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using System.IO;

    namespace PellucidLog { public static class Logfile {
    public static void WriteEventLog(string sClassName, string sFunctionName, int iErrorCode, string sErrorMessage, System.Diagnostics.TraceEventType eletLogType) { LogEvent(sClassName + " " + sFunctionName + " " + sErrorMessage, eletLogType); }

        public static void LogEvent(String Message, System.Diagnostics.TraceEventType type)
        {
            try
            {
                LogToFile(DateTime.Now.ToString("hh:mm:ss") + " : " + type.ToString() + ": " + Message + System.Environment.NewLine);
            }
            catch (Exception ex)
            {
                LogToFile(DateTime.Now.ToString("hh:mm:ss") + " : " + type.ToString() + ": " + ex.Message + System.Environment.NewLine);
            }
            finally
            {
    
            }
        }
        private readonly static object _syncklock = new object();
    
        public static void LogToFile(string msg)
        {
            lock (_syncklock)
            {
                try
                {
                    // if (Convert.ToBoolean(LogDisplay))
                    {
                        var path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
                        Java.IO.File Download = new Java.IO.File(path);
                        if (!Download.Exists() && !Download.IsDirectory)
                        {
                            Download.Mkdir();
                        }
    
                        string directroy = System.IO.Path.Combine(path, "Pellucid");
                        Java.IO.File Pellucid = new Java.IO.File(directroy);
                        if (!Pellucid.Exists() && !Pellucid.IsDirectory)
                        {
                            Pellucid.Mkdir();
                        }
    
                        string _Filename = Path.Combine(directroy, "ServicesMobility.log");
                        if (!File.Exists(_Filename))
                        {
                            FileStream CreateFileStream = new FileStream(_Filename, FileMode.CreateNew, FileAccess.Write, FileShare.ReadWrite);
                            CreateFileStream.Close();
                            CreateFileStream = null;
                        }
                        int _ConfigFileSizeRange = 20000;//Convert.ToInt32(WebConfigurationManager.AppSettings["FileSize"].ToString());
                        FileInfo _LogFileInfo = new FileInfo(_Filename);
                        int _LogFileLength = Convert.ToInt32(_LogFileInfo.Length.ToString());
                        if (_LogFileLength > _ConfigFileSizeRange)
                        {                          
    
                            string destinationFileName = Path.Combine(directroy, "LogEntries");// + DateTime.Now.ToString("MM.dd.yy"), DateTime.Now.ToString("hh.mm.ss") + ".txt");                            
                            Java.IO.File PellucidDestination = new Java.IO.File(destinationFileName);
                            if (!PellucidDestination.Exists() && !PellucidDestination.IsDirectory)
                            {
                                PellucidDestination.Mkdir();
                            }
                            string destinationDate = Path.Combine(destinationFileName, DateTime.Now.ToString("MM.dd.yy"));
                            Java.IO.File PellucidDatetime = new Java.IO.File(destinationDate);
    
                            if (!PellucidDatetime.Exists() && !PellucidDatetime.IsDirectory)
                            {
                                PellucidDatetime.Mkdir();
                            }
    
                            string destinationDatefile = Path.Combine(destinationDate, DateTime.Now.ToString("hh.mm.ss") + ".txt");
    
                            if (!Directory.Exists(Path.GetDirectoryName(destinationDatefile)))
                            {
                                Directory.CreateDirectory(Path.GetDirectoryName(destinationDatefile));
                            }
                            File.Copy(_Filename, destinationDatefile);
                            File.WriteAllText(_Filename, "");
                        }
                        File.AppendAllText(_Filename, msg);
                    }
                }
                catch (Exception ex)
                {
                    LogToFile(DateTime.Now.ToString("hh:mm:ss") + " : " + "LogToFile method" + ": " + ex.Message + System.Environment.NewLine);
                }
            }
        }
    }
    

    }

    Thursday, February 8, 2018 2:16 PM