Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typemaps per RID support and disable marshal methods by default #8164

Merged
merged 16 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 222 additions & 63 deletions src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1326,10 +1326,10 @@ public void Dispose ()
using (var builder = CreateApkBuilder (Path.Combine ("temp", TestContext.CurrentContext.Test.Name))) {
builder.ThrowOnBuildFailure = false;
Assert.IsFalse (builder.Build (proj), "Build should have failed with XA4212.");
StringAssertEx.Contains ($"error XA4", builder.LastBuildOutput, "Error should be XA4212");
StringAssertEx.Contains ($"error : XA4", builder.LastBuildOutput, "Error should be XA4212");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just wondering how this PR caused : to be inserted. This doesn't make sense to me. (Yet appears necessary, considering that the PR is green?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It calls Log.LogError from TaskLoggingHelper which prepends error : to the message. The old code used JI's custom logger which didn't use that format.

StringAssertEx.Contains ($"Type `UnnamedProject.MyBadJavaObject` implements `Android.Runtime.IJavaObject`", builder.LastBuildOutput, "Error should mention MyBadJavaObject");
Assert.IsTrue (builder.Build (proj, parameters: new [] { "AndroidErrorOnCustomJavaObject=False" }), "Build should have succeeded.");
StringAssertEx.Contains ($"warning XA4", builder.LastBuildOutput, "warning XA4212");
StringAssertEx.Contains ($"warning : XA4", builder.LastBuildOutput, "warning XA4212");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class BuildTest2 : BaseTest
new object[] {
/* isClassic */ false,
/* isRelease */ true,
/* marshalMethodsEnabled */ true,
/* marshalMethodsEnabled */ false,
},
new object[] {
/* isClassic */ false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.IO;
using System.Linq;
using System.Text;
using Java.Interop.Tools.Cecil;
using Mono.Cecil;
using Mono.Cecil.Cil;
using Mono.Linker;
using Mono.Tuner;
using MonoDroid.Tuner;
using NUnit.Framework;
using Xamarin.ProjectTools;
Expand Down Expand Up @@ -514,7 +517,7 @@ public void AndroidUseNegotiateAuthentication ([Values (true, false, null)] bool
}

[Test]
public void DoNotErrorOnPerArchJavaTypeDuplicates ()
public void DoNotErrorOnPerArchJavaTypeDuplicates ([Values(true, false)] bool enableMarshalMethods)
{
if (!Builder.UseDotNet)
Assert.Ignore ("Test only valid on .NET");
Expand All @@ -525,26 +528,73 @@ public void DoNotErrorOnPerArchJavaTypeDuplicates ()
lib.Sources.Add (new BuildItem.Source ("Library1.cs") {
TextContent = () => @"
namespace Lib1;
public class Library1 : Java.Lang.Object {
public class Library1 : Com.Example.Androidlib.MyRunner {
private static bool Is64Bits = IntPtr.Size >= 8;

public static bool Is64 () {
return Is64Bits;
}

public override void Run () => Console.WriteLine (Is64Bits);
}",
});
lib.Sources.Add (new BuildItem ("AndroidJavaSource", "MyRunner.java") {
Encoding = new UTF8Encoding (encoderShouldEmitUTF8Identifier: false),
TextContent = () => @"
package com.example.androidlib;

public abstract class MyRunner {
public abstract void run();
}"
});
var proj = new XamarinAndroidApplicationProject { IsRelease = true, ProjectName = "App1" };
proj.References.Add(new BuildItem.ProjectReference (Path.Combine ("..", "Lib1", "Lib1.csproj"), "Lib1"));
proj.MainActivity = proj.DefaultMainActivity.Replace (
"base.OnCreate (bundle);",
"base.OnCreate (bundle);\n" +
"if (Lib1.Library1.Is64 ()) Console.WriteLine (\"Hello World!\");");
proj.SetProperty ("AndroidEnableMarshalMethods", enableMarshalMethods.ToString ());


using var lb = CreateDllBuilder (Path.Combine (path, "Lib1"));
using var b = CreateApkBuilder (Path.Combine (path, "App1"));
Assert.IsTrue (lb.Build (lib), "build should have succeeded.");
Assert.IsTrue (b.Build (proj), "build should have succeeded.");

var intermediate = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath);
var dll = $"{lib.ProjectName}.dll";
Assert64Bit ("android-arm", expected64: false);
Assert64Bit ("android-arm64", expected64: true);
Assert64Bit ("android-x86", expected64: false);
Assert64Bit ("android-x64", expected64: true);

void Assert64Bit(string rid, bool expected64)
{
var assembly = AssemblyDefinition.ReadAssembly (Path.Combine (intermediate, rid, "linked", "shrunk", dll));
var type = assembly.MainModule.FindType ("Lib1.Library1");
Assert.NotNull (type, "Should find Lib1.Library1!");
var cctor = type.GetTypeConstructor ();
Assert.NotNull (type, "Should find Lib1.Library1.cctor!");
Assert.AreNotEqual (0, cctor.Body.Instructions.Count);

/*
* IL snippet
* .method private hidebysig specialname rtspecialname static
* void .cctor () cil managed
* {
* // Is64Bits = 4 >= 8;
* IL_0000: ldc.i4 4
* IL_0005: ldc.i4.8
* ...
*/
var instruction = cctor.Body.Instructions [0];
Assert.AreEqual (OpCodes.Ldc_I4, instruction.OpCode);
if (expected64) {
Assert.AreEqual (8, instruction.Operand, $"Expected 64-bit: {expected64}");
} else {
Assert.AreEqual (4, instruction.Operand, $"Expected 64-bit: {expected64}");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,64 @@
"Comment": null,
"Entries": {
"AndroidManifest.xml": {
"Size": 3032
"Size": 3036
},
"assemblies/_Microsoft.Android.Resource.Designer.dll": {
"Size": 1024
},
"assemblies/Java.Interop.dll": {
"Size": 58703
"Size": 58990
},
"assemblies/Mono.Android.dll": {
"Size": 86588
"Size": 88074
},
"assemblies/Mono.Android.Runtime.dll": {
"Size": 5798
"Size": 5819
},
"assemblies/rc.bin": {
"Size": 1235
},
"assemblies/System.Console.dll": {
"Size": 6442
"Size": 6448
},
"assemblies/System.Linq.dll": {
"Size": 9123
"Size": 9135
},
"assemblies/System.Private.CoreLib.dll": {
"Size": 536436
"Size": 537441
},
"assemblies/System.Runtime.dll": {
"Size": 2623
"Size": 2629
},
"assemblies/System.Runtime.InteropServices.dll": {
"Size": 3752
"Size": 3768
},
"assemblies/UnnamedProject.dll": {
"Size": 3349
"Size": 3222
},
"classes.dex": {
"Size": 19748
"Size": 377064
},
"lib/arm64-v8a/libmono-component-marshal-ilgen.so": {
"Size": 93552
"Size": 97392
},
"lib/arm64-v8a/libmonodroid.so": {
"Size": 380832
"Size": 380704
},
"lib/arm64-v8a/libmonosgen-2.0.so": {
"Size": 3160360
"Size": 3177168
},
"lib/arm64-v8a/libSystem.IO.Compression.Native.so": {
"Size": 723560
},
"lib/arm64-v8a/libSystem.Native.so": {
"Size": 94392
"Size": 94424
},
"lib/arm64-v8a/libSystem.Security.Cryptography.Native.Android.so": {
"Size": 154904
},
"lib/arm64-v8a/libxamarin-app.so": {
"Size": 16624
"Size": 11080
},
"META-INF/BNDLTOOL.RSA": {
"Size": 1213
Expand Down Expand Up @@ -95,5 +95,5 @@
"Size": 1904
}
},
"PackageSize": 2685258
"PackageSize": 2771274
}
Loading