简体   繁体   中英

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.

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

Probably you forgot to add inside the 'conditional compilation symbols' field inside the build section of the Android project that symbol: ANDROID

I don't know why on iOS when the project is created, the symbol is automatically inserted, while in the Android project it is not inserted.

安卓构建部分 Here the symbols are not preset.

ios构建部分

Here the symbols are preset.

You can find more info here under Conditional Compilation section: microsoft xamarin docs

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