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.
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.