Skip to content

Commit 3f14705

Browse files
committed
Merge branch 'main' into dev/grendel/tmt-fix
* main: [monodroid] typemaps may need to load assemblies (#8625) Bump $(AndroidNetPreviousVersion) to 34.0.79 (#8693) Bump to xamarin/java.interop/main@07c73009 (#8681)
2 parents 3629d11 + 06b1d7f commit 3f14705

22 files changed

+147
-56
lines changed

Configuration.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
<DebugType Condition=" '$(DebugType)' == '' ">portable</DebugType>
4444
<Deterministic Condition=" '$(Deterministic)' == '' ">True</Deterministic>
4545
<LangVersion Condition=" '$(LangVersion)' == '' ">latest</LangVersion>
46-
<AndroidNetPreviousVersion Condition=" '$(AndroidNetPreviousVersion)' == '' ">34.0.56</AndroidNetPreviousVersion>
46+
<AndroidNetPreviousVersion Condition=" '$(AndroidNetPreviousVersion)' == '' ">34.0.79</AndroidNetPreviousVersion>
4747
</PropertyGroup>
4848
<PropertyGroup Condition=" '$(HostOS)' == '' ">
4949
<HostOS Condition="$([MSBuild]::IsOSPlatform('windows'))">Windows</HostOS>

external/Java.Interop

src/Mono.Android/Java.Interop/TypeManager.cs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership
267267
{
268268
Type? type = null;
269269
IntPtr class_ptr = JNIEnv.GetObjectClass (handle);
270-
string class_name = GetClassName (class_ptr);
270+
string? class_name = GetClassName (class_ptr);
271271
lock (TypeManagerMapDictionaries.AccessLock) {
272272
while (class_ptr != IntPtr.Zero && !TypeManagerMapDictionaries.JniToManaged.TryGetValue (class_name, out type)) {
273273

@@ -279,23 +279,33 @@ internal static IJavaPeerable CreateInstance (IntPtr handle, JniHandleOwnership
279279

280280
IntPtr super_class_ptr = JNIEnv.GetSuperclass (class_ptr);
281281
JNIEnv.DeleteLocalRef (class_ptr);
282+
class_name = null;
282283
class_ptr = super_class_ptr;
283-
class_name = GetClassName (class_ptr);
284+
if (class_ptr != IntPtr.Zero) {
285+
class_name = GetClassName (class_ptr);
286+
}
284287
}
285288
}
286289

287-
JNIEnv.DeleteLocalRef (class_ptr);
290+
if (class_ptr != IntPtr.Zero) {
291+
JNIEnv.DeleteLocalRef (class_ptr);
292+
class_ptr = IntPtr.Zero;
293+
}
294+
295+
if (targetType != null &&
296+
(type == null ||
297+
!targetType.IsAssignableFrom (type))) {
298+
type = targetType;
299+
}
288300

289301
if (type == null) {
302+
class_name = JNIEnv.GetClassNameFromInstance (handle);
290303
JNIEnv.DeleteRef (handle, transfer);
291304
throw new NotSupportedException (
292-
FormattableString.Invariant ($"Internal error finding wrapper class for '{JNIEnv.GetClassNameFromInstance (handle)}'. (Where is the Java.Lang.Object wrapper?!)"),
305+
FormattableString.Invariant ($"Internal error finding wrapper class for '{class_name}'. (Where is the Java.Lang.Object wrapper?!)"),
293306
CreateJavaLocationException ());
294307
}
295308

296-
if (targetType != null && !targetType.IsAssignableFrom (type))
297-
type = targetType;
298-
299309
if (type.IsInterface || type.IsAbstract) {
300310
var invokerType = JavaObjectExtensions.GetInvokerType (type);
301311
if (invokerType == null)

src/Xamarin.Android.Build.Tasks/Tasks/GenerateCompressedAssembliesNativeSourceFiles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void GenerateCompressedAssemblySources ()
7474

7575
void Generate (IDictionary<string, CompressedAssemblyInfo> dict)
7676
{
77-
var composer = new CompressedAssembliesNativeAssemblyGenerator (dict);
77+
var composer = new CompressedAssembliesNativeAssemblyGenerator (Log, dict);
7878
LLVMIR.LlvmIrModule compressedAssemblies = composer.Construct ();
7979

8080
foreach (string abi in SupportedAbis) {

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void SaveResource (string resource, string filename, string destDir, Func<string
570570

571571
void WriteTypeMappings (List<JavaType> types, TypeDefinitionCache cache)
572572
{
573-
var tmg = new TypeMapGenerator ((string message) => Log.LogDebugMessage (message), SupportedAbis);
573+
var tmg = new TypeMapGenerator (Log, SupportedAbis);
574574
if (!tmg.Generate (Debug, SkipJniAddNativeMethodRegistrationAttributeScan, types, cache, TypemapOutputDirectory, GenerateNativeAssembly, out ApplicationConfigTaskState appConfState)) {
575575
throw new XamarinAndroidException (4308, Properties.Resources.XA4308);
576576
}

src/Xamarin.Android.Build.Tasks/Tasks/GenerateJniRemappingNativeCode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public override bool RunTask ()
5454

5555
void GenerateEmpty ()
5656
{
57-
Generate (new JniRemappingAssemblyGenerator (), typeReplacementsCount: 0);
57+
Generate (new JniRemappingAssemblyGenerator (Log), typeReplacementsCount: 0);
5858
}
5959

6060
void Generate ()
@@ -74,7 +74,7 @@ void Generate ()
7474
}
7575
}
7676

77-
Generate (new JniRemappingAssemblyGenerator (typeReplacements, methodReplacements), typeReplacements.Count);
77+
Generate (new JniRemappingAssemblyGenerator (Log, typeReplacements, methodReplacements), typeReplacements.Count);
7878
}
7979

8080
void Generate (JniRemappingAssemblyGenerator jniRemappingComposer, int typeReplacementsCount)

src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,13 +386,13 @@ void AddEnvironment ()
386386

387387
if (enableMarshalMethods) {
388388
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (
389+
Log,
389390
assemblyCount,
390391
uniqueAssemblyNames,
391-
marshalMethodsState?.MarshalMethods,
392-
Log
392+
marshalMethodsState?.MarshalMethods
393393
);
394394
} else {
395-
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (assemblyCount, uniqueAssemblyNames);
395+
marshalMethodsAsmGen = new MarshalMethodsNativeAssemblyGenerator (Log, assemblyCount, uniqueAssemblyNames);
396396
}
397397
LLVMIR.LlvmIrModule marshalMethodsModule = marshalMethodsAsmGen.Construct ();
398398

src/Xamarin.Android.Build.Tasks/Tasks/JavaCompileToolTask.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ private void GenerateResponseFile ()
9090
file.Replace (@"\", @"\\").Normalize (NormalizationForm.FormC)));
9191
}
9292
}
93+
Log.LogDebugMessage ($"javac response file contents: {TemporarySourceListFile}");
94+
foreach (var line in File.ReadLines (TemporarySourceListFile)) {
95+
Log.LogDebugMessage ($" {line}");
96+
}
9397
}
9498
}
9599
}

src/Xamarin.Android.Build.Tasks/Tasks/R8.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Microsoft.Build.Framework;
23
using Microsoft.Build.Utilities;
34
using System.Collections.Generic;
@@ -82,16 +83,15 @@ protected override CommandLineBuilder GetCommandLineBuilder ()
8283

8384
if (EnableShrinking) {
8485
if (!string.IsNullOrEmpty (AcwMapFile)) {
85-
var acwLines = File.ReadAllLines (AcwMapFile);
86+
var acwMap = MonoAndroidHelper.LoadMapFile (BuildEngine4, Path.GetFullPath (AcwMapFile), StringComparer.OrdinalIgnoreCase);
87+
var javaTypes = new List<string> (acwMap.Values.Count);
88+
foreach (var v in acwMap.Values) {
89+
javaTypes.Add (v);
90+
}
91+
javaTypes.Sort (StringComparer.Ordinal);
8692
using (var appcfg = File.CreateText (ProguardGeneratedApplicationConfiguration)) {
87-
for (int i = 0; i + 2 < acwLines.Length; i += 3) {
88-
try {
89-
var line = acwLines [i + 2];
90-
var java = line.Substring (line.IndexOf (';') + 1);
91-
appcfg.WriteLine ("-keep class " + java + " { *; }");
92-
} catch {
93-
// skip invalid lines
94-
}
93+
foreach (var java in javaTypes) {
94+
appcfg.WriteLine ($"-keep class {java} {{ *; }}");
9595
}
9696
}
9797
}

src/Xamarin.Android.Build.Tasks/Utilities/ApplicationConfigNativeAssemblyGenerator.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Java.Interop.Tools.TypeNameMappings;
77
using Microsoft.Build.Framework;
88
using Microsoft.Build.Utilities;
9+
using Microsoft.Android.Build.Tasks;
910
using Xamarin.Android.Tasks.LLVMIR;
1011

1112
namespace Xamarin.Android.Tasks
@@ -135,7 +136,6 @@ sealed class XamarinAndroidBundledAssembly
135136

136137
SortedDictionary <string, string>? environmentVariables;
137138
SortedDictionary <string, string>? systemProperties;
138-
TaskLoggingHelper log;
139139
StructureInstance? application_config;
140140
List<StructureInstance<DSOCacheEntry>>? dsoCache;
141141
List<StructureInstance<XamarinAndroidBundledAssembly>>? xamarinAndroidBundledAssemblies;
@@ -172,6 +172,7 @@ sealed class XamarinAndroidBundledAssembly
172172
public bool MarshalMethodsEnabled { get; set; }
173173

174174
public ApplicationConfigNativeAssemblyGenerator (IDictionary<string, string> environmentVariables, IDictionary<string, string> systemProperties, TaskLoggingHelper log)
175+
: base (log)
175176
{
176177
if (environmentVariables != null) {
177178
this.environmentVariables = new SortedDictionary<string, string> (environmentVariables, StringComparer.Ordinal);
@@ -180,8 +181,6 @@ public ApplicationConfigNativeAssemblyGenerator (IDictionary<string, string> env
180181
if (systemProperties != null) {
181182
this.systemProperties = new SortedDictionary<string, string> (systemProperties, StringComparer.Ordinal);
182183
}
183-
184-
this.log = log;
185184
}
186185

187186
protected override void Construct (LlvmIrModule module)
@@ -322,7 +321,7 @@ List<StructureInstance<DSOCacheEntry>> InitDSOCache ()
322321
continue;
323322
}
324323

325-
dsos.Add ((name, $"dsoName{dsos.Count.ToString (CultureInfo.InvariantCulture)}", ELFHelper.IsEmptyAOTLibrary (log, item.ItemSpec)));
324+
dsos.Add ((name, $"dsoName{dsos.Count.ToString (CultureInfo.InvariantCulture)}", ELFHelper.IsEmptyAOTLibrary (Log, item.ItemSpec)));
326325
}
327326

328327
var dsoCache = new List<StructureInstance<DSOCacheEntry>> ();

src/Xamarin.Android.Build.Tasks/Utilities/CompressedAssembliesNativeAssemblyGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33

4+
using Microsoft.Build.Utilities;
5+
46
using Xamarin.Android.Tasks.LLVMIR;
57

68
namespace Xamarin.Android.Tasks
@@ -66,7 +68,8 @@ sealed class CompressedAssemblies
6668
StructureInfo compressedAssemblyDescriptorStructureInfo;
6769
StructureInfo compressedAssembliesStructureInfo;
6870

69-
public CompressedAssembliesNativeAssemblyGenerator (IDictionary<string, CompressedAssemblyInfo> assemblies)
71+
public CompressedAssembliesNativeAssemblyGenerator (TaskLoggingHelper log, IDictionary<string, CompressedAssemblyInfo> assemblies)
72+
: base (log)
7073
{
7174
this.assemblies = assemblies;
7275
}

src/Xamarin.Android.Build.Tasks/Utilities/JniRemappingAssemblyGenerator.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.Text;
44

5+
using Microsoft.Build.Utilities;
6+
57
using Xamarin.Android.Tasks.LLVMIR;
68

79
namespace Xamarin.Android.Tasks
@@ -190,10 +192,12 @@ sealed class JniRemappingTypeReplacementEntry
190192

191193
public int ReplacementMethodIndexEntryCount { get; private set; } = 0;
192194

193-
public JniRemappingAssemblyGenerator ()
195+
public JniRemappingAssemblyGenerator (TaskLoggingHelper log)
196+
: base (log)
194197
{}
195198

196-
public JniRemappingAssemblyGenerator (List<JniRemappingTypeReplacement> typeReplacements, List<JniRemappingMethodReplacement> methodReplacements)
199+
public JniRemappingAssemblyGenerator (TaskLoggingHelper log, List<JniRemappingTypeReplacement> typeReplacements, List<JniRemappingMethodReplacement> methodReplacements)
200+
: base (log)
197201
{
198202
this.typeReplacementsInput = typeReplacements ?? throw new ArgumentNullException (nameof (typeReplacements));
199203
this.methodReplacementsInput = methodReplacements ?? throw new ArgumentNullException (nameof (methodReplacements));

src/Xamarin.Android.Build.Tasks/Utilities/LlvmIrGenerator/LlvmIrComposer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
using System.IO.Hashing;
44
using System.Text;
55

6+
using Microsoft.Build.Utilities;
7+
68
using Xamarin.Android.Tools;
79

810
namespace Xamarin.Android.Tasks.LLVMIR
@@ -11,6 +13,13 @@ abstract class LlvmIrComposer
1113
{
1214
bool constructed;
1315

16+
protected readonly TaskLoggingHelper Log;
17+
18+
protected LlvmIrComposer (TaskLoggingHelper log)
19+
{
20+
this.Log = log ?? throw new ArgumentNullException (nameof (log));
21+
}
22+
1423
protected abstract void Construct (LlvmIrModule module);
1524

1625
public LlvmIrModule Construct ()

src/Xamarin.Android.Build.Tasks/Utilities/MarshalMethodsNativeAssemblyGenerator.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ public MarshalMethodAssemblyIndexValuePlaceholder (MarshalMethodInfo mmi, Assemb
228228
ICollection<string> uniqueAssemblyNames;
229229
int numberOfAssembliesInApk;
230230
IDictionary<string, IList<MarshalMethodEntry>> marshalMethods;
231-
TaskLoggingHelper logger;
232231

233232
StructureInfo marshalMethodsManagedClassStructureInfo;
234233
StructureInfo marshalMethodNameStructureInfo;
@@ -243,7 +242,8 @@ public MarshalMethodAssemblyIndexValuePlaceholder (MarshalMethodInfo mmi, Assemb
243242
/// <summary>
244243
/// Constructor to be used ONLY when marshal methods are DISABLED
245244
/// </summary>
246-
public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames)
245+
public MarshalMethodsNativeAssemblyGenerator (TaskLoggingHelper log, int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames)
246+
: base (log)
247247
{
248248
this.numberOfAssembliesInApk = numberOfAssembliesInApk;
249249
this.uniqueAssemblyNames = uniqueAssemblyNames ?? throw new ArgumentNullException (nameof (uniqueAssemblyNames));
@@ -254,12 +254,12 @@ public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, IColl
254254
/// <summary>
255255
/// Constructor to be used ONLY when marshal methods are ENABLED
256256
/// </summary>
257-
public MarshalMethodsNativeAssemblyGenerator (int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames, IDictionary<string, IList<MarshalMethodEntry>> marshalMethods, TaskLoggingHelper logger)
257+
public MarshalMethodsNativeAssemblyGenerator (TaskLoggingHelper log, int numberOfAssembliesInApk, ICollection<string> uniqueAssemblyNames, IDictionary<string, IList<MarshalMethodEntry>> marshalMethods)
258+
: base (log)
258259
{
259260
this.numberOfAssembliesInApk = numberOfAssembliesInApk;
260261
this.uniqueAssemblyNames = uniqueAssemblyNames ?? throw new ArgumentNullException (nameof (uniqueAssemblyNames));
261262
this.marshalMethods = marshalMethods;
262-
this.logger = logger ?? throw new ArgumentNullException (nameof (logger));
263263

264264
generateEmptyCode = false;
265265
defaultCallMarker = LlvmIrCallMarker.Tail;
@@ -334,7 +334,7 @@ void Init ()
334334

335335
foreach (MarshalMethodInfo method in allMethods) {
336336
if (seenNativeSymbols.Contains (method.NativeSymbolName)) {
337-
logger.LogDebugMessage ($"Removed MM duplicate '{method.NativeSymbolName}' (implemented: {method.Method.ImplementedMethod.FullName}; registered: {method.Method.RegisteredMethod.FullName}");
337+
Log.LogDebugMessage ($"Removed MM duplicate '{method.NativeSymbolName}' (implemented: {method.Method.ImplementedMethod.FullName}; registered: {method.Method.RegisteredMethod.FullName}");
338338
continue;
339339
}
340340

src/Xamarin.Android.Build.Tasks/Utilities/NativeTypeMappingData.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44

5+
using Microsoft.Build.Utilities;
6+
using Microsoft.Android.Build.Tasks;
7+
58
namespace Xamarin.Android.Tasks
69
{
710
class NativeTypeMappingData
@@ -12,7 +15,7 @@ class NativeTypeMappingData
1215
public uint MapModuleCount { get; }
1316
public uint JavaTypeCount { get; }
1417

15-
public NativeTypeMappingData (TypeMapGenerator.ModuleReleaseData[] modules)
18+
public NativeTypeMappingData (TaskLoggingHelper log, TypeMapGenerator.ModuleReleaseData[] modules)
1619
{
1720
Modules = modules ?? throw new ArgumentNullException (nameof (modules));
1821

@@ -21,15 +24,37 @@ public NativeTypeMappingData (TypeMapGenerator.ModuleReleaseData[] modules)
2124
var tempJavaTypes = new Dictionary<string, TypeMapGenerator.TypeMapReleaseEntry> (StringComparer.Ordinal);
2225
var moduleComparer = new TypeMapGenerator.ModuleUUIDArrayComparer ();
2326

27+
TypeMapGenerator.ModuleReleaseData? monoAndroid = null;
2428
foreach (TypeMapGenerator.ModuleReleaseData data in modules) {
29+
if (data.AssemblyName == "Mono.Android") {
30+
monoAndroid = data;
31+
break;
32+
}
33+
}
34+
35+
if (monoAndroid != null) {
36+
ProcessModule (monoAndroid);
37+
}
38+
39+
foreach (TypeMapGenerator.ModuleReleaseData data in modules) {
40+
if (data.AssemblyName == "Mono.Android") {
41+
continue;
42+
}
43+
ProcessModule (data);
44+
};
45+
46+
void ProcessModule (TypeMapGenerator.ModuleReleaseData data)
47+
{
2548
int moduleIndex = Array.BinarySearch (modules, data, moduleComparer);
2649
if (moduleIndex < 0)
2750
throw new InvalidOperationException ($"Unable to map module with MVID {data.Mvid} to array index");
2851

2952
foreach (TypeMapGenerator.TypeMapReleaseEntry entry in data.Types) {
3053
entry.ModuleIndex = moduleIndex;
31-
if (tempJavaTypes.ContainsKey (entry.JavaName))
54+
if (tempJavaTypes.ContainsKey (entry.JavaName)) {
55+
log.LogDebugMessage ($"Skipping typemap entry for `{entry.ManagedTypeName}, {data.AssemblyName}`; `{entry.JavaName}` is already mapped.");
3256
continue;
57+
}
3358
tempJavaTypes.Add (entry.JavaName, entry);
3459
}
3560
}

0 commit comments

Comments
 (0)