15
15
16
16
using Xamarin . Android . Tools ;
17
17
using Microsoft . Android . Build . Tasks ;
18
- using Java . Interop . Tools . JavaCallableWrappers . Adapters ;
19
18
using System . Threading . Tasks ;
20
19
using System . Collections . Concurrent ;
21
20
@@ -35,9 +34,6 @@ public class GenerateJavaStubs : AndroidTask
35
34
[ Required ]
36
35
public ITaskItem [ ] ResolvedUserAssemblies { get ; set ; }
37
36
38
- [ Required ]
39
- public string AcwMapFile { get ; set ; }
40
-
41
37
[ Required ]
42
38
public ITaskItem [ ] FrameworkDirectories { get ; set ; }
43
39
@@ -54,40 +50,20 @@ public class GenerateJavaStubs : AndroidTask
54
50
public bool LinkingEnabled { get ; set ; }
55
51
public bool HaveMultipleRIDs { get ; set ; }
56
52
public bool EnableMarshalMethods { get ; set ; }
57
- public string ManifestTemplate { get ; set ; }
58
- public string [ ] MergedManifestDocuments { get ; set ; }
59
53
60
54
public bool Debug { get ; set ; }
61
- public bool MultiDex { get ; set ; }
62
- public string ApplicationLabel { get ; set ; }
63
- public string PackageName { get ; set ; }
64
- public string VersionName { get ; set ; }
65
- public string VersionCode { get ; set ; }
66
- public string [ ] ManifestPlaceholders { get ; set ; }
67
-
68
- public string AndroidSdkDir { get ; set ; }
69
55
70
56
public string AndroidSdkPlatform { get ; set ; }
71
57
public string OutputDirectory { get ; set ; }
72
- public string MergedAndroidManifestOutput { get ; set ; }
73
-
74
- public bool EmbedAssemblies { get ; set ; }
75
- public bool NeedsInternet { get ; set ; }
76
58
77
59
public bool ErrorOnCustomJavaObject { get ; set ; }
78
60
79
- public string BundledWearApplicationName { get ; set ; }
80
-
81
61
public string PackageNamingPolicy { get ; set ; }
82
62
83
63
public string ApplicationJavaClass { get ; set ; }
84
64
85
65
public bool SkipJniAddNativeMethodRegistrationAttributeScan { get ; set ; }
86
66
87
- public string CheckedBuild { get ; set ; }
88
-
89
- public string SupportedOSPlatformVersion { get ; set ; }
90
-
91
67
public ITaskItem [ ] Environments { get ; set ; }
92
68
93
69
[ Output ]
@@ -98,9 +74,6 @@ public class GenerateJavaStubs : AndroidTask
98
74
99
75
public string CodeGenerationTarget { get ; set ; } = "" ;
100
76
101
- [ Required ]
102
- public string TargetName { get ; set ; } = "" ;
103
-
104
77
AndroidRuntime androidRuntime ;
105
78
JavaPeerStyle codeGenerationTarget ;
106
79
@@ -119,13 +92,6 @@ public override bool RunTask ()
119
92
Log . LogMessage ( e . ToString ( ) ) ;
120
93
}
121
94
122
- if ( Log . HasLoggedErrors ) {
123
- // Ensure that on a rebuild, we don't *skip* the `_GenerateJavaStubs` target,
124
- // by ensuring that the target outputs have been deleted.
125
- Files . DeleteFile ( MergedAndroidManifestOutput , Log ) ;
126
- Files . DeleteFile ( AcwMapFile , Log ) ;
127
- }
128
-
129
95
return ! Log . HasLoggedErrors ;
130
96
}
131
97
@@ -275,137 +241,18 @@ void Run (bool useMarshalMethods)
275
241
// Set for use by <GeneratePackageManagerJava/> task later
276
242
NativeCodeGenState . TemplateJniAddNativeMethodRegistrationAttributePresent = templateCodeGenState . JniAddNativeMethodRegistrationAttributePresent ;
277
243
278
- var acwMapGen = new ACWMapGenerator ( Log ) ;
279
- if ( ! acwMapGen . Generate ( templateCodeGenState , AcwMapFile ) ) {
280
- Log . LogDebugMessage ( "ACW map generation failed" ) ;
281
- }
282
-
283
- IList < string > additionalProviders = MergeManifest ( templateCodeGenState , MaybeGetArchAssemblies ( userAssembliesPerArch , templateCodeGenState . TargetArch ) ) ;
284
- GenerateAdditionalProviderSources ( templateCodeGenState , additionalProviders ) ;
285
-
286
- if ( useMarshalMethods ) {
287
- // Save NativeCodeGenState for <GeneratePackageManagerJava/> task later
288
- Log . LogDebugMessage ( $ "Saving { nameof ( NativeCodeGenState ) } to { nameof ( NativeCodeGenStateRegisterTaskKey ) } ") ;
289
- BuildEngine4 . RegisterTaskObjectAssemblyLocal ( ProjectSpecificTaskObjectKey ( NativeCodeGenStateRegisterTaskKey ) , nativeCodeGenStates , RegisteredTaskObjectLifetime . Build ) ;
290
- } else {
291
- // Otherwise, dispose all XAAssemblyResolvers
292
- Log . LogDebugMessage ( $ "Disposing all { nameof ( NativeCodeGenState ) } .{ nameof ( NativeCodeGenState . Resolver ) } ") ;
293
- foreach ( var state in nativeCodeGenStates . Values ) {
294
- state . Resolver . Dispose ( ) ;
295
- }
296
- }
297
-
298
- Dictionary < string , ITaskItem > MaybeGetArchAssemblies ( Dictionary < AndroidTargetArch , Dictionary < string , ITaskItem > > dict , AndroidTargetArch arch )
299
- {
300
- if ( ! dict . TryGetValue ( arch , out Dictionary < string , ITaskItem > archDict ) ) {
301
- return new Dictionary < string , ITaskItem > ( StringComparer . OrdinalIgnoreCase ) ;
302
- }
303
-
304
- return archDict ;
305
- }
306
- }
307
-
308
- void GenerateAdditionalProviderSources ( NativeCodeGenState codeGenState , IList < string > additionalProviders )
309
- {
310
- if ( androidRuntime != Xamarin . Android . Tasks . AndroidRuntime . CoreCLR ) {
311
- // Create additional runtime provider java sources.
312
- bool isMonoVM = androidRuntime == Xamarin . Android . Tasks . AndroidRuntime . MonoVM ;
313
- string providerTemplateFile = isMonoVM ?
314
- "MonoRuntimeProvider.Bundled.java" :
315
- "NativeAotRuntimeProvider.java" ;
316
- string providerTemplate = GetResource ( providerTemplateFile ) ;
317
-
318
- foreach ( var provider in additionalProviders ) {
319
- var contents = providerTemplate . Replace ( isMonoVM ? "MonoRuntimeProvider" : "NativeAotRuntimeProvider" , provider ) ;
320
- var real_provider = isMonoVM ?
321
- Path . Combine ( OutputDirectory , "src" , "mono" , provider + ".java" ) :
322
- Path . Combine ( OutputDirectory , "src" , "net" , "dot" , "jni" , "nativeaot" , provider + ".java" ) ;
323
- Files . CopyIfStringChanged ( contents , real_provider ) ;
324
- }
325
- } else {
326
- Log . LogDebugMessage ( $ "Skipping android.content.ContentProvider generation for: { androidRuntime } ") ;
327
- }
328
-
329
- // For NativeAOT, generate JavaInteropRuntime.java
330
- if ( androidRuntime == Xamarin . Android . Tasks . AndroidRuntime . NativeAOT ) {
331
- const string fileName = "JavaInteropRuntime.java" ;
332
- string template = GetResource ( fileName ) ;
333
- var contents = template . Replace ( "@MAIN_ASSEMBLY_NAME@" , TargetName ) ;
334
- var path = Path . Combine ( OutputDirectory , "src" , "net" , "dot" , "jni" , "nativeaot" , fileName ) ;
335
- Log . LogDebugMessage ( $ "Writing: { path } ") ;
336
- Files . CopyIfStringChanged ( contents , path ) ;
337
- }
338
-
339
- // Create additional application java sources.
340
- StringWriter regCallsWriter = new StringWriter ( ) ;
341
- regCallsWriter . WriteLine ( "// Application and Instrumentation ACWs must be registered first." ) ;
342
- foreach ( TypeDefinition type in codeGenState . JavaTypesForJCW ) {
343
- if ( JavaNativeTypeManager . IsApplication ( type , codeGenState . TypeCache ) || JavaNativeTypeManager . IsInstrumentation ( type , codeGenState . TypeCache ) ) {
344
- if ( codeGenState . Classifier != null && ! codeGenState . Classifier . FoundDynamicallyRegisteredMethods ( type ) ) {
345
- continue ;
346
- }
347
-
348
- string javaKey = JavaNativeTypeManager . ToJniName ( type , codeGenState . TypeCache ) . Replace ( '/' , '.' ) ;
349
- regCallsWriter . WriteLine (
350
- codeGenerationTarget == JavaPeerStyle . XAJavaInterop1 ?
351
- "\t \t mono.android.Runtime.register (\" {0}\" , {1}.class, {1}.__md_methods);" :
352
- "\t \t net.dot.jni.ManagedPeer.registerNativeMembers ({1}.class, {1}.__md_methods);" ,
353
- type . GetAssemblyQualifiedName ( codeGenState . TypeCache ) ,
354
- javaKey
355
- ) ;
356
- }
357
- }
358
- regCallsWriter . Close ( ) ;
359
-
360
- var real_app_dir = Path . Combine ( OutputDirectory , "src" , "net" , "dot" , "android" ) ;
361
- string applicationTemplateFile = "ApplicationRegistration.java" ;
362
- SaveResource (
363
- applicationTemplateFile ,
364
- applicationTemplateFile ,
365
- real_app_dir ,
366
- template => template . Replace ( "// REGISTER_APPLICATION_AND_INSTRUMENTATION_CLASSES_HERE" , regCallsWriter . ToString ( ) )
367
- ) ;
244
+ // Save NativeCodeGenState for later tasks
245
+ Log . LogDebugMessage ( $ "Saving { nameof ( NativeCodeGenState ) } to { nameof ( NativeCodeGenStateRegisterTaskKey ) } ") ;
246
+ BuildEngine4 . RegisterTaskObjectAssemblyLocal ( MonoAndroidHelper . GetProjectBuildSpecificTaskObjectKey ( NativeCodeGenStateRegisterTaskKey , WorkingDirectory , IntermediateOutputDirectory ) , nativeCodeGenStates , RegisteredTaskObjectLifetime . Build ) ;
368
247
}
369
248
370
- IList < string > MergeManifest ( NativeCodeGenState codeGenState , Dictionary < string , ITaskItem > userAssemblies )
249
+ internal static Dictionary < string , ITaskItem > MaybeGetArchAssemblies ( Dictionary < AndroidTargetArch , Dictionary < string , ITaskItem > > dict , AndroidTargetArch arch )
371
250
{
372
- var manifest = new ManifestDocument ( ManifestTemplate ) {
373
- PackageName = PackageName ,
374
- VersionName = VersionName ,
375
- ApplicationLabel = ApplicationLabel ?? PackageName ,
376
- Placeholders = ManifestPlaceholders ,
377
- Resolver = codeGenState . Resolver ,
378
- SdkDir = AndroidSdkDir ,
379
- TargetSdkVersion = AndroidSdkPlatform ,
380
- MinSdkVersion = MonoAndroidHelper . ConvertSupportedOSPlatformVersionToApiLevel ( SupportedOSPlatformVersion ) . ToString ( ) ,
381
- Debug = Debug ,
382
- MultiDex = MultiDex ,
383
- NeedsInternet = NeedsInternet ,
384
- AndroidRuntime = androidRuntime ,
385
- } ;
386
- // Only set manifest.VersionCode if there is no existing value in AndroidManifest.xml.
387
- if ( manifest . HasVersionCode ) {
388
- Log . LogDebugMessage ( $ "Using existing versionCode in: { ManifestTemplate } ") ;
389
- } else if ( ! string . IsNullOrEmpty ( VersionCode ) ) {
390
- manifest . VersionCode = VersionCode ;
391
- }
392
- manifest . Assemblies . AddRange ( userAssemblies . Values . Select ( item => item . ItemSpec ) ) ;
393
-
394
- if ( ! String . IsNullOrWhiteSpace ( CheckedBuild ) ) {
395
- // We don't validate CheckedBuild value here, this will be done in BuildApk. We just know that if it's
396
- // on then we need android:debuggable=true and android:extractNativeLibs=true
397
- manifest . ForceDebuggable = true ;
398
- manifest . ForceExtractNativeLibs = true ;
399
- }
400
-
401
- IList < string > additionalProviders = manifest . Merge ( Log , codeGenState . TypeCache , codeGenState . AllJavaTypes , ApplicationJavaClass , EmbedAssemblies , BundledWearApplicationName , MergedManifestDocuments ) ;
402
-
403
- // Only write the new manifest if it actually changed
404
- if ( manifest . SaveIfChanged ( Log , MergedAndroidManifestOutput ) ) {
405
- Log . LogDebugMessage ( $ "Saving: { MergedAndroidManifestOutput } ") ;
251
+ if ( ! dict . TryGetValue ( arch , out Dictionary < string , ITaskItem > archDict ) ) {
252
+ return new Dictionary < string , ITaskItem > ( StringComparer . OrdinalIgnoreCase ) ;
406
253
}
407
254
408
- return additionalProviders ;
255
+ return archDict ;
409
256
}
410
257
411
258
( bool success , NativeCodeGenState ? stubsState ) GenerateJavaSourcesAndMaybeClassifyMarshalMethods ( AndroidTargetArch arch , Dictionary < string , ITaskItem > assemblies , Dictionary < string , ITaskItem > userAssemblies , bool useMarshalMethods , bool generateJavaCode )
@@ -464,20 +311,6 @@ void RewriteMarshalMethods (NativeCodeGenState state, bool brokenExceptionTransi
464
311
rewriter . Rewrite ( brokenExceptionTransitionsEnabled ) ;
465
312
}
466
313
467
- string GetResource ( string resource )
468
- {
469
- using ( var stream = GetType ( ) . Assembly . GetManifestResourceStream ( resource ) )
470
- using ( var reader = new StreamReader ( stream ) )
471
- return reader . ReadToEnd ( ) ;
472
- }
473
-
474
- void SaveResource ( string resource , string filename , string destDir , Func < string , string > applyTemplate )
475
- {
476
- string template = GetResource ( resource ) ;
477
- template = applyTemplate ( template ) ;
478
- Files . CopyIfStringChanged ( template , Path . Combine ( destDir , filename ) ) ;
479
- }
480
-
481
314
void WriteTypeMappings ( NativeCodeGenState state )
482
315
{
483
316
if ( androidRuntime == Xamarin . Android . Tasks . AndroidRuntime . NativeAOT ) {
0 commit comments