Home » Android » Xamarin Android app closes immediately after launching

Xamarin Android app closes immediately after launching

Posted by: admin June 16, 2020 Leave a comment

Questions:

I am using Xamarin in Visual Studio.

I created an boilerplate android app (click me) and I tried the following:

Scenario 1:

  1. Switch to release mode

  2. right click on project > Export Android Package (.apk)

  3. Side loaded generated .apk it onto my phone

  4. App launches with no issue

Scenario 2

  1. Switch to release mode

  2. Tools > Android > Publish Android App

  3. Signed APK after creating private key store

  4. Side load generated .apk (in release folder) onto phone

  5. App closes immediately after launching (no error message, nothing)

What could be the issue here?

Do I need to do anything else after signing the package?

In both scenarios, the output apk is the same size 5227kb

UPDATE:

I have tried the same thing running on the android emulator. Same results. The app runs fine in debug mode on the emulator. Once I’ve switched it to Release, I get “Unfortunately, App1 has stopped”

Here’s the error log http://pastebin.com/4j1Vwtgf

UPDATE 2:

Here’s the logs when I’m deploying under release mode:

1>Starting deploy 5" KitKat (4.4) XXHDPI Phone ...
1>Deploying 5" KitKat (4.4) XXHDPI Phone ...
1>Build started.
1>C:\Program Files (x86)\MSBuild.0\bin\csc.exe /noconfig /nowarn:1701,1702,2008 /nostdlib+ /errorreport:prompt /warn:4 /define:TRACE;__XAMARIN_ANDROID_v1_0__;__MOBILE__;__ANDROID__;__ANDROID_1__;__ANDROID_2__;__ANDROID_3__;__ANDROID_4__;__ANDROID_5__;__ANDROID_6__;__ANDROID_7__;__ANDROID_8__;__ANDROID_9__;__ANDROID_10__;__ANDROID_11__;__ANDROID_12__;__ANDROID_13__;__ANDROID_14__;__ANDROID_15__;__ANDROID_16__;__ANDROID_17__;__ANDROID_18__;__ANDROID_19__;__ANDROID_20__;__ANDROID_21__;__ANDROID_22__;__ANDROID_23__ /errorendlocation /preferreduilang:en-US /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v6.0\Mono.Android.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Release\App1.dll /ruleset:"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Team Tools\Static Analysis Tools\Rule Sets\MinimumRecommendedRules.ruleset" /target:library /utf8output MainActivity.cs Resources\Resource.Designer.cs Properties\AssemblyInfo.cs "C:\Users\a\AppData\Local\Temp\MonoAndroid,Version=v6.0.AssemblyAttributes.cs" obj\Release\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs obj\Release\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs obj\Release\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
1>App1 -> C:\Users\a\Documents\Visual Studio 2015\Projects\App1\App1\bin\Release\App1.dll
1>  Processing: obj\Release\res\layout\main.xml
1>  Processing: obj\Release\res\values\strings.xml
1>C:\Program Files (x86)\Java\jdk1.7.0_55\bin\keytool.exe -list -alias androiddebugkey -storepass android -keypass android -keystore "C:\Users\al\AppData\Local\Xamarin\Mono for Android\debug.keystore" 
1>C:\Program Files (x86)\Java\jdk1.7.0_55\bin\jarsigner.exe -keystore "C:\Users\a\AppData\Local\Xamarin\Mono for Android\debug.keystore" -storepass android -keypass android -digestalg SHA1 -sigalg md5withRSA -signedjar bin\Release\App1.App1-Signed-Unaligned.apk "C:\Users\a\Documents\Visual Studio 2015\Projects\App1\App1\obj\Release\android\bin\App1.App1.apk" androiddebugkey 
1>No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2046-02-20) or after any future revocation date.
1>C:\Program Files (x86)\Android\android-sdk\build-tools.0.1\zipalign.exe 4 "C:\Users\a\Documents\Visual Studio 2015\Projects\App1\App1\bin\Release\App1.App1-Signed-Unaligned.apk" "bin\Release\App1.App1-Signed.apk" 
1>Build succeeded.
1>Deploy successfully on 5" KitKat (4.4) XXHDPI Phone

And here are the build logs for debug mode

2>Starting deploy 5" KitKat (4.4) XXHDPI Phone ...
2>Deploying 5" KitKat (4.4) XXHDPI Phone ...
2>Build started.
2>C:\Program Files (x86)\Java\jdk1.7.0_55\bin\keytool.exe -list -alias androiddebugkey -storepass android -keypass android -keystore "C:\Users\a\AppData\Local\Xamarin\Mono for Android\debug.keystore" 
2>C:\Program Files (x86)\Java\jdk1.7.0_55\bin\jarsigner.exe -keystore "C:\Users\a\AppData\Local\Xamarin\Mono for Android\debug.keystore" -storepass android -keypass android -digestalg SHA1 -sigalg md5withRSA -signedjar bin\Debug\App1.App1-Signed-Unaligned.apk "C:\Users\a\Documents\Visual Studio 2015\Projects\App1\App1\obj\Debug\android\bin\App1.App1.apk" androiddebugkey 
2>No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2046-02-20) or after any future revocation date.
2>C:\Program Files (x86)\Android\android-sdk\build-tools.0.1\zipalign.exe 4 "C:\Users\a\Documents\Visual Studio 2015\Projects\App1\App1\bin\Debug\App1.App1-Signed-Unaligned.apk" "bin\Debug\App1.App1-Signed.apk" 
2>Build succeeded.
2>Deploy successfully on 5" KitKat (4.4) XXHDPI Phone
How to&Answers:

Inside your pastebin logs, it looks like you have a specific error here. It seems that the libmonodroid.so is not being loaded here.

03-25 00:40:18.531 E/AndroidRuntime( 1407): java.lang.UnsatisfiedLinkError: 
Couldn't load monodroid from loader 
dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/App1.App1- 
1.apk"],nativeLibraryDirectories=[/data/app-lib/App1.App1-1, /system/lib]]]: 
findLibrary returned null

This issue typically means that for some reason libmonodroid.so is not being installed.

I would recommend the following items:

  1. use adb uninstall <package name> to completely remove any old App1.App1 applications.
  2. Clean / Rebuild your project (Also be sure to manually delete the bin/obj folders in your solution.

If this continues to be an issue. Ensure a File->New Android Project will Debug and also install correctly as a Release .apk. You should also gather adb logcat of your attempts once you follow steps #1-2.

Answer:

I had the same error running release builds in the emulator (VS Android emulator). Turns out, we had disabled x86 as supported architecture.

Advanced build properties

Answer:

I had a similar issue with a basic app when I started with Xamarin. The problem was with the apk file I was using. I picked it from my bin/Release folder while the path for the actual signed + published + zip-aligned app was at the project folder level outside the bin directory.

The signed.apk file in Release mode uses the debug keystore and so it is not possible to publish that, more info here.

Was a pretty silly miss by me, so in case someone else also has this issue please make sure you are picking the right apk file before diving into further debugging.

Answer:

I found a possible solution.
On your device, in the developer options menu, you have to enable ‘Force gpu rendering’.

Hope that helps.