Skip to content

Commit 74573d9

Browse files
committed
[Java.Interop.Tools.JavaCallableWrappers] Support MultiDexApplication
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=40976 Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=41342 There is a desire to *trivially* enable use of MultiDexApplication by setting the $(AndroidEnableMultiDex) MSBuild property to True. When False (the default), the normal Java Callable Wrapper (JCW) base class naming logic would be used (e.g. use the `[Register]`ed type name of the base class in the `extends` clause). This normal logic hinders "trivial" use of MultiDexApplication, as it would require that any custom `Android.App.Application` type be changed to instead inherit `Android.Support.Multidex.MultiDexApplication`, and default `MultiDexApplication` use is required to support other functionality such as enhanced Fast Deployment. The original fix for Bug #40976 altered JCW generation so that a new `$(AndroidApplicationJavaClass)` MSBuild property -- controlled by `$(AndroidEnableMultiDex)` -- would explicitly provide the base class for all `Application` subclasses. Unfortunately, the fix applied to *all* `Application` subclasses, including "indirect" subclasses. This C# hierarchy: partial class BaseApp : Android.App.Application { } partial class App : BaseApp { } would result in the Java Callable Wrappers for *both* `BaseApp` and `App` inheriting from `android.app.Application`, which would cause everything to break if `BaseApp` e.g. implemented a new interface. This was bug #41342: indirect subclasses had the wrong base class. For Java.Interop purposes, merge both fixes together so that `JavaCallableWrapperGenerator.AndroidJavaClass` can be used to override the default `android.app.Application` base class, but *only* when the base class is `android.app.Application`. This allows "fixing" the JCW for `BaseApp` to inherit from `MultiDexApplication` if desired while `App` continues to inherit from `BaseApp`.
1 parent 93af129 commit 74573d9

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

src/Java.Interop.Tools.JavaCallableWrappers/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGenerator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public JavaCallableWrapperGenerator (TypeDefinition type, Action<string, object[
6565
}
6666
}
6767

68+
public string ApplicationJavaClass { get; set; }
69+
6870
public bool UseSharedRuntime;
6971

7072
public bool GenerateOnCreateOverrides { get; set; }
@@ -530,6 +532,8 @@ void GenerateHeader (TextWriter sw)
530532
sw.WriteLine ("public " + (type.IsAbstract ? "abstract " : "") + "class " + name);
531533

532534
string extendsType = GetJavaTypeName (type.BaseType);
535+
if (extendsType == "android.app.Application" && !string.IsNullOrEmpty (ApplicationJavaClass))
536+
extendsType = ApplicationJavaClass;
533537
sw.WriteLine ("\textends " + extendsType);
534538
sw.WriteLine ("\timplements");
535539
sw.Write ("\t\tmono.android.IGCUserPeer");

src/Java.Interop.Tools.JavaCallableWrappers/Test/Java.Interop.Tools.JavaCallableWrappers/JavaCallableWrapperGeneratorTests.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,51 +94,55 @@ public void GenerateInParallel ()
9494
}
9595

9696
[Test]
97-
public void GenerateApplication ()
97+
public void GenerateApplication (
98+
[Values (null, "android.app.Application", "android.support.multidex.MultiDexApplication")] string applicationJavaClass
99+
)
98100
{
99-
var actual = Generate (typeof (ApplicationName));
100-
var expected = @"package application;
101+
var actual = Generate (typeof (ApplicationName), applicationJavaClass);
102+
var expected = $@"package application;
101103
102104
103105
public class Name
104-
extends android.app.Application
106+
extends {applicationJavaClass ?? "android.app.Application"}
105107
implements
106108
mono.android.IGCUserPeer
107-
{
109+
{{
108110
/** @hide */
109111
public static final String __md_methods;
110-
static {
112+
static {{
111113
__md_methods =
112114
"""";
113-
}
115+
}}
114116
115117
public Name ()
116-
{
118+
{{
117119
mono.MonoPackageManager.setContext (this);
118-
}
120+
}}
119121
120122
private java.util.ArrayList refList;
121123
public void monodroidAddReference (java.lang.Object obj)
122-
{
124+
{{
123125
if (refList == null)
124126
refList = new java.util.ArrayList ();
125127
refList.add (obj);
126-
}
128+
}}
127129
128130
public void monodroidClearReferences ()
129-
{
131+
{{
130132
if (refList != null)
131133
refList.clear ();
132-
}
133-
}
134+
}}
135+
}}
134136
";
135137
Assert.AreEqual (expected, actual);
136138
}
137139

138-
static string Generate (Type type)
140+
static string Generate (Type type, string applicationJavaClass = null)
139141
{
140142
var td = SupportDeclarations.GetTypeDefinition (type);
141-
var g = new JavaCallableWrapperGenerator (td, null);
143+
var g = new JavaCallableWrapperGenerator (td, null) {
144+
ApplicationJavaClass = applicationJavaClass,
145+
};
142146
var o = new StringWriter ();
143147
g.Generate ("__o");
144148
g.Generate (o);
@@ -147,9 +151,11 @@ static string Generate (Type type)
147151

148152

149153
[Test]
150-
public void GenerateIndirectApplication ()
154+
public void GenerateIndirectApplication (
155+
[Values (null, "android.app.Application", "android.support.multidex.MultiDexApplication")] string applicationJavaClass
156+
)
151157
{
152-
var actual = Generate (typeof (IndirectApplication));
158+
var actual = Generate (typeof (IndirectApplication), applicationJavaClass);
153159
var expected = @"package md5fef72cac46d04ae5bdc90af5bb6221ad;
154160
155161

0 commit comments

Comments
 (0)