Skip to content

Commit 5992bc4

Browse files
[Xamarin.Android.Build.Tasks] <GenerateJavaStubs /> generates a shorter acw-map.txt
Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=61073 Context: dotnet/java-interop#227 The Java-to-Managed typemaps list types such as: ``` android/app/Activity Android.App.Activity, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065 ``` This is found in the intermediate dir after a build in `acw-map.txt`, or `$(_AcwMapFile)`. Let’s assume you have an Android project with the following assembly-level attribute: ``` [assembly:AssemblyVersion("1.0.0.*")] ``` Then on *every* build, the typemap is invalidated because your version number has been incremented. Changes: - Bumped Java.Interop to master/429dc2a - `JNIEnv` needs to use the shorter type name when calling `monodroid_typemap_managed_to_java` - `GenerateJavaStubs` should not be writing lines for `type.GetAssemblyQualifiedName` into `acw-map.txt` - `JnienvTest` needed some updates to use the new type name format - Wrote a test using `[assembly:AssemblyVersion("1.0.0.*")]` that checks `acw-map.txt` contents
1 parent 59cfa5d commit 5992bc4

File tree

5 files changed

+39
-7
lines changed

5 files changed

+39
-7
lines changed

external/Java.Interop

src/Mono.Android/Android.Runtime/JNIEnv.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ public static string GetJniName (Type type)
880880
{
881881
if (type == null)
882882
throw new ArgumentNullException ("type");
883-
var java = monodroid_typemap_managed_to_java (type.AssemblyQualifiedName);
883+
var java = monodroid_typemap_managed_to_java (type.FullName + ", " + type.Assembly.GetName ().Name);
884884
return java == IntPtr.Zero
885885
? JavaNativeTypeManager.ToJniName (type)
886886
: Marshal.PtrToStringAnsi (java);

src/Mono.Android/Test/Java.Interop/JnienvTest.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,18 @@ public void JavaToManagedTypeMapping ()
388388
[DllImport ("__Internal")]
389389
static extern IntPtr monodroid_typemap_managed_to_java (string java);
390390

391+
string GetTypeName (Type type)
392+
{
393+
return type.FullName + ", " + type.Assembly.GetName ().Name;
394+
}
395+
391396
[Test]
392397
public void ManagedToJavaTypeMapping ()
393398
{
394-
var m = monodroid_typemap_managed_to_java (typeof (Activity).AssemblyQualifiedName);
395-
Assert.AreNotEqual (IntPtr.Zero, m);
396-
m = monodroid_typemap_managed_to_java (typeof (JnienvTest).AssemblyQualifiedName);
397-
Assert.AreEqual (IntPtr.Zero, m);
399+
var m = monodroid_typemap_managed_to_java (GetTypeName (typeof (Activity)));
400+
Assert.AreNotEqual (IntPtr.Zero, m, "`Activity` subclasses Java.Lang.Object, it should be in the typemap!");
401+
m = monodroid_typemap_managed_to_java (GetTypeName (typeof (JnienvTest)));
402+
Assert.AreEqual (IntPtr.Zero, m, "`JnienvTest` does *not* subclass Java.Lang.Object, it should *not* be in the typemap!");
398403
}
399404

400405
[Test]

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,6 @@ void Run (DirectoryAssemblyResolver res)
163163
string javaKey = JavaNativeTypeManager.ToJniName (type).Replace ('/', '.');
164164

165165
acw_map.WriteLine ("{0};{1}", type.GetPartialAssemblyQualifiedName (), javaKey);
166-
acw_map.WriteLine ("{0};{1}", type.GetAssemblyQualifiedName (), javaKey);
167166

168167
TypeDefinition conflict;
169168
if (managed.TryGetValue (managedKey, out conflict)) {

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Diagnostics;
33
using System.IO;
44
using System.Linq;
5+
using System.Reflection;
56
using System.Text;
67
using System.Text.RegularExpressions;
78
using System.Threading.Tasks;
@@ -174,6 +175,33 @@ public void BuildApplicationWithLibraryAndClean ([Values (false, true)] bool isR
174175
}
175176
}
176177

178+
[Test]
179+
public void BuildIncrementingAssemblyVersion ()
180+
{
181+
var proj = new XamarinAndroidApplicationProject ();
182+
proj.Sources.Add (new BuildItem ("Compile", "AssemblyInfo.cs") {
183+
TextContent = () => "[assembly: System.Reflection.AssemblyVersion (\"1.0.0.*\")]"
184+
});
185+
186+
using (var b = CreateApkBuilder ("temp/BuildIncrementingAssemblyVersion")) {
187+
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
188+
189+
var acwmapPath = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "acw-map.txt");
190+
var assemblyPath = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, "UnnamedProject.dll");
191+
var firstAssemblyVersion = AssemblyName.GetAssemblyName (assemblyPath).Version;
192+
var expectedAcwMap = File.ReadAllText (acwmapPath);
193+
194+
b.Target = "Rebuild";
195+
b.BuildLogFile = "rebuild.log";
196+
Assert.IsTrue (b.Build (proj), "Rebuild should have succeeded.");
197+
198+
var secondAssemblyVersion = AssemblyName.GetAssemblyName (assemblyPath).Version;
199+
Assert.AreNotEqual (firstAssemblyVersion, secondAssemblyVersion);
200+
var actualAcwMap = File.ReadAllText (acwmapPath);
201+
Assert.AreEqual (expectedAcwMap, actualAcwMap);
202+
}
203+
}
204+
177205
[Test]
178206
public void BuildMkBundleApplicationRelease ()
179207
{

0 commit comments

Comments
 (0)