From 318dd7eaa0445f1522f81ba51cc2988b547345e2 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 29 Aug 2017 15:35:57 +0100 Subject: [PATCH 1/3] [Xamarin.Android.Build.Tasks] No .dll's in Release if Optimize != True (#749) Context: https://bugzilla.xamarin.com/show_bug.cgi?id=58580 Unable to locate assemblies when running projects when `$(Configuration)`=Release and `$(Optimize)` != True. Commit edabe8ac reworked the build logic so that `$(Optimize)` controls the deployment of the debug or release runtimes. Input Property: || Output Property DebugSymbols | DebugType | EmbedAssembliesIntoApk | Optimize || AndroidIncludeDebugSymbols ================+============+==========================+============++=========================== True *any* True True False (Release runtime) True *any* True False True (Debug runtime) True *any* False True True (Debug runtime) True *any* False False True (Debug runtime) True *empty* True True True (Debug runtime) True *empty* True False True (Debug runtime) True *empty* False True True (Debug runtime) True *empty* False False True (Debug runtime) False - - - False (Release runtime) *empty* *any* *empty* False True (Debug runtime) In this scenario, `$(DebugSymols)` and `$(EmbedAssembliesIntoApk)` are both empty. As a result `$(EmbedAssembliesIntoApk)` gets a default value of 'False'. But because `$(DebugSymols)` is empty we fall through the MSBuild case statement and end up with `$(AndroidIncludeDebugSymbols)` = `True`. Which is NOT what we want in this case. So if `$(EmbedAssembliesIntoApk)` we should always default `$(AndroidIncludeDebugSymbols)` to false. In addition there was a weird issue with the way we install debug apps. If the app is using the Shared Runtime the deployment process sends a broadcast to the Shared Runtime app which returns the external storage directory. This tends to be something like /mnt/shell/emulated/0/ So when we deploy our debug assemblies we do so to a directory like /mnt/shell/emulated/0/Android/data/$(PackageName)/files/.__override__ This works as expected. Now if we try to debug an app without using the Shared Runtime it will work as expected.. unless the Shared Runtime was not installed. In this case the broadcast fails and we fall back to getting the external storage using adb shell echo -b ${EXTERNAL_STORAGE} This returns /mnt/shell/emulated/legacy so we end up deploying the debug assemblies to /mnt/shell/emulated/legacy/Android/data/$(PackageName)/files/.__override__ So all is well and good. However problems start in our runtime and the MonoPackageManager. We use android.os.Environment.getExternalStorageDirectory (); to get the external storage directory. In this case it ALWAYS returns /mnt/shell/emulated/0/ So if we are debugging without the Shared Runtime being installed, we will NEVER find the fast deployed assebmies. So the app will crash. The fix in this case is to check both directories when we are running the debug runtime. --- src/Mono.Android/java/mono/android/Runtime.java | 2 +- .../Resources/MonoPackageManager.api4.java | 14 +++++++++++--- .../Resources/MonoPackageManager.java | 14 +++++++++++--- .../Xamarin.Android.Common.targets | 5 +++++ src/monodroid/jni/mono_android_Runtime.h | 4 ++-- src/monodroid/jni/monodroid-glue.c | 16 ++++++++++++---- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/Mono.Android/java/mono/android/Runtime.java b/src/Mono.Android/java/mono/android/Runtime.java index ed0cbbf7103..16fcd441cf1 100644 --- a/src/Mono.Android/java/mono/android/Runtime.java +++ b/src/Mono.Android/java/mono/android/Runtime.java @@ -6,7 +6,7 @@ private Runtime () { } - public static native void init (String lang, String[] runtimeApks, String runtimeDataDir, String[] appDirs, ClassLoader loader, String externalStorageDir, String[] assemblies, String packageName); + public static native void init (String lang, String[] runtimeApks, String runtimeDataDir, String[] appDirs, ClassLoader loader, String[] externalStorageDirs, String[] assemblies, String packageName); public static native void register (String managedType, java.lang.Class nativeClass, String methods); public static native void notifyTimeZoneChanged (); public static native int createNewContext (String[] runtimeApks, String[] assemblies, ClassLoader loader); diff --git a/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java index 9ec169cb914..ab92c6aa962 100644 --- a/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java +++ b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java @@ -38,6 +38,13 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack String cacheDir = context.getCacheDir ().getAbsolutePath (); String dataDir = context.getApplicationInfo ().dataDir + "/lib"; ClassLoader loader = context.getClassLoader (); + java.io.File external0 = android.os.Environment.getExternalStorageDirectory (); + String externalDir = new java.io.File ( + external0, + "Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (); + String externalLegacyDir = new java.io.File ( + external0, + "../legacy/Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (); Runtime.init ( language, @@ -49,9 +56,10 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack dataDir, }, loader, - new java.io.File ( - android.os.Environment.getExternalStorageDirectory (), - "Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (), + new String[] { + externalDir, + externalLegacyDir + }, MonoPackageManager_Resources.Assemblies, context.getPackageName ()); diff --git a/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java index dd4052bd739..d43792d46de 100644 --- a/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java +++ b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java @@ -38,6 +38,13 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack String cacheDir = context.getCacheDir ().getAbsolutePath (); String dataDir = getNativeLibraryPath (context); ClassLoader loader = context.getClassLoader (); + java.io.File external0 = android.os.Environment.getExternalStorageDirectory (); + String externalDir = new java.io.File ( + external0, + "Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (); + String externalLegacyDir = new java.io.File ( + external0, + "../legacy/Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (); Runtime.init ( language, @@ -49,9 +56,10 @@ public static void LoadApplication (Context context, ApplicationInfo runtimePack dataDir, }, loader, - new java.io.File ( - android.os.Environment.getExternalStorageDirectory (), - "Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (), + new String[] { + externalDir, + externalLegacyDir + }, MonoPackageManager_Resources.Assemblies, context.getPackageName ()); diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 36eabbf3d07..880d8867261 100755 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -279,6 +279,11 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved. True + + + True + + False diff --git a/src/monodroid/jni/mono_android_Runtime.h b/src/monodroid/jni/mono_android_Runtime.h index 3aaaa966d01..8bd4ae1bf3c 100644 --- a/src/monodroid/jni/mono_android_Runtime.h +++ b/src/monodroid/jni/mono_android_Runtime.h @@ -10,10 +10,10 @@ extern "C" { /* * Class: mono_android_Runtime * Method: init - * Signature: (Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/ClassLoader;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V + * Signature: (Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/ClassLoader;[Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V */ JNIEXPORT void JNICALL Java_mono_android_Runtime_init - (JNIEnv *, jclass, jstring, jobjectArray, jstring, jobjectArray, jobject, jstring, jobjectArray, jstring); + (JNIEnv *, jclass, jstring, jobjectArray, jstring, jobjectArray, jobject, jobjectArray, jobjectArray, jstring); /* * Class: mono_android_Runtime diff --git a/src/monodroid/jni/monodroid-glue.c b/src/monodroid/jni/monodroid-glue.c index 57183905b96..f610239f2bc 100644 --- a/src/monodroid/jni/monodroid-glue.c +++ b/src/monodroid/jni/monodroid-glue.c @@ -329,7 +329,7 @@ monodroid_get_system_property (const char *name, char **value) #ifdef RELEASE #define MAX_OVERRIDES 1 #else -#define MAX_OVERRIDES 2 +#define MAX_OVERRIDES 3 #endif static char* override_dirs [MAX_OVERRIDES]; @@ -410,6 +410,7 @@ get_primary_override_dir (JNIEnv *env, jstring home) static char *primary_override_dir; static char *external_override_dir; +static char *external_legacy_override_dir; static void create_update_dir (char *override_dir) @@ -651,6 +652,7 @@ get_libmonosgen_path () // external storage locations so we need to file copy the shared object to an internal // storage location before loading it. copy_monosgen_to_internal_location (primary_override_dir, external_override_dir); + copy_monosgen_to_internal_location (primary_override_dir, external_legacy_override_dir); int i; for (i = 0; i < MAX_OVERRIDES; ++i) @@ -2512,6 +2514,7 @@ get_ds2_path () for (i = 0; i < sizeof (gdbservers)/sizeof (gdbservers [0]); ++i) { file = monodroid_strdup_printf ("%s", gdbservers [i]); copy_file_to_internal_location (primary_override_dir, external_override_dir, file); + copy_file_to_internal_location (primary_override_dir, external_legacy_override_dir, file); free (file); } @@ -3893,7 +3896,7 @@ create_and_initialize_domain (JNIEnv* env, jobjectArray runtimeApks, jobjectArra } JNIEXPORT void JNICALL -Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApks, jstring runtimeNativeLibDir, jobjectArray appDirs, jobject loader, jstring externalStorageDir, jobjectArray assemblies, jstring packageName) +Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobjectArray runtimeApks, jstring runtimeNativeLibDir, jobjectArray appDirs, jobject loader, jobjectArray externalStorageDirs, jobjectArray assemblies, jstring packageName) { char *runtime_args = NULL; char *connect_args; @@ -3923,9 +3926,13 @@ Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobject setup_environment (env, runtimeApks); primary_override_dir = get_primary_override_dir (env, (*env)->GetObjectArrayElement (env, appDirs, 0)); - esd = (*env)->GetStringUTFChars (env, externalStorageDir, NULL); + esd = (*env)->GetStringUTFChars (env, (*env)->GetObjectArrayElement (env, externalStorageDirs, 0), NULL); external_override_dir = monodroid_strdup_printf ("%s", esd); - (*env)->ReleaseStringUTFChars (env, externalStorageDir, esd); + (*env)->ReleaseStringUTFChars (env, (*env)->GetObjectArrayElement (env, externalStorageDirs, 0), esd); + + esd = (*env)->GetStringUTFChars (env, (*env)->GetObjectArrayElement (env, externalStorageDirs, 1), NULL); + external_legacy_override_dir = monodroid_strdup_printf ("%s", esd); + (*env)->ReleaseStringUTFChars (env, (*env)->GetObjectArrayElement (env, externalStorageDirs, 1), esd); init_categories (primary_override_dir); create_update_dir (primary_override_dir); @@ -3937,6 +3944,7 @@ Java_mono_android_Runtime_init (JNIEnv *env, jclass klass, jstring lang, jobject #ifndef RELEASE override_dirs [1] = external_override_dir; + override_dirs [2] = external_legacy_override_dir; for (i = 0; i < MAX_OVERRIDES; ++i) { const char *p = override_dirs [i]; if (!directory_exists (p)) From d079a42e28d30797cd9a0b1c34a2fcb3e340c384 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Thu, 24 Aug 2017 03:21:21 +0100 Subject: [PATCH 2/3] [Xamarin.Android.Build.Tasks] Library project C# changes & Rebuilds (#764) Fixes: https://bugzilla.xamarin.com/show_bug.cgi?id=58865 Consider a project with an `App.csproj` and a referenced `Lib.csproj`. Not all changes to C# sources within `Lib.csproj` should require rebuilding and redeploying the `.apk` when Fast Deployment is enabled; only those that result in changes to Java Callable Wrappers should require rebuilding and redeploying the `.apk`. Unfortunately, that wasn't the case: *any* change to `Lib.csproj` would result rebuilding and redeploying the `.apk`, because `AndroidManifest.xml` was constantly being modified, which would cause the `_CompileToDalvikWithDx` target to be invoked, resulting in a `.apk` rebuild + redeploy. The item was constantly being duplicated in the manifest. As a result it was always newer and was triggering a change of build targets. So we need to double check we are NOT adding 100% duplicate values to the manifest. --- .../Tests/Xamarin.Android.Build.Tests/ManifestTest.cs | 4 ++++ .../Utilities/ManifestDocument.cs | 11 +++++++++++ .../Utilities/XDocumentExtensions.cs | 5 +++++ 3 files changed, 20 insertions(+) diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ManifestTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ManifestTest.cs index 882b4d3f1c9..30fcc1ca399 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ManifestTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ManifestTest.cs @@ -486,6 +486,8 @@ public void MergeLibraryManifest () android:authorities='${applicationId}.FacebookInitProvider' android:name='.internal.FacebookInitProvider' android:exported='false' /> + + ", encoding: System.Text.Encoding.UTF8); @@ -522,6 +524,8 @@ public void MergeLibraryManifest () "${applicationId}.FacebookInitProvider was not replaced with com.xamarin.manifest.FacebookInitProvider"); Assert.IsTrue (manifest.Contains ("com.xamarin.test.internal.FacebookInitProvider"), ".internal.FacebookInitProvider was not replaced with com.xamarin.test.internal.FacebookInitProvider"); + Assert.AreEqual (manifest.IndexOf ("meta-data", StringComparison.OrdinalIgnoreCase), + manifest.LastIndexOf ("meta-data", StringComparison.OrdinalIgnoreCase), "There should be only one meta-data element"); } } } diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs index b009cb91338..c16b4ca4ffa 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs @@ -401,6 +401,16 @@ void MergeLibraryManifest (string mergedManifest) } } + void RemoveDuplicateElements () + { + var duplicates = doc.Descendants () + .GroupBy (x => x.ToFullString ()) + .SelectMany (x => x.Skip (1)); + foreach (var duplicate in duplicates) + duplicate.Remove (); + + } + IEnumerable FixupNameElements(string packageName, IEnumerable nodes) { foreach (var element in nodes.Select ( x => x as XElement).Where (x => x != null && ManifestAttributeFixups.ContainsKey (x.Name.LocalName))) { @@ -838,6 +848,7 @@ public void Save (string filename) public void Save (System.IO.TextWriter stream) { + RemoveDuplicateElements (); var ms = new MemoryStream (); doc.Save (ms); ms.Flush (); diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs b/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs index d05efb5ab68..5bac4ba45a3 100644 --- a/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs +++ b/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs @@ -22,6 +22,11 @@ public static string[] GetPaths (this XDocument doc, params string[] paths) e = e.Elements (p); return e.Select (p => p.Value).ToArray (); } + + public static string ToFullString (this XElement element) + { + return element.ToString (SaveOptions.DisableFormatting); + } } } From b88b026fbbcd1e38fc13f87be3815c37ba9fae79 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Tue, 29 Aug 2017 16:05:48 +0100 Subject: [PATCH 3/3] [Xamarin.Android.Build.Tasks] DesignTimeBuild failure on Rebuilds (#757) Context: https://bugzilla.xamarin.com/show_bug.cgi?id=58682 Seeing a new seemingly DesignTimeBuild-related build failure when invoking the `Rebuild` msbuild target. The `$(DesignTimeBuild)` value was empty when running under mac. This cuases the error error MSB4044: The "GetAdditionalResourcesFromAssemblies" task was not given a value for the required parameter "DesignTimeBuild". So we need to make sure that the value is set before we get to `GetAdditionalResourcesFromAssemblies`. The reason we got the error is before we used `$(DesignTimeBuild)` only in conditionals. However when moving to use it as a property of a task it cannot be empty. So we need to make sure we call `_SetupDesignTimeBuildForBuild` early in the build process. --- src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets index 880d8867261..6a891b37b2d 100755 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -1030,7 +1030,7 @@ because xbuild doesn't support framework reference assemblies. - +