Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Add AndroidGenerateJniMarshalMethods (#…
Browse files Browse the repository at this point in the history
…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*.
  • Loading branch information
radekdoulik authored and jonpryor committed May 30, 2018
1 parent 10e1ec1 commit 106a621
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 4 deletions.
20 changes: 19 additions & 1 deletion Documentation/guides/BuildProcess.md
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,24 @@ when packaing Release applications.
See [Lint Help](http://www.androiddocs.com/tools/help/lint.html) for more details on
the android `lint` tooling.

- **AndroidGenerateJniMarshalMethods** – A bool property which
enables generating of JNI marshal methods as part of the build
process. This greatly reduces the System.Reflection usage in the
binding helper code.

By default this will be set to False. If the developers wish to use
the new JNI marshal methods feature, they can set

<AndroidGenerateJniMarshalMethods>True</AndroidGenerateJniMarshalMethods>

in their csproj. Alternatively provide the property on the command
line via

/p:AndroidGenerateJniMarshalMethods=True

**Experimental**. Added in Xamarin.Android 8.4.
The default value is False.

### Binding Project Build Properties

The following MSBuild properties are used with
Expand Down Expand Up @@ -761,7 +779,7 @@ resources.
- **AndroidUseAapt2** &ndash; A bool property which allows the developer to
control the use of the `aapt2` tool for packaging.
By default this will be set to false and we will use `aapt`.
If the developer wishes too use the new `aapt2` functionality
If the developer wishes to use the new `aapt2` functionality
they can set
<AndroidUseAapt2>True</AndroidUseAapt2>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Mono.Linker;
using Mono.Cecil;

namespace MonoDroid.Tuner
{
public class AndroidLinkContext : LinkContext
{
public AndroidLinkContext (Pipeline pipeline, AssemblyResolver resolver)
: base (pipeline, resolver) {}

public AndroidLinkContext (Pipeline pipeline, AssemblyResolver resolver, ReaderParameters readerParameters, UnintializedContextFactory factory)
: base (pipeline, resolver, readerParameters, factory) {}

public bool PreserveJniMarshalMethods { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void Run (Pipeline pipeline, LinkContext context)

static LinkContext CreateLinkContext (LinkerOptions options, Pipeline pipeline, ILogger logger)
{
var context = new LinkContext (pipeline, options.Resolver);
var context = new AndroidLinkContext (pipeline, options.Resolver);
if (options.DumpDependencies) {
var prepareDependenciesDump = context.Annotations.GetType ().GetMethod ("PrepareDependenciesDump", new Type[] {});
if (prepareDependenciesDump != null)
Expand All @@ -51,6 +51,7 @@ static LinkContext CreateLinkContext (LinkerOptions options, Pipeline pipeline,
context.SymbolReaderProvider = new DefaultSymbolReaderProvider (true);
context.SymbolWriterProvider = new DefaultSymbolWriterProvider ();
context.OutputDirectory = options.OutputDirectory;
context.PreserveJniMarshalMethods = options.PreserveJniMarshalMethods;
return context;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ class LinkerOptions
public bool DumpDependencies { get; set; }
public string HttpClientHandlerType { get; set; }
public string TlsProvider { get; set; }
public bool PreserveJniMarshalMethods { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void Process (LinkContext context)
marshalTypes.Clear ();
base.Process (context);

if (UpdateMarshalTypes ())
if (PreserveJniMarshalMethods () && UpdateMarshalTypes ())
base.Process (context);
}

Expand Down Expand Up @@ -196,6 +196,14 @@ bool UpdateMarshalRegisterMethod (MethodDefinition method, HashSet<string> marke
return true;
}

bool PreserveJniMarshalMethods ()
{
if (_context is AndroidLinkContext ac)
return ac.PreserveJniMarshalMethods;

return false;
}

// If this is one of our infrastructure methods that has [Register], like:
// [Register ("hasWindowFocus", "()Z", "GetHasWindowFocusHandler")],
// we need to preserve the "GetHasWindowFocusHandler" method as well.
Expand All @@ -207,7 +215,7 @@ protected override void DoAdditionalMethodProcessing (MethodDefinition method)
return;

MethodDefinition marshalMethod;
if (method.TryGetMarshalMethod (nativeMethod, signature, out marshalMethod)) {
if (PreserveJniMarshalMethods () && method.TryGetMarshalMethod (nativeMethod, signature, out marshalMethod)) {
MarkMethod (marshalMethod);
marshalTypes.Add (marshalMethod.DeclaringType);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Xamarin.Android.Build.Tasks/Tasks/LinkAssemblies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class LinkAssemblies : Task, ML.ILogger

public string TlsProvider { get; set; }

public bool PreserveJniMarshalMethods { get; set; }

IEnumerable<AssemblyDefinition> GetRetainAssemblies (DirectoryAssemblyResolver res)
{
List<AssemblyDefinition> retainList = null;
Expand Down Expand Up @@ -83,6 +85,7 @@ public override bool Execute ()
Log.LogDebugMessage (" LinkOnlyNewerThan: {0}", LinkOnlyNewerThan);
Log.LogDebugMessage (" HttpClientHandlerType: {0}", HttpClientHandlerType);
Log.LogDebugMessage (" TlsProvider: {0}", TlsProvider);
Log.LogDebugMessage (" PreserveJniMarshalMethods: {0}", PreserveJniMarshalMethods);

var rp = new ReaderParameters {
InMemory = true,
Expand Down Expand Up @@ -115,6 +118,7 @@ bool Execute (DirectoryAssemblyResolver res)
options.DumpDependencies = DumpDependencies;
options.HttpClientHandlerType = HttpClientHandlerType;
options.TlsProvider = TlsProvider;
options.PreserveJniMarshalMethods = PreserveJniMarshalMethods;

var skiplist = new List<string> ();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Compile Include="Tasks\JavaDoc.cs" />
<Compile Include="Linker\MonoDroid.Tuner\AndroidLinkContext.cs" />
</ItemGroup>
<ItemGroup>
<_MonoAndroidEnum Include="$(AndroidGeneratedClassDirectory)\Android.Content.PM.LaunchMode.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
<_InstantRunEnabled Condition=" '$(_InstantRunEnabled)' == '' ">False</_InstantRunEnabled>
<_AndroidBuildPropertiesCache>$(IntermediateOutputPath)build.props</_AndroidBuildPropertiesCache>

<AndroidGenerateJniMarshalMethods Condition=" '$(AndroidGenerateJniMarshalMethods)' == '' ">False</AndroidGenerateJniMarshalMethods>

</PropertyGroup>

<Choose>
Expand Down Expand Up @@ -2057,6 +2059,7 @@ because xbuild doesn't support framework reference assemblies.
LinkSkip="$(AndroidLinkSkip)"
LinkDescriptions="@(LinkDescription)"
ProguardConfiguration="$(_ProguardProjectConfiguration)"
PreserveJniMarshalMethods="$(AndroidGenerateJniMarshalMethods)"
EnableProguard="$(AndroidEnableProguard)"
DumpDependencies="$(LinkerDumpDependencies)"
ResolvedAssemblies="@(ResolvedAssemblies->'$(MonoAndroidLinkerInputDir)%(Filename)%(Extension)')"
Expand Down

0 comments on commit 106a621

Please # to comment.