From 5992bc4f0a938aa888ede60f6740e1c1d5cb5032 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Mon, 18 Dec 2017 15:39:50 -0600 Subject: [PATCH] [Xamarin.Android.Build.Tasks] generates a shorter acw-map.txt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=61073 Context: https://github.com/xamarin/java.interop/pull/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 --- external/Java.Interop | 2 +- src/Mono.Android/Android.Runtime/JNIEnv.cs | 2 +- .../Test/Java.Interop/JnienvTest.cs | 13 ++++++--- .../Tasks/GenerateJavaStubs.cs | 1 - .../Xamarin.Android.Build.Tests/BuildTest.cs | 28 +++++++++++++++++++ 5 files changed, 39 insertions(+), 7 deletions(-) diff --git a/external/Java.Interop b/external/Java.Interop index 57d5d514c41..429dc2a6857 160000 --- a/external/Java.Interop +++ b/external/Java.Interop @@ -1 +1 @@ -Subproject commit 57d5d514c41cba1af3e1dc71ea157cff0c5a43b6 +Subproject commit 429dc2a68579c9a820f03a7f09f35da3db9cd54a diff --git a/src/Mono.Android/Android.Runtime/JNIEnv.cs b/src/Mono.Android/Android.Runtime/JNIEnv.cs index 81b5ef420fa..b51c5626c30 100644 --- a/src/Mono.Android/Android.Runtime/JNIEnv.cs +++ b/src/Mono.Android/Android.Runtime/JNIEnv.cs @@ -880,7 +880,7 @@ public static string GetJniName (Type type) { if (type == null) throw new ArgumentNullException ("type"); - var java = monodroid_typemap_managed_to_java (type.AssemblyQualifiedName); + var java = monodroid_typemap_managed_to_java (type.FullName + ", " + type.Assembly.GetName ().Name); return java == IntPtr.Zero ? JavaNativeTypeManager.ToJniName (type) : Marshal.PtrToStringAnsi (java); diff --git a/src/Mono.Android/Test/Java.Interop/JnienvTest.cs b/src/Mono.Android/Test/Java.Interop/JnienvTest.cs index ffef95baf03..e5e9f236590 100644 --- a/src/Mono.Android/Test/Java.Interop/JnienvTest.cs +++ b/src/Mono.Android/Test/Java.Interop/JnienvTest.cs @@ -388,13 +388,18 @@ public void JavaToManagedTypeMapping () [DllImport ("__Internal")] static extern IntPtr monodroid_typemap_managed_to_java (string java); + string GetTypeName (Type type) + { + return type.FullName + ", " + type.Assembly.GetName ().Name; + } + [Test] public void ManagedToJavaTypeMapping () { - var m = monodroid_typemap_managed_to_java (typeof (Activity).AssemblyQualifiedName); - Assert.AreNotEqual (IntPtr.Zero, m); - m = monodroid_typemap_managed_to_java (typeof (JnienvTest).AssemblyQualifiedName); - Assert.AreEqual (IntPtr.Zero, m); + var m = monodroid_typemap_managed_to_java (GetTypeName (typeof (Activity))); + Assert.AreNotEqual (IntPtr.Zero, m, "`Activity` subclasses Java.Lang.Object, it should be in the typemap!"); + m = monodroid_typemap_managed_to_java (GetTypeName (typeof (JnienvTest))); + Assert.AreEqual (IntPtr.Zero, m, "`JnienvTest` does *not* subclass Java.Lang.Object, it should *not* be in the typemap!"); } [Test] diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs index 48dfd69ad6d..2c681cf2c78 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs @@ -163,7 +163,6 @@ void Run (DirectoryAssemblyResolver res) string javaKey = JavaNativeTypeManager.ToJniName (type).Replace ('/', '.'); acw_map.WriteLine ("{0};{1}", type.GetPartialAssemblyQualifiedName (), javaKey); - acw_map.WriteLine ("{0};{1}", type.GetAssemblyQualifiedName (), javaKey); TypeDefinition conflict; if (managed.TryGetValue (managedKey, out conflict)) { diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs index b5fc5a1beb1..23fe16a168b 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Reflection; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -174,6 +175,33 @@ public void BuildApplicationWithLibraryAndClean ([Values (false, true)] bool isR } } + [Test] + public void BuildIncrementingAssemblyVersion () + { + var proj = new XamarinAndroidApplicationProject (); + proj.Sources.Add (new BuildItem ("Compile", "AssemblyInfo.cs") { + TextContent = () => "[assembly: System.Reflection.AssemblyVersion (\"1.0.0.*\")]" + }); + + using (var b = CreateApkBuilder ("temp/BuildIncrementingAssemblyVersion")) { + Assert.IsTrue (b.Build (proj), "Build should have succeeded."); + + var acwmapPath = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, "acw-map.txt"); + var assemblyPath = Path.Combine (Root, b.ProjectDirectory, proj.OutputPath, "UnnamedProject.dll"); + var firstAssemblyVersion = AssemblyName.GetAssemblyName (assemblyPath).Version; + var expectedAcwMap = File.ReadAllText (acwmapPath); + + b.Target = "Rebuild"; + b.BuildLogFile = "rebuild.log"; + Assert.IsTrue (b.Build (proj), "Rebuild should have succeeded."); + + var secondAssemblyVersion = AssemblyName.GetAssemblyName (assemblyPath).Version; + Assert.AreNotEqual (firstAssemblyVersion, secondAssemblyVersion); + var actualAcwMap = File.ReadAllText (acwmapPath); + Assert.AreEqual (expectedAcwMap, actualAcwMap); + } + } + [Test] public void BuildMkBundleApplicationRelease () {