Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] <GenerateJavaStubs /> generates a short…
Browse files Browse the repository at this point in the history
…er 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
  • Loading branch information
jonathanpeppers committed Dec 21, 2017
1 parent 59cfa5d commit 5992bc4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
2 changes: 1 addition & 1 deletion external/Java.Interop
2 changes: 1 addition & 1 deletion src/Mono.Android/Android.Runtime/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions src/Mono.Android/Test/Java.Interop/JnienvTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 0 additions & 1 deletion src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ()
{
Expand Down

0 comments on commit 5992bc4

Please sign in to comment.