Skip to content

Commit 106a621

Browse files
radekdoulikjonpryor
authored andcommitted
[Xamarin.Android.Build.Tasks] Add AndroidGenerateJniMarshalMethods (#1745)
This option makes preserving new marshal methods, generated by `jnimarshalmethod-gen.exe`, optional in the linker. It also introduces new `$(AndroidGenerateJniMarshalMethods)` MSBuild property to toggle building with generated marshal methods. It defaults to *False*.
1 parent 10e1ec1 commit 106a621

File tree

8 files changed

+56
-4
lines changed

8 files changed

+56
-4
lines changed

Documentation/guides/BuildProcess.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,24 @@ when packaing Release applications.
663663
See [Lint Help](http://www.androiddocs.com/tools/help/lint.html) for more details on
664664
the android `lint` tooling.
665665

666+
- **AndroidGenerateJniMarshalMethods** – A bool property which
667+
enables generating of JNI marshal methods as part of the build
668+
process. This greatly reduces the System.Reflection usage in the
669+
binding helper code.
670+
671+
By default this will be set to False. If the developers wish to use
672+
the new JNI marshal methods feature, they can set
673+
674+
<AndroidGenerateJniMarshalMethods>True</AndroidGenerateJniMarshalMethods>
675+
676+
in their csproj. Alternatively provide the property on the command
677+
line via
678+
679+
/p:AndroidGenerateJniMarshalMethods=True
680+
681+
**Experimental**. Added in Xamarin.Android 8.4.
682+
The default value is False.
683+
666684
### Binding Project Build Properties
667685

668686
The following MSBuild properties are used with
@@ -761,7 +779,7 @@ resources.
761779
- **AndroidUseAapt2** &ndash; A bool property which allows the developer to
762780
control the use of the `aapt2` tool for packaging.
763781
By default this will be set to false and we will use `aapt`.
764-
If the developer wishes too use the new `aapt2` functionality
782+
If the developer wishes to use the new `aapt2` functionality
765783
they can set
766784
767785
<AndroidUseAapt2>True</AndroidUseAapt2>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Mono.Linker;
2+
using Mono.Cecil;
3+
4+
namespace MonoDroid.Tuner
5+
{
6+
public class AndroidLinkContext : LinkContext
7+
{
8+
public AndroidLinkContext (Pipeline pipeline, AssemblyResolver resolver)
9+
: base (pipeline, resolver) {}
10+
11+
public AndroidLinkContext (Pipeline pipeline, AssemblyResolver resolver, ReaderParameters readerParameters, UnintializedContextFactory factory)
12+
: base (pipeline, resolver, readerParameters, factory) {}
13+
14+
public bool PreserveJniMarshalMethods { get; set; }
15+
}
16+
}

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Linker.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void Run (Pipeline pipeline, LinkContext context)
3737

3838
static LinkContext CreateLinkContext (LinkerOptions options, Pipeline pipeline, ILogger logger)
3939
{
40-
var context = new LinkContext (pipeline, options.Resolver);
40+
var context = new AndroidLinkContext (pipeline, options.Resolver);
4141
if (options.DumpDependencies) {
4242
var prepareDependenciesDump = context.Annotations.GetType ().GetMethod ("PrepareDependenciesDump", new Type[] {});
4343
if (prepareDependenciesDump != null)
@@ -51,6 +51,7 @@ static LinkContext CreateLinkContext (LinkerOptions options, Pipeline pipeline,
5151
context.SymbolReaderProvider = new DefaultSymbolReaderProvider (true);
5252
context.SymbolWriterProvider = new DefaultSymbolWriterProvider ();
5353
context.OutputDirectory = options.OutputDirectory;
54+
context.PreserveJniMarshalMethods = options.PreserveJniMarshalMethods;
5455
return context;
5556
}
5657

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/LinkerOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,6 @@ class LinkerOptions
2222
public bool DumpDependencies { get; set; }
2323
public string HttpClientHandlerType { get; set; }
2424
public string TlsProvider { get; set; }
25+
public bool PreserveJniMarshalMethods { get; set; }
2526
}
2627
}

src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public override void Process (LinkContext context)
2222
marshalTypes.Clear ();
2323
base.Process (context);
2424

25-
if (UpdateMarshalTypes ())
25+
if (PreserveJniMarshalMethods () && UpdateMarshalTypes ())
2626
base.Process (context);
2727
}
2828

@@ -196,6 +196,14 @@ bool UpdateMarshalRegisterMethod (MethodDefinition method, HashSet<string> marke
196196
return true;
197197
}
198198

199+
bool PreserveJniMarshalMethods ()
200+
{
201+
if (_context is AndroidLinkContext ac)
202+
return ac.PreserveJniMarshalMethods;
203+
204+
return false;
205+
}
206+
199207
// If this is one of our infrastructure methods that has [Register], like:
200208
// [Register ("hasWindowFocus", "()Z", "GetHasWindowFocusHandler")],
201209
// we need to preserve the "GetHasWindowFocusHandler" method as well.
@@ -207,7 +215,7 @@ protected override void DoAdditionalMethodProcessing (MethodDefinition method)
207215
return;
208216

209217
MethodDefinition marshalMethod;
210-
if (method.TryGetMarshalMethod (nativeMethod, signature, out marshalMethod)) {
218+
if (PreserveJniMarshalMethods () && method.TryGetMarshalMethod (nativeMethod, signature, out marshalMethod)) {
211219
MarkMethod (marshalMethod);
212220
marshalTypes.Add (marshalMethod.DeclaringType);
213221
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class LinkAssemblies : Task, ML.ILogger
5151

5252
public string TlsProvider { get; set; }
5353

54+
public bool PreserveJniMarshalMethods { get; set; }
55+
5456
IEnumerable<AssemblyDefinition> GetRetainAssemblies (DirectoryAssemblyResolver res)
5557
{
5658
List<AssemblyDefinition> retainList = null;
@@ -83,6 +85,7 @@ public override bool Execute ()
8385
Log.LogDebugMessage (" LinkOnlyNewerThan: {0}", LinkOnlyNewerThan);
8486
Log.LogDebugMessage (" HttpClientHandlerType: {0}", HttpClientHandlerType);
8587
Log.LogDebugMessage (" TlsProvider: {0}", TlsProvider);
88+
Log.LogDebugMessage (" PreserveJniMarshalMethods: {0}", PreserveJniMarshalMethods);
8689

8790
var rp = new ReaderParameters {
8891
InMemory = true,
@@ -115,6 +118,7 @@ bool Execute (DirectoryAssemblyResolver res)
115118
options.DumpDependencies = DumpDependencies;
116119
options.HttpClientHandlerType = HttpClientHandlerType;
117120
options.TlsProvider = TlsProvider;
121+
options.PreserveJniMarshalMethods = PreserveJniMarshalMethods;
118122

119123
var skiplist = new List<string> ();
120124

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@
575575
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
576576
</None>
577577
<Compile Include="Tasks\JavaDoc.cs" />
578+
<Compile Include="Linker\MonoDroid.Tuner\AndroidLinkContext.cs" />
578579
</ItemGroup>
579580
<ItemGroup>
580581
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.LaunchMode.cs" />

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
303303
<_InstantRunEnabled Condition=" '$(_InstantRunEnabled)' == '' ">False</_InstantRunEnabled>
304304
<_AndroidBuildPropertiesCache>$(IntermediateOutputPath)build.props</_AndroidBuildPropertiesCache>
305305

306+
<AndroidGenerateJniMarshalMethods Condition=" '$(AndroidGenerateJniMarshalMethods)' == '' ">False</AndroidGenerateJniMarshalMethods>
307+
306308
</PropertyGroup>
307309

308310
<Choose>
@@ -2057,6 +2059,7 @@ because xbuild doesn't support framework reference assemblies.
20572059
LinkSkip="$(AndroidLinkSkip)"
20582060
LinkDescriptions="@(LinkDescription)"
20592061
ProguardConfiguration="$(_ProguardProjectConfiguration)"
2062+
PreserveJniMarshalMethods="$(AndroidGenerateJniMarshalMethods)"
20602063
EnableProguard="$(AndroidEnableProguard)"
20612064
DumpDependencies="$(LinkerDumpDependencies)"
20622065
ResolvedAssemblies="@(ResolvedAssemblies->'$(MonoAndroidLinkerInputDir)%(Filename)%(Extension)')"

0 commit comments

Comments
 (0)