2

I'm creating a simple android app that contains simple controls/views like Label, Entry and Buttons

  • Target Framework: Android 8.1
  • Target Android Version: Android 8.1
  • Minimum Android Version: Android 4.4

After archive and generate APK file, i successfully installed it in my MEmu emulator (Android Version: 5.1). But when i tried to open it, the app crashes.

This is the error i get from device log tool:

System.NullReferenceException: Object reference not set to an instance of an object
  at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in <99988d4ab8d144898ef5bc7586876d75>:0 
  at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in <99988d4ab8d144898ef5bc7586876d75>:0 
  at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.LoadApplication (Xamarin.Forms.Application application) [0x0026f] in <99988d4ab8d144898ef5bc7586876d75>:0 
  at SampleApp.Droid.MainActivity.OnCreate (Android.OS.Bundle bundle) [0x00028] in <eaa39f9ef27d400ebfed424165f990c2>:0 
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_savedInstanceState) [0x0000f] in <818821ea7e204c78a45bc29cdc69e744>:0 
  at (wrapper dynamic-method) System.Object.fe8f1617-954f-4716-901a-433b7f8b44bf(intptr,intptr,intptr)

I suspect that this is Xamarin.Forms.Platform.Android.FormsAppCompatActivity issue. Because when i changed the MainActivity parent class to Xamarin.Forms.Platform.Android.FormsApplicationActivity it runs without error.

But how can i fix this error without switching to FormsApplicationActivity??

Edit: When i disable ProGuard in the "Packaging Properties", the app runs smoothly. (why?)

Middleman
  • 111
  • 1
  • 12
  • When using `FormsAppCompatActivity` have you created the proper theme/styles, assigned that theme in your `Xamarin.Android` project, setup the Tabbar.axml, Toolbar.axml layouts.... – SushiHangover Jun 20 '18 at 03:27
  • @SushiHangover I've already edit the application element in AndroidManifest.xml (added `android:theme="@style/MainTheme"`). Tabbar and Toolbar.axml theme is `"@style/ThemeOverlay.AppCompat.Dark.ActionBar"`. btw when **ProGuard** is disable the app runs without problem. – Middleman Jun 20 '18 at 04:29
  • `ProGuard is disable the app`... Oh..... that needs to be in your question, what is the error seen in `logcat` and do you have a custom proguard config? – SushiHangover Jun 20 '18 at 04:32
  • @SushiHangover i mean when i disable the ProGuard in the "Packaging Properties", the app runs without problem. Is logcat and device log (from visual studio) is different? – Middleman Jun 20 '18 at 04:37
  • do you have a custom proguard config? and what version of Forms? 3.0 or 2.5 – SushiHangover Jun 20 '18 at 04:39
  • @SushiHangover i don't have a custom proguard, this is my first app with xamarin, the version is 3.0 – Middleman Jun 20 '18 at 06:09

1 Answers1

1

It is a bug/issue with Forms' 3.0 (at least version 3.0.0.561731) as there is something within the Java android.support.v7.widget namespace that is being stripped and Xamarin is not generating a proper proguard configuration to automatically prevent the problem.

I'm not sure which actual Java classes and/or fields are the ones causing the issue, but you can do this to workaround around it:

Create a proguard configuration text-based file in your Xamarin.Android application project and assign it a build type of ProguardConfiguration, i.e.:

<ItemGroup>
   <ProguardConfiguration Include="Proguard.txt" />
</ItemGroup>

And include the following:

# Proguard issue in Forms' version 3.0.0.561731
# MonoDroid: System.NullReferenceException: Object reference not set to an instance of an object
# MonoDroid:   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.InternalSetPage (Xamarin.Forms.Page page) [0x0006f] in <09e4bdebfa024bfd9231e2318fd7c3d7>:0
# MonoDroid:   at Xamarin.Forms.Platform.Android.FormsAppCompatActivity.SetMainPage () [0x0000c] in <09e4bdebfa024bfd9231e2318fd7c3d7>:0

-keep public class android.support.v7.widget.** { *; }

Delete the app from the device/emulator and perform a clean all/rebuild all and redeploy the app and that should solve the problem.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • When i try to rebuild after cleaning it produces a build error: "java.exe" exited with code 1 - i will keep the "ProGuard" option enabled right? – Middleman Jun 20 '18 at 06:48
  • @Middleman Yes, keep proguard option enabled... the java issue is mostly likely that you used VS to create the txt file as it will end up as a UTF-8 w/BOM file, it can not have the BOM in it: https://stackoverflow.com/a/38745003/4984832 – SushiHangover Jun 20 '18 at 06:58
  • Thank you! now it works like a charm :) as far as i understand that custom Proguard prevents the `android.support.v7.widget to be shrink, that's why it solves the default proguard configuration messed? – Middleman Jun 20 '18 at 07:21
  • @Middleman Correct, I would assume that is it a just one class (or maybe a few) within that Java namespace that are being stripped that causes the problem but including all the ****widget.** classes solves it. Unfortunately it occurs within the bootstrapping of the MonoDroid/JNI app and does not report the actual Java class... **usually** you will get a full "Java class not found exception" **after** the bootstrap happens when Proguard has removed something you are actually using and it makes adding that class to the proguard cfg file fairly easy as you have the full java name – SushiHangover Jun 20 '18 at 07:31