Config Transformations From Visual Studio (Xamarin) - android

I'm trying to implement a pre-build transformation to my Android.Manifest file within a Xamarin.Android project within Visual Studio - The purpose is simply to change the bundleID of my app depending on whether I build a debug or release version.
I've added a new .netFramework project to my solution, referenced Microsoft.Build and then added a new class named BuildTask and referenced the DLL in my Xamarin.Android project.
I then intend to add a UsingTask to my Xamarin.Android project's .csproj file at the very bottom, just above the closing tag - I presume this is correct.
Before I add the UsingTask I wanted to make sure the Xamarin.Android project builds, but unfortunately I'm getting errors about a missing reference in my Xamarin.Android project saying I'm missing a reference to System.Collections, but the error disappears the second I remove the reference to my new BuildTask.DLL
I've never done this before so I may be heading down the rabbit hole... If anyone's got any advise, it'd be greatly appreciated.
Here's my BuildTask class for reference:
using System;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Xml;
public class BuildTask : Task
{
private const string AndroidNamespace = "http://schemas.android.com/apk/res/android";
[Required]
public string PackageName { get; set; }
[Required]
public string ApplicationLabel { get; set; }
[Required]
public string ManifestFilename { get; set; }
public string Debuggable { get; set; }
public override bool Execute()
{
var xml = new XmlDocument();
xml.Load(ManifestFilename);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("android", AndroidNamespace);
if (xml.DocumentElement != null)
{
xml.DocumentElement.SetAttribute("package", PackageName);
var appNode = xml.DocumentElement.SelectSingleNode("/manifest/application", nsmgr);
if (appNode != null && appNode.Attributes != null)
{
var labelAttribute = appNode.Attributes["label", AndroidNamespace];
if (labelAttribute != null)
{
labelAttribute.Value = ApplicationLabel;
}
var debuggableAttribute = appNode.Attributes["debuggable", AndroidNamespace];
if (debuggableAttribute != null)
{
debuggableAttribute.Value = Debuggable;
}
xml.Save(ManifestFilename);
return true;
}
}
return false;
}
}
Thanks.

That because you are referencing full .NET Framework to Xamarin.Android project. This full .NET framework is incompatible with the Xamarin.Android. You should use one of the following:
Create an Android Library Project.
Create a Portable Class Library Project.
Create a Shared Project.

Related

Cannot find symbol dependencyResolutionManagement in GradleSettingsModel

I am trying to write a plugin that adds my repo to settings.gradle if available or in build.gradle in other cases, like this:
public static void addLibraryRepositories(GradleBuildModel file, GradleSettingsModel settings, String url) {
// Check if settings.gradle contains the repositories
RepositoriesModel repositoryModel = null;
if (settings != null) {
repositoryModel = settings.dependencyResolutionManagement().repositories();
}
// if not use the build.gradle file
if (repositoryModel == null || repositoryModel.repositories().size() <= 0) {
repositoryModel = file.repositories();
}
// add my maven repo
if (!repositoryModel.containsMavenRepositoryByUrl(url)) {
repositoryModel.addMavenRepositoryByUrl(url, "My Repo");
file.applyChanges();
}
}
But I am getting this error when running gradle task 'buildPlugin':
error: cannot find symbol repositoryModel = settings.dependencyResolutionManagement().repositories();
^
symbol: method dependencyResolutionManagement()
location: variable settings of type GradleSettingsModel
Using IntelliJ 2022.1.3, there is no error on IDE, I can navigate to the definition of the interface and the GradleSettingsModelImpl implementation, in project view I can see it is included in android-gradle-dsl.jar.
I have these deps in plugin.xml
<depends>com.intellij.modules.all</depends>
<depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.android</depends>
<depends>org.intellij.groovy</depends>
I can call other methods of the same class without this error.
Any help is appreciated.
PS Already tried invalidating cache.

Loading a Font with PdfSharp .Net Standard preview from Xamarin.Forms fails: No appropriate font found

I am currently assessing how to generate a PDF from Xamarin.Forms (currently running the app on Android, only) and checking the .NET Standard port of PdfSharp.
Drawing to the PDF and showing it works, but I'm having issues writing text to the document. When I am trying to load an XFont with the following code
var font = new XFont("sans-serif", 20);
it fails with the exception
System.InvalidOperationException: No appropriate font found.
According to these samples, it is supposed to work this way, but they are for PdfSharp.Xamarin and not the PdfSharp .NET Standard. According to this answer the "sans-serif" font family should be correct, but I've desperately tried other options, like "Roboto", but to no avail.
Is PdfSharp for .NET Standard compatible with Xamarin at all? (It lists PdfSharp.Xamarin as a source it has been created from, hence I assumed it.) Is there anything else I have missed?
EDIT
I tried PdfSharp.Xamarin and it did work. Obviously this is an issue with the .NET Standard port.
I had similar issue and i resolved it by writing my own implementation of IFontResolver and assigning it to GlobalFontSettings.FontResolver.
public class FileFontResolver : IFontResolver // FontResolverBase
{
public string DefaultFontName => throw new NotImplementedException();
public byte[] GetFont(string faceName)
{
using (var ms = new MemoryStream())
{
using (var fs = File.Open(faceName, FileMode.Open))
{
fs.CopyTo(ms);
ms.Position = 0;
return ms.ToArray();
}
}
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if (familyName.Equals("Verdana", StringComparison.CurrentCultureIgnoreCase))
{
if (isBold && isItalic)
{
return new FontResolverInfo("Fonts/Verdana-BoldItalic.ttf");
}
else if (isBold)
{
return new FontResolverInfo("Fonts/Verdana-Bold.ttf");
}
else if (isItalic)
{
return new FontResolverInfo("Fonts/Verdana-Italic.ttf");
}
else
{
return new FontResolverInfo("Fonts/Verdana-Regular.ttf");
}
}
return null;
}
}
Then tell PDFSharp to use it:
GlobalFontSettings.FontResolver = new FileFontResolver();

Xamarin.Forms (Read file from platform project in PCL code)

I am building a Xamarin.Forms project with a PCL, iOS and Android project. One of my requirements is that I have to read a JSON file stored in the platform project (iOS/Android) from the PCL.
How can I do this please? I can't find a solution for this problem. :(
Thank you very much,
Nikolai
If the file that you want to read is embedded in the platform project assembly you can use the following code:
var assembly = typeof(MyPage).GetTypeInfo().Assembly;
Stream stream = assembly.GetManifestResourceStream("WorkingWithFiles.PCLTextResource.txt");
string text = "";
using (var reader = new System.IO.StreamReader (stream)) {
text = reader.ReadToEnd ();
}
Make sure that you replace WorkingWithFiles with namespace of your project and PCLTextResource.txt with name of the file.
Check Xamarin documentation at Loading Files Embedded as Resources for more details
If on the other hand you want to create and read/write files at runtime you can use PCLStorage library:
public async Task PCLStorageSample()
{
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFolder folder = await rootFolder.CreateFolderAsync("MySubFolder",
CreationCollisionOption.OpenIfExists);
IFile file = await folder.CreateFileAsync("answer.txt",
CreationCollisionOption.ReplaceExisting);
await file.WriteAllTextAsync("42");
}
I got it working by using an IoC container.
In my iOS project I have created a ConfigAccess class:
public class ConfigAccess : IConfigAccess
{
public string ReadConfigAsString()
{
return System.IO.File.ReadAllText("config.json");
}
}
I also had to add the following line to my AppDelegate.cs
SimpleIoc.Default.Register<IConfigAccess, ConfigAccess>();
And in my PCL I am simply asking for a ConfigAccess object during runtime:
var configAccess = SimpleIoc.Default.GetInstance<IConfigAccess>();
var test = configAccess.ReadConfigAsString();

TXW.create() different behavior on jdk and android?

I've created webservice on desktop (jdk, working good) and i'm trying to move it to android (make it running on android device). But i'm having some issues and after debugging for few days i've found different behaviour of the next code:
Schema schema = TXW.create(Schema.class,ResultFactory.createSerializer(result));
Passed result object is almost empty before invocation and has only systemId property. On JDK created schema has not null nsUri property, but on Android it's just empty!
JDK:
Android:
What's the reason and how can i fix it?
It makes schema invalid and it throws exception in cxf later:
javax.xml.ws.WebServiceException: java.lang.RuntimeException: Invalid schema document passed to AbstractDataBinding.addSchemaDocument, not in W3C schema namespace: schema
Update1:
I'v found TXW code pkg.getAnnotation(XmlNamespace.class) return null in android but instance value in jdk:
if(nsUri.equals("##default")) {
Package pkg = c.getPackage();
if(pkg!=null) {
XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class); // xn = null in android
if(xn!=null)
nsUri = xn.value();
}
}
if(nsUri.equals("##default"))
nsUri = "";
return new QName(nsUri,localName);
The reason was in TXW.java:
Package pkg = c.getPackage();
XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class);
if(xn!=null) // always null in android as it does not support package annotations
nsUri = xn.value();
So i had to hack it - create public static variable and set it before:
public static java.lang.String XmlNamespaceAnnotationValue = null;
...
Package pkg = c.getPackage();
if(pkg!=null) {
if (XmlNamespaceAnnotationValue != null) {
nsUri = XmlNamespaceAnnotationValue;
} else {
XmlNamespace xn = pkg.getAnnotation(XmlNamespace.class);
if(xn!=null)
nsUri = xn.value();
}
}
Before usage:
TXW.XmlNamespaceAnnotationValue = "http://www.w3.org/2001/XMLSchema";

Cordova plugin Android Activity - accessing resources

I am developing a Cordova plugin for Android and I am having difficulty overcoming accessing project resources from within an activity - the plugin should be project independent, but accessing the resources (e.g. R.java) is proving tricky.
My plugin, for now, is made up of two very simple classes: RedLaser.java and RedLaserScanner.java.
RedLaser.java
Inherits from CordovaPlugin and so contains the execute method and looks similar to the following.
public class RedLaser extends CordovaPlugin {
private static final string SCAN_ACTION = "scan";
public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
if (action.equals(SCAN_ACTION)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
scan(args, callbackContext);
}
});
return true;
}
return false;
}
private void scan(JSONArray args, CallbackContext callbackContext) {
Intent intent = new Intent(this.cordova.getActivity().getApplicationContext(), RedLaserScanner.class);
this.cordova.startActivityForResult((CordovaPlugin) this, intent, 1);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Do something with the result
}
}
RedLaserScanner.java
The RedLaserScanner contains the Android Activity logic and inherits from BarcodeScanActivity (which is a RedLaser SDK class, presumably itself inherits from Activity);
A very simple structure is as follows:
public class RedLaserScanner extends BarcodeScanActivity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.preview_overlay_new_portrait);
}
}
I am having trouble because I need to access the project's resources to access R.layout.preview_overlay_new_portrait (which are scatted in the Eclipse project) - but I cannot do this unless I import com.myProject.myApp.R - which makes my plugin have a dependency on the project itself.
I did some investigation and found cordova.getActivity().getResources() which seems useful, but this is not accessible from within my RedLaserScanner - because it does not inherit from CordovaPlugin.
Can somebody please help me with some pointers?
Thanks
I just ran into the same issue and it turns out to be pretty easy to solve. RedLaserScanner extends an activity, so you can just call getResources() like this:
setContentView(getResources("preview_overlay_new_portrait", "layout", getPackageName()));
Hooks can be used to replace source file contents to remove wrong imports and/or add the right imports of resources.
I created a script that do it without needing to specify the files. It tries to find source files (with .java extension), removes any resource import already in it and then put the right resources import (if needed), using the Cordova application package name.
This is the script:
#!/usr/bin/env node
/*
* A hook to add resources class (R.java) import to Android classes which uses it.
*/
function getRegexGroupMatches(string, regex, index) {
index || (index = 1)
var matches = [];
var match;
if (regex.global) {
while (match = regex.exec(string)) {
matches.push(match[index]);
console.log('Match:', match);
}
}
else {
if (match = regex.exec(string)) {
matches.push(match[index]);
}
}
return matches;
}
module.exports = function (ctx) {
// If Android platform is not installed, don't even execute
if (ctx.opts.cordova.platforms.indexOf('android') < 0)
return;
var fs = ctx.requireCordovaModule('fs'),
path = ctx.requireCordovaModule('path'),
Q = ctx.requireCordovaModule('q');
var deferral = Q.defer();
var platformSourcesRoot = path.join(ctx.opts.projectRoot, 'platforms/android/src');
var pluginSourcesRoot = path.join(ctx.opts.plugin.dir, 'src/android');
var androidPluginsData = JSON.parse(fs.readFileSync(path.join(ctx.opts.projectRoot, 'plugins', 'android.json'), 'utf8'));
var appPackage = androidPluginsData.installed_plugins[ctx.opts.plugin.id]['PACKAGE_NAME'];
fs.readdir(pluginSourcesRoot, function (err, files) {
if (err) {
console.error('Error when reading file:', err)
deferral.reject();
return
}
var deferrals = [];
files.filter(function (file) { return path.extname(file) === '.java'; })
.forEach(function (file) {
var deferral = Q.defer();
var filename = path.basename(file);
var file = path.join(pluginSourcesRoot, filename);
fs.readFile(file, 'utf-8', function (err, contents) {
if (err) {
console.error('Error when reading file:', err)
deferral.reject();
return
}
if (contents.match(/[^\.\w]R\./)) {
console.log('Trying to get packages from file:', filename);
var packages = getRegexGroupMatches(contents, /package ([^;]+);/);
for (var p = 0; p < packages.length; p++) {
try {
var package = packages[p];
var sourceFile = path.join(platformSourcesRoot, package.replace(/\./g, '/'), filename)
if (!fs.existsSync(sourceFile))
throw 'Can\'t find file in installed platform directory: "' + sourceFile + '".';
var sourceFileContents = fs.readFileSync(sourceFile, 'utf8');
if (!sourceFileContents)
throw 'Can\'t read file contents.';
var newContents = sourceFileContents
.replace(/(import ([^;]+).R;)/g, '')
.replace(/(package ([^;]+);)/g, '$1 import ' + appPackage + '.R;');
fs.writeFileSync(sourceFile, newContents, 'utf8');
break;
}
catch (ex) {
console.log('Could not add import to "' + filename + '" using package "' + package + '". ' + ex);
}
}
}
});
deferrals.push(deferral.promise);
});
Q.all(deferrals)
.then(function() {
console.log('Done with the hook!');
deferral.resolve();
})
});
return deferral.promise;
}
Just add as an after_plugin_install hook (for Android platform) in your plugin.xml:
<hook type="after_plugin_install" src="scripts/android/addResourcesClassImport.js" />
Hope it helps someone!
I implemented a helper for this to keep things clean. It also helps when you create a plugin which takes config.xml arguments which you store in a string resource file in the plugin.
private int getAppResource(String name, String type) {
return cordova.getActivity().getResources().getIdentifier(name, type, cordova.getActivity().getPackageName());
}
You can use it as follows:
getAppResource("app_name", "string");
That would return the string resource ID for app_name, the actually value still needs to be retrieved by calling:
this.activity.getString(getAppResource("app_name", "string"))
Or for the situation in the original question:
setContentView(getAppResource("preview_overlay_new_portrait", "layout"));
These days I just create a helper which returns the value immediately from the the helper:
private String getStringResource(String name) {
return this.activity.getString(
this.activity.getResources().getIdentifier(
name, "string", this.activity.getPackageName()));
}
which in turn you'd call like this:
this.getStringResource("app_name");
I think it's important to point out that when you have the resource ID you're not always there yet.
try using android.R.layout.preview_overlay_new_portrait

Categories

Resources