Code written in Cross-platform library with __ANDROID__ does not execute in Xamarin forms application
NickName:Zohaib Ask DateTime:2019-10-04T21:47:28

Code written in Cross-platform library with __ANDROID__ does not execute in Xamarin forms application

I am using a multi-targeting library in my Xamarin forms application. The library has come code written with __ANDROID__ conditional compiler symbol. When i run the application on Android, it does not execute the code written with the symbol.

The multi-targeting library project is using MSBuild.Sdk.Extras.

<Project Sdk="MSBuild.Sdk.Extras" ToolsVersion="16.0">

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;MonoAndroid90</TargetFrameworks>
  </PropertyGroup>

I also tried it by defining conditional symbol in Xamarin.Android Project Properties --> Build --> General --> Conditional compilation symbols. But it still does not run that particular code.

#if __ANDROID__
        // do some andoid specific work
#endif

It should run this code block when running the application on Android.

Copyright Notice:Content Author:「Zohaib」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/58237698/code-written-in-cross-platform-library-with-android-does-not-execute-in-xama

Answers
byczu 2019-10-04T13:59:46

Seems like you have defined __ANDROID__ symbol in the wrong project. If you define it in e.g. Project.Android it's only defined in this project, not the other ones.\nThere is a possibility to define solution level conditional symbols and you can read about it here",


More about “Code written in Cross-platform library with __ANDROID__ does not execute in Xamarin forms application” related questions

Code written in Cross-platform library with __ANDROID__ does not execute in Xamarin forms application

I am using a multi-targeting library in my Xamarin forms application. The library has come code written with __ANDROID__ conditional compiler symbol. When i run the application on Android, it does ...

Show Detail

#if __ANDROID__ is being omitted on Android device

I am using Cross-Platform Xamarin Forms with MVVM. In one ViewModel class, I wanted to use library that I got ready, in this library are #if statements that depend on the platform. #if __ANDROID__ ...

Show Detail

Xamarin Forms: how to implement platform specific code

I do have a Xamarin Forms project where I want Android, iOS and WinPhone as targets. (Right now I only look at Android.) I am having trouble playing Audio and think I will need to implement it

Show Detail

Using Xamarin.Forms.Dependency just crashes on Android

I've created a library using Xamarin.Forms.Dependency to execute native code using a shared project. On Windows 8.1 and UWP it executes the native code correctly, but on Android it always crashes.

Show Detail

Authentication with Amazon Cognito User Pools in Xamarin.Forms Cross-Platform App

I am developing a cross-platform mobile application using Xamarin.Forms. I have an Amazon Cognito User Pool, equipped to do identity federation via third parties (Facebook, Google), as well as native

Show Detail

Performance improvement of Xamarin forms application

I am developing an Xamarin forms (Android and iOS) applicaion and checking the logged in status when application starts and assign the appropriate values if the user already logged in to application.

Show Detail

Is it possible to add Xamarin.forms code to an existing Native Android Studio Application as a library?

I have an existing Native Android Application - written in Java on Android Studio. A client wanted to add some new features that were being developed by another team and a different company that t...

Show Detail

UserControl for cross-platform Xamarin Forms

I have been looking for a long time now but still haven't found a way to create WPF like UserControls for cross-platform Xamarin Forms. Can this even be done? I am using Xamarin with Visual Studio ...

Show Detail

Xamarin.Forms - App does not open

I am learning Xamarin.Forms with Visual Studio 2015 on a Windows 10 machine. When I create a Xamarin.Forms(Android/iOS) cross-platform project and start the AVD, the emulator works, but the applica...

Show Detail

Google Places Api in Xamarin.Forms Application

I start whit a cross-platform project where i need to use Google Places Api and other but unfortunately i don't find a library for use in Xamarin.Forms. By the way i can use web service api for get...

Show Detail