How to build Binding class in Xamarin Android? - c#

I am trying to create a Binding class in Visual Studio like in Android Studio https://developer.android.com/topic/libraries/data-binding/generated-binding
What I have:
Visual Studio 2022 17.4.5
Xamarin 17.4
What I've done:
Create Android App (Xamarin) project. Blank App and minimum android version 10.0
install Xamarin.AndroidX.DataBinding.ViewBinding 7.4.0.1 nuget package
Adds to the .csproj file
<AutoGenerateLayoutBindings>true</AutoGenerateLayoutBindings>
Adds a root element to activity_main.xml
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/hello_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, world!"
android:textSize="24sp"
android:layout_centerInParent="true" />
</RelativeLayout>
</layout>
Build project. In output window
Rebuild started...
1>------ Rebuild All started: Project: App4, Configuration: Debug Any CPU ------
Restored C:\Users\koly8\source\repos\App4\App4.csproj (in 73 ms).
1> App4 -> C:\Users\koly8\source\repos\App4\bin\Debug\App4.dll
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
nothing have been created
Maybe missed something

I tested the code you provided but failed, then refer to the doc: Data Binding Library. It said:
To configure your app to use data binding, enable the dataBinding build option in your build.gradle file in the app module, as shown in the following example:
android {
...
buildFeatures {
dataBinding true
}
}
The dataBinding library is actually part of the gradle build toolkit so isn't available in Xamarin.Android applications. For xamarin android the most recommended way is to use data binding is to use the native libraries, for example MVVMCross.

Related

"CreateAssetPack" task failed when building MAUI application for iOS

I am creating a new application on MAUI from scratch. The application is successfully built for Android. But when I try to build for iOS, an error occurs, the reasons for which I can not find:
1>C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\15.4.414\tools\msbuild\iOS\Xamarin.iOS.Common.targets(580,3): error MSB4044: The "CreateAssetPack" task was not given a value for the required parameter "Source".
1>Done building project "Clicker.Mobile.App.csproj" -- FAILED.
Installed all the latest updates on Macbook as well as Visual Studio (installed on Windows). The issue is reproducible on multiple computers. Most likely I'm missing something in setting up for iOS, but the error message does not give anything more informative.
CreateAssetPack task:
<CreateAssetPack
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true' And '$(EmbedOnDemandResources)' == 'false'"
ToolExe="$(ZipExe)"
ToolPath="$(ZipPath)"
Source="%(_AssetPack.Identity)"
OutputFile="$(IpaPackageDir)OnDemandResources\%(_AssetPack.DirectoryName)"
/>
I assume, judging by the compiler logs, that the _AssetPack.Identity property is empty.
I find the description of this element:
<!-- Look for the *.assetpack folders in the ODR folder -->
<CollectAssetPacks
SessionId="$(BuildSessionId)"
Condition="'$(IsMacEnabled)' == 'true' And Exists('$(_IntermediateODRDir)')"
OnDemandResourcesPath="$(_IntermediateODRDir)"
>
<Output TaskParameter="AssetPacks" ItemName="_AssetPack"/>
</CollectAssetPacks>
<ItemGroup>
<_AssetPack>
<_DirectoryName>$([System.IO.Path]::GetDirectoryName('%(Identity)'))</_DirectoryName>
</_AssetPack>
<_AssetPack>
<DirectoryName>$([System.IO.Path]::GetFileName('%(_AssetPack._DirectoryName)'))</DirectoryName>
</_AssetPack>
<_AssetPack>
<CodesignStampFile>$(DeviceSpecificOutputPath)OnDemandResources-codesign\%(DirectoryName)</CodesignStampFile>
</_AssetPack>
</ItemGroup>
But further it is not clear what needs to be specified in the project in order for this object to be initialized.
Which way to look?

Personalized font using Android 5 or higher - XML

I am following this tutorial to change the fonts of the app in Xamarin.Android, it is just adding the .ttf file to the project and then referring to the TextView / EditText where you want to use it in the XML, like this:
android:fontFamily="#font/sourcerfont"
The problem I am having is that it only works with virtual devices, and not with physical ones.
I reread the documentation (the tutorial) and found this part that caught my attention, and I get the impression that the error could be there since my physical device has API 23 - Android 6.0.1; and the virtual one has Android 9.0:
This feature is also supported on devices till Android 4.1 by using
Support Library 26 or more.
Could you tell me what I'm doing wrong?
I don't think you can only use custom fonts on Android apps with versions older than 8.
The only way that works for me on all devices is this but it is very tedious with large apps:
TextView Lbl = FindViewById <TextView> (Resource.Id.Lbl34);
tf = Typeface.CreateFromAsset (Android.App.Application.Context.Assets, "quicks.ttf");
Lbl.SetTypeface (tf, TypefaceStyle.Normal);
I reread the documentation (the tutorial) and found this part that caught my attention, and I get the impression that the error could be there since my physical device has API 23 - Android 6.0.1; and the virtual one has Android 9.0
I download Karantina font family, then creating a folder named font under the Resources folder. Put the font file inside it, build action is set to AndroidResource , see the following screenshot:
I use this custom font:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<TextView
android:text="My text content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/karantinaregular" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="karantina Bold"
android:fontFamily="#font/karantinabold"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="karantina Light"
android:fontFamily="#font/karantinalight"/>
I test my code on Android virtual 8.1(API27), Android physical device 8.1(API27) and Android physical device 6.0(API23), it all works, having no problem.

Unable to archive a project because: The project does not have a Package Name (but it does!)

Since 2-3 hours my project does not allow me anymore to export a signed APK because of that error.
I have set a package name (I'm working on this project since a week) and suddenly stopped working after I created a new emulator device, I've done nothing apart creating a new device.
If I run the app in debug mode works as expected
[EDIT]
I've found the issue, looks like the problem is related to the project path name being too long, seriously?!
I don't want to change my default folder where I save all my projects based on which type of tool I'm developing.
Anyway, I've tested it, I created a new solution in the root folder of my D: partition and inserted all the files from my original solution, tried to use 'Archive all' and worked
This error helped me to understand the problem:
Failed to generate Java type for class: Google.Android.Material.BottomNavigation.BottomNavigationView/IOnNavigationItemReselectedListenerImplementor due to System.IO.PathTooLongException
Now the question is: How I can keep my original folder?
[ORIGINAL]
I've also created a new project but the problem persists... I'm out of idea about what could be
This is the log generated when manually using MSBuild
_ResolveAndroidSigningKey:
Creating "obj\Release\100\android_debug_keystore.flag" because "AlwaysCreate" was specified.
_Sign:
D:\Android\android-sdk\build-tools\29.0.2\zipalign.exe -p 4 "D:\Software Development\Visual Studio\C#\Mobile\Android\
CGSJDSportsNotification\CGSJDSportsNotification.Android\obj\Release\100\android\bin\com.mutu_adi_marian.cgs_jdsportsn
otification.apk" "bin\Release\\com.mutu_adi_marian.cgs_jdsportsnotification-Signed.apk"
C:\Program Files\Android\Jdk\microsoft_dist_openjdk_1.8.0.25\bin\java.exe -jar D:\Android\android-sdk\build-tools\29.
0.2\lib\apksigner.jar sign --ks "C:\Users\mutua\AppData\Local\Xamarin\Mono for Android\debug.keystore" --ks-pass pass
:android --ks-key-alias androiddebugkey --key-pass pass:android --min-sdk-version 25 --max-sdk-version 29 "D:\Softwa
re Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotification.Android\bin\Release\co
m.mutu_adi_marian.cgs_jdsportsnotification-Signed.apk"
Signed android package 'bin\Release\com.mutu_adi_marian.cgs_jdsportsnotification-Signed.apk'
Done Building Project "D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotif
ication.Android\CGSJDSportsNotification.Android.csproj" (SignAndroidPackage target(s)).
Project "D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotification.sln" (
1:2) is building "D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotificati
on\CGSJDSportsNotification.csproj" (3:11) on node 1 (SignAndroidPackage target(s)).
D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotification\CGSJDSportsNoti
fication.csproj : error MSB4057: The target "SignAndroidPackage" does not exist in the project.
Done Building Project "D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotif
ication\CGSJDSportsNotification.csproj" (SignAndroidPackage target(s)) -- FAILED.
Done Building Project "D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotif
ication.sln" (SignAndroidPackage target(s)) -- FAILED.
Build FAILED.
"D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotification.sln" (SignAndr
oidPackage target) (1:2) ->
"D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotification\CGSJDSportsNot
ification.csproj" (SignAndroidPackage target) (3:11) ->
D:\Software Development\Visual Studio\C#\Mobile\Android\CGSJDSportsNotification\CGSJDSportsNotification\CGSJDSportsNo
tification.csproj : error MSB4057: The target "SignAndroidPackage" does not exist in the project.
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:09.02
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.mutu_adi_marian.cgs_jdsportsnotification" android:installLocation="auto">
<uses-sdk android:minSdkVersion="25" android:targetSdkVersion="29" />
<application android:label="CGSJDSportsNotification.Android" android:icon="#drawable/appIcon"></application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
</manifest>
Below the solution:
First of all be sure to have EnableLongPaths set to 1 into the
registry (I already had this to 1)
Open your <project_name>.csproj and <project_name>.Android.csproj file with Notepad++ or even Windows Notepad and firstly try only with this line
<PropertyGroup>
<UseShortFileNames>True</UseShortFileNames>
</PropertyGroup>
Delete the bin and obj folder from <project_name>.csproj and <project_name>.Android.csproj, open the solution and try to use 'Archive all', if still does not work close the solution, reopen the files and add also this line
<PropertyGroup>
<IntermediateOutputPath>SHORTEN_FOLDER: I still used my main prject folder but in the root I created a .in folder</IntermediateOutputPath>
</PropertyGroup>
Delete the bin and obj folder from <project_name>.csproj and <project_name>.Android.csproj, open the solution and try to use 'Archive all'.
At this point Visual Studio still complained about the Archive Path being too long, so I just moved my Archive folder to a shorten path and this fixed the issue.
(To change the Archive path go to Tools>Options>Xamarin>Archives location)
I hope this will help you as well, maybe a second option that can solve this issue (because before creating a new device everything was working fine) is to reinstall Visual Studio (definitely not a valid option for me)

VS 2015 Toolset may be unknown or missing

I try to publish project to my file directory.
I take a this message. But do not shown any error. Error list is empyty.
2> Building with tools version "14.0".
2> Project file contains ToolsVersion="12.0". This toolset may be
unknown or missing, in which case you may be able to resolve this by
installing the appropriate version of MSBuild, or the build may have
been forced to a particular ToolsVersion for policy reasons. Treating
the project as if it had ToolsVersion="14.0". For more information,
please see http://go.microsoft.com/fwlink/?LinkId=293424. 2> Target
"ValidateMSBuildToolsVersion" skipped. Previously built
unsuccessfully. 2>Done building project "project_name.csproj" – FAILED. 2>
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
========== Publish: 0 succeeded, 1 failed, 0 skipped ==========
VS 2015 Toolset may be unknown or missing
According to the error message "Project file contains ToolsVersion="12.0". ", it shows that your project was upgraded from Visual Studio 2013. So when you publish it with Visual Studio 2015, you may get this error "This toolset may be unknown or missing..."
To resolve this issue, you can try to update your project file: Right your project->Unload project->Edit Yourprojectname.csproj->change the ToolsVersion="12.0" to ToolsVersion="14.0" in the second line.
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
If you are using MSBuild command line build your project, you can can override the ToolsVersion Settings of Projects and Solutions by using command prompt:
msbuild.exe someproj.proj /tv:14.0
Why don't you try changing the version of the "Microsoft.Net.Compilers" from from v2.6.1 to v2.4.0 as per explained here

Out of memory error when compiling UWP app in release mode with .Net native

I have a solution where my main project is a uwp app, and two other projects referenced by the app :
UWP App ---> references 2.
Universal Class library ----> references 3.
PCL Library
2 --> uses EF7 and SQLight.
3 --> uses PCLCrypto
Now, everything works just fine and excellent when I'm on Debug mode x86 targeting local machine.
I want to compile the solution in release mode using .Net native tool chain, here's my config :
When I launch the compilation, it takes a remarkably long time, and too much memory is consumed by the .Net native tools :
After some time, everything stops with 2 errors on visual studio :
Here's what the output window says :
1>------ Build started: Project: ServerCommunication, Configuration:
Release Any CPU ------ 1> ServerCommunication ->
C:\Users\aymen\Documents\Visual Studio
2015\Projects\Applinova\Liberte\ServerCommunication\bin\Release\ServerCommunication.dll
2>------ Build started: Project: NewspaperDataLayer, Configuration:
Release x86 ------ 2>C:\Users\aymen\Documents\Visual Studio
2015\Projects\Applinova\Liberte\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets(225,5):
warning : All projects referencing ServerCommunication.csproj must
install nuget package Microsoft.Bcl.Build. For more information, see
http://go.microsoft.com/fwlink/?LinkID=317569. 2> NewspaperDataLayer
-> C:\Users\aymen\Documents\Visual Studio 2015\Projects\Applinova\Liberte\NewspaperDataLayer\bin\x86\Release\NewspaperDataLayer.dll
3>------ Build started: Project: Liberte, Configuration: Release x86
--some warnings here that I just skipped
3> Starting .NET Native compilation 3> Processing application code
3>C:\Users\aymen\Documents\Visual Studio
2015\Projects\Applinova\Liberte\Liberte\Resources.System.Linq.Expressions.rd.xml(35):
warning : Method 'CreateLambda' within
'System.Linq.Expressions.Expression' could not be found.
3>C:\Users\aymen\Documents\Visual Studio
2015\Projects\Applinova\Liberte\Liberte\Resources.System.Linq.Expressions.rd.xml(91):
warning : Method 'ParameterIsAssignable' within
'System.Linq.Expressions.Expression' could not be found. 3> Computing
application closure and generating interop code 3> Generating
serialization code 3> Compiling interop code 3> Cleaning up
unreferenced code 3> Generating native code 3>STARTPROCESSTASK :
error : Out of Memory 3>C:\Program Files
(x86)\MSBuild\Microsoft.NetNative\x86\ilc\IlcInternals.targets(1129,5):
error : ILT0005: 'C:\Program Files
(x86)\MSBuild\Microsoft.NetNative\x86\ilc\Tools\nutc_driver.exe
#"C:\Users\aymen\Documents\Visual Studio
2015\Projects\Applinova\Liberte\Liberte\obj\x86\Release\ilc\intermediate\MDIL\Liberte.rsp"'
returned exit code 1
========== Build: 2 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Deploy: 0 succeeded, 0 failed, 0 skipped ==========
What would possibly be wrong with this please ??
I had the same problem (I am also using EF7 and other libraries) but I solved using Visual Studio 15 preview (it is not visual studio 2015 update 2, which I didn't try). They improved the .NET native tools so now you should not have any problem, even if using a lot of third party library.
UPDATE
I have solved all my issues after i have installed visual studio 2015 update 3 and switched from EF7 to EF core also available via nuget
I have been struggled past few days around trying to fix this problem, but currently there is no easy solution for it.
There is a closed Issue on EF7 github, more info here (https://github.com/aspnet/EntityFramework/issues/4387) where they suggest as solution update EF7 to v1.0.0-rc2-16898.
I didn't manage how to update and as it still a nightly version, my final solution ("fast" workaround) was change my entire DB to sqlite-pcl (https://visualstudiogallery.msdn.microsoft.com/4913e7d5-96c9-4dde-a1a1-69820d615936).
If you aren't in a rush to lunch your application, you can always wait until final version of RC2 it should be done during April.

Categories