From 59ec488b4005a09fc0e5f330f217e78c3fa14724 Mon Sep 17 00:00:00 2001 From: Dean Ellis Date: Mon, 25 Apr 2016 16:44:32 +0100 Subject: [PATCH] [Xamarin.Android.Build.Tasks] Import from monodroid/301e7238 Added Xamarin.Android.Build.Tasks and its supporting Libraries. Code has also been updated to make uses of the new libraries in the Java.Interop repository Note : Xamarin.Android.Build.Tasks has a compile time code generation step which generates Profile.g.cs. This file contains the list of SharedRuntimeAssemblies. If building manually you will need to make sure that Xamarin.Android.Tools.BootstrapTasks is built first. --- .gitignore | 1 + Xamarin.Android.sln | 162 +- .../Generator/Generator.cs | 58 + src/Xamarin.Android.Build.Tasks/Ionic.Zip.xml | 18132 ++++++++++++++++ .../Linker/LinkModes.cs | 11 + .../Linker/Mobile.Tuner/MobileProfile.cs | 97 + .../MonoDroid.Tuner/ApplyPreserveAttribute.cs | 122 + .../Linker/MonoDroid.Tuner/Extensions.cs | 101 + .../MonoDroid.Tuner/FixAbstractMethodsStep.cs | 225 + .../GenerateProguardConfiguration.cs | 87 + .../Linker/MonoDroid.Tuner/Linker.cs | 156 + .../Linker/MonoDroid.Tuner/LinkerOptions.cs | 24 + .../Linker/MonoDroid.Tuner/MarkJavaObjects.cs | 255 + .../MonoDroid.Tuner/MonoDroidMarkStep.cs | 170 + .../MonoDroid.Tuner/MonoDroidProfile.cs | 23 + .../MonoDroid.Tuner/PreserveApplications.cs | 93 + .../Linker/MonoDroid.Tuner/PreserveCode.cs | 76 + .../MonoDroid.Tuner/PreserveDynamicTypes.cs | 42 + .../MonoDroid.Tuner/PreserveExportedTypes.cs | 84 + .../MonoDroid.Tuner/PreserveJavaExceptions.cs | 64 + .../PreserveJavaTypeRegistrations.cs | 33 + .../PreserveLinqExpressions.cs | 52 + .../PreserveRuntimeSerialization.cs | 98 + .../MonoDroid.Tuner/PreserveTypeConverters.cs | 46 + .../MonoDroid.Tuner/RemoveAttributes.cs | 54 + .../RemoveLibraryResourceZip.cs | 26 + .../Linker/PreserveLists/Mono.Android.xml | 57 + .../Linker/PreserveLists/OpenTK.xml | 16 + .../System.Data.Services.Client.xml | 39 + .../PreserveLists/System.ServiceModel.xml | 11 + .../Linker/PreserveLists/System.xml | 29 + .../Linker/PreserveLists/mscorlib.xml | 405 + .../Mono.Android/ActivityAttribute.Partial.cs | 192 + .../ApplicationAttribute.Partial.cs | 232 + .../BroadcastReceiverAttribute.Partial.cs | 73 + .../ContentProviderAttribute.Partial.cs | 114 + .../GrantUriPermissionAttribute.Partial.cs | 53 + .../InstrumentationAttribute.Partial.cs | 71 + .../IntentFilterAttribute.Partial.cs | 153 + .../Mono.Android/MetaDataAttribute.Partial.cs | 54 + .../PermissionAttribute.Partial.cs | 94 + .../PermissionGroupAttribute.Partial.cs | 82 + .../PermissionTreeAttribute.Partial.cs | 76 + .../Mono.Android/ServiceAttribute.Partial.cs | 79 + .../SupportsGLTextureAttribute.Partial.cs | 44 + .../UsesFeatureAttribute.Partial.cs | 78 + .../UsesLibraryAttribute.Partial.cs | 66 + .../UsesPermissionAttribute.Partial.cs | 77 + .../Properties/AssemblyInfo.cs | 38 + .../Resources/ApplicationRegistration.java | 9 + .../Resources/IncrementalClassLoader.java | 104 + .../Resources/MonoPackageManager.api4.java | 78 + .../Resources/MonoPackageManager.java | 90 + .../Resources/NOTICE.txt | 4 + .../Resources/NotifyTimeZoneChanges.java | 8 + .../Resources/Placeholder.java | 25 + .../Resources/Seppuku.java | 19 + .../Resources/StubApplication.java | 447 + .../Resources/machine.config | 273 + .../Resources/proguard_xamarin.cfg | 31 + src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs | 306 + .../Tasks/AdjustJavacVersionArguments.cs | 70 + .../Tasks/AndroidCreateDebugKey.cs | 40 + .../Tasks/AndroidSignPackage.cs | 82 + .../Tasks/AndroidToolTask.cs | 91 + .../Tasks/AndroidUpdateResDir.cs | 197 + .../Tasks/AndroidZipAlign.cs | 61 + src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs | 413 + .../Tasks/AsyncTask.cs | 280 + .../Tasks/BuildApk.cs | 549 + ...ulateAdditionalResourceCacheDirectories.cs | 51 + .../Tasks/CheckDuplicateJavaLibraries.cs | 40 + .../Tasks/CheckForRemovedItems.cs | 44 + .../Tasks/CheckProjectItems.cs | 45 + .../Tasks/CheckTargetFrameworks.cs | 76 + .../Tasks/CilStrip.cs | 108 + .../Tasks/ClassParse.cs | 87 + .../Tasks/CollectLibraryAssets.cs | 23 + .../Tasks/CompileToDalvik.cs | 131 + .../Tasks/ConvertDebuggingFiles.cs | 37 + .../Tasks/ConvertResourcesCases.cs | 81 + .../Tasks/CopyAndConvertResources.cs | 112 + .../Tasks/CopyConfigFiles.cs | 37 + .../Tasks/CopyGeneratedJavaResourceClasses.cs | 64 + .../Tasks/CopyIfChanged.cs | 50 + .../Tasks/CopyMdbFiles.cs | 35 + .../Tasks/CopyResource.cs | 81 + .../CreateAdditionalLibraryResourceCache.cs | 58 + .../Tasks/CreateAndroidResourceStamp.cs | 37 + .../Tasks/CreateLibraryResourceArchive.cs | 213 + .../CreateManagedLibraryResourceArchive.cs | 118 + .../Tasks/CreateMsymManifest.cs | 33 + .../Tasks/CreateMultiDexMainDexClassList.cs | 86 + .../Tasks/CreateNativeLibraryArchive.cs | 76 + .../Tasks/CreateResgenManifest.cs | 45 + .../Tasks/CreateTemporaryDirectory.cs | 26 + .../Tasks/Crunch.cs | 94 + .../Tasks/DetermineJavaLibrariesToCompile.cs | 98 + src/Xamarin.Android.Build.Tasks/Tasks/Dx.cs | 90 + .../Tasks/GenerateJavaStubs.cs | 278 + .../Tasks/GenerateManagedAidlProxies.cs | 116 + .../Tasks/GeneratePackageManagerJava.cs | 113 + .../Tasks/GenerateResourceDesigner.cs | 275 + .../Tasks/Generator.cs | 152 + .../Tasks/GetAddOnPlatformLibraries.cs | 37 + .../GetAdditionalResourcesFromAssemblies.cs | 367 + .../Tasks/GetAndroidDefineConstants.cs | 38 + .../Tasks/GetAndroidPackageName.cs | 77 + .../Tasks/GetAppSettingsDirectory.cs | 27 + .../Tasks/GetConvertedJavaLibraries.cs | 39 + .../Tasks/GetExtraPackages.cs | 66 + .../Tasks/GetFilesThatExist.cs | 33 + .../Tasks/GetImportedLibraries.cs | 66 + .../Tasks/GetJavaPlatformJar.cs | 130 + .../Tasks/GetMonoPlatformJar.cs | 43 + .../Tasks/ImportJavaDoc.cs | 56 + src/Xamarin.Android.Build.Tasks/Tasks/Jack.cs | 136 + .../Tasks/JarToXml.cs | 194 + .../Tasks/JavaCompileToolTask.cs | 98 + .../Tasks/JavaToolTask.cs | 178 + .../Tasks/Javac.cs | 65 + src/Xamarin.Android.Build.Tasks/Tasks/Jill.cs | 88 + .../Tasks/KeyTool.cs | 72 + .../Tasks/LinkAssemblies.cs | 165 + src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs | 294 + .../Tasks/LogErrorsForFiles.cs | 32 + .../Tasks/LogWarningsForFiles.cs | 32 + src/Xamarin.Android.Build.Tasks/Tasks/MDoc.cs | 65 + .../Tasks/MakeBundleNativeCodeExternal.cs | 247 + .../Tasks/MergeResources.cs | 89 + .../Tasks/NdkUtils.cs | 201 + .../ParseAndroidWearProjectAndManifest.cs | 50 + .../Tasks/PrepareWearApplicationFiles.cs | 69 + .../Tasks/Proguard.cs | 222 + ...eadAdditionalResourcesFromAssemblyCache.cs | 65 + .../Tasks/ReadImportedLibrariesCache.cs | 70 + .../Tasks/ReadLibraryProjectImportsCache.cs | 81 + .../Tasks/ReadResolvedSdksCache.cs | 141 + .../Tasks/RemoveDirFixed.cs | 111 + .../Tasks/RemoveRegisterAttribute.cs | 68 + .../Tasks/ResolveAssemblies.cs | 184 + .../Tasks/ResolveLibraryProjectImports.cs | 268 + .../Tasks/ResolveSdksTask.cs | 387 + .../Tasks/StripEmbeddedLibraries.cs | 88 + .../Utilities/AndroidAddOnManifest.cs | 87 + .../Utilities/InvalidActivityNameException.cs | 11 + .../Utilities/JavaResourceParser.cs | 159 + .../Utilities/MSBuildExtensions.cs | 141 + .../Utilities/ManifestDocument.cs | 835 + .../Utilities/ManifestDocumentElement.cs | 337 + .../Utilities/MonoAndroidHelper.cs | 441 + .../Utilities/Profile.cs | 45 + .../ResourceDesignerImportGenerator.cs | 61 + .../Utilities/ResourceMerger.cs | 296 + .../Utilities/XDocumentExtensions.cs | 27 + .../Xamarin.Android.Analysis.targets | 82 + .../Xamarin.Android.Bindings.targets | 567 + .../Xamarin.Android.Build.Tasks.csproj | 638 + .../Xamarin.Android.Build.Tasks.targets | 23 + .../Xamarin.Android.CSharp.targets | 49 + .../Xamarin.Android.Common.targets | 2543 +++ .../Xamarin.Android.FSharp.targets | 48 + .../Xamarin.Android.PCLSupport.targets | 38 + .../Xamarin.Android.VisualBasic.targets | 44 + .../packages.config | 8 + .../AndroidAppManifest.cs | 314 + .../AndroidLogger.cs | 123 + .../AndroidSdk.cs | 149 + .../AndroidTargetArch.cs | 19 + .../AndroidVersion.cs | 108 + .../FileUtil.cs | 58 + .../GdbPaths.cs | 94 + .../MonoDroidSdk.cs | 60 + src/Xamarin.Android.Build.Utilities/OS.cs | 227 + .../ProcessUtils.cs | 124 + .../Properties/AssemblyInfo.cs | 15 + .../SatelliteAssembly.cs | 29 + .../Sdks/AndroidSdkBase.cs | 144 + .../Sdks/AndroidSdkUnix.cs | 244 + .../Sdks/AndroidSdkWindows.cs | 205 + .../Sdks/MonoDroidSdkBase.cs | 280 + .../Sdks/MonoDroidSdkUnix.cs | 95 + .../Sdks/MonoDroidSdkWindows.cs | 76 + .../Xamarin.Android.Build.Utilities.csproj | 59 + ...amarin.Android.Tools.BootstrapTasks.csproj | 1 + .../GenerateProfile.cs | 47 + src/Xamarin.AndroidTools.Aidl/AidlAst.cs | 170 + src/Xamarin.AndroidTools.Aidl/AidlCompiler.cs | 82 + src/Xamarin.AndroidTools.Aidl/AidlParser.cs | 220 + .../BindingDatabase.cs | 63 + .../CSharpCodeGenerator.cs | 661 + .../ConverterOptions.cs | 23 + .../Xamarin.AndroidTools.Aidl.csproj | 61 + src/Xamarin.AndroidTools.Aidl/packages.config | 5 + 194 files changed, 43528 insertions(+), 59 deletions(-) create mode 100644 src/Xamarin.Android.Build.Tasks/Generator/Generator.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Ionic.Zip.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/LinkModes.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/ApplyPreserveAttribute.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Linker.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/LinkerOptions.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MarkJavaObjects.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveCode.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveDynamicTypes.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveExportedTypes.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaExceptions.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaTypeRegistrations.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveLinqExpressions.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveRuntimeSerialization.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveTypeConverters.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveAttributes.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveLibraryResourceZip.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/Mono.Android.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/OpenTK.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.Data.Services.Client.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.ServiceModel.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/mscorlib.xml create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/ActivityAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/BroadcastReceiverAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/ContentProviderAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/GrantUriPermissionAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/IntentFilterAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/MetaDataAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/ServiceAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/UsesLibraryAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Properties/AssemblyInfo.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/ApplicationRegistration.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/IncrementalClassLoader.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/NOTICE.txt create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/NotifyTimeZoneChanges.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/Placeholder.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/Seppuku.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/StubApplication.java create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/machine.config create mode 100644 src/Xamarin.Android.Build.Tasks/Resources/proguard_xamarin.cfg create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AdjustJavacVersionArguments.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AndroidCreateDebugKey.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AndroidSignPackage.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AndroidToolTask.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AndroidUpdateResDir.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AndroidZipAlign.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/AsyncTask.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CalculateAdditionalResourceCacheDirectories.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CheckDuplicateJavaLibraries.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CheckForRemovedItems.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CheckProjectItems.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CheckTargetFrameworks.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CilStrip.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ClassParse.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CollectLibraryAssets.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CompileToDalvik.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ConvertDebuggingFiles.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ConvertResourcesCases.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CopyAndConvertResources.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CopyConfigFiles.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CopyGeneratedJavaResourceClasses.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CopyIfChanged.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CopyMdbFiles.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CopyResource.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateAdditionalLibraryResourceCache.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateAndroidResourceStamp.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateLibraryResourceArchive.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateManagedLibraryResourceArchive.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateMsymManifest.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateMultiDexMainDexClassList.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateNativeLibraryArchive.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateResgenManifest.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/CreateTemporaryDirectory.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Crunch.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/DetermineJavaLibrariesToCompile.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Dx.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GenerateManagedAidlProxies.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetAddOnPlatformLibraries.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidDefineConstants.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidPackageName.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetAppSettingsDirectory.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetConvertedJavaLibraries.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetExtraPackages.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetFilesThatExist.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetImportedLibraries.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/GetMonoPlatformJar.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ImportJavaDoc.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Jack.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/JarToXml.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/JavaCompileToolTask.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/JavaToolTask.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Jill.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/KeyTool.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/LinkAssemblies.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/LogErrorsForFiles.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/LogWarningsForFiles.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/MDoc.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/MergeResources.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/NdkUtils.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ParseAndroidWearProjectAndManifest.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/PrepareWearApplicationFiles.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ReadAdditionalResourcesFromAssemblyCache.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ReadImportedLibrariesCache.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ReadLibraryProjectImportsCache.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ReadResolvedSdksCache.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/RemoveDirFixed.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Tasks/StripEmbeddedLibraries.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/AndroidAddOnManifest.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/InvalidActivityNameException.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/JavaResourceParser.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/MSBuildExtensions.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocumentElement.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/ResourceDesignerImportGenerator.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/ResourceMerger.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.Analysis.targets create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.Bindings.targets create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.CSharp.targets create mode 100755 src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.FSharp.targets create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.PCLSupport.targets create mode 100644 src/Xamarin.Android.Build.Tasks/Xamarin.Android.VisualBasic.targets create mode 100644 src/Xamarin.Android.Build.Tasks/packages.config create mode 100644 src/Xamarin.Android.Build.Utilities/AndroidAppManifest.cs create mode 100644 src/Xamarin.Android.Build.Utilities/AndroidLogger.cs create mode 100644 src/Xamarin.Android.Build.Utilities/AndroidSdk.cs create mode 100644 src/Xamarin.Android.Build.Utilities/AndroidTargetArch.cs create mode 100644 src/Xamarin.Android.Build.Utilities/AndroidVersion.cs create mode 100644 src/Xamarin.Android.Build.Utilities/FileUtil.cs create mode 100644 src/Xamarin.Android.Build.Utilities/GdbPaths.cs create mode 100644 src/Xamarin.Android.Build.Utilities/MonoDroidSdk.cs create mode 100644 src/Xamarin.Android.Build.Utilities/OS.cs create mode 100644 src/Xamarin.Android.Build.Utilities/ProcessUtils.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Properties/AssemblyInfo.cs create mode 100644 src/Xamarin.Android.Build.Utilities/SatelliteAssembly.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkBase.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkUnix.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkWindows.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkBase.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkUnix.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkWindows.cs create mode 100644 src/Xamarin.Android.Build.Utilities/Xamarin.Android.Build.Utilities.csproj create mode 100644 src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateProfile.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/AidlAst.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/AidlCompiler.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/AidlParser.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/BindingDatabase.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/CSharpCodeGenerator.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/ConverterOptions.cs create mode 100644 src/Xamarin.AndroidTools.Aidl/Xamarin.AndroidTools.Aidl.csproj create mode 100644 src/Xamarin.AndroidTools.Aidl/packages.config diff --git a/.gitignore b/.gitignore index 313036baf4a..5891fbce090 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ bin Configuration.Override.props obj packages +.DS_Store diff --git a/Xamarin.Android.sln b/Xamarin.Android.sln index 9abd8dba766..40355ed7f2a 100644 --- a/Xamarin.Android.sln +++ b/Xamarin.Android.sln @@ -1,4 +1,4 @@ - + Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build-Tools", "Build-Tools", "{E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62}" @@ -11,6 +11,8 @@ Project("{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}") = "mono-runtimes", "build-tool EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jnienv-gen", "build-tools\jnienv-gen\jnienv-gen.csproj", "{AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "api-merge", "build-tools\api-merge\api-merge.csproj", "{3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}" +EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{04E3E11E-B47D-4599-8AFC-50515A95E715}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop", "external\Java.Interop\src\Java.Interop\Java.Interop.csproj", "{94BD81F7-B06F-4295-9636-F8A3B6BDC762}" @@ -23,23 +25,29 @@ Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Java.Interop.Tools.TypeName EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Android", "src\Mono.Android\Mono.Android.csproj", "{66CF299A-CE95-4131-BCD8-DB66E30C4BF7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "api-merge", "build-tools\api-merge\api-merge.csproj", "{3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Android.Export", "src\Mono.Android.Export\Mono.Android.Export.csproj", "{B8105878-D423-4159-A3E7-028298281EC6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Cecil", "external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil.csproj", "{D48EE8D0-0A0A-4493-AEF5-DAF5F8CF86AD}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.AndroidTools.Aidl", "src\Xamarin.AndroidTools.Aidl\Xamarin.AndroidTools.Aidl.csproj", "{D27AD8F7-7710-40BE-B03B-55EFBEC13C44}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Diagnostics", "external\Java.Interop\src\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj", "{64CC4E44-CE3A-4319-BF3F-6CF8BD513870}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Build.Tasks", "src\Xamarin.Android.Build.Tasks\Xamarin.Android.Build.Tasks.csproj", "{3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Tools.AnnotationSupport", "external\Java.Interop\src\Xamarin.Android.Tools.AnnotationSupport\Xamarin.Android.Tools.AnnotationSupport.csproj", "{07BC4495-1267-4B78-9EA6-B76FEEA2A64A}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Build.Utilities", "src\Xamarin.Android.Build.Utilities\Xamarin.Android.Build.Utilities.csproj", "{91713046-C358-4647-B162-ED4E1442F3D8}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Tools.ApiXmlAdjuster", "external\Java.Interop\src\Xamarin.Android.Tools.ApiXmlAdjuster\Xamarin.Android.Tools.ApiXmlAdjuster.csproj", "{1268EADF-8344-431C-81F6-FCB7CBC99F49}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Tools.Bytecode", "external\Java.Interop\src\Xamarin.Android.Tools.Bytecode\Xamarin.Android.Tools.Bytecode.csproj", "{B17475BC-45A2-47A3-B8FC-62F3A0959EE0}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{864062D3-A415-4A6F-9324-5820237BA058}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "jcw-gen", "external\Java.Interop\tools\jcw-gen\jcw-gen.csproj", "{52C7D9B6-E8C8-47D0-9471-652D278D7D77}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.JavaCallableWrappers", "external\Java.Interop\src\Java.Interop.Tools.JavaCallableWrappers\Java.Interop.Tools.JavaCallableWrappers.csproj", "{D18FCF91-8876-48A0-A693-2DC1E7D3D80A}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tools", "Tools", "{864062D3-A415-4A6F-9324-5820237BA058}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Tools.ApiXmlAdjuster", "external\Java.Interop\src\Xamarin.Android.Tools.ApiXmlAdjuster\Xamarin.Android.Tools.ApiXmlAdjuster.csproj", "{1268EADF-8344-431C-81F6-FCB7CBC99F49}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Xamarin.Android.Tools.AnnotationSupport", "external\Java.Interop\src\Xamarin.Android.Tools.AnnotationSupport\Xamarin.Android.Tools.AnnotationSupport.csproj", "{07BC4495-1267-4B78-9EA6-B76FEEA2A64A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Diagnostics", "external\Java.Interop\src\Java.Interop.Tools.Diagnostics\Java.Interop.Tools.Diagnostics.csproj", "{64CC4E44-CE3A-4319-BF3F-6CF8BD513870}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Java.Interop.Tools.Cecil", "external\Java.Interop\src\Java.Interop.Tools.Cecil\Java.Interop.Tools.Cecil.csproj", "{D48EE8D0-0A0A-4493-AEF5-DAF5F8CF86AD}" EndProject Project("{9344BDBB-3E7F-41FC-A0DD-8665D75EE146}") = "monodroid", "src\monodroid\monodroid.mdproj", "{53EE4C57-1C03-405A-8243-8DA539546C88}" EndProject @@ -53,38 +61,46 @@ Global XAIntegrationRelease|Any CPU = XAIntegrationRelease|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.Release|AnyCPU.ActiveCfg = Release|AnyCPU - {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.Release|AnyCPU.Build.0 = Release|AnyCPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.Release|AnyCPU.Build.0 = Release|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Release|AnyCPU.Build.0 = Release|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|An yCPU + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationRelease|Any CPU.Build.0 = Debug|An yCPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Release|AnyCPU.Build.0 = Release|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.Release|AnyCPU.ActiveCfg = Release|AnyCPU + {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.Release|AnyCPU.Build.0 = Release|AnyCPU {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Release|AnyCPU.ActiveCfg = Release|AnyCPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Release|AnyCPU.Build.0 = Release|AnyCPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Debug|AnyCPU.Build.0 = Debug|Any CPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU - {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Debug|AnyCPU.Build.0 = Debug|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Release|AnyCPU.ActiveCfg = Release|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Release|AnyCPU.Build.0 = Release|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU - {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Debug|AnyCPU.Build.0 = Debug|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Release|AnyCPU.ActiveCfg = Release|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Release|AnyCPU.Build.0 = Release|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU - {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.Release|AnyCPU.Build.0 = Release|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {91713046-C358-4647-B162-ED4E1442F3D8}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU {94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU {94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Debug|AnyCPU.Build.0 = Debug|Any CPU {94BD81F7-B06F-4295-9636-F8A3B6BDC762}.Release|AnyCPU.ActiveCfg = Release|Any CPU @@ -93,30 +109,22 @@ Global {94BD81F7-B06F-4295-9636-F8A3B6BDC762}.XAIntegrationDebug|Any CPU.Build.0 = XAIntegrationDebug|Any CPU {94BD81F7-B06F-4295-9636-F8A3B6BDC762}.XAIntegrationRelease|Any CPU.ActiveCfg = XAIntegrationRelease|Any CPU {94BD81F7-B06F-4295-9636-F8A3B6BDC762}.XAIntegrationRelease|Any CPU.Build.0 = XAIntegrationRelease|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|AnyCPU.Build.0 = Debug|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.Release|AnyCPU.ActiveCfg = Release|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.Release|AnyCPU.Build.0 = Release|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU - {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Debug|AnyCPU.Build.0 = Debug|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Release|AnyCPU.ActiveCfg = Release|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.Release|AnyCPU.Build.0 = Release|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU - {66CF299A-CE95-4131-BCD8-DB66E30C4BF7}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Release|AnyCPU.ActiveCfg = Release|Any CPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Release|AnyCPU.Build.0 = Release|Any CPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|An yCPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.XAIntegrationRelease|Any CPU.Build.0 = Debug|An yCPU - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.Release|AnyCPU.Build.0 = Release|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.Release|AnyCPU.Build.0 = Release|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU {B8105878-D423-4159-A3E7-028298281EC6}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU {B8105878-D423-4159-A3E7-028298281EC6}.Debug|AnyCPU.Build.0 = Debug|Any CPU {B8105878-D423-4159-A3E7-028298281EC6}.Release|AnyCPU.ActiveCfg = Release|Any CPU @@ -141,19 +149,55 @@ Global {8A6CB07C-E493-4A4F-AB94-038645A27118}.XAIntegrationDebug|AnyCPU.Build.0 = Debug|Any CPU {8A6CB07C-E493-4A4F-AB94-038645A27118}.XAIntegrationRelease|AnyCPU.ActiveCfg = Debug|Any CPU {8A6CB07C-E493-4A4F-AB94-038645A27118}.XAIntegrationRelease|AnyCPU.Build.0 = Debug|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.Release|AnyCPU.Build.0 = Release|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.Release|AnyCPU.Build.0 = Release|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {D14A1B5C-2060-4930-92BE-F7190256C735}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.Release|AnyCPU.ActiveCfg = Release|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.Release|AnyCPU.Build.0 = Release|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Debug|AnyCPU.ActiveCfg = Debug|Any CPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Debug|AnyCPU.Build.0 = Debug|Any CPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Release|AnyCPU.ActiveCfg = Release|AnyCPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.Release|AnyCPU.Build.0 = Release|AnyCPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationDebug|Any CPU.ActiveCfg = Debug|Any CPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationDebug|Any CPU.Build.0 = Debug|Any CPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationRelease|Any CPU.ActiveCfg = Debug|Any CPU + {E8492EFB-D14A-4F32-AA28-88848322ECEA}.XAIntegrationRelease|Any CPU.Build.0 = Debug|Any CPU EndGlobalSection GlobalSection(NestedProjects) = preSolution {8FF78EB6-6FC8-46A7-8A15-EBBA9045C5FA} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62} {E8492EFB-D14A-4F32-AA28-88848322ECEA} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62} {C03E6CF1-7460-4CDC-A4AB-292BBC0F61F2} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62} {AFB8F6D1-6EA9-42C3-950B-98F34C669AD2} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62} + {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62} {94BD81F7-B06F-4295-9636-F8A3B6BDC762} = {04E3E11E-B47D-4599-8AFC-50515A95E715} {D14A1B5C-2060-4930-92BE-F7190256C735} = {04E3E11E-B47D-4599-8AFC-50515A95E715} {74598F5C-B8CC-4CE6-8EE2-AB9CA1400076} = {04E3E11E-B47D-4599-8AFC-50515A95E715} {E706B6F2-5562-4765-8F07-8CF84A797B30} = {04E3E11E-B47D-4599-8AFC-50515A95E715} {66CF299A-CE95-4131-BCD8-DB66E30C4BF7} = {04E3E11E-B47D-4599-8AFC-50515A95E715} - {3FC3E78B-F7D4-42EA-BBE8-4535DF42BFF8} = {E351F97D-EA4F-4E7F-AAA0-8EBB1F2A4A62} {B8105878-D423-4159-A3E7-028298281EC6} = {04E3E11E-B47D-4599-8AFC-50515A95E715} + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44} = {04E3E11E-B47D-4599-8AFC-50515A95E715} + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7} = {04E3E11E-B47D-4599-8AFC-50515A95E715} + {91713046-C358-4647-B162-ED4E1442F3D8} = {04E3E11E-B47D-4599-8AFC-50515A95E715} + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0} = {04E3E11E-B47D-4599-8AFC-50515A95E715} {52C7D9B6-E8C8-47D0-9471-652D278D7D77} = {864062D3-A415-4A6F-9324-5820237BA058} {D18FCF91-8876-48A0-A693-2DC1E7D3D80A} = {864062D3-A415-4A6F-9324-5820237BA058} {1268EADF-8344-431C-81F6-FCB7CBC99F49} = {864062D3-A415-4A6F-9324-5820237BA058} diff --git a/src/Xamarin.Android.Build.Tasks/Generator/Generator.cs b/src/Xamarin.Android.Build.Tasks/Generator/Generator.cs new file mode 100644 index 00000000000..d61b3c269fe --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Generator/Generator.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Mono.Cecil; +using Microsoft.Build.Utilities; + +using Java.Interop.Tools.Diagnostics; +using Java.Interop.Tools.JavaCallableWrappers; + +namespace Xamarin.Android.Tasks +{ + class Generator + { + public static bool CreateJavaSources (TaskLoggingHelper log, IEnumerable javaTypes, string outputPath, bool useSharedRuntime, bool generateOnCreateOverrides, bool hasExportReference) + { + bool ok = true; + foreach (var t in javaTypes) { + try { + GenerateJavaSource (log, t, outputPath, useSharedRuntime, generateOnCreateOverrides, hasExportReference); + } catch (XamarinAndroidException xae) { + ok = false; + log.LogError ( + subcategory: "", + errorCode: "XA" + xae.Code, + helpKeyword: string.Empty, + file: xae.SourceFile, + lineNumber: xae.SourceLine, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: xae.MessageWithoutCode, + messageArgs: new object [0] + ); + } + } + return ok; + } + + static void GenerateJavaSource (TaskLoggingHelper log, TypeDefinition t, string outputPath, bool useSharedRuntime, bool generateOnCreateOverrides, bool hasExportReference) + { + try { + var jti = new JavaCallableWrapperGenerator (t, log.LogWarning) { + UseSharedRuntime = useSharedRuntime, + GenerateOnCreateOverrides = generateOnCreateOverrides, + }; + + jti.Generate (outputPath); + if (jti.HasExport && !hasExportReference) + Diagnostic.Error (4210, "You need to add a reference to Mono.Android.Export.dll when you use ExportAttribute or ExportFieldAttribute."); + } catch (Exception ex) { + if (ex is XamarinAndroidException) + throw; + Diagnostic.Error (4209, "Failed to create JavaTypeInfo for class: {0} due to {1}", t.FullName, ex); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Ionic.Zip.xml b/src/Xamarin.Android.Build.Tasks/Ionic.Zip.xml new file mode 100644 index 00000000000..d11dd755e2c --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Ionic.Zip.xml @@ -0,0 +1,18132 @@ + + + + Ionic.Zip + + + + + An enum that specifies the source of the ZipEntry. + + + + + Default value. Invalid on a bonafide ZipEntry. + + + + + The entry was instantiated by calling AddFile() or another method that + added an entry from the filesystem. + + + + + The entry was instantiated via or + . + + + + + The ZipEntry was instantiated by reading a zipfile. + + + + + The content for the ZipEntry will be or was provided by the WriteDelegate. + + + + + The content for the ZipEntry will be obtained from the stream dispensed by the OpenDelegate. + The entry was instantiated via . + + + + + The content for the ZipEntry will be or was obtained from a ZipOutputStream. + + + + + Provides a stream metaphor for generating zip files. + + + + + This class writes zip files, as defined in the specification + for zip files described by PKWare. The compression for this + implementation is provided by a managed-code version of Zlib, included with + DotNetZip in the classes in the Ionic.Zlib namespace. + + + + This class provides an alternative programming model to the one enabled by the + class. Use this when creating zip files, as an + alternative to the class, when you would like to use a + Stream type to write the zip file. + + + + Both the ZipOutputStream class and the ZipFile class can be used + to create zip files. Both of them support many of the common zip features, + including Unicode, different compression levels, and ZIP64. They provide + very similar performance when creating zip files. + + + + The ZipFile class is generally easier to use than + ZipOutputStream and should be considered a higher-level interface. For + example, when creating a zip file via calls to the PutNextEntry() and + Write() methods on the ZipOutputStream class, the caller is + responsible for opening the file, reading the bytes from the file, writing + those bytes into the ZipOutputStream, setting the attributes on the + ZipEntry, and setting the created, last modified, and last accessed + timestamps on the zip entry. All of these things are done automatically by a + call to ZipFile.AddFile(). + For this reason, the ZipOutputStream is generally recommended for use + only when your application emits arbitrary data, not necessarily data from a + filesystem file, directly into a zip file, and does so using a Stream + metaphor. + + + + Aside from the differences in programming model, there are other + differences in capability between the two classes. + + + + + ZipFile can be used to read and extract zip files, in addition to + creating zip files. ZipOutputStream cannot read zip files. If you want + to use a stream to read zip files, check out the class. + + + + ZipOutputStream does not support the creation of segmented or spanned + zip files. + + + + ZipOutputStream cannot produce a self-extracting archive. + + + + + Be aware that the ZipOutputStream class implements the interface. In order for + ZipOutputStream to produce a valid zip file, you use use it within + a using clause (Using in VB), or call the Dispose() method + explicitly. See the examples for how to employ a using clause. + + + + Also, a note regarding compression performance: On the desktop .NET + Framework, DotNetZip can use a multi-threaded compression implementation + that provides significant speed increases on large files, over 300k or so, + at the cost of increased memory use at runtime. (The output of the + compression is almost exactly the same size). But, the multi-threaded + approach incurs a performance hit on smaller files. There's no way for the + ZipOutputStream to know whether parallel compression will be beneficial, + because the ZipOutputStream does not know how much data you will write + through the stream. You may wish to set the property to zero, if you are compressing + large files through ZipOutputStream. This will cause parallel + compression to be used, always. + + + + + + Create a ZipOutputStream, wrapping an existing stream. + + + + + The class is generally easier to use when creating + zip files. The ZipOutputStream offers a different metaphor for creating a + zip file, based on the class. + + + + + + The stream to wrap. It must be writable. This stream will be closed at + the time the ZipOutputStream is closed. + + + + + This example shows how to create a zip file, using the + ZipOutputStream class. + + + private void Zipup() + { + if (filesToZip.Count == 0) + { + System.Console.WriteLine("Nothing to do."); + return; + } + + using (var raw = File.Open(_outputFileName, FileMode.Create, FileAccess.ReadWrite )) + { + using (var output= new ZipOutputStream(raw)) + { + output.Password = "VerySecret!"; + output.Encryption = EncryptionAlgorithm.WinZipAes256; + + foreach (string inputFileName in filesToZip) + { + System.Console.WriteLine("file: {0}", inputFileName); + + output.PutNextEntry(inputFileName); + using (var input = File.Open(inputFileName, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Write )) + { + byte[] buffer= new byte[2048]; + int n; + while ((n= input.Read(buffer,0,buffer.Length)) > 0) + { + output.Write(buffer,0,n); + } + } + } + } + } + } + + + + Private Sub Zipup() + Dim outputFileName As String = "XmlData.zip" + Dim filesToZip As String() = Directory.GetFiles(".", "*.xml") + If (filesToZip.Length = 0) Then + Console.WriteLine("Nothing to do.") + Else + Using raw As FileStream = File.Open(outputFileName, FileMode.Create, FileAccess.ReadWrite) + Using output As ZipOutputStream = New ZipOutputStream(raw) + output.Password = "VerySecret!" + output.Encryption = EncryptionAlgorithm.WinZipAes256 + Dim inputFileName As String + For Each inputFileName In filesToZip + Console.WriteLine("file: {0}", inputFileName) + output.PutNextEntry(inputFileName) + Using input As FileStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Dim n As Integer + Dim buffer As Byte() = New Byte(2048) {} + Do While (n = input.Read(buffer, 0, buffer.Length) > 0) + output.Write(buffer, 0, n) + Loop + End Using + Next + End Using + End Using + End If + End Sub + + + + + + Create a ZipOutputStream that writes to a filesystem file. + + + + The class is generally easier to use when creating + zip files. The ZipOutputStream offers a different metaphor for creating a + zip file, based on the class. + + + + The name of the zip file to create. + + + + + This example shows how to create a zip file, using the + ZipOutputStream class. + + + private void Zipup() + { + if (filesToZip.Count == 0) + { + System.Console.WriteLine("Nothing to do."); + return; + } + + using (var output= new ZipOutputStream(outputFileName)) + { + output.Password = "VerySecret!"; + output.Encryption = EncryptionAlgorithm.WinZipAes256; + + foreach (string inputFileName in filesToZip) + { + System.Console.WriteLine("file: {0}", inputFileName); + + output.PutNextEntry(inputFileName); + using (var input = File.Open(inputFileName, FileMode.Open, FileAccess.Read, + FileShare.Read | FileShare.Write )) + { + byte[] buffer= new byte[2048]; + int n; + while ((n= input.Read(buffer,0,buffer.Length)) > 0) + { + output.Write(buffer,0,n); + } + } + } + } + } + + + + Private Sub Zipup() + Dim outputFileName As String = "XmlData.zip" + Dim filesToZip As String() = Directory.GetFiles(".", "*.xml") + If (filesToZip.Length = 0) Then + Console.WriteLine("Nothing to do.") + Else + Using output As ZipOutputStream = New ZipOutputStream(outputFileName) + output.Password = "VerySecret!" + output.Encryption = EncryptionAlgorithm.WinZipAes256 + Dim inputFileName As String + For Each inputFileName In filesToZip + Console.WriteLine("file: {0}", inputFileName) + output.PutNextEntry(inputFileName) + Using input As FileStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Dim n As Integer + Dim buffer As Byte() = New Byte(2048) {} + Do While (n = input.Read(buffer, 0, buffer.Length) > 0) + output.Write(buffer, 0, n) + Loop + End Using + Next + End Using + End If + End Sub + + + + + + Create a ZipOutputStream. + + + + See the documentation for the ZipOutputStream(Stream) + constructor for an example. + + + + The stream to wrap. It must be writable. + + + + true if the application would like the stream + to remain open after the ZipOutputStream has been closed. + + + + Provides a string representation of the instance. + + + This can be useful for debugging purposes. + + + a string representation of the instance. + + + + Returns true if an entry by the given name has already been written + to the ZipOutputStream. + + + + The name of the entry to scan for. + + + + true if an entry by the given name has already been written. + + + + + Write the data from the buffer to the stream. + + + + As the application writes data into this stream, the data may be + compressed and encrypted before being written out to the underlying + stream, depending on the settings of the + and the properties. + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Specify the name of the next entry that will be written to the zip file. + + + + + Call this method just before calling , to + specify the name of the entry that the next set of bytes written to + the ZipOutputStream belongs to. All subsequent calls to Write, + until the next call to PutNextEntry, + will be inserted into the named entry in the zip file. + + + + If the used in PutNextEntry() ends in + a slash, then the entry added is marked as a directory. Because directory + entries do not contain data, a call to Write(), before an + intervening additional call to PutNextEntry(), will throw an + exception. + + + + If you don't call Write() between two calls to + PutNextEntry(), the first entry is inserted into the zip file as a + file of zero size. This may be what you want. + + + + Because PutNextEntry() closes out the prior entry, if any, this + method may throw if there is a problem with the prior entry. + + + + This method returns the ZipEntry. You can modify public properties + on the ZipEntry, such as , , and so on, until the first call to + ZipOutputStream.Write(), or until the next call to + PutNextEntry(). If you modify the ZipEntry after + having called Write(), you may get a runtime exception, or you may + silently get an invalid zip archive. + + + + + + + This example shows how to create a zip file, using the + ZipOutputStream class. + + + private void Zipup() + { + using (FileStream fs raw = File.Open(_outputFileName, FileMode.Create, FileAccess.ReadWrite )) + { + using (var output= new ZipOutputStream(fs)) + { + output.Password = "VerySecret!"; + output.Encryption = EncryptionAlgorithm.WinZipAes256; + output.PutNextEntry("entry1.txt"); + byte[] buffer= System.Text.Encoding.ASCII.GetBytes("This is the content for entry #1."); + output.Write(buffer,0,buffer.Length); + output.PutNextEntry("entry2.txt"); // this will be zero length + output.PutNextEntry("entry3.txt"); + buffer= System.Text.Encoding.ASCII.GetBytes("This is the content for entry #3."); + output.Write(buffer,0,buffer.Length); + } + } + } + + + + + The name of the entry to be added, including any path to be used + within the zip file. + + + + The ZipEntry created. + + + + + + Dispose the stream + + + + + This method writes the Zip Central directory, then closes the stream. The + application must call Dispose() (or Close) in order to produce a valid zip file. + + + + Typically the application will call Dispose() implicitly, via a using + statement in C#, or a Using statement in VB. + + + + + set this to true, always. + + + + This is a no-op. + + + + + This method always throws a NotSupportedException. + + ignored + ignored + ignored + nothing + + + + This method always throws a NotSupportedException. + + ignored + ignored + nothing + + + + This method always throws a NotSupportedException. + + ignored + + + + Sets the password to be used on the ZipOutputStream instance. + + + + + + When writing a zip archive, this password is applied to the entries, not + to the zip archive itself. It applies to any ZipEntry subsequently + written to the ZipOutputStream. + + + + Using a password does not encrypt or protect the "directory" of the + archive - the list of entries contained in the archive. If you set the + Password property, the password actually applies to individual + entries that are added to the archive, subsequent to the setting of this + property. The list of filenames in the archive that is eventually created + will appear in clear text, but the contents of the individual files are + encrypted. This is how Zip encryption works. + + + + If you set this property, and then add a set of entries to the archive via + calls to PutNextEntry, then each entry is encrypted with that + password. You may also want to change the password between adding + different entries. If you set the password, add an entry, then set the + password to null (Nothing in VB), and add another entry, the + first entry is encrypted and the second is not. + + + + When setting the Password, you may also want to explicitly set the property, to specify how to encrypt the entries added + to the ZipFile. If you set the Password to a non-null value and do not + set , then PKZip 2.0 ("Weak") encryption is used. + This encryption is relatively weak but is very interoperable. If + you set the password to a null value (Nothing in VB), + Encryption is reset to None. + + + + Special case: if you wrap a ZipOutputStream around a non-seekable stream, + and use encryption, and emit an entry of zero bytes, the Close() or + PutNextEntry() following the entry will throw an exception. + + + + + + + The Encryption to use for entries added to the ZipOutputStream. + + + + + The specified Encryption is applied to the entries subsequently + written to the ZipOutputStream instance. + + + + If you set this to something other than + EncryptionAlgorithm.None, you will also need to set the + to a non-null, non-empty value in + order to actually get encryption on the entry. + + + + + ZipOutputStream.Password + ZipEntry.Encryption + + + + Size of the work buffer to use for the ZLIB codec during compression. + + + + Setting this may affect performance. For larger files, setting this to a + larger size may improve performance, but I'm not sure. Sorry, I don't + currently have good recommendations on how to set it. You can test it if + you like. + + + + + The compression strategy to use for all entries. + + + + Set the Strategy used by the ZLIB-compatible compressor, when compressing + data for the entries in the zip archive. Different compression strategies + work better on different sorts of data. The strategy parameter can affect + the compression ratio and the speed of compression but not the correctness + of the compresssion. For more information see . + + + + + The type of timestamp attached to the ZipEntry. + + + + Set this in order to specify the kind of timestamp that should be emitted + into the zip file for each entry. + + + + + Sets the compression level to be used for entries subsequently added to + the zip archive. + + + + + Varying the compression level used on entries can affect the + size-vs-speed tradeoff when compression and decompressing data streams + or files. + + + + As with some other properties on the ZipOutputStream class, like , and , + setting this property on a ZipOutputStream + instance will cause the specified CompressionLevel to be used on all + items that are subsequently added to the + ZipOutputStream instance. + + + + If you do not set this property, the default compression level is used, + which normally gives a good balance of compression efficiency and + compression speed. In some tests, using BestCompression can + double the time it takes to compress, while delivering just a small + increase in compression efficiency. This behavior will vary with the + type of data you compress. If you are in doubt, just leave this setting + alone, and accept the default. + + + + + + The compression method used on each entry added to the ZipOutputStream. + + + + + A comment attached to the zip archive. + + + + + + The application sets this property to specify a comment to be embedded + into the generated zip archive. + + + + According to PKWARE's + zip specification, the comment is not encrypted, even if there is a + password set on the zip file. + + + + The specification does not describe how to indicate the encoding used + on a comment string. Many "compliant" zip tools and libraries use + IBM437 as the code page for comments; DotNetZip, too, follows that + practice. On the other hand, there are situations where you want a + Comment to be encoded with something else, for example using code page + 950 "Big-5 Chinese". To fill that need, DotNetZip will encode the + comment following the same procedure it follows for encoding + filenames: (a) if is + Never, it uses the default encoding (IBM437). (b) if is Always, it always uses the + alternate encoding (). (c) if is AsNecessary, it uses the + alternate encoding only if the default encoding is not sufficient for + encoding the comment - in other words if decoding the result does not + produce the original string. This decision is taken at the time of + the call to ZipFile.Save(). + + + + + + + Specify whether to use ZIP64 extensions when saving a zip archive. + + + + + The default value for the property is . is + safest, in the sense that you will not get an Exception if a + pre-ZIP64 limit is exceeded. + + + + You must set this property before calling Write(). + + + + + + + Indicates whether ZIP64 extensions were used when saving the zip archive. + + + + The value is defined only after the ZipOutputStream has been closed. + + + + + Whether the ZipOutputStream should use case-insensitive comparisons when + checking for uniqueness of zip entries. + + + + + Though the zip specification doesn't prohibit zipfiles with duplicate + entries, Sane zip files have no duplicates, and the DotNetZip library + cannot create zip files with duplicate entries. If an application attempts + to call with a name that duplicates one + already used within the archive, the library will throw an Exception. + + + This property allows the application to specify whether the + ZipOutputStream instance considers ordinal case when checking for + uniqueness of zip entries. + + + + + + Indicates whether to encode entry filenames and entry comments using + Unicode (UTF-8). + + + + + The + PKWare zip specification provides for encoding file names and file + comments in either the IBM437 code page, or in UTF-8. This flag selects + the encoding according to that specification. By default, this flag is + false, and filenames and comments are encoded into the zip file in the + IBM437 codepage. Setting this flag to true will specify that filenames + and comments that cannot be encoded with IBM437 will be encoded with + UTF-8. + + + + Zip files created with strict adherence to the PKWare specification with + respect to UTF-8 encoding can contain entries with filenames containing + any combination of Unicode characters, including the full range of + characters from Chinese, Latin, Hebrew, Greek, Cyrillic, and many other + alphabets. However, because at this time, the UTF-8 portion of the PKWare + specification is not broadly supported by other zip libraries and + utilities, such zip files may not be readable by your favorite zip tool or + archiver. In other words, interoperability will decrease if you set this + flag to true. + + + + In particular, Zip files created with strict adherence to the PKWare + specification with respect to UTF-8 encoding will not work well with + Explorer in Windows XP or Windows Vista, because Windows compressed + folders, as far as I know, do not support UTF-8 in zip files. Vista can + read the zip files, but shows the filenames incorrectly. Unpacking from + Windows Vista Explorer will result in filenames that have rubbish + characters in place of the high-order UTF-8 bytes. + + + + Also, zip files that use UTF-8 encoding will not work well with Java + applications that use the java.util.zip classes, as of v5.0 of the Java + runtime. The Java runtime does not correctly implement the PKWare + specification in this regard. + + + + As a result, we have the unfortunate situation that "correct" behavior by + the DotNetZip library with regard to Unicode encoding of filenames during + zip creation will result in zip files that are readable by strictly + compliant and current tools (for example the most recent release of the + commercial WinZip tool); but these zip files will not be readable by + various other tools or libraries, including Windows Explorer. + + + + The DotNetZip library can read and write zip files with UTF8-encoded + entries, according to the PKware spec. If you use DotNetZip for both + creating and reading the zip file, and you use UTF-8, there will be no + loss of information in the filenames. For example, using a self-extractor + created by this library will allow you to unpack files correctly with no + loss of information in the filenames. + + + + If you do not set this flag, it will remain false. If this flag is false, + the ZipOutputStream will encode all filenames and comments using + the IBM437 codepage. This can cause "loss of information" on some + filenames, but the resulting zipfile will be more interoperable with other + utilities. As an example of the loss of information, diacritics can be + lost. The o-tilde character will be down-coded to plain o. The c with a + cedilla (Unicode 0xE7) used in Portugese will be downcoded to a c. + Likewise, the O-stroke character (Unicode 248), used in Danish and + Norwegian, will be down-coded to plain o. Chinese characters cannot be + represented in codepage IBM437; when using the default encoding, Chinese + characters in filenames will be represented as ?. These are all examples + of "information loss". + + + + The loss of information associated to the use of the IBM437 encoding is + inconvenient, and can also lead to runtime errors. For example, using + IBM437, any sequence of 4 Chinese characters will be encoded as ????. If + your application creates a ZipOutputStream, does not set the + encoding, then adds two files, each with names of four Chinese characters + each, this will result in a duplicate filename exception. In the case + where you add a single file with a name containing four Chinese + characters, the zipfile will save properly, but extracting that file + later, with any zip tool, will result in an error, because the question + mark is not legal for use within filenames on Windows. These are just a + few examples of the problems associated to loss of information. + + + + This flag is independent of the encoding of the content within the entries + in the zip file. Think of the zip file as a container - it supports an + encoding. Within the container are other "containers" - the file entries + themselves. The encoding within those entries is independent of the + encoding of the zip archive container for those entries. + + + + Rather than specify the encoding in a binary fashion using this flag, an + application can specify an arbitrary encoding via the property. Setting the encoding + explicitly when creating zip archives will result in non-compliant zip + files that, curiously, are fairly interoperable. The challenge is, the + PKWare specification does not provide for a way to specify that an entry + in a zip archive uses a code page that is neither IBM437 nor UTF-8. + Therefore if you set the encoding explicitly when creating a zip archive, + you must take care upon reading the zip archive to use the same code page. + If you get it wrong, the behavior is undefined and may result in incorrect + filenames, exceptions, stomach upset, hair loss, and acne. + + + + + + + The text encoding to use when emitting entries into the zip archive, for + those entries whose filenames or comments cannot be encoded with the + default (IBM437) encoding. + + + + + In its + zip specification, PKWare describes two options for encoding + filenames and comments: using IBM437 or UTF-8. But, some archiving tools + or libraries do not follow the specification, and instead encode + characters using the system default code page. For example, WinRAR when + run on a machine in Shanghai may encode filenames with the Big-5 Chinese + (950) code page. This behavior is contrary to the Zip specification, but + it occurs anyway. + + + + When using DotNetZip to write zip archives that will be read by one of + these other archivers, set this property to specify the code page to use + when encoding the and for each ZipEntry in the zip file, for + values that cannot be encoded with the default codepage for zip files, + IBM437. This is why this property is "provisional". In all cases, IBM437 + is used where possible, in other words, where no loss of data would + result. It is possible, therefore, to have a given entry with a + Comment encoded in IBM437 and a FileName encoded with the + specified "provisional" codepage. + + + + Be aware that a zip file created after you've explicitly set the + ProvisionalAlternateEncoding property to a value other than + IBM437 may not be compliant to the PKWare specification, and may not be + readable by compliant archivers. On the other hand, many (most?) + archivers are non-compliant and can read zip files created in arbitrary + code pages. The trick is to use or specify the proper codepage when + reading the zip. + + + + When creating a zip archive using this library, it is possible to change + the value of ProvisionalAlternateEncoding between each entry you + add, and between adding entries and the call to Close(). Don't do + this. It will likely result in a zipfile that is not readable. For best + interoperability, either leave ProvisionalAlternateEncoding + alone, or specify it only once, before adding any entries to the + ZipOutputStream instance. There is one exception to this + recommendation, described later. + + + + When using an arbitrary, non-UTF8 code page for encoding, there is no + standard way for the creator application - whether DotNetZip, WinZip, + WinRar, or something else - to formally specify in the zip file which + codepage has been used for the entries. As a result, readers of zip files + are not able to inspect the zip file and determine the codepage that was + used for the entries contained within it. It is left to the application + or user to determine the necessary codepage when reading zip files encoded + this way. If you use an incorrect codepage when reading a zipfile, you + will get entries with filenames that are incorrect, and the incorrect + filenames may even contain characters that are not legal for use within + filenames in Windows. Extracting entries with illegal characters in the + filenames will lead to exceptions. It's too bad, but this is just the way + things are with code pages in zip files. Caveat Emptor. + + + + One possible approach for specifying the code page for a given zip file is + to describe the code page in a human-readable form in the Zip comment. For + example, the comment may read "Entries in this archive are encoded in the + Big5 code page". For maximum interoperability, the zip comment in this + case should be encoded in the default, IBM437 code page. In this case, + the zip comment is encoded using a different page than the filenames. To + do this, Specify ProvisionalAlternateEncoding to your desired + region-specific code page, once before adding any entries, and then set + the property and reset + ProvisionalAlternateEncoding to IBM437 before calling Close(). + + + + + + A Text Encoding to use when encoding the filenames and comments for + all the ZipEntry items, during a ZipFile.Save() operation. + + + + Whether the encoding specified here is used during the save depends + on . + + + + + + A flag that tells if and when this instance should apply + AlternateEncoding to encode the filenames and comments associated to + of ZipEntry objects contained within this instance. + + + + + The default text encoding used in zip archives. It is numeric 437, also + known as IBM437. + + + + + + The size threshold for an entry, above which a parallel deflate is used. + + + + + + DotNetZip will use multiple threads to compress any ZipEntry, when + the CompressionMethod is Deflate, and if the entry is + larger than the given size. Zero means "always use parallel + deflate", while -1 means "never use parallel deflate". + + + + If the entry size cannot be known before compression, as with any entry + added via a ZipOutputStream, then Parallel deflate will never be + performed, unless the value of this property is zero. + + + + A parallel deflate operations will speed up the compression of + large files, on computers with multiple CPUs or multiple CPU + cores. For files above 1mb, on a dual core or dual-cpu (2p) + machine, the time required to compress the file can be 70% of the + single-threaded deflate. For very large files on 4p machines the + compression can be done in 30% of the normal time. The downside + is that parallel deflate consumes extra memory during the deflate, + and the deflation is slightly less effective. + + + + Parallel deflate tends to not be as effective as single-threaded deflate + because the original data stream is split into multiple independent + buffers, each of which is compressed in parallel. But because they are + treated independently, there is no opportunity to share compression + dictionaries, and additional framing bytes must be added to the output + stream. For that reason, a deflated stream may be slightly larger when + compressed using parallel deflate, as compared to a traditional + single-threaded deflate. For files of about 512k, the increase over the + normal deflate is as much as 5% of the total compressed size. For larger + files, the difference can be as small as 0.1%. + + + + Multi-threaded compression does not give as much an advantage when using + Encryption. This is primarily because encryption tends to slow down + the entire pipeline. Also, multi-threaded compression gives less of an + advantage when using lower compression levels, for example . You may have to perform + some tests to determine the best approach for your situation. + + + + The default value for this property is -1, which means parallel + compression will not be performed unless you set it to zero. + + + + + + + The maximum number of buffer pairs to use when performing + parallel compression. + + + + + This property sets an upper limit on the number of memory + buffer pairs to create when performing parallel + compression. The implementation of the parallel + compression stream allocates multiple buffers to + facilitate parallel compression. As each buffer fills up, + the stream uses + ThreadPool.QueueUserWorkItem() to compress those + buffers in a background threadpool thread. After a buffer + is compressed, it is re-ordered and written to the output + stream. + + + + A higher number of buffer pairs enables a higher degree of + parallelism, which tends to increase the speed of compression on + multi-cpu computers. On the other hand, a higher number of buffer + pairs also implies a larger memory consumption, more active worker + threads, and a higher cpu utilization for any compression. This + property enables the application to limit its memory consumption and + CPU utilization behavior depending on requirements. + + + + For each compression "task" that occurs in parallel, there are 2 + buffers allocated: one for input and one for output. This property + sets a limit for the number of pairs. The total amount of storage + space allocated for buffering will then be (N*S*2), where N is the + number of buffer pairs, S is the size of each buffer (). By default, DotNetZip allocates 4 buffer + pairs per CPU core, so if your machine has 4 cores, and you retain + the default buffer size of 128k, then the + ParallelDeflateOutputStream will use 4 * 4 * 2 * 128kb of buffer + memory in total, or 4mb, in blocks of 128kb. If you then set this + property to 8, then the number will be 8 * 2 * 128kb of buffer + memory, or 2mb. + + + + CPU utilization will also go up with additional buffers, because a + larger number of buffer pairs allows a larger number of background + threads to compress in parallel. If you find that parallel + compression is consuming too much memory or CPU, you can adjust this + value downward. + + + + The default value is 16. Different values may deliver better or + worse results, depending on your priorities and the dynamic + performance characteristics of your storage and compute resources. + + + + This property is not the number of buffer pairs to use; it is an + upper limit. An illustration: Suppose you have an application that + uses the default value of this property (which is 16), and it runs + on a machine with 2 CPU cores. In that case, DotNetZip will allocate + 4 buffer pairs per CPU core, for a total of 8 pairs. The upper + limit specified by this property has no effect. + + + + The application can set this value at any time, but it is + effective only if set before calling + ZipOutputStream.Write() for the first time. + + + + + + + + + Always returns false. + + + + + Always returns false. + + + + + Always returns true. + + + + + Always returns a NotSupportedException. + + + + + Setting this property always returns a NotSupportedException. Getting it + returns the value of the Position on the underlying stream. + + + + + Provides a stream metaphor for reading zip files. + + + + + This class provides an alternative programming model for reading zip files to + the one enabled by the class. Use this when reading zip + files, as an alternative to the class, when you would + like to use a Stream class to read the file. + + + + Some application designs require a readable stream for input. This stream can + be used to read a zip file, and extract entries. + + + + Both the ZipInputStream class and the ZipFile class can be used + to read and extract zip files. Both of them support many of the common zip + features, including Unicode, different compression levels, and ZIP64. The + programming models differ. For example, when extracting entries via calls to + the GetNextEntry() and Read() methods on the + ZipInputStream class, the caller is responsible for creating the file, + writing the bytes into the file, setting the attributes on the file, and + setting the created, last modified, and last accessed timestamps on the + file. All of these things are done automatically by a call to ZipEntry.Extract(). For this reason, the + ZipInputStream is generally recommended for when your application wants + to extract the data, without storing that data into a file. + + + + Aside from the obvious differences in programming model, there are some + differences in capability between the ZipFile class and the + ZipInputStream class. + + + + + ZipFile can be used to create or update zip files, or read and + extract zip files. ZipInputStream can be used only to read and + extract zip files. If you want to use a stream to create zip files, check + out the . + + + + ZipInputStream cannot read segmented or spanned + zip files. + + + + ZipInputStream will not read Zip file comments. + + + + When reading larger files, ZipInputStream will always underperform + ZipFile. This is because the ZipInputStream does a full scan on the + zip file, while the ZipFile class reads the central directory of the + zip file. + + + + + + + + + Create a ZipInputStream, wrapping it around an existing stream. + + + + + + While the class is generally easier + to use, this class provides an alternative to those + applications that want to read from a zipfile directly, + using a . + + + + Both the ZipInputStream class and the ZipFile class can be used + to read and extract zip files. Both of them support many of the common zip + features, including Unicode, different compression levels, and ZIP64. The + programming models differ. For example, when extracting entries via calls to + the GetNextEntry() and Read() methods on the + ZipInputStream class, the caller is responsible for creating the file, + writing the bytes into the file, setting the attributes on the file, and + setting the created, last modified, and last accessed timestamps on the + file. All of these things are done automatically by a call to ZipEntry.Extract(). For this reason, the + ZipInputStream is generally recommended for when your application wants + to extract the data, without storing that data into a file. + + + + Aside from the obvious differences in programming model, there are some + differences in capability between the ZipFile class and the + ZipInputStream class. + + + + + ZipFile can be used to create or update zip files, or read and extract + zip files. ZipInputStream can be used only to read and extract zip + files. If you want to use a stream to create zip files, check out the . + + + + ZipInputStream cannot read segmented or spanned + zip files. + + + + ZipInputStream will not read Zip file comments. + + + + When reading larger files, ZipInputStream will always underperform + ZipFile. This is because the ZipInputStream does a full scan on the + zip file, while the ZipFile class reads the central directory of the + zip file. + + + + + + + + The stream to read. It must be readable. This stream will be closed at + the time the ZipInputStream is closed. + + + + + This example shows how to read a zip file, and extract entries, using the + ZipInputStream class. + + + private void Unzip() + { + byte[] buffer= new byte[2048]; + int n; + using (var raw = File.Open(inputFileName, FileMode.Open, FileAccess.Read)) + { + using (var input= new ZipInputStream(raw)) + { + ZipEntry e; + while (( e = input.GetNextEntry()) != null) + { + if (e.IsDirectory) continue; + string outputPath = Path.Combine(extractDir, e.FileName); + using (var output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite)) + { + while ((n= input.Read(buffer, 0, buffer.Length)) > 0) + { + output.Write(buffer,0,n); + } + } + } + } + } + } + + + + Private Sub UnZip() + Dim inputFileName As String = "MyArchive.zip" + Dim extractDir As String = "extract" + Dim buffer As Byte() = New Byte(2048) {} + Using raw As FileStream = File.Open(inputFileName, FileMode.Open, FileAccess.Read) + Using input As ZipInputStream = New ZipInputStream(raw) + Dim e As ZipEntry + Do While (Not e = input.GetNextEntry Is Nothing) + If Not e.IsDirectory Then + Using output As FileStream = File.Open(Path.Combine(extractDir, e.FileName), _ + FileMode.Create, FileAccess.ReadWrite) + Dim n As Integer + Do While (n = input.Read(buffer, 0, buffer.Length) > 0) + output.Write(buffer, 0, n) + Loop + End Using + End If + Loop + End Using + End Using + End Sub + + + + + + Create a ZipInputStream, given the name of an existing zip file. + + + + + + This constructor opens a FileStream for the given zipfile, and + wraps a ZipInputStream around that. See the documentation for the + constructor for full details. + + + + While the class is generally easier + to use, this class provides an alternative to those + applications that want to read from a zipfile directly, + using a . + + + + + + The name of the filesystem file to read. + + + + + This example shows how to read a zip file, and extract entries, using the + ZipInputStream class. + + + private void Unzip() + { + byte[] buffer= new byte[2048]; + int n; + using (var input= new ZipInputStream(inputFileName)) + { + ZipEntry e; + while (( e = input.GetNextEntry()) != null) + { + if (e.IsDirectory) continue; + string outputPath = Path.Combine(extractDir, e.FileName); + using (var output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite)) + { + while ((n= input.Read(buffer, 0, buffer.Length)) > 0) + { + output.Write(buffer,0,n); + } + } + } + } + } + + + + Private Sub UnZip() + Dim inputFileName As String = "MyArchive.zip" + Dim extractDir As String = "extract" + Dim buffer As Byte() = New Byte(2048) {} + Using input As ZipInputStream = New ZipInputStream(inputFileName) + Dim e As ZipEntry + Do While (Not e = input.GetNextEntry Is Nothing) + If Not e.IsDirectory Then + Using output As FileStream = File.Open(Path.Combine(extractDir, e.FileName), _ + FileMode.Create, FileAccess.ReadWrite) + Dim n As Integer + Do While (n = input.Read(buffer, 0, buffer.Length) > 0) + output.Write(buffer, 0, n) + Loop + End Using + End If + Loop + End Using + End Sub + + + + + + Create a ZipInputStream, explicitly specifying whether to + keep the underlying stream open. + + + + See the documentation for the ZipInputStream(Stream) + constructor for a discussion of the class, and an example of how to use the class. + + + + The stream to read from. It must be readable. + + + + true if the application would like the stream + to remain open after the ZipInputStream has been closed. + + + + Provides a string representation of the instance. + + + This can be useful for debugging purposes. + + + a string representation of the instance. + + + + Read the data from the stream into the buffer. + + + + + The data for the zipentry will be decrypted and uncompressed, as + necessary, before being copied into the buffer. + + + + You must set the property before calling + Read() the first time for an encrypted entry. To determine if an + entry is encrypted and requires a password, check the ZipEntry.Encryption property. + + + + The buffer to hold the data read from the stream. + the offset within the buffer to copy the first byte read. + the number of bytes to read. + the number of bytes read, after decryption and decompression. + + + + Read the next entry from the zip file. + + + + + Call this method just before calling , + to position the pointer in the zip file to the next entry that can be + read. Subsequent calls to Read(), will decrypt and decompress the + data in the zip file, until Read() returns 0. + + + + Each time you call GetNextEntry(), the pointer in the wrapped + stream is moved to the next entry in the zip file. If you call , and thus re-position the pointer within + the file, you will need to call GetNextEntry() again, to insure + that the file pointer is positioned at the beginning of a zip entry. + + + + This method returns the ZipEntry. Using a stream approach, you will + read the raw bytes for an entry in a zip file via calls to Read(). + Alternatively, you can extract an entry into a file, or a stream, by + calling , or one of its siblings. + + + + + + The ZipEntry read. Returns null (or Nothing in VB) if there are no more + entries in the zip file. + + + + + + Dispose the stream. + + + + + This method disposes the ZipInputStream. It may also close the + underlying stream, depending on which constructor was used. + + + + Typically the application will call Dispose() implicitly, via + a using statement in C#, or a Using statement in VB. + + + + Application code won't call this code directly. This method may + be invoked in two distinct scenarios. If disposing == true, the + method has been called directly or indirectly by a user's code, + for example via the public Dispose() method. In this case, both + managed and unmanaged resources can be referenced and disposed. + If disposing == false, the method has been called by the runtime + from inside the object finalizer and this method should not + reference other objects; in that case only unmanaged resources + must be referenced or disposed. + + + + + true if the Dispose method was invoked by user code. + + + + + This is a no-op. + + + + + This method always throws a NotSupportedException. + + ignored + ignored + ignored + + + + This method seeks in the underlying stream. + + + + + Call this method if you want to seek around within the zip file for random access. + + + + Applications can intermix calls to Seek() with calls to . After a call to Seek(), + GetNextEntry() will get the next ZipEntry that falls after + the current position in the input stream. You're on your own for finding + out just where to seek in the stream, to get to the various entries. + + + + + the offset point to seek to + the reference point from which to seek + The new position + + + + This method always throws a NotSupportedException. + + ignored + + + + The text encoding to use when reading entries into the zip archive, for + those entries whose filenames or comments cannot be encoded with the + default (IBM437) encoding. + + + + + In its + zip specification, PKWare describes two options for encoding + filenames and comments: using IBM437 or UTF-8. But, some archiving tools + or libraries do not follow the specification, and instead encode + characters using the system default code page. For example, WinRAR when + run on a machine in Shanghai may encode filenames with the Big-5 Chinese + (950) code page. This behavior is contrary to the Zip specification, but + it occurs anyway. + + + + When using DotNetZip to read zip archives that use something other than + UTF-8 or IBM437, set this property to specify the code page to use when + reading encoded filenames and comments for each ZipEntry in the zip + file. + + + + This property is "provisional". When the entry in the zip archive is not + explicitly marked as using UTF-8, then IBM437 is used to decode filenames + and comments. If a loss of data would result from using IBM436 - + specifically when encoding and decoding is not reflexive - the codepage + specified here is used. It is possible, therefore, to have a given entry + with a Comment encoded in IBM437 and a FileName encoded with + the specified "provisional" codepage. + + + + When a zip file uses an arbitrary, non-UTF8 code page for encoding, there + is no standard way for the reader application - whether DotNetZip, WinZip, + WinRar, or something else - to know which codepage has been used for the + entries. Readers of zip files are not able to inspect the zip file and + determine the codepage that was used for the entries contained within it. + It is left to the application or user to determine the necessary codepage + when reading zip files encoded this way. If you use an incorrect codepage + when reading a zipfile, you will get entries with filenames that are + incorrect, and the incorrect filenames may even contain characters that + are not legal for use within filenames in Windows. Extracting entries with + illegal characters in the filenames will lead to exceptions. It's too bad, + but this is just the way things are with code pages in zip files. Caveat + Emptor. + + + + + + + Size of the work buffer to use for the ZLIB codec during decompression. + + + + Setting this affects the performance and memory efficiency of compression + and decompression. For larger files, setting this to a larger size may + improve performance, but the exact numbers vary depending on available + memory, and a bunch of other variables. I don't have good firm + recommendations on how to set it. You'll have to test it yourself. Or + just leave it alone and accept the default. + + + + + Sets the password to be used on the ZipInputStream instance. + + + + + + When reading a zip archive, this password is used to read and decrypt the + entries that are encrypted within the zip file. When entries within a zip + file use different passwords, set the appropriate password for the entry + before the first call to Read() for each entry. + + + + When reading an entry that is not encrypted, the value of this property is + ignored. + + + + + + + This example uses the ZipInputStream to read and extract entries from a + zip file, using a potentially different password for each entry. + + + byte[] buffer= new byte[2048]; + int n; + using (var raw = File.Open(_inputFileName, FileMode.Open, FileAccess.Read )) + { + using (var input= new ZipInputStream(raw)) + { + ZipEntry e; + while (( e = input.GetNextEntry()) != null) + { + input.Password = PasswordForEntry(e.FileName); + if (e.IsDirectory) continue; + string outputPath = Path.Combine(_extractDir, e.FileName); + using (var output = File.Open(outputPath, FileMode.Create, FileAccess.ReadWrite)) + { + while ((n= input.Read(buffer,0,buffer.Length)) > 0) + { + output.Write(buffer,0,n); + } + } + } + } + } + + + + + + + Always returns true. + + + + + Returns the value of CanSeek for the underlying (wrapped) stream. + + + + + Always returns false. + + + + + Returns the length of the underlying stream. + + + + + Gets or sets the position of the underlying stream. + + + Setting the position is equivalent to calling Seek(value, SeekOrigin.Begin). + + + + + Sort-of like a factory method, ForUpdate is used only when + the application needs to update the zip entry metadata for + a segmented zip file, when the starting segment is earlier + than the ending segment, for a particular entry. + + + + The update is always contiguous, never rolls over. As a + result, this method doesn't need to return a ZSS; it can + simply return a FileStream. That's why it's "sort of" + like a Factory method. + + + Caller must Close/Dispose the stream object returned by + this method. + + + + + + Read from the stream + + the buffer to read + the offset at which to start + the number of bytes to read + the number of bytes actually read + + + + Write to the stream. + + the buffer from which to write + the offset at which to start writing + the number of bytes to write + + + + Name of the filesystem file corresponding to the current segment. + + + + The name is not always the name currently being used in the + filesystem. When rwMode is RwMode.Write, the filesystem file has a + temporary name until the stream is closed or until the next segment is + started. + + + + + + This class exposes a set of COM-accessible wrappers for static + methods available on the ZipFile class. You don't need this + class unless you are using DotNetZip from a COM environment. + + + + + A wrapper for ZipFile.IsZipFile(string) + + The filename to of the zip file to check. + true if the file contains a valid zip file. + + + + A wrapper for ZipFile.IsZipFile(string, bool) + + + We cannot use "overloaded" Method names in COM interop. + So, here, we use a unique name. + + The filename to of the zip file to check. + true if the file contains a valid zip file. + + + + A wrapper for ZipFile.CheckZip(string) + + The filename to of the zip file to check. + + true if the named zip file checks OK. Otherwise, false. + + + + A COM-friendly wrapper for the static method . + + + The filename to of the zip file to check. + + The password to check. + + true if the named zip file checks OK. Otherwise, false. + + + + A wrapper for ZipFile.FixZipDirectory(string) + + The filename to of the zip file to fix. + + + + A wrapper for ZipFile.LibraryVersion + + + the version number on the DotNetZip assembly, formatted as a string. + + + + + An enum providing the options when an error occurs during opening or reading + of a file or directory that is being saved to a zip file. + + + + + This enum describes the actions that the library can take when an error occurs + opening or reading a file, as it is being saved into a Zip archive. + + + + In some cases an error will occur when DotNetZip tries to open a file to be + added to the zip archive. In other cases, an error might occur after the + file has been successfully opened, while DotNetZip is reading the file. + + + + The first problem might occur when calling AddDirectory() on a directory + that contains a Clipper .dbf file; the file is locked by Clipper and + cannot be opened by another process. An example of the second problem is + the ERROR_LOCK_VIOLATION that results when a file is opened by another + process, but not locked, and a range lock has been taken on the file. + Microsoft Outlook takes range locks on .PST files. + + + + + + Throw an exception when an error occurs while zipping. This is the default + behavior. (For COM clients, this is a 0 (zero).) + + + + + When an error occurs during zipping, for example a file cannot be opened, + skip the file causing the error, and continue zipping. (For COM clients, + this is a 1.) + + + + + When an error occurs during zipping, for example a file cannot be opened, + retry the operation that caused the error. Be careful with this option. If + the error is not temporary, the library will retry forever. (For COM + clients, this is a 2.) + + + + + When an error occurs, invoke the zipError event. The event type used is + . A typical use of this option: + a GUI application may wish to pop up a dialog to allow the user to view the + error that occurred, and choose an appropriate action. After your + processing in the error event, if you want to skip the file, set on the + ZipProgressEventArgs.CurrentEntry to Skip. If you want the + exception to be thrown, set ZipErrorAction on the CurrentEntry + to Throw. If you want to cancel the zip, set + ZipProgressEventArgs.Cancel to true. Cancelling differs from using + Skip in that a cancel will not save any further entries, if there are any. + (For COM clients, the value of this enum is a 3.) + + + + + An enum that provides the various encryption algorithms supported by this + library. + + + + + + PkzipWeak implies the use of Zip 2.0 encryption, which is known to be + weak and subvertible. + + + + A note on interoperability: Values of PkzipWeak and None are + specified in PKWARE's zip + specification, and are considered to be "standard". Zip archives + produced using these options will be interoperable with many other zip tools + and libraries, including Windows Explorer. + + + + Values of WinZipAes128 and WinZipAes256 are not part of the Zip + specification, but rather imply the use of a vendor-specific extension from + WinZip. If you want to produce interoperable Zip archives, do not use these + values. For example, if you produce a zip archive using WinZipAes256, you + will be able to open it in Windows Explorer on Windows XP and Vista, but you + will not be able to extract entries; trying this will lead to an "unspecified + error". For this reason, some people have said that a zip archive that uses + WinZip's AES encryption is not actually a zip archive at all. A zip archive + produced this way will be readable with the WinZip tool (Version 11 and + beyond). + + + + There are other third-party tools and libraries, both commercial and + otherwise, that support WinZip's AES encryption. These will be able to read + AES-encrypted zip archives produced by DotNetZip, and conversely applications + that use DotNetZip to read zip archives will be able to read AES-encrypted + archives produced by those tools or libraries. Consult the documentation for + those other tools and libraries to find out if WinZip's AES encryption is + supported. + + + + In case you care: According to the WinZip specification, the + actual AES key used is derived from the via an + algorithm that complies with RFC 2898, using an iteration + count of 1000. The algorithm is sometimes referred to as PBKDF2, which stands + for "Password Based Key Derivation Function #2". + + + + A word about password strength and length: The AES encryption technology is + very good, but any system is only as secure as the weakest link. If you want + to secure your data, be sure to use a password that is hard to guess. To make + it harder to guess (increase its "entropy"), you should make it longer. If + you use normal characters from an ASCII keyboard, a password of length 20 will + be strong enough that it will be impossible to guess. For more information on + that, I'd encourage you to read this + article. + + + + The WinZip AES algorithms are not supported with the version of DotNetZip that + runs on the .NET Compact Framework. This is because .NET CF lacks the + HMACSHA1 class that is required for producing the archive. + + + + + + No encryption at all. + + + + + Traditional or Classic pkzip encryption. + + + + + WinZip AES encryption (128 key bits). + + + + + WinZip AES encryption (256 key bits). + + + + + An encryption algorithm that is not supported by DotNetZip. + + + + + An enum for the options when extracting an entry would overwrite an existing file. + + + + + This enum describes the actions that the library can take when an + Extract() or ExtractWithPassword() method is called to extract an + entry to a filesystem, and the extraction would overwrite an existing filesystem + file. + + + + + + + Throw an exception when extraction would overwrite an existing file. (For + COM clients, this is a 0 (zero).) + + + + + When extraction would overwrite an existing file, overwrite the file silently. + The overwrite will happen even if the target file is marked as read-only. + (For COM clients, this is a 1.) + + + + + When extraction would overwrite an existing file, don't overwrite the file, silently. + (For COM clients, this is a 2.) + + + + + When extraction would overwrite an existing file, invoke the ExtractProgress + event, using an event type of . In + this way, the application can decide, just-in-time, whether to overwrite the + file. For example, a GUI application may wish to pop up a dialog to allow + the user to choose. You may want to examine the property before making + the decision. If, after your processing in the Extract progress event, you + want to NOT extract the file, set + on the ZipProgressEventArgs.CurrentEntry to DoNotOverwrite. + If you do want to extract the file, set ZipEntry.ExtractExistingFile + to OverwriteSilently. If you want to cancel the Extraction, set + ZipProgressEventArgs.Cancel to true. Cancelling differs from using + DoNotOverwrite in that a cancel will not extract any further entries, if + there are any. (For COM clients, the value of this enum is a 3.) + + + + + Enumerates the options for a logical conjunction. This enum is intended for use + internally by the FileSelector class. + + + + + FileSelector encapsulates logic that selects files from a source - a zip file + or the filesystem - based on a set of criteria. This class is used internally + by the DotNetZip library, in particular for the AddSelectedFiles() methods. + This class can also be used independently of the zip capability in DotNetZip. + + + + + + The FileSelector class is used internally by the ZipFile class for selecting + files for inclusion into the ZipFile, when the method, or one of + its overloads, is called. It's also used for the methods. Typically, an + application that creates or manipulates Zip archives will not directly + interact with the FileSelector class. + + + + Some applications may wish to use the FileSelector class directly, to + select files from disk volumes based on a set of criteria, without creating or + querying Zip archives. The file selection criteria include: a pattern to + match the filename; the last modified, created, or last accessed time of the + file; the size of the file; and the attributes of the file. + + + + Consult the documentation for + for more information on specifying the selection criteria. + + + + + + + Constructor that allows the caller to specify file selection criteria. + + + + + This constructor allows the caller to specify a set of criteria for + selection of files. + + + + See for a description of + the syntax of the selectionCriteria string. + + + + By default the FileSelector will traverse NTFS Reparse Points. To + change this, use FileSelector(String, bool). + + + + The criteria for file selection. + + + + Constructor that allows the caller to specify file selection criteria. + + + + + This constructor allows the caller to specify a set of criteria for + selection of files. + + + + See for a description of + the syntax of the selectionCriteria string. + + + + The criteria for file selection. + + whether to traverse NTFS reparse points (junctions). + + + + + Returns a string representation of the FileSelector object. + + The string representation of the boolean logic statement of the file + selection criteria for this instance. + + + + Returns the names of the files in the specified directory + that fit the selection criteria specified in the FileSelector. + + + + This is equivalent to calling + with recurseDirectories = false. + + + + The name of the directory over which to apply the FileSelector + criteria. + + + + A collection of strings containing fully-qualified pathnames of files + that match the criteria specified in the FileSelector instance. + + + + + Returns the names of the files in the specified directory that fit the + selection criteria specified in the FileSelector, optionally recursing + through subdirectories. + + + + This method applies the file selection criteria contained in the + FileSelector to the files contained in the given directory, and + returns the names of files that conform to the criteria. + + + + The name of the directory over which to apply the FileSelector + criteria. + + + + Whether to recurse through subdirectories when applying the file + selection criteria. + + + + A collection of strings containing fully-qualified pathnames of files + that match the criteria specified in the FileSelector instance. + + + + + Retrieve the ZipEntry items in the ZipFile that conform to the specified criteria. + + + + + This method applies the criteria set in the FileSelector instance (as described in + the ) to the specified ZipFile. Using this + method, for example, you can retrieve all entries from the given ZipFile that + have filenames ending in .txt. + + + + Normally, applications would not call this method directly. This method is used + by the ZipFile class. + + + + Using the appropriate SelectionCriteria, you can retrieve entries based on size, + time, and attributes. See for a + description of the syntax of the SelectionCriteria string. + + + + + The ZipFile from which to retrieve entries. + + a collection of ZipEntry objects that conform to the criteria. + + + + Retrieve the ZipEntry items in the ZipFile that conform to the specified criteria. + + + + + This method applies the criteria set in the FileSelector instance (as described in + the ) to the specified ZipFile. Using this + method, for example, you can retrieve all entries from the given ZipFile that + have filenames ending in .txt. + + + + Normally, applications would not call this method directly. This method is used + by the ZipFile class. + + + + This overload allows the selection of ZipEntry instances from the ZipFile to be restricted + to entries contained within a particular directory in the ZipFile. + + + + Using the appropriate SelectionCriteria, you can retrieve entries based on size, + time, and attributes. See for a + description of the syntax of the SelectionCriteria string. + + + + + The ZipFile from which to retrieve entries. + + + the directory in the archive from which to select entries. If null, then + all directories in the archive are used. + + + a collection of ZipEntry objects that conform to the criteria. + + + + The string specifying which files to include when retrieving. + + + + + Specify the criteria in statements of 3 elements: a noun, an operator, + and a value. Consider the string "name != *.doc" . The noun is + "name". The operator is "!=", implying "Not Equal". The value is + "*.doc". That criterion, in English, says "all files with a name that + does not end in the .doc extension." + + + + Supported nouns include "name" (or "filename") for the filename; + "atime", "mtime", and "ctime" for last access time, last modfied time, + and created time of the file, respectively; "attributes" (or "attrs") + for the file attributes; "size" (or "length") for the file length + (uncompressed); and "type" for the type of object, either a file or a + directory. The "attributes", "type", and "name" nouns all support = + and != as operators. The "size", "atime", "mtime", and "ctime" nouns + support = and !=, and >, >=, <, <= as well. The times are + taken to be expressed in local time. + + + + Specify values for the file attributes as a string with one or more of + the characters H,R,S,A,I,L in any order, implying file attributes of + Hidden, ReadOnly, System, Archive, NotContextIndexed, and ReparsePoint + (symbolic link) respectively. + + + + To specify a time, use YYYY-MM-DD-HH:mm:ss or YYYY/MM/DD-HH:mm:ss as + the format. If you omit the HH:mm:ss portion, it is assumed to be + 00:00:00 (midnight). + + + + The value for a size criterion is expressed in integer quantities of + bytes, kilobytes (use k or kb after the number), megabytes (m or mb), + or gigabytes (g or gb). + + + + The value for a name is a pattern to match against the filename, + potentially including wildcards. The pattern follows CMD.exe glob + rules: * implies one or more of any character, while ? implies one + character. If the name pattern contains any slashes, it is matched to + the entire filename, including the path; otherwise, it is matched + against only the filename without the path. This means a pattern of + "*\*.*" matches all files one directory level deep, while a pattern of + "*.*" matches all files in all directories. + + + + To specify a name pattern that includes spaces, use single quotes + around the pattern. A pattern of "'* *.*'" will match all files that + have spaces in the filename. The full criteria string for that would + be "name = '* *.*'" . + + + + The value for a type criterion is either F (implying a file) or D + (implying a directory). + + + + Some examples: + + + + + criteria + Files retrieved + + + + name != *.xls + any file with an extension that is not .xls + + + + + name = *.mp3 + any file with a .mp3 extension. + + + + + *.mp3 + (same as above) any file with a .mp3 extension. + + + + + attributes = A + all files whose attributes include the Archive bit. + + + + + attributes != H + all files whose attributes do not include the Hidden bit. + + + + + mtime > 2009-01-01 + all files with a last modified time after January 1st, 2009. + + + + + ctime > 2009/01/01-03:00:00 + all files with a created time after 3am (local time), + on January 1st, 2009. + + + + + size > 2gb + all files whose uncompressed size is greater than 2gb. + + + + + type = D + all directories in the filesystem. + + + + + + You can combine criteria with the conjunctions AND, OR, and XOR. Using + a string like "name = *.txt AND size >= 100k" for the + selectionCriteria retrieves entries whose names end in .txt, and whose + uncompressed size is greater than or equal to 100 kilobytes. + + + + For more complex combinations of criteria, you can use parenthesis to + group clauses in the boolean logic. Absent parenthesis, the + precedence of the criterion atoms is determined by order of + appearance. Unlike the C# language, the AND conjunction does not take + precendence over the logical OR. This is important only in strings + that contain 3 or more criterion atoms. In other words, "name = *.txt + and size > 1000 or attributes = H" implies "((name = *.txt AND size + > 1000) OR attributes = H)" while "attributes = H OR name = *.txt + and size > 1000" evaluates to "((attributes = H OR name = *.txt) + AND size > 1000)". When in doubt, use parenthesis. + + + + Using time properties requires some extra care. If you want to + retrieve all entries that were last updated on 2009 February 14, + specify "mtime >= 2009-02-14 AND mtime < 2009-02-15". Read this + to say: all files updated after 12:00am on February 14th, until + 12:00am on February 15th. You can use the same bracketing approach to + specify any time period - a year, a month, a week, and so on. + + + + The syntax allows one special case: if you provide a string with no + spaces, it is treated as a pattern to match for the filename. + Therefore a string like "*.xls" will be equivalent to specifying "name + = *.xls". This "shorthand" notation does not work with compound + criteria. + + + + There is no logic in this class that insures that the inclusion + criteria are internally consistent. For example, it's possible to + specify criteria that says the file must have a size of less than 100 + bytes, as well as a size that is greater than 1000 bytes. Obviously + no file will ever satisfy such criteria, but this class does not check + for or detect such inconsistencies. + + + + + + Thrown in the setter if the value has an invalid syntax. + + + + + Indicates whether searches will traverse NTFS reparse points, like Junctions. + + + + + Summary description for EnumUtil. + + + + + Returns the value of the DescriptionAttribute if the specified Enum + value has one. If not, returns the ToString() representation of the + Enum value. + + The Enum to get the description for + + + + + Converts the string representation of the name or numeric value of one + or more enumerated constants to an equivalent enumerated object. + Note: use the DescriptionAttribute on enum values to enable this. + + The System.Type of the enumeration. + + A string containing the name or value to convert. + + + + + + Converts the string representation of the name or numeric value of one + or more enumerated constants to an equivalent enumerated object. A + parameter specified whether the operation is case-sensitive. Note: + use the DescriptionAttribute on enum values to enable this. + + The System.Type of the enumeration. + + A string containing the name or value to convert. + + + Whether the operation is case-sensitive or not. + + + + + This is a helper class supporting WinZip AES encryption. + This class is intended for use only by the DotNetZip library. + + + + Most uses of the DotNetZip library will not involve direct calls into + the WinZipAesCrypto class. Instead, the WinZipAesCrypto class is + instantiated and used by the ZipEntry() class when WinZip AES + encryption or decryption on an entry is employed. + + + + + A stream that encrypts as it writes, or decrypts as it reads. The + Crypto is AES in CTR (counter) mode, which is compatible with the AES + encryption employed by WinZip 12.0. + + + + The AES/CTR encryption protocol used by WinZip works like this: + + - start with a counter, initialized to zero. + + - to encrypt, take the data by 16-byte blocks. For each block: + - apply the transform to the counter + - increement the counter + - XOR the result of the transform with the plaintext to + get the ciphertext. + - compute the mac on the encrypted bytes + - when finished with all blocks, store the computed MAC. + + - to decrypt, take the data by 16-byte blocks. For each block: + - compute the mac on the encrypted bytes, + - apply the transform to the counter + - increement the counter + - XOR the result of the transform with the ciphertext to + get the plaintext. + - when finished with all blocks, compare the computed MAC against + the stored MAC + + + + + + + The constructor. + + The underlying stream + To either encrypt or decrypt. + The pre-initialized WinZipAesCrypto object. + The maximum number of bytes to read from the stream. + + + + Close the stream. + + + + + Flush the content in the stream. + + + + + This method throws a NotImplementedException. + + + + + This method throws a NotImplementedException. + + + + + Returns the final HMAC-SHA1-80 for the data that was encrypted. + + + + + Returns true if the stream can be read. + + + + + Always returns false. + + + + + Returns true if the CryptoMode is Encrypt. + + + + + Getting this property throws a NotImplementedException. + + + + + Getting or Setting this property throws a NotImplementedException. + + + + + Issued when an ZipEntry.ExtractWithPassword() method is invoked + with an incorrect password. + + + + + Base class for all exceptions defined by and throw by the Zip library. + + + + + Default ctor. + + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + The innerException for this exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The serialization info for the exception. + The streaming context from which to deserialize. + + + + Default ctor. + + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + The innerException for this exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The serialization info for the exception. + The streaming context from which to deserialize. + + + + Indicates that a read was attempted on a stream, and bad or incomplete data was + received. + + + + + Default ctor. + + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + The innerException for this exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The serialization info for the exception. + The streaming context from which to deserialize. + + + + Issued when an CRC check fails upon extracting an entry from a zip archive. + + + + + Default ctor. + + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The serialization info for the exception. + The streaming context from which to deserialize. + + + + Issued when errors occur saving a self-extracting archive. + + + + + Default ctor. + + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The serialization info for the exception. + The streaming context from which to deserialize. + + + + Indicates that an operation was attempted on a ZipFile which was not possible + given the state of the instance. For example, if you call Save() on a ZipFile + which has no filename set, you can get this exception. + + + + + Default ctor. + + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The message in the exception. + The innerException for this exception. + + + + Come on, you know how exceptions work. Why are you looking at this documentation? + + The serialization info for the exception. + The streaming context from which to deserialize. + + + + Collects general purpose utility methods. + + + + private null constructor + + + + Utility routine for transforming path names from filesystem format (on Windows that means backslashes) to + a format suitable for use within zipfiles. This means trimming the volume letter and colon (if any) And + swapping backslashes for forward slashes. + + source path. + transformed path + + + + Finds a signature in the zip stream. This is useful for finding + the end of a zip entry, for example, or the beginning of the next ZipEntry. + + + + + Scans through 64k at a time. + + + + If the method fails to find the requested signature, the stream Position + after completion of this method is unchanged. If the method succeeds in + finding the requested signature, the stream position after completion is + direct AFTER the signature found in the stream. + + + + The stream to search + The 4-byte signature to find + The number of bytes read + + + + Create a pseudo-random filename, suitable for use as a temporary + file, and open it. + + + + The System.IO.Path.GetRandomFileName() method is not available on + the Compact Framework, so this library provides its own substitute + on NETCF. + + + This method produces a filename of the form + DotNetZip-xxxxxxxx.tmp, where xxxxxxxx is replaced by randomly + chosen characters, and creates that file. + + + + + + Workitem 7889: handle ERROR_LOCK_VIOLATION during read + + + This could be gracefully handled with an extension attribute, but + This assembly is built for .NET 2.0, so I cannot use them. + + + + + A decorator stream. It wraps another stream, and performs bookkeeping + to keep track of the stream Position. + + + + In some cases, it is not possible to get the Position of a stream, let's + say, on a write-only output stream like ASP.NET's + Response.OutputStream, or on a different write-only stream + provided as the destination for the zip by the application. In this + case, programmers can use this counting stream to count the bytes read + or written. + + + Consider the scenario of an application that saves a self-extracting + archive (SFX), that uses a custom SFX stub. + + + Saving to a filesystem file, the application would open the + filesystem file (getting a FileStream), save the custom sfx stub + into it, and then call ZipFile.Save(), specifying the same + FileStream. ZipFile.Save() does the right thing for the zipentry + offsets, by inquiring the Position of the FileStream before writing + any data, and then adding that initial offset into any ZipEntry + offsets in the zip directory. Everything works fine. + + + Now suppose the application is an ASPNET application and it saves + directly to Response.OutputStream. It's not possible for DotNetZip to + inquire the Position, so the offsets for the SFX will be wrong. + + + The workaround is for the application to use this class to wrap + HttpResponse.OutputStream, then write the SFX stub and the ZipFile + into that wrapper stream. Because ZipFile.Save() can inquire the + Position, it will then do the right thing with the offsets. + + + + + + The constructor. + + The underlying stream + + + + Adjust the byte count on the stream. + + + + the number of bytes to subtract from the count. + + + + + Subtract delta from the count of bytes written to the stream. + This is necessary when seeking back, and writing additional data, + as happens in some cases when saving Zip files. + + + + + + The read method. + + The buffer to hold the data read from the stream. + the offset within the buffer to copy the first byte read. + the number of bytes to read. + the number of bytes read, after decryption and decompression. + + + + Write data into the stream. + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Flushes the underlying stream. + + + + + Seek in the stream. + + the offset point to seek to + the reference point from which to seek + The new position + + + + Set the length of the underlying stream. Be careful with this! + + + the length to set on the underlying stream. + + + + Gets the wrapped stream. + + + + + The count of bytes written out to the stream. + + + + + the count of bytes that have been read from the stream. + + + + + Whether the stream can be read. + + + + + Whether it is possible to call Seek() on the stream. + + + + + Whether it is possible to call Write() on the stream. + + + + + The length of the underlying stream. + + + + + Returns the sum of number of bytes written, plus the initial + offset before writing. + + + + + The Position of the stream. + + + + + This class implements the "traditional" or "classic" PKZip encryption, + which today is considered to be weak. On the other hand it is + ubiquitous. This class is intended for use only by the DotNetZip + library. + + + + Most uses of the DotNetZip library will not involve direct calls into + the ZipCrypto class. Instead, the ZipCrypto class is instantiated and + used by the ZipEntry() class when encryption or decryption on an entry + is employed. If for some reason you really wanted to use a weak + encryption algorithm in some other application, you might use this + library. But you would be much better off using one of the built-in + strong encryption libraries in the .NET Framework, like the AES + algorithm or SHA. + + + + + The default constructor for ZipCrypto. + + + + This class is intended for internal use by the library only. It's + probably not useful to you. Seriously. Stop reading this + documentation. It's a waste of your time. Go do something else. + Check the football scores. Go get an ice cream with a friend. + Seriously. + + + + + + Call this method on a cipher text to render the plaintext. You must + first initialize the cipher with a call to InitCipher. + + + + + var cipher = new ZipCrypto(); + cipher.InitCipher(Password); + // Decrypt the header. This has a side effect of "further initializing the + // encryption keys" in the traditional zip encryption. + byte[] DecryptedMessage = cipher.DecryptMessage(EncryptedMessage); + + + + The encrypted buffer. + + The number of bytes to encrypt. + Should be less than or equal to CipherText.Length. + + + The plaintext. + + + + This is the converse of DecryptMessage. It encrypts the plaintext + and produces a ciphertext. + + + The plain text buffer. + + + The number of bytes to encrypt. + Should be less than or equal to plainText.Length. + + + The ciphertext. + + + + This initializes the cipher with the given password. + See AppNote.txt for details. + + + + The passphrase for encrypting or decrypting with this cipher. + + + + + Step 1 - Initializing the encryption keys + ----------------------------------------- + Start with these keys: + Key(0) := 305419896 (0x12345678) + Key(1) := 591751049 (0x23456789) + Key(2) := 878082192 (0x34567890) + + Then, initialize the keys with a password: + + loop for i from 0 to length(password)-1 + update_keys(password(i)) + end loop + + Where update_keys() is defined as: + + update_keys(char): + Key(0) := crc32(key(0),char) + Key(1) := Key(1) + (Key(0) bitwiseAND 000000ffH) + Key(1) := Key(1) * 134775813 + 1 + Key(2) := crc32(key(2),key(1) rightshift 24) + end update_keys + + Where crc32(old_crc,char) is a routine that given a CRC value and a + character, returns an updated CRC value after applying the CRC-32 + algorithm described elsewhere in this document. + + + + + After the keys are initialized, then you can use the cipher to + encrypt the plaintext. + + + + Essentially we encrypt the password with the keys, then discard the + ciphertext for the password. This initializes the keys for later use. + + + + + + + From AppNote.txt: + unsigned char decrypt_byte() + local unsigned short temp + temp :=- Key(2) | 2 + decrypt_byte := (temp * (temp ^ 1)) bitshift-right 8 + end decrypt_byte + + + + + A Stream for reading and concurrently decrypting data from a zip file, + or for writing and concurrently encrypting data to a zip file. + + + + The constructor. + The underlying stream + To either encrypt or decrypt. + The pre-initialized ZipCrypto object. + + + + Delegate in which the application writes the ZipEntry content for the named entry. + + + The name of the entry that must be written. + The stream to which the entry data should be written. + + + When you add an entry and specify a WriteDelegate, via , the application + code provides the logic that writes the entry data directly into the zip file. + + + + + This example shows how to define a WriteDelegate that obtains a DataSet, and then + writes the XML for the DataSet into the zip archive. There's no need to + save the XML to a disk file first. + + + private void WriteEntry (String filename, Stream output) + { + DataSet ds1 = ObtainDataSet(); + ds1.WriteXml(output); + } + + private void Run() + { + using (var zip = new ZipFile()) + { + zip.AddEntry(zipEntryName, WriteEntry); + zip.Save(zipFileName); + } + } + + + + Private Sub WriteEntry (ByVal filename As String, ByVal output As Stream) + DataSet ds1 = ObtainDataSet() + ds1.WriteXml(stream) + End Sub + + Public Sub Run() + Using zip = New ZipFile + zip.AddEntry(zipEntryName, New WriteDelegate(AddressOf WriteEntry)) + zip.Save(zipFileName) + End Using + End Sub + + + + + + + Delegate in which the application opens the stream, just-in-time, for the named entry. + + + + The name of the ZipEntry that the application should open the stream for. + + + + When you add an entry via , the application code provides the logic that + opens and closes the stream for the given ZipEntry. + + + + + + + Delegate in which the application closes the stream, just-in-time, for the named entry. + + + + The name of the ZipEntry that the application should close the stream for. + + + The stream to be closed. + + + When you add an entry via , the application code provides the logic that + opens and closes the stream for the given ZipEntry. + + + + + + + Delegate for the callback by which the application tells the + library the CompressionLevel to use for a file. + + + + + Using this callback, the application can, for example, specify that + previously-compressed files (.mp3, .png, .docx, etc) should use a + CompressionLevel of None, or can set the compression level based + on any other factor. + + + + + + + In an EventArgs type, indicates which sort of progress event is being + reported. + + + There are events for reading, events for saving, and events for + extracting. This enumeration allows a single EventArgs type to be sued to + describe one of multiple subevents. For example, a SaveProgress event is + invoked before, after, and during the saving of a single entry. The value + of an enum with this type, specifies which event is being triggered. The + same applies to Extraction, Reading and Adding events. + + + + + Indicates that a Add() operation has started. + + + + + Indicates that an individual entry in the archive has been added. + + + + + Indicates that a Add() operation has completed. + + + + + Indicates that a Read() operation has started. + + + + + Indicates that an individual entry in the archive is about to be read. + + + + + Indicates that an individual entry in the archive has just been read. + + + + + Indicates that a Read() operation has completed. + + + + + The given event reports the number of bytes read so far + during a Read() operation. + + + + + Indicates that a Save() operation has started. + + + + + Indicates that an individual entry in the archive is about to be written. + + + + + Indicates that an individual entry in the archive has just been saved. + + + + + Indicates that a Save() operation has completed. + + + + + Indicates that the zip archive has been created in a + temporary location during a Save() operation. + + + + + Indicates that the temporary file is about to be renamed to the final archive + name during a Save() operation. + + + + + Indicates that the temporary file is has just been renamed to the final archive + name during a Save() operation. + + + + + Indicates that the self-extracting archive has been compiled + during a Save() operation. + + + + + The given event is reporting the number of source bytes that have run through the compressor so far + during a Save() operation. + + + + + Indicates that an entry is about to be extracted. + + + + + Indicates that an entry has just been extracted. + + + + + Indicates that extraction of an entry would overwrite an existing + filesystem file. You must use + + ExtractExistingFileAction.InvokeExtractProgressEvent in the call + to ZipEntry.Extract() in order to receive this event. + + + + + The given event is reporting the number of bytes written so far for + the current entry during an Extract() operation. + + + + + Indicates that an ExtractAll operation is about to begin. + + + + + Indicates that an ExtractAll operation has completed. + + + + + Indicates that an error has occurred while saving a zip file. + This generally means the file cannot be opened, because it has been + removed, or because it is locked by another process. It can also + mean that the file cannot be Read, because of a range lock conflict. + + + + + Provides information about the progress of a save, read, or extract operation. + This is a base class; you will probably use one of the classes derived from this one. + + + + + The total number of entries to be saved or extracted. + + + + + The name of the last entry saved or extracted. + + + + + In an event handler, set this to cancel the save or extract + operation that is in progress. + + + + + The type of event being reported. + + + + + Returns the archive name associated to this event. + + + + + The number of bytes read or written so far for this entry. + + + + + Total number of bytes that will be read or written for this entry. + This number will be -1 if the value cannot be determined. + + + + + Provides information about the progress of a Read operation. + + + + + Provides information about the progress of a Add operation. + + + + + Provides information about the progress of a save operation. + + + + + Constructor for the SaveProgressEventArgs. + + the name of the zip archive. + whether this is before saving the entry, or after + The total number of entries in the zip archive. + Number of entries that have been saved. + The entry involved in the event. + + + + Number of entries saved so far. + + + + + Provides information about the progress of the extract operation. + + + + + Constructor for the ExtractProgressEventArgs. + + the name of the zip archive. + whether this is before saving the entry, or after + The total number of entries in the zip archive. + Number of entries that have been extracted. + The entry involved in the event. + The location to which entries are extracted. + + + + Number of entries extracted so far. This is set only if the + EventType is Extracting_BeforeExtractEntry or Extracting_AfterExtractEntry, and + the Extract() is occurring witin the scope of a call to ExtractAll(). + + + + + Returns the extraction target location, a filesystem path. + + + + + Provides information about the an error that occurred while zipping. + + + + + Returns the exception that occurred, if any. + + + + + Returns the name of the file that caused the exception, if any. + + + + + Represents a single entry in a ZipFile. Typically, applications get a ZipEntry + by enumerating the entries within a ZipFile, or by adding an entry to a ZipFile. + + + + + Reads one entry from the zip directory structure in the zip file. + + + + The zipfile for which a directory entry will be read. From this param, the + method gets the ReadStream and the expected text encoding + (ProvisionalAlternateEncoding) which is used if the entry is not marked + UTF-8. + + + + a list of previously seen entry names; used to prevent duplicates. + + + the entry read from the archive. + + + + Returns true if the passed-in value is a valid signature for a ZipDirEntry. + + the candidate 4-byte signature value. + true, if the signature is valid according to the PKWare spec. + + + + Default constructor. + + + Applications should never need to call this directly. It is exposed to + support COM Automation environments. + + + + + Sets the NTFS Creation, Access, and Modified times for the given entry. + + + + + When adding an entry from a file or directory, the Creation, Access, and + Modified times for the given entry are automatically set from the + filesystem values. When adding an entry from a stream or string, the + values are implicitly set to DateTime.Now. The application may wish to + set these values to some arbitrary value, before saving the archive, and + can do so using the various setters. If you want to set all of the times, + this method is more efficient. + + + + The values you set here will be retrievable with the , and properties. + + + + When this method is called, if both and are false, then the + EmitTimesInWindowsFormatWhenSaving flag is automatically set. + + + + DateTime values provided here without a DateTimeKind are assumed to be Local Time. + + + + the creation time of the entry. + the last access time of the entry. + the last modified time of the entry. + + + + + + + + + Provides a string representation of the instance. + a string representation of the instance. + + + + Extract the entry to the filesystem, starting at the current + working directory. + + + + This method has a bunch of overloads! One of them is sure to + be the right one for you... If you don't like these, check + out the ExtractWithPassword() methods. + + + + + + + + + This method extracts an entry from a zip file into the current + working directory. The path of the entry as extracted is the full + path as specified in the zip archive, relative to the current + working directory. After the file is extracted successfully, the + file attributes and timestamps are set. + + + + The action taken when extraction an entry would overwrite an + existing file is determined by the property. + + + + Within the call to Extract(), the content for the entry is + written into a filesystem file, and then the last modified time of the + file is set according to the property on + the entry. See the remarks the property for + some details about the last modified time. + + + + + + + Extract the entry to a file in the filesystem, using the specified + behavior when extraction would overwrite an existing file. + + + + + See the remarks on the property, for some + details about how the last modified time of the file is set after + extraction. + + + + + The action to take if extraction would overwrite an existing file. + + + + + Extracts the entry to the specified stream. + + + + + The caller can specify any write-able stream, for example a , a , or ASP.NET's + Response.OutputStream. The content will be decrypted and + decompressed as necessary. If the entry is encrypted and no password + is provided, this method will throw. + + + The position on the stream is not reset by this method before it extracts. + You may want to call stream.Seek() before calling ZipEntry.Extract(). + + + + + the stream to which the entry should be extracted. + + + + + + Extract the entry to the filesystem, starting at the specified base + directory. + + + the pathname of the base directory + + + + + + This example extracts only the entries in a zip file that are .txt files, + into a directory called "textfiles". + + using (ZipFile zip = ZipFile.Read("PackedDocuments.zip")) + { + foreach (string s1 in zip.EntryFilenames) + { + if (s1.EndsWith(".txt")) + { + zip[s1].Extract("textfiles"); + } + } + } + + + Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip") + Dim s1 As String + For Each s1 In zip.EntryFilenames + If s1.EndsWith(".txt") Then + zip(s1).Extract("textfiles") + End If + Next + End Using + + + + + + + Using this method, existing entries in the filesystem will not be + overwritten. If you would like to force the overwrite of existing + files, see the property, or call + . + + + + See the remarks on the property, for some + details about how the last modified time of the created file is set. + + + + + + Extract the entry to the filesystem, starting at the specified base + directory, and using the specified behavior when extraction would + overwrite an existing file. + + + + + See the remarks on the property, for some + details about how the last modified time of the created file is set. + + + + + + String sZipPath = "Airborne.zip"; + String sFilePath = "Readme.txt"; + String sRootFolder = "Digado"; + using (ZipFile zip = ZipFile.Read(sZipPath)) + { + if (zip.EntryFileNames.Contains(sFilePath)) + { + // use the string indexer on the zip file + zip[sFileName].Extract(sRootFolder, + ExtractExistingFileAction.OverwriteSilently); + } + } + + + + Dim sZipPath as String = "Airborne.zip" + Dim sFilePath As String = "Readme.txt" + Dim sRootFolder As String = "Digado" + Using zip As ZipFile = ZipFile.Read(sZipPath) + If zip.EntryFileNames.Contains(sFilePath) + ' use the string indexer on the zip file + zip(sFilePath).Extract(sRootFolder, _ + ExtractExistingFileAction.OverwriteSilently) + End If + End Using + + + + the pathname of the base directory + + The action to take if extraction would overwrite an existing file. + + + + + Extract the entry to the filesystem, using the current working directory + and the specified password. + + + + This method has a bunch of overloads! One of them is sure to be + the right one for you... + + + + + + + + + Existing entries in the filesystem will not be overwritten. If you + would like to force the overwrite of existing files, see the property, or call + . + + + + See the remarks on the property for some + details about how the "last modified" time of the created file is + set. + + + + + In this example, entries that use encryption are extracted using a + particular password. + + using (var zip = ZipFile.Read(FilePath)) + { + foreach (ZipEntry e in zip) + { + if (e.UsesEncryption) + e.ExtractWithPassword("Secret!"); + else + e.Extract(); + } + } + + + Using zip As ZipFile = ZipFile.Read(FilePath) + Dim e As ZipEntry + For Each e In zip + If (e.UsesEncryption) + e.ExtractWithPassword("Secret!") + Else + e.Extract + End If + Next + End Using + + + The Password to use for decrypting the entry. + + + + Extract the entry to the filesystem, starting at the specified base + directory, and using the specified password. + + + + + + + + Existing entries in the filesystem will not be overwritten. If you + would like to force the overwrite of existing files, see the property, or call + . + + + + See the remarks on the property, for some + details about how the last modified time of the created file is set. + + + + The pathname of the base directory. + The Password to use for decrypting the entry. + + + + Extract the entry to a file in the filesystem, relative to the + current directory, using the specified behavior when extraction + would overwrite an existing file. + + + + + See the remarks on the property, for some + details about how the last modified time of the created file is set. + + + + The Password to use for decrypting the entry. + + + The action to take if extraction would overwrite an existing file. + + + + + Extract the entry to the filesystem, starting at the specified base + directory, and using the specified behavior when extraction would + overwrite an existing file. + + + + See the remarks on the property, for some + details about how the last modified time of the created file is set. + + + the pathname of the base directory + + The action to take if extraction would + overwrite an existing file. + + The Password to use for decrypting the entry. + + + + Extracts the entry to the specified stream, using the specified + Password. For example, the caller could extract to Console.Out, or + to a MemoryStream. + + + + + The caller can specify any write-able stream, for example a , a , or ASP.NET's + Response.OutputStream. The content will be decrypted and + decompressed as necessary. If the entry is encrypted and no password + is provided, this method will throw. + + + The position on the stream is not reset by this method before it extracts. + You may want to call stream.Seek() before calling ZipEntry.Extract(). + + + + + + the stream to which the entry should be extracted. + + + The password to use for decrypting the entry. + + + + + Opens a readable stream corresponding to the zip entry in the + archive. The stream decompresses and decrypts as necessary, as it + is read. + + + + + + DotNetZip offers a variety of ways to extract entries from a zip + file. This method allows an application to extract an entry by + reading a . + + + + The return value is of type . Use it as you would any + stream for reading. When an application calls on that stream, it will + receive data from the zip entry that is decrypted and decompressed + as necessary. + + + + CrcCalculatorStream adds one additional feature: it keeps a + CRC32 checksum on the bytes of the stream as it is read. The CRC + value is available in the property on the + CrcCalculatorStream. When the read is complete, your + application + should check this CRC against the + property on the ZipEntry to validate the content of the + ZipEntry. You don't have to validate the entry using the CRC, but + you should, to verify integrity. Check the example for how to do + this. + + + + If the entry is protected with a password, then you need to provide + a password prior to calling , either by + setting the property on the entry, or the + property on the ZipFile + itself. Or, you can use , the + overload of OpenReader that accepts a password parameter. + + + + If you want to extract entry data into a write-able stream that is + already opened, like a , do not + use this method. Instead, use . + + + + Your application may use only one stream created by OpenReader() at + a time, and you should not call other Extract methods before + completing your reads on a stream obtained from OpenReader(). This + is because there is really only one source stream for the compressed + content. A call to OpenReader() seeks in the source stream, to the + beginning of the compressed content. A subsequent call to + OpenReader() on a different entry will seek to a different position + in the source stream, as will a call to Extract() or one of its + overloads. This will corrupt the state for the decompressing stream + from the original call to OpenReader(). + + + + The OpenReader() method works only when the ZipEntry is + obtained from an instance of ZipFile. This method will throw + an exception if the ZipEntry is obtained from a . + + + + + This example shows how to open a zip archive, then read in a named + entry via a stream. After the read loop is complete, the code + compares the calculated during the read loop with the expected CRC + on the ZipEntry, to verify the extraction. + + using (ZipFile zip = new ZipFile(ZipFileToRead)) + { + ZipEntry e1= zip["Elevation.mp3"]; + using (Ionic.Zlib.CrcCalculatorStream s = e1.OpenReader()) + { + byte[] buffer = new byte[4096]; + int n, totalBytesRead= 0; + do { + n = s.Read(buffer,0, buffer.Length); + totalBytesRead+=n; + } while (n>0); + if (s.Crc32 != e1.Crc32) + throw new Exception(string.Format("The Zip Entry failed the CRC Check. (0x{0:X8}!=0x{1:X8})", s.Crc32, e1.Crc32)); + if (totalBytesRead != e1.UncompressedSize) + throw new Exception(string.Format("We read an unexpected number of bytes. ({0}!={1})", totalBytesRead, e1.UncompressedSize)); + } + } + + + Using zip As New ZipFile(ZipFileToRead) + Dim e1 As ZipEntry = zip.Item("Elevation.mp3") + Using s As Ionic.Zlib.CrcCalculatorStream = e1.OpenReader + Dim n As Integer + Dim buffer As Byte() = New Byte(4096) {} + Dim totalBytesRead As Integer = 0 + Do + n = s.Read(buffer, 0, buffer.Length) + totalBytesRead = (totalBytesRead + n) + Loop While (n > 0) + If (s.Crc32 <> e1.Crc32) Then + Throw New Exception(String.Format("The Zip Entry failed the CRC Check. (0x{0:X8}!=0x{1:X8})", s.Crc32, e1.Crc32)) + End If + If (totalBytesRead <> e1.UncompressedSize) Then + Throw New Exception(String.Format("We read an unexpected number of bytes. ({0}!={1})", totalBytesRead, e1.UncompressedSize)) + End If + End Using + End Using + + + + The Stream for reading. + + + + Opens a readable stream for an encrypted zip entry in the archive. + The stream decompresses and decrypts as necessary, as it is read. + + + + + See the documentation on the method for + full details. This overload allows the application to specify a + password for the ZipEntry to be read. + + + + The password to use for decrypting the entry. + The Stream for reading. + + + + Validates that the args are consistent. + + + Only one of {baseDir, outStream} can be non-null. + If baseDir is non-null, then the outputFile is created. + + + + + Reads one ZipEntry from the given stream. The content for + the entry does not get decompressed or decrypted. This method + basically reads metadata, and seeks. + + the ZipContainer this entry belongs to. + + true of this is the first entry being read from the stream. + + the ZipEntry read from the stream. + + + + Finds a particular segment in the given extra field. + This is used when modifying a previously-generated + extra field, in particular when removing the AES crypto + segment in the extra field. + + + + + At current cursor position in the stream, read the extra + field, and set the properties on the ZipEntry instance + appropriately. This can be called when processing the + Extra field in the Central Directory, or in the local + header. + + + + + generate and return a byte array that encodes the filename + for the entry. + + + + side effects: generate and store into _CommentBytes the + byte array for any comment attached to the entry. Also + sets _actualEncoding to indicate the actual encoding + used. The same encoding is used for both filename and + comment. + + + + + + Stores the position of the entry source stream, or, if the position is + already stored, seeks to that position. + + + + + This method is called in prep for reading the source stream. If PKZIP + encryption is used, then we need to calc the CRC32 before doing the + encryption, because the CRC is used in the 12th byte of the PKZIP + encryption header. So, we need to be able to seek backward in the source + when saving the ZipEntry. This method is called from the place that + calculates the CRC, and also from the method that does the encryption of + the file data. + + + + The first time through, this method sets the _sourceStreamOriginalPosition + field. Subsequent calls to this method seek to that position. + + + + + + Copy metadata that may have been changed by the app. We do this when + resetting the zipFile instance. If the app calls Save() on a ZipFile, then + tries to party on that file some more, we may need to Reset() it , which + means re-reading the entries and then copying the metadata. I think. + + + + + Set the input stream and get its length, if possible. The length is + used for progress updates, AND, to allow an optimization in case of + a stream/file of zero length. In that case we skip the Encrypt and + compression Stream. (like DeflateStream or BZip2OutputStream) + + + + + Prepare the given stream for output - wrap it in a CountingStream, and + then in a CRC stream, and an encryptor and deflator as appropriate. + + + + Previously this was used in ZipEntry.Write(), but in an effort to + introduce some efficiencies in that method I've refactored to put the + code inline. This method still gets called by ZipOutputStream. + + + + + + True if the referenced entry is a directory. + + + + + Provides a human-readable string with information about the ZipEntry. + + + + + The time and date at which the file indicated by the ZipEntry was + last modified. + + + + + The DotNetZip library sets the LastModified value for an entry, equal to + the Last Modified time of the file in the filesystem. If an entry is + added from a stream, the library uses System.DateTime.Now for this + value, for the given entry. + + + + This property allows the application to retrieve and possibly set the + LastModified value on an entry, to an arbitrary value. values with a + setting of DateTimeKind.Unspecified are taken to be expressed as + DateTimeKind.Local. + + + + Be aware that because of the way PKWare's + Zip specification describes how times are stored in the zip file, + the full precision of the System.DateTime datatype is not stored + for the last modified time when saving zip files. For more information on + how times are formatted, see the PKZip specification. + + + + The actual last modified time of a file can be stored in multiple ways in + the zip file, and they are not mutually exclusive: + + + + + In the so-called "DOS" format, which has a 2-second precision. Values + are rounded to the nearest even second. For example, if the time on the + file is 12:34:43, then it will be stored as 12:34:44. This first value + is accessible via the LastModified property. This value is always + present in the metadata for each zip entry. In some cases the value is + invalid, or zero. + + + + In the so-called "Windows" or "NTFS" format, as an 8-byte integer + quantity expressed as the number of 1/10 milliseconds (in other words + the number of 100 nanosecond units) since January 1, 1601 (UTC). This + format is how Windows represents file times. This time is accessible + via the ModifiedTime property. + + + + In the "Unix" format, a 4-byte quantity specifying the number of seconds since + January 1, 1970 UTC. + + + + In an older format, now deprecated but still used by some current + tools. This format is also a 4-byte quantity specifying the number of + seconds since January 1, 1970 UTC. + + + + + + Zip tools and libraries will always at least handle (read or write) the + DOS time, and may also handle the other time formats. Keep in mind that + while the names refer to particular operating systems, there is nothing in + the time formats themselves that prevents their use on other operating + systems. + + + + When reading ZIP files, the DotNetZip library reads the Windows-formatted + time, if it is stored in the entry, and sets both LastModified and + ModifiedTime to that value. When writing ZIP files, the DotNetZip + library by default will write both time quantities. It can also emit the + Unix-formatted time if desired (See .) + + + + The last modified time of the file created upon a call to + ZipEntry.Extract() may be adjusted during extraction to compensate + for differences in how the .NET Base Class Library deals with daylight + saving time (DST) versus how the Windows filesystem deals with daylight + saving time. Raymond Chen provides + some good context. + + + + In a nutshell: Daylight savings time rules change regularly. In 2007, for + example, the inception week of DST changed. In 1977, DST was in place all + year round. In 1945, likewise. And so on. Win32 does not attempt to + guess which time zone rules were in effect at the time in question. It + will render a time as "standard time" and allow the app to change to DST + as necessary. .NET makes a different choice. + + + + Compare the output of FileInfo.LastWriteTime.ToString("f") with what you + see in the Windows Explorer property sheet for a file that was last + written to on the other side of the DST transition. For example, suppose + the file was last modified on October 17, 2003, during DST but DST is not + currently in effect. Explorer's file properties reports Thursday, October + 17, 2003, 8:45:38 AM, but .NETs FileInfo reports Thursday, October 17, + 2003, 9:45 AM. + + + + Win32 says, "Thursday, October 17, 2002 8:45:38 AM PST". Note: Pacific + STANDARD Time. Even though October 17 of that year occurred during Pacific + Daylight Time, Win32 displays the time as standard time because that's + what time it is NOW. + + + + .NET BCL assumes that the current DST rules were in place at the time in + question. So, .NET says, "Well, if the rules in effect now were also in + effect on October 17, 2003, then that would be daylight time" so it + displays "Thursday, October 17, 2003, 9:45 AM PDT" - daylight time. + + + + So .NET gives a value which is more intuitively correct, but is also + potentially incorrect, and which is not invertible. Win32 gives a value + which is intuitively incorrect, but is strictly correct. + + + + Because of this funkiness, this library adds one hour to the LastModified + time on the extracted file, if necessary. That is to say, if the time in + question had occurred in what the .NET Base Class Library assumed to be + DST. This assumption may be wrong given the constantly changing DST rules, + but it is the best we can do. + + + + + + + + Last Modified time for the file represented by the entry. + + + + + + This value corresponds to the "last modified" time in the NTFS file times + as described in the Zip + specification. When getting this property, the value may be + different from . When setting the property, + the property also gets set, but with a lower + precision. + + + + Let me explain. It's going to take a while, so get + comfortable. Originally, waaaaay back in 1989 when the ZIP specification + was originally described by the esteemed Mr. Phil Katz, the dominant + operating system of the time was MS-DOS. MSDOS stored file times with a + 2-second precision, because, c'mon, who is ever going to need better + resolution than THAT? And so ZIP files, regardless of the platform on + which the zip file was created, store file times in exactly the same format that DOS used + in 1989. + + + + Since then, the ZIP spec has evolved, but the internal format for file + timestamps remains the same. Despite the fact that the way times are + stored in a zip file is rooted in DOS heritage, any program on any + operating system can format a time in this way, and most zip tools and + libraries DO - they round file times to the nearest even second and store + it just like DOS did 25+ years ago. + + + + PKWare extended the ZIP specification to allow a zip file to store what + are called "NTFS Times" and "Unix(tm) times" for a file. These are the + last write, last access, and file creation + times of a particular file. These metadata are not actually specific + to NTFS or Unix. They are tracked for each file by NTFS and by various + Unix filesystems, but they are also tracked by other filesystems, too. + The key point is that the times are formatted in the zip file + in the same way that NTFS formats the time (ticks since win32 epoch), + or in the same way that Unix formats the time (seconds since Unix + epoch). As with the DOS time, any tool or library running on any + operating system is capable of formatting a time in one of these ways + and embedding it into the zip file. + + + + These extended times are higher precision quantities than the DOS time. + As described above, the (DOS) LastModified has a precision of 2 seconds. + The Unix time is stored with a precision of 1 second. The NTFS time is + stored with a precision of 0.0000001 seconds. The quantities are easily + convertible, except for the loss of precision you may incur. + + + + A zip archive can store the {C,A,M} times in NTFS format, in Unix format, + or not at all. Often a tool running on Unix or Mac will embed the times + in Unix format (1 second precision), while WinZip running on Windows might + embed the times in NTFS format (precision of of 0.0000001 seconds). When + reading a zip file with these "extended" times, in either format, + DotNetZip represents the values with the + ModifiedTime, AccessedTime and CreationTime + properties on the ZipEntry. + + + + While any zip application or library, regardless of the platform it + runs on, could use any of the time formats allowed by the ZIP + specification, not all zip tools or libraries do support all these + formats. Storing the higher-precision times for each entry is + optional for zip files, and many tools and libraries don't use the + higher precision quantities at all. The old DOS time, represented by + , is guaranteed to be present, though it + sometimes unset. + + + + Ok, getting back to the question about how the LastModified + property relates to this ModifiedTime + property... LastModified is always set, while + ModifiedTime is not. (The other times stored in the NTFS + times extension, CreationTime and AccessedTime also + may not be set on an entry that is read from an existing zip file.) + When reading a zip file, then LastModified takes the DOS time + that is stored with the file. If the DOS time has been stored as zero + in the zipfile, then this library will use DateTime.Now for the + LastModified value. If the ZIP file was created by an evolved + tool, then there will also be higher precision NTFS or Unix times in + the zip file. In that case, this library will read those times, and + set LastModified and ModifiedTime to the same value, the + one corresponding to the last write time of the file. If there are no + higher precision times stored for the entry, then ModifiedTime + remains unset (likewise AccessedTime and CreationTime), + and LastModified keeps its DOS time. + + + + When creating zip files with this library, by default the extended time + properties (ModifiedTime, AccessedTime, and + CreationTime) are set on the ZipEntry instance, and these data are + stored in the zip archive for each entry, in NTFS format. If you add an + entry from an actual filesystem file, then the entry gets the actual file + times for that file, to NTFS-level precision. If you add an entry from a + stream, or a string, then the times get the value DateTime.Now. In + this case LastModified and ModifiedTime will be identical, + to 2 seconds of precision. You can explicitly set the + CreationTime, AccessedTime, and ModifiedTime of an + entry using the property setters. If you want to set all of those + quantities, it's more efficient to use the method. Those + changes are not made permanent in the zip file until you call or one of its cousins. + + + + When creating a zip file, you can override the default behavior of + this library for formatting times in the zip file, disabling the + embedding of file times in NTFS format or enabling the storage of file + times in Unix format, or both. You may want to do this, for example, + when creating a zip file on Windows, that will be consumed on a Mac, + by an application that is not hip to the "NTFS times" format. To do + this, use the and + properties. A valid zip + file may store the file times in both formats. But, there are no + guarantees that a program running on Mac or Linux will gracefully + handle the NTFS-formatted times when Unix times are present, or that a + non-DotNetZip-powered application running on Windows will be able to + handle file times in Unix format. DotNetZip will always do something + reasonable; other libraries or tools may not. When in doubt, test. + + + + I'll bet you didn't think one person could type so much about time, eh? + And reading it was so enjoyable, too! Well, in appreciation, maybe you + should donate? + + + + + + + + + + + Last Access time for the file represented by the entry. + + + This value may or may not be meaningful. If the ZipEntry was read from an existing + Zip archive, this information may not be available. For an explanation of why, see + . + + + + + + + + The file creation time for the file represented by the entry. + + + + This value may or may not be meaningful. If the ZipEntry was read + from an existing zip archive, and the creation time was not set on the entry + when the zip file was created, then this property may be meaningless. For an + explanation of why, see . + + + + + + + + Specifies whether the Creation, Access, and Modified times for the given + entry will be emitted in "Windows format" when the zip archive is saved. + + + + + An application creating a zip archive can use this flag to explicitly + specify that the file times for the entry should or should not be stored + in the zip archive in the format used by Windows. The default value of + this property is true. + + + + When adding an entry from a file or directory, the Creation (), Access (), and Modified + () times for the given entry are automatically + set from the filesystem values. When adding an entry from a stream or + string, all three values are implicitly set to DateTime.Now. Applications + can also explicitly set those times by calling . + + + + PKWARE's + zip specification describes multiple ways to format these times in a + zip file. One is the format Windows applications normally use: 100ns ticks + since Jan 1, 1601 UTC. The other is a format Unix applications typically + use: seconds since January 1, 1970 UTC. Each format can be stored in an + "extra field" in the zip entry when saving the zip archive. The former + uses an extra field with a Header Id of 0x000A, while the latter uses a + header ID of 0x5455. + + + + Not all zip tools and libraries can interpret these fields. Windows + compressed folders is one that can read the Windows Format timestamps, + while I believe the Infozip + tools can read the Unix format timestamps. Although the time values are + easily convertible, subject to a loss of precision, some tools and + libraries may be able to read only one or the other. DotNetZip can read or + write times in either or both formats. + + + + The times stored are taken from , , and . + + + + This property is not mutually exclusive from the property. It is + possible that a zip entry can embed the timestamps in both forms, one + form, or neither. But, there are no guarantees that a program running on + Mac or Linux will gracefully handle NTFS Formatted times, or that a + non-DotNetZip-powered application running on Windows will be able to + handle file times in Unix format. When in doubt, test. + + + + Normally you will use the ZipFile.EmitTimesInWindowsFormatWhenSaving + property, to specify the behavior for all entries in a zip, rather than + the property on each individual entry. + + + + + + + + + + + + + Specifies whether the Creation, Access, and Modified times for the given + entry will be emitted in "Unix(tm) format" when the zip archive is saved. + + + + + An application creating a zip archive can use this flag to explicitly + specify that the file times for the entry should or should not be stored + in the zip archive in the format used by Unix. By default this flag is + false, meaning the Unix-format times are not stored in the zip + archive. + + + + When adding an entry from a file or directory, the Creation (), Access (), and Modified + () times for the given entry are automatically + set from the filesystem values. When adding an entry from a stream or + string, all three values are implicitly set to DateTime.Now. Applications + can also explicitly set those times by calling . + + + + PKWARE's + zip specification describes multiple ways to format these times in a + zip file. One is the format Windows applications normally use: 100ns ticks + since Jan 1, 1601 UTC. The other is a format Unix applications typically + use: seconds since Jan 1, 1970 UTC. Each format can be stored in an + "extra field" in the zip entry when saving the zip archive. The former + uses an extra field with a Header Id of 0x000A, while the latter uses a + header ID of 0x5455. + + + + Not all tools and libraries can interpret these fields. Windows + compressed folders is one that can read the Windows Format timestamps, + while I believe the Infozip + tools can read the Unix format timestamps. Although the time values are + easily convertible, subject to a loss of precision, some tools and + libraries may be able to read only one or the other. DotNetZip can read or + write times in either or both formats. + + + + The times stored are taken from , , and . + + + + This property is not mutually exclusive from the property. It is + possible that a zip entry can embed the timestamps in both forms, one + form, or neither. But, there are no guarantees that a program running on + Mac or Linux will gracefully handle NTFS Formatted times, or that a + non-DotNetZip-powered application running on Windows will be able to + handle file times in Unix format. When in doubt, test. + + + + Normally you will use the ZipFile.EmitTimesInUnixFormatWhenSaving + property, to specify the behavior for all entries, rather than the + property on each individual entry. + + + + + + + + + + + + + The type of timestamp attached to the ZipEntry. + + + + This property is valid only for a ZipEntry that was read from a zip archive. + It indicates the type of timestamp attached to the entry. + + + + + + + + The file attributes for the entry. + + + + + + The attributes in NTFS include + ReadOnly, Archive, Hidden, System, and Indexed. When adding a + ZipEntry to a ZipFile, these attributes are set implicitly when + adding an entry from the filesystem. When adding an entry from a stream + or string, the Attributes are not set implicitly. Regardless of the way + an entry was added to a ZipFile, you can set the attributes + explicitly if you like. + + + + When reading a ZipEntry from a ZipFile, the attributes are + set according to the data stored in the ZipFile. If you extract the + entry from the archive to a filesystem file, DotNetZip will set the + attributes on the resulting file accordingly. + + + + The attributes can be set explicitly by the application. For example the + application may wish to set the FileAttributes.ReadOnly bit for all + entries added to an archive, so that on unpack, this attribute will be set + on the extracted file. Any changes you make to this property are made + permanent only when you call a Save() method on the ZipFile + instance that contains the ZipEntry. + + + + For example, an application may wish to zip up a directory and set the + ReadOnly bit on every file in the archive, so that upon later extraction, + the resulting files will be marked as ReadOnly. Not every extraction tool + respects these attributes, but if you unpack with DotNetZip, as for + example in a self-extracting archive, then the attributes will be set as + they are stored in the ZipFile. + + + + These attributes may not be interesting or useful if the resulting archive + is extracted on a non-Windows platform. How these attributes get used + upon extraction depends on the platform and tool used. + + + + This property is only partially supported in the Silverlight version + of the library: applications can read attributes on entries within + ZipFiles. But extracting entries within Silverlight will not set the + attributes on the extracted files. + + + + + + + The name of the filesystem file, referred to by the ZipEntry. + + + + + This property specifies the thing-to-be-zipped on disk, and is set only + when the ZipEntry is being created from a filesystem file. If the + ZipFile is instantiated by reading an existing .zip archive, then + the LocalFileName will be null (Nothing in VB). + + + + When it is set, the value of this property may be different than , which is the path used in the archive itself. If you + call Zip.AddFile("foop.txt", AlternativeDirectory), then the path + used for the ZipEntry within the zip archive will be different + than this path. + + + + If the entry is being added from a stream, then this is null (Nothing in VB). + + + + + + + + The name of the file contained in the ZipEntry. + + + + + + This is the name of the entry in the ZipFile itself. When creating + a zip archive, if the ZipEntry has been created from a filesystem + file, via a call to or , or a related overload, the value + of this property is derived from the name of that file. The + FileName property does not include drive letters, and may include a + different directory path, depending on the value of the + directoryPathInArchive parameter used when adding the entry into + the ZipFile. + + + + In some cases there is no related filesystem file - for example when a + ZipEntry is created using or one of the similar overloads. In this case, the value of + this property is derived from the fileName and the directory path passed + to that method. + + + + When reading a zip file, this property takes the value of the entry name + as stored in the zip file. If you extract such an entry, the extracted + file will take the name given by this property. + + + + Applications can set this property when creating new zip archives or when + reading existing archives. When setting this property, the actual value + that is set will replace backslashes with forward slashes, in accordance + with the Zip + specification, for compatibility with Unix(tm) and ... get + this.... Amiga! + + + + If an application reads a ZipFile via or a related overload, and then explicitly + sets the FileName on an entry contained within the ZipFile, and + then calls , the application will effectively + rename the entry within the zip archive. + + + + If an application sets the value of FileName, then calls + Extract() on the entry, the entry is extracted to a file using the + newly set value as the filename. The FileName value is made + permanent in the zip archive only after a call to one of the + ZipFile.Save() methods on the ZipFile that contains the + ZipEntry. + + + + If an application attempts to set the FileName to a value that + would result in a duplicate entry in the ZipFile, an exception is + thrown. + + + + When a ZipEntry is contained within a ZipFile, applications + cannot rename the entry within the context of a foreach (For + Each in VB) loop, because of the way the ZipFile stores + entries. If you need to enumerate through all the entries and rename one + or more of them, use ZipFile.EntriesSorted as the + collection. See also, ZipFile.GetEnumerator(). + + + + + + + The stream that provides content for the ZipEntry. + + + + + + The application can use this property to set the input stream for an + entry on a just-in-time basis. Imagine a scenario where the application + creates a ZipFile comprised of content obtained from hundreds of + files, via calls to AddFile(). The DotNetZip library opens streams + on these files on a just-in-time basis, only when writing the entry out to + an external store within the scope of a ZipFile.Save() call. Only + one input stream is opened at a time, as each entry is being written out. + + + + Now imagine a different application that creates a ZipFile + with content obtained from hundreds of streams, added through . Normally the + application would supply an open stream to that call. But when large + numbers of streams are being added, this can mean many open streams at one + time, unnecessarily. + + + + To avoid this, call and specify delegates that open and close the stream at + the time of Save. + + + + + Setting the value of this property when the entry was not added from a + stream (for example, when the ZipEntry was added with or , or when the entry was added by + reading an existing zip archive) will throw an exception. + + + + + + + + A flag indicating whether the InputStream was provided Just-in-time. + + + + + + When creating a zip archive, an application can obtain content for one or + more of the ZipEntry instances from streams, using the method. At the time + of calling that method, the application can supply null as the value of + the stream parameter. By doing so, the application indicates to the + library that it will provide a stream for the entry on a just-in-time + basis, at the time one of the ZipFile.Save() methods is called and + the data for the various entries are being compressed and written out. + + + + In this case, the application can set the + property, typically within the SaveProgress event (event type: ) for that entry. + + + + The application will later want to call Close() and Dispose() on that + stream. In the SaveProgress event, when the event type is , the application can + do so. This flag indicates that the stream has been provided by the + application on a just-in-time basis and that it is the application's + responsibility to call Close/Dispose on that stream. + + + + + + + + An enum indicating the source of the ZipEntry. + + + + + The version of the zip engine needed to read the ZipEntry. + + + + + This is a readonly property, indicating the version of the Zip + specification that the extracting tool or library must support to + extract the given entry. Generally higher versions indicate newer + features. Older zip engines obviously won't know about new features, and + won't be able to extract entries that depend on those newer features. + + + + + value + Features + + + + 20 + a basic Zip Entry, potentially using PKZIP encryption. + + + + + 45 + The ZIP64 extension is used on the entry. + + + + + 46 + File is compressed using BZIP2 compression* + + + + 50 + File is encrypted using PkWare's DES, 3DES, (broken) RC2 or RC4 + + + + 51 + File is encrypted using PKWare's AES encryption or corrected RC2 encryption. + + + + 52 + File is encrypted using corrected RC2-64 encryption** + + + + 61 + File is encrypted using non-OAEP key wrapping*** + + + + 63 + File is compressed using LZMA, PPMd+, Blowfish, or Twofish + + + + + + There are other values possible, not listed here. DotNetZip supports + regular PKZip encryption, and ZIP64 extensions. DotNetZip cannot extract + entries that require a zip engine higher than 45. + + + + This value is set upon reading an existing zip file, or after saving a zip + archive. + + + + + + The comment attached to the ZipEntry. + + + + + Each entry in a zip file can optionally have a comment associated to + it. The comment might be displayed by a zip tool during extraction, for + example. + + + + By default, the Comment is encoded in IBM437 code page. You can + specify an alternative with and + . + + + + + + + + Indicates whether the entry requires ZIP64 extensions. + + + + + + This property is null (Nothing in VB) until a Save() method on the + containing instance has been called. The property is + non-null (HasValue is true) only after a Save() method has + been called. + + + + After the containing ZipFile has been saved, the Value of this + property is true if any of the following three conditions holds: the + uncompressed size of the entry is larger than 0xFFFFFFFF; the compressed + size of the entry is larger than 0xFFFFFFFF; the relative offset of the + entry within the zip archive is larger than 0xFFFFFFFF. These quantities + are not known until a Save() is attempted on the zip archive and + the compression is applied. + + + + If none of the three conditions holds, then the Value is false. + + + + A Value of false does not indicate that the entry, as saved in the + zip archive, does not use ZIP64. It merely indicates that ZIP64 is + not required. An entry may use ZIP64 even when not required if + the property on the containing + ZipFile instance is set to , or if + the property on the containing + ZipFile instance is set to + and the output stream was not seekable. + + + + + + + + Indicates whether the entry actually used ZIP64 extensions, as it was most + recently written to the output file or stream. + + + + + + This Nullable property is null (Nothing in VB) until a Save() + method on the containing instance has been + called. HasValue is true only after a Save() method has been + called. + + + + The value of this property for a particular ZipEntry may change + over successive calls to Save() methods on the containing ZipFile, + even if the file that corresponds to the ZipEntry does not. This + may happen if other entries contained in the ZipFile expand, + causing the offset for this particular entry to exceed 0xFFFFFFFF. + + + + + + + The bitfield for the entry as defined in the zip spec. You probably + never need to look at this. + + + + + You probably do not need to concern yourself with the contents of this + property, but in case you do: + + + + + bit + meaning + + + + 0 + set if encryption is used. + + + + 1-2 + + set to determine whether normal, max, fast deflation. DotNetZip library + always leaves these bits unset when writing (indicating "normal" + deflation"), but can read an entry with any value here. + + + + + 3 + + Indicates that the Crc32, Compressed and Uncompressed sizes are zero in the + local header. This bit gets set on an entry during writing a zip file, when + it is saved to a non-seekable output stream. + + + + + + 4 + reserved for "enhanced deflating". This library doesn't do enhanced deflating. + + + + 5 + set to indicate the zip is compressed patched data. This library doesn't do that. + + + + 6 + + set if PKWare's strong encryption is used (must also set bit 1 if bit 6 is + set). This bit is not set if WinZip's AES encryption is set. + + + + 7 + not used + + + + 8 + not used + + + + 9 + not used + + + + 10 + not used + + + + 11 + + Language encoding flag (EFS). If this bit is set, the filename and comment + fields for this file must be encoded using UTF-8. This library currently + does not support UTF-8. + + + + + 12 + Reserved by PKWARE for enhanced compression. + + + + 13 + + Used when encrypting the Central Directory to indicate selected data + values in the Local Header are masked to hide their actual values. See + the section in the Zip + specification describing the Strong Encryption Specification for + details. + + + + + 14 + Reserved by PKWARE. + + + + 15 + Reserved by PKWARE. + + + + + + + + + The compression method employed for this ZipEntry. + + + + + + The + Zip specification allows a variety of compression methods. This + library supports just two: 0x08 = Deflate. 0x00 = Store (no compression), + for reading or writing. + + + + When reading an entry from an existing zipfile, the value you retrieve + here indicates the compression method used on the entry by the original + creator of the zip. When writing a zipfile, you can specify either 0x08 + (Deflate) or 0x00 (None). If you try setting something else, you will get + an exception. + + + + You may wish to set CompressionMethod to CompressionMethod.None (0) + when zipping already-compressed data like a jpg, png, or mp3 file. + This can save time and cpu cycles. + + + + When setting this property on a ZipEntry that is read from an + existing zip file, calling ZipFile.Save() will cause the new + CompressionMethod to be used on the entry in the newly saved zip file. + + + + Setting this property may have the side effect of modifying the + CompressionLevel property. If you set the CompressionMethod to a + value other than None, and CompressionLevel is previously + set to None, then CompressionLevel will be set to + Default. + + + + + + + In this example, the first entry added to the zip archive uses the default + behavior - compression is used where it makes sense. The second entry, + the MP3 file, is added to the archive without being compressed. + + using (ZipFile zip = new ZipFile(ZipFileToCreate)) + { + ZipEntry e1= zip.AddFile(@"notes\Readme.txt"); + ZipEntry e2= zip.AddFile(@"music\StopThisTrain.mp3"); + e2.CompressionMethod = CompressionMethod.None; + zip.Save(); + } + + + + Using zip As New ZipFile(ZipFileToCreate) + zip.AddFile("notes\Readme.txt") + Dim e2 as ZipEntry = zip.AddFile("music\StopThisTrain.mp3") + e2.CompressionMethod = CompressionMethod.None + zip.Save + End Using + + + + + + Sets the compression level to be used for the entry when saving the zip + archive. This applies only for CompressionMethod = DEFLATE. + + + + + When using the DEFLATE compression method, Varying the compression + level used on entries can affect the size-vs-speed tradeoff when + compression and decompressing data streams or files. + + + + If you do not set this property, the default compression level is used, + which normally gives a good balance of compression efficiency and + compression speed. In some tests, using BestCompression can + double the time it takes to compress, while delivering just a small + increase in compression efficiency. This behavior will vary with the + type of data you compress. If you are in doubt, just leave this setting + alone, and accept the default. + + + + When setting this property on a ZipEntry that is read from an + existing zip file, calling ZipFile.Save() will cause the new + CompressionLevel to be used on the entry in the newly saved zip file. + + + + Setting this property may have the side effect of modifying the + CompressionMethod property. If you set the CompressionLevel + to a value other than None, CompressionMethod will be set + to Deflate, if it was previously None. + + + + Setting this property has no effect if the CompressionMethod is something + other than Deflate or None. + + + + + + + + The compressed size of the file, in bytes, within the zip archive. + + + + When reading a ZipFile, this value is read in from the existing + zip file. When creating or updating a ZipFile, the compressed + size is computed during compression. Therefore the value on a + ZipEntry is valid after a call to Save() (or one of its + overloads) in that case. + + + + + + + The size of the file, in bytes, before compression, or after extraction. + + + + When reading a ZipFile, this value is read in from the existing + zip file. When creating or updating a ZipFile, the uncompressed + size is computed during compression. Therefore the value on a + ZipEntry is valid after a call to Save() (or one of its + overloads) in that case. + + + + + + + The ratio of compressed size to uncompressed size of the ZipEntry. + + + + + This is a ratio of the compressed size to the uncompressed size of the + entry, expressed as a double in the range of 0 to 100+. A value of 100 + indicates no compression at all. It could be higher than 100 when the + compression algorithm actually inflates the data, as may occur for small + files, or uncompressible data that is encrypted. + + + + You could format it for presentation to a user via a format string of + "{3,5:F0}%" to see it as a percentage. + + + + If the size of the original uncompressed file is 0, implying a + denominator of 0, the return value will be zero. + + + + This property is valid after reading in an existing zip file, or after + saving the ZipFile that contains the ZipEntry. You cannot know the + effect of a compression transform until you try it. + + + + + + + The 32-bit CRC (Cyclic Redundancy Check) on the contents of the ZipEntry. + + + + + You probably don't need to concern yourself with this. It is used + internally by DotNetZip to verify files or streams upon extraction. + + The value is a 32-bit + CRC using 0xEDB88320 for the polynomial. This is the same CRC-32 used in + PNG, MPEG-2, and other protocols and formats. It is a read-only property; when + creating a Zip archive, the CRC for each entry is set only after a call to + Save() on the containing ZipFile. When reading an existing zip file, the value + of this property reflects the stored CRC for the entry. + + + + + + True if the entry is a directory (not a file). + This is a readonly property on the entry. + + + + + A derived property that is true if the entry uses encryption. + + + + + This is a readonly property on the entry. When reading a zip file, + the value for the ZipEntry is determined by the data read + from the zip file. After saving a ZipFile, the value of this + property for each ZipEntry indicates whether encryption was + actually used (which will have been true if the was set and the property + was something other than . + + + + + + Set this to specify which encryption algorithm to use for the entry when + saving it to a zip archive. + + + + + + Set this property in order to encrypt the entry when the ZipFile is + saved. When setting this property, you must also set a on the entry. If you set a value other than on this property and do not set a + Password then the entry will not be encrypted. The ZipEntry + data is encrypted as the ZipFile is saved, when you call or one of its cousins on the containing + ZipFile instance. You do not need to specify the Encryption + when extracting entries from an archive. + + + + The Zip specification from PKWare defines a set of encryption algorithms, + and the data formats for the zip archive that support them, and PKWare + supports those algorithms in the tools it produces. Other vendors of tools + and libraries, such as WinZip or Xceed, typically support a + subset of the algorithms specified by PKWare. These tools can + sometimes support additional different encryption algorithms and data + formats, not specified by PKWare. The AES Encryption specified and + supported by WinZip is the most popular example. This library supports a + subset of the complete set of algorithms specified by PKWare and other + vendors. + + + + There is no common, ubiquitous multi-vendor standard for strong encryption + within zip files. There is broad support for so-called "traditional" Zip + encryption, sometimes called Zip 2.0 encryption, as specified + by PKWare, but this encryption is considered weak and + breakable. This library currently supports the Zip 2.0 "weak" encryption, + and also a stronger WinZip-compatible AES encryption, using either 128-bit + or 256-bit key strength. If you want DotNetZip to support an algorithm + that is not currently supported, call the author of this library and maybe + we can talk business. + + + + The class also has a property. In most cases you will use + that property when setting encryption. This property takes + precedence over any Encryption set on the ZipFile itself. + Typically, you would use the per-entry Encryption when most entries in the + zip archive use one encryption algorithm, and a few entries use a + different one. If all entries in the zip file use the same Encryption, + then it is simpler to just set this property on the ZipFile itself, when + creating a zip archive. + + + + Some comments on updating archives: If you read a ZipFile, you can + modify the Encryption on an encrypted entry: you can remove encryption + from an entry that was encrypted; you can encrypt an entry that was not + encrypted previously; or, you can change the encryption algorithm. The + changes in encryption are not made permanent until you call Save() on the + ZipFile. To effect changes in encryption, the entry content is + streamed through several transformations, depending on the modification + the application has requested. For example if the entry is not encrypted + and the application sets Encryption to PkzipWeak, then at + the time of Save(), the original entry is read and decompressed, + then re-compressed and encrypted. Conversely, if the original entry is + encrypted with PkzipWeak encryption, and the application sets the + Encryption property to WinZipAes128, then at the time of + Save(), the original entry is decrypted via PKZIP encryption and + decompressed, then re-compressed and re-encrypted with AES. This all + happens automatically within the library, but it can be time-consuming for + large entries. + + + + Additionally, when updating archives, it is not possible to change the + password when changing the encryption algorithm. To change both the + algorithm and the password, you need to Save() the zipfile twice. First + set the Encryption to None, then call Save(). Then set the + Encryption to the new value (not "None"), then call Save() + once again. + + + + The WinZip AES encryption algorithms are not supported on the .NET Compact + Framework. + + + + + + This example creates a zip archive that uses encryption, and then extracts + entries from the archive. When creating the zip archive, the ReadMe.txt + file is zipped without using a password or encryption. The other file + uses encryption. + + + // Create a zip archive with AES Encryption. + using (ZipFile zip = new ZipFile()) + { + zip.AddFile("ReadMe.txt") + ZipEntry e1= zip.AddFile("2008-Regional-Sales-Report.pdf"); + e1.Encryption= EncryptionAlgorithm.WinZipAes256; + e1.Password= "Top.Secret.No.Peeking!"; + zip.Save("EncryptedArchive.zip"); + } + + // Extract a zip archive that uses AES Encryption. + // You do not need to specify the algorithm during extraction. + using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip")) + { + // Specify the password that is used during extraction, for + // all entries that require a password: + zip.Password= "Top.Secret.No.Peeking!"; + zip.ExtractAll("extractDirectory"); + } + + + + ' Create a zip that uses Encryption. + Using zip As New ZipFile() + zip.AddFile("ReadMe.txt") + Dim e1 as ZipEntry + e1= zip.AddFile("2008-Regional-Sales-Report.pdf") + e1.Encryption= EncryptionAlgorithm.WinZipAes256 + e1.Password= "Top.Secret.No.Peeking!" + zip.Save("EncryptedArchive.zip") + End Using + + ' Extract a zip archive that uses AES Encryption. + ' You do not need to specify the algorithm during extraction. + Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip")) + ' Specify the password that is used during extraction, for + ' all entries that require a password: + zip.Password= "Top.Secret.No.Peeking!" + zip.ExtractAll("extractDirectory") + End Using + + + + + + Thrown in the setter if EncryptionAlgorithm.Unsupported is specified. + + + ZipEntry.Password + ZipFile.Encryption + + + + The Password to be used when encrypting a ZipEntry upon + ZipFile.Save(), or when decrypting an entry upon Extract(). + + + + + This is a write-only property on the entry. Set this to request that the + entry be encrypted when writing the zip archive, or set it to specify the + password to be used when extracting an existing entry that is encrypted. + + + + The password set here is implicitly used to encrypt the entry during the + operation, or to decrypt during the or operation. If you set + the Password on a ZipEntry after calling Save(), there is no + effect. + + + + Consider setting the property when using a + password. Answering concerns that the standard password protection + supported by all zip tools is weak, WinZip has extended the ZIP + specification with a way to use AES Encryption to protect entries in the + Zip file. Unlike the "PKZIP 2.0" encryption specified in the PKZIP + specification, AES + Encryption uses a standard, strong, tested, encryption + algorithm. DotNetZip can create zip archives that use WinZip-compatible + AES encryption, if you set the property. But, + archives created that use AES encryption may not be readable by all other + tools and libraries. For example, Windows Explorer cannot read a + "compressed folder" (a zip file) that uses AES encryption, though it can + read a zip file that uses "PKZIP encryption." + + + + The class also has a + property. This property takes precedence over any password set on the + ZipFile itself. Typically, you would use the per-entry Password when most + entries in the zip archive use one password, and a few entries use a + different password. If all entries in the zip file use the same password, + then it is simpler to just set this property on the ZipFile itself, + whether creating a zip archive or extracting a zip archive. + + + + Some comments on updating archives: If you read a ZipFile, you + cannot modify the password on any encrypted entry, except by extracting + the entry with the original password (if any), removing the original entry + via , and then adding a new + entry with a new Password. + + + + For example, suppose you read a ZipFile, and there is an encrypted + entry. Setting the Password property on that ZipEntry and then + calling Save() on the ZipFile does not update the password + on that entry in the archive. Neither is an exception thrown. Instead, + what happens during the Save() is the existing entry is copied + through to the new zip archive, in its original encrypted form. Upon + re-reading that archive, the entry can be decrypted with its original + password. + + + + If you read a ZipFile, and there is an un-encrypted entry, you can set the + Password on the entry and then call Save() on the ZipFile, and get + encryption on that entry. + + + + + + + This example creates a zip file with two entries, and then extracts the + entries from the zip file. When creating the zip file, the two files are + added to the zip file using password protection. Each entry uses a + different password. During extraction, each file is extracted with the + appropriate password. + + + // create a file with encryption + using (ZipFile zip = new ZipFile()) + { + ZipEntry entry; + entry= zip.AddFile("Declaration.txt"); + entry.Password= "123456!"; + entry = zip.AddFile("Report.xls"); + entry.Password= "1Secret!"; + zip.Save("EncryptedArchive.zip"); + } + + // extract entries that use encryption + using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip")) + { + ZipEntry entry; + entry = zip["Declaration.txt"]; + entry.Password = "123456!"; + entry.Extract("extractDir"); + entry = zip["Report.xls"]; + entry.Password = "1Secret!"; + entry.Extract("extractDir"); + } + + + + + Using zip As New ZipFile + Dim entry as ZipEntry + entry= zip.AddFile("Declaration.txt") + entry.Password= "123456!" + entry = zip.AddFile("Report.xls") + entry.Password= "1Secret!" + zip.Save("EncryptedArchive.zip") + End Using + + + ' extract entries that use encryption + Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip")) + Dim entry as ZipEntry + entry = zip("Declaration.txt") + entry.Password = "123456!" + entry.Extract("extractDir") + entry = zip("Report.xls") + entry.Password = "1Secret!" + entry.Extract("extractDir") + End Using + + + + + + + ZipFile.Password + + + + The action the library should take when extracting a file that already exists. + + + + + This property affects the behavior of the Extract methods (one of the + Extract() or ExtractWithPassword() overloads), when + extraction would would overwrite an existing filesystem file. If you do + not set this property, the library throws an exception when extracting + an entry would overwrite an existing file. + + + + This property has no effect when extracting to a stream, or when the file to be + extracted does not already exist. + + + + + + + This example shows how to set the ExtractExistingFile property in + an ExtractProgress event, in response to user input. The + ExtractProgress event is invoked if and only if the + ExtractExistingFile property was previously set to + ExtractExistingFileAction.InvokeExtractProgressEvent. + + public static void ExtractProgress(object sender, ExtractProgressEventArgs e) + { + if (e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry) + Console.WriteLine("extract {0} ", e.CurrentEntry.FileName); + + else if (e.EventType == ZipProgressEventType.Extracting_ExtractEntryWouldOverwrite) + { + ZipEntry entry = e.CurrentEntry; + string response = null; + // Ask the user if he wants overwrite the file + do + { + Console.Write("Overwrite {0} in {1} ? (y/n/C) ", entry.FileName, e.ExtractLocation); + response = Console.ReadLine(); + Console.WriteLine(); + + } while (response != null && response[0]!='Y' && + response[0]!='N' && response[0]!='C'); + + if (response[0]=='C') + e.Cancel = true; + else if (response[0]=='Y') + entry.ExtractExistingFile = ExtractExistingFileAction.OverwriteSilently; + else + entry.ExtractExistingFile= ExtractExistingFileAction.DoNotOverwrite; + } + } + + + + + + The action to take when an error is encountered while + opening or reading files as they are saved into a zip archive. + + + + + Errors can occur within a call to ZipFile.Save, as the various files contained + in a ZipFile are being saved into the zip archive. During the + Save, DotNetZip will perform a File.Open on the file + associated to the ZipEntry, and then will read the entire contents of + the file as it is zipped. Either the open or the Read may fail, because + of lock conflicts or other reasons. Using this property, you can + specify the action to take when such errors occur. + + + + Typically you will NOT set this property on individual ZipEntry + instances. Instead, you will set the ZipFile.ZipErrorAction property on + the ZipFile instance, before adding any entries to the + ZipFile. If you do this, errors encountered on behalf of any of + the entries in the ZipFile will be handled the same way. + + + + But, if you use a handler, you will want + to set this property on the ZipEntry within the handler, to + communicate back to DotNetZip what you would like to do with the + particular error. + + + + + + + + + Indicates whether the entry was included in the most recent save. + + + An entry can be excluded or skipped from a save if there is an error + opening or reading the entry. + + + + + + A callback that allows the application to specify the compression to use + for a given entry that is about to be added to the zip archive. + + + + + See + + + + + + Set to indicate whether to use UTF-8 encoding for filenames and comments. + + + + + + If this flag is set, the comment and filename for the entry will be + encoded with UTF-8, as described in the Zip + specification, if necessary. "Necessary" means, the filename or + entry comment (if any) cannot be reflexively encoded and decoded using the + default code page, IBM437. + + + + Setting this flag to true is equivalent to setting to System.Text.Encoding.UTF8. + + + + This flag has no effect or relation to the text encoding used within the + file itself. + + + + + + + The text encoding to use for the FileName and Comment on this ZipEntry, + when the default encoding is insufficient. + + + + + + Don't use this property. See . + + + + + + + Specifies the alternate text encoding used by this ZipEntry + + + + The default text encoding used in Zip files for encoding filenames and + comments is IBM437, which is something like a superset of ASCII. In + cases where this is insufficient, applications can specify an + alternate encoding. + + + When creating a zip file, the usage of the alternate encoding is + governed by the property. + Typically you would set both properties to tell DotNetZip to employ an + encoding that is not IBM437 in the zipfile you are creating. + + + Keep in mind that because the ZIP specification states that the only + valid encodings to use are IBM437 and UTF-8, if you use something + other than that, then zip tools and libraries may not be able to + successfully read the zip archive you generate. + + + The zip specification states that applications should presume that + IBM437 is in use, except when a special bit is set, which indicates + UTF-8. There is no way to specify an arbitrary code page, within the + zip file itself. When you create a zip file encoded with gb2312 or + ibm861 or anything other than IBM437 or UTF-8, then the application + that reads the zip file needs to "know" which code page to use. In + some cases, the code page used when reading is chosen implicitly. For + example, WinRar uses the ambient code page for the host desktop + operating system. The pitfall here is that if you create a zip in + Copenhagen and send it to Tokyo, the reader of the zipfile may not be + able to decode successfully. + + + + This example shows how to create a zipfile encoded with a + language-specific encoding: + + using (var zip = new ZipFile()) + { + zip.AlternateEnoding = System.Text.Encoding.GetEncoding("ibm861"); + zip.AlternateEnodingUsage = ZipOption.Always; + zip.AddFileS(arrayOfFiles); + zip.Save("Myarchive-Encoded-in-IBM861.zip"); + } + + + + + + + Describes if and when this instance should apply + AlternateEncoding to encode the FileName and Comment, when + saving. + + + + + + Indicates whether an entry is marked as a text file. Be careful when + using on this property. Unless you have a good reason, you should + probably ignore this property. + + + + + The ZIP format includes a provision for specifying whether an entry in + the zip archive is a text or binary file. This property exposes that + metadata item. Be careful when using this property: It's not clear + that this property as a firm meaning, across tools and libraries. + + + + To be clear, when reading a zip file, the property value may or may + not be set, and its value may or may not be valid. Not all entries + that you may think of as "text" entries will be so marked, and entries + marked as "text" are not guaranteed in any way to be text entries. + Whether the value is set and set correctly depends entirely on the + application that produced the zip file. + + + + There are many zip tools available, and when creating zip files, some + of them "respect" the IsText metadata field, and some of them do not. + Unfortunately, even when an application tries to do "the right thing", + it's not always clear what "the right thing" is. + + + + There's no firm definition of just what it means to be "a text file", + and the zip specification does not help in this regard. Twenty years + ago, text was ASCII, each byte was less than 127. IsText meant, all + bytes in the file were less than 127. These days, it is not the case + that all text files have all bytes less than 127. Any unicode file + may have bytes that are above 0x7f. The zip specification has nothing + to say on this topic. Therefore, it's not clear what IsText really + means. + + + + This property merely tells a reading application what is stored in the + metadata for an entry, without guaranteeing its validity or its + meaning. + + + + When DotNetZip is used to create a zipfile, it attempts to set this + field "correctly." For example, if a file ends in ".txt", this field + will be set. Your application may override that default setting. When + writing a zip file, you must set the property before calling + Save() on the ZipFile. + + + + When reading a zip file, a more general way to decide just what kind + of file is contained in a particular entry is to use the file type + database stored in the operating system. The operating system stores + a table that says, a file with .jpg extension is a JPG image file, a + file with a .xml extension is an XML document, a file with a .txt is a + pure ASCII text document, and so on. To get this information on + Windows, you + need to read and parse the registry. + + + + + using (var zip = new ZipFile()) + { + var e = zip.UpdateFile("Descriptions.mme", ""); + e.IsText = true; + zip.Save(zipPath); + } + + + + Using zip As New ZipFile + Dim e2 as ZipEntry = zip.AddFile("Descriptions.mme", "") + e.IsText= True + zip.Save(zipPath) + End Using + + + + + + An enum that specifies the type of timestamp available on the ZipEntry. + + + + + + The last modified time of a file can be stored in multiple ways in + a zip file, and they are not mutually exclusive: + + + + + In the so-called "DOS" format, which has a 2-second precision. Values + are rounded to the nearest even second. For example, if the time on the + file is 12:34:43, then it will be stored as 12:34:44. This first value + is accessible via the LastModified property. This value is always + present in the metadata for each zip entry. In some cases the value is + invalid, or zero. + + + + In the so-called "Windows" or "NTFS" format, as an 8-byte integer + quantity expressed as the number of 1/10 milliseconds (in other words + the number of 100 nanosecond units) since January 1, 1601 (UTC). This + format is how Windows represents file times. This time is accessible + via the ModifiedTime property. + + + + In the "Unix" format, a 4-byte quantity specifying the number of seconds since + January 1, 1970 UTC. + + + + In an older format, now deprecated but still used by some current + tools. This format is also a 4-byte quantity specifying the number of + seconds since January 1, 1970 UTC. + + + + + + This bit field describes which of the formats were found in a ZipEntry that was read. + + + + + + + Default value. + + + + + A DOS timestamp with 2-second precision. + + + + + A Windows timestamp with 100-ns precision. + + + + + A Unix timestamp with 1-second precision. + + + + + A Unix timestamp with 1-second precision, stored in InfoZip v1 format. This + format is outdated and is supported for reading archives only. + + + + + The method of compression to use for a particular ZipEntry. + + + + PKWare's + ZIP Specification describes a number of distinct + cmopression methods that can be used within a zip + file. DotNetZip supports a subset of them. + + + + + No compression at all. For COM environments, the value is 0 (zero). + + + + + DEFLATE compression, as described in IETF RFC + 1951. This is the "normal" compression used in zip + files. For COM environments, the value is 8. + + + + + BZip2 compression, a compression algorithm developed by Julian Seward. + For COM environments, the value is 12. + + + + + The ZipFile type represents a zip archive file. + + + + + This is the main type in the DotNetZip class library. This class reads and + writes zip files, as defined in the specification + for zip files described by PKWare. The compression for this + implementation is provided by a managed-code version of Zlib, included with + DotNetZip in the classes in the Ionic.Zlib namespace. + + + + This class provides a general purpose zip file capability. Use it to read, + create, or update zip files. When you want to create zip files using a + Stream type to write the zip file, you may want to consider the class. + + + + Both the ZipOutputStream class and the ZipFile class can + be used to create zip files. Both of them support many of the common zip + features, including Unicode, different compression methods and levels, + and ZIP64. They provide very similar performance when creating zip + files. + + + + The ZipFile class is generally easier to use than + ZipOutputStream and should be considered a higher-level interface. For + example, when creating a zip file via calls to the PutNextEntry() and + Write() methods on the ZipOutputStream class, the caller is + responsible for opening the file, reading the bytes from the file, writing + those bytes into the ZipOutputStream, setting the attributes on the + ZipEntry, and setting the created, last modified, and last accessed + timestamps on the zip entry. All of these things are done automatically by a + call to ZipFile.AddFile(). + For this reason, the ZipOutputStream is generally recommended for use + only when your application emits arbitrary data, not necessarily data from a + filesystem file, directly into a zip file, and does so using a Stream + metaphor. + + + + Aside from the differences in programming model, there are other + differences in capability between the two classes. + + + + + ZipFile can be used to read and extract zip files, in addition to + creating zip files. ZipOutputStream cannot read zip files. If you want + to use a stream to read zip files, check out the class. + + + + ZipOutputStream does not support the creation of segmented or spanned + zip files. + + + + ZipOutputStream cannot produce a self-extracting archive. + + + + + Be aware that the ZipFile class implements the interface. In order for ZipFile to + produce a valid zip file, you use use it within a using clause (Using + in VB), or call the Dispose() method explicitly. See the examples + for how to employ a using clause. + + + + + + + Adds an item, either a file or a directory, to a zip file archive. + + + + + This method is handy if you are adding things to zip archive and don't + want to bother distinguishing between directories or files. Any files are + added as single entries. A directory added through this method is added + recursively: all files and subdirectories contained within the directory + are added to the ZipFile. + + + + The name of the item may be a relative path or a fully-qualified + path. Remember, the items contained in ZipFile instance get written + to the disk only when you call or a similar + save method. + + + + The directory name used for the file within the archive is the same + as the directory name (potentially a relative path) specified in the + . + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + + + This method has two overloads. + + the name of the file or directory to add. + + The ZipEntry added. + + + + Adds an item, either a file or a directory, to a zip file archive, + explicitly specifying the directory path to be used in the archive. + + + + + If adding a directory, the add is recursive on all files and + subdirectories contained within it. + + + The name of the item may be a relative path or a fully-qualified path. + The item added by this call to the ZipFile is not read from the + disk nor written to the zip file archive until the application calls + Save() on the ZipFile. + + + + This version of the method allows the caller to explicitly specify the + directory path to be used in the archive, which would override the + "natural" path of the filesystem file. + + + + Encryption will be used on the file data if the Password has + been set on the ZipFile object, prior to calling this method. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + Thrown if the file or directory passed in does not exist. + + + the name of the file or directory to add. + + + + The name of the directory path to use within the zip archive. This path + need not refer to an extant directory in the current filesystem. If the + files within the zip are later extracted, this is the path used for the + extracted file. Passing null (Nothing in VB) will use the + path on the fileOrDirectoryName. Passing the empty string ("") will + insert the item at the root path within the archive. + + + + + + + + This example shows how to zip up a set of files into a flat hierarchy, + regardless of where in the filesystem the files originated. The resulting + zip archive will contain a toplevel directory named "flat", which itself + will contain files Readme.txt, MyProposal.docx, and Image1.jpg. A + subdirectory under "flat" called SupportFiles will contain all the files + in the "c:\SupportFiles" directory on disk. + + + String[] itemnames= { + "c:\\fixedContent\\Readme.txt", + "MyProposal.docx", + "c:\\SupportFiles", // a directory + "images\\Image1.jpg" + }; + + try + { + using (ZipFile zip = new ZipFile()) + { + for (int i = 1; i < itemnames.Length; i++) + { + // will add Files or Dirs, recurses and flattens subdirectories + zip.AddItem(itemnames[i],"flat"); + } + zip.Save(ZipToCreate); + } + } + catch (System.Exception ex1) + { + System.Console.Error.WriteLine("exception: {0}", ex1); + } + + + + Dim itemnames As String() = _ + New String() { "c:\fixedContent\Readme.txt", _ + "MyProposal.docx", _ + "SupportFiles", _ + "images\Image1.jpg" } + Try + Using zip As New ZipFile + Dim i As Integer + For i = 1 To itemnames.Length - 1 + ' will add Files or Dirs, recursing and flattening subdirectories. + zip.AddItem(itemnames(i), "flat") + Next i + zip.Save(ZipToCreate) + End Using + Catch ex1 As Exception + Console.Error.WriteLine("exception: {0}", ex1.ToString()) + End Try + + + The ZipEntry added. + + + + Adds a File to a Zip file archive. + + + + + This call collects metadata for the named file in the filesystem, + including the file attributes and the timestamp, and inserts that metadata + into the resulting ZipEntry. Only when the application calls Save() on + the ZipFile, does DotNetZip read the file from the filesystem and + then write the content to the zip file archive. + + + + This method will throw an exception if an entry with the same name already + exists in the ZipFile. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + In this example, three files are added to a Zip archive. The ReadMe.txt + file will be placed in the root of the archive. The .png file will be + placed in a folder within the zip called photos\personal. The pdf file + will be included into a folder within the zip called Desktop. + + + try + { + using (ZipFile zip = new ZipFile()) + { + zip.AddFile("c:\\photos\\personal\\7440-N49th.png"); + zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf"); + zip.AddFile("ReadMe.txt"); + + zip.Save("Package.zip"); + } + } + catch (System.Exception ex1) + { + System.Console.Error.WriteLine("exception: " + ex1); + } + + + + Try + Using zip As ZipFile = New ZipFile + zip.AddFile("c:\photos\personal\7440-N49th.png") + zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf") + zip.AddFile("ReadMe.txt") + zip.Save("Package.zip") + End Using + Catch ex1 As Exception + Console.Error.WriteLine("exception: {0}", ex1.ToString) + End Try + + + + This method has two overloads. + + + + + + + The name of the file to add. It should refer to a file in the filesystem. + The name of the file may be a relative path or a fully-qualified path. + + The ZipEntry corresponding to the File added. + + + + Adds a File to a Zip file archive, potentially overriding the path to be + used within the zip archive. + + + + + The file added by this call to the ZipFile is not written to the + zip file archive until the application calls Save() on the ZipFile. + + + + This method will throw an exception if an entry with the same name already + exists in the ZipFile. + + + + This version of the method allows the caller to explicitly specify the + directory path to be used in the archive. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + In this example, three files are added to a Zip archive. The ReadMe.txt + file will be placed in the root of the archive. The .png file will be + placed in a folder within the zip called images. The pdf file will be + included into a folder within the zip called files\docs, and will be + encrypted with the given password. + + + try + { + using (ZipFile zip = new ZipFile()) + { + // the following entry will be inserted at the root in the archive. + zip.AddFile("c:\\datafiles\\ReadMe.txt", ""); + // this image file will be inserted into the "images" directory in the archive. + zip.AddFile("c:\\photos\\personal\\7440-N49th.png", "images"); + // the following will result in a password-protected file called + // files\\docs\\2008-Regional-Sales-Report.pdf in the archive. + zip.Password = "EncryptMe!"; + zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf", "files\\docs"); + zip.Save("Archive.zip"); + } + } + catch (System.Exception ex1) + { + System.Console.Error.WriteLine("exception: {0}", ex1); + } + + + + Try + Using zip As ZipFile = New ZipFile + ' the following entry will be inserted at the root in the archive. + zip.AddFile("c:\datafiles\ReadMe.txt", "") + ' this image file will be inserted into the "images" directory in the archive. + zip.AddFile("c:\photos\personal\7440-N49th.png", "images") + ' the following will result in a password-protected file called + ' files\\docs\\2008-Regional-Sales-Report.pdf in the archive. + zip.Password = "EncryptMe!" + zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf", "files\documents") + zip.Save("Archive.zip") + End Using + Catch ex1 As Exception + Console.Error.WriteLine("exception: {0}", ex1) + End Try + + + + + + + + + The name of the file to add. The name of the file may be a relative path + or a fully-qualified path. + + + + Specifies a directory path to use to override any path in the fileName. + This path may, or may not, correspond to a real directory in the current + filesystem. If the files within the zip are later extracted, this is the + path used for the extracted file. Passing null (Nothing in + VB) will use the path on the fileName, if any. Passing the empty string + ("") will insert the item at the root path within the archive. + + + The ZipEntry corresponding to the file added. + + + + This method removes a collection of entries from the ZipFile. + + + + A collection of ZipEntry instances from this zip file to be removed. For + example, you can pass in an array of ZipEntry instances; or you can call + SelectEntries(), and then add or remove entries from that + ICollection<ZipEntry> (ICollection(Of ZipEntry) in VB), and pass + that ICollection to this method. + + + + + + + + This method removes a collection of entries from the ZipFile, by name. + + + + A collection of strings that refer to names of entries to be removed + from the ZipFile. For example, you can pass in an array or a + List of Strings that provide the names of entries to be removed. + + + + + + + + This method adds a set of files to the ZipFile. + + + + + Use this method to add a set of files to the zip archive, in one call. + For example, a list of files received from + System.IO.Directory.GetFiles() can be added to a zip archive in one + call. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to each + ZipEntry added. + + + + + The collection of names of the files to add. Each string should refer to a + file in the filesystem. The name of the file may be a relative path or a + fully-qualified path. + + + + This example shows how to create a zip file, and add a few files into it. + + String ZipFileToCreate = "archive1.zip"; + String DirectoryToZip = "c:\\reports"; + using (ZipFile zip = new ZipFile()) + { + // Store all files found in the top level directory, into the zip archive. + String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip); + zip.AddFiles(filenames); + zip.Save(ZipFileToCreate); + } + + + + Dim ZipFileToCreate As String = "archive1.zip" + Dim DirectoryToZip As String = "c:\reports" + Using zip As ZipFile = New ZipFile + ' Store all files found in the top level directory, into the zip archive. + Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip) + zip.AddFiles(filenames) + zip.Save(ZipFileToCreate) + End Using + + + + + + + + Adds or updates a set of files in the ZipFile. + + + + + Any files that already exist in the archive are updated. Any files that + don't yet exist in the archive are added. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to each + ZipEntry added. + + + + + The collection of names of the files to update. Each string should refer to a file in + the filesystem. The name of the file may be a relative path or a fully-qualified path. + + + + + + Adds a set of files to the ZipFile, using the + specified directory path in the archive. + + + + + Any directory structure that may be present in the + filenames contained in the list is "flattened" in the + archive. Each file in the list is added to the archive in + the specified top-level directory. + + + + For ZipFile properties including , , , , , , and , their respective values at the + time of this call will be applied to each ZipEntry added. + + + + + The names of the files to add. Each string should refer to + a file in the filesystem. The name of the file may be a + relative path or a fully-qualified path. + + + + Specifies a directory path to use to override any path in the file name. + Th is path may, or may not, correspond to a real directory in the current + filesystem. If the files within the zip are later extracted, this is the + path used for the extracted file. Passing null (Nothing in + VB) will use the path on each of the fileNames, if any. Passing + the empty string ("") will insert the item at the root path within the + archive. + + + + + + + Adds a set of files to the ZipFile, using the specified directory + path in the archive, and preserving the full directory structure in the + filenames. + + + + + If preserveDirHierarchy is true, any directory structure present in the + filenames contained in the list is preserved in the archive. On the other + hand, if preserveDirHierarchy is false, any directory structure that may + be present in the filenames contained in the list is "flattened" in the + archive; Each file in the list is added to the archive in the specified + top-level directory. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to each + ZipEntry added. + + + + + + The names of the files to add. Each string should refer to a file in the + filesystem. The name of the file may be a relative path or a + fully-qualified path. + + + + Specifies a directory path to use to override any path in the file name. + This path may, or may not, correspond to a real directory in the current + filesystem. If the files within the zip are later extracted, this is the + path used for the extracted file. Passing null (Nothing in + VB) will use the path on each of the fileNames, if any. Passing + the empty string ("") will insert the item at the root path within the + archive. + + + + whether the entries in the zip archive will reflect the directory + hierarchy that is present in the various filenames. For example, if + includes two paths, \Animalia\Chordata\Mammalia\Info.txt and + \Plantae\Magnoliophyta\Dicotyledon\Info.txt, then calling this method with + = false will result in an + exception because of a duplicate entry name, while calling this method + with = true will result in the + full direcory paths being included in the entries added to the ZipFile. + + + + + + Adds or updates a set of files to the ZipFile, using the specified + directory path in the archive. + + + + + + Any files that already exist in the archive are updated. Any files that + don't yet exist in the archive are added. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to each + ZipEntry added. + + + + + The names of the files to add or update. Each string should refer to a + file in the filesystem. The name of the file may be a relative path or a + fully-qualified path. + + + + Specifies a directory path to use to override any path in the file name. + This path may, or may not, correspond to a real directory in the current + filesystem. If the files within the zip are later extracted, this is the + path used for the extracted file. Passing null (Nothing in + VB) will use the path on each of the fileNames, if any. Passing + the empty string ("") will insert the item at the root path within the + archive. + + + + + + + Adds or Updates a File in a Zip file archive. + + + + + This method adds a file to a zip archive, or, if the file already exists + in the zip archive, this method Updates the content of that given filename + in the zip archive. The UpdateFile method might more accurately be + called "AddOrUpdateFile". + + + + Upon success, there is no way for the application to learn whether the file + was added versus updated. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + This example shows how to Update an existing entry in a zipfile. The first + call to UpdateFile adds the file to the newly-created zip archive. The + second call to UpdateFile updates the content for that file in the zip + archive. + + + using (ZipFile zip1 = new ZipFile()) + { + // UpdateFile might more accurately be called "AddOrUpdateFile" + zip1.UpdateFile("MyDocuments\\Readme.txt"); + zip1.UpdateFile("CustomerList.csv"); + zip1.Comment = "This zip archive has been created."; + zip1.Save("Content.zip"); + } + + using (ZipFile zip2 = ZipFile.Read("Content.zip")) + { + zip2.UpdateFile("Updates\\Readme.txt"); + zip2.Comment = "This zip archive has been updated: The Readme.txt file has been changed."; + zip2.Save(); + } + + + + Using zip1 As New ZipFile + ' UpdateFile might more accurately be called "AddOrUpdateFile" + zip1.UpdateFile("MyDocuments\Readme.txt") + zip1.UpdateFile("CustomerList.csv") + zip1.Comment = "This zip archive has been created." + zip1.Save("Content.zip") + End Using + + Using zip2 As ZipFile = ZipFile.Read("Content.zip") + zip2.UpdateFile("Updates\Readme.txt") + zip2.Comment = "This zip archive has been updated: The Readme.txt file has been changed." + zip2.Save + End Using + + + + + + + + + The name of the file to add or update. It should refer to a file in the + filesystem. The name of the file may be a relative path or a + fully-qualified path. + + + + The ZipEntry corresponding to the File that was added or updated. + + + + + Adds or Updates a File in a Zip file archive. + + + + + This method adds a file to a zip archive, or, if the file already exists + in the zip archive, this method Updates the content of that given filename + in the zip archive. + + + + This version of the method allows the caller to explicitly specify the + directory path to be used in the archive. The entry to be added or + updated is found by using the specified directory path, combined with the + basename of the specified filename. + + + + Upon success, there is no way for the application to learn if the file was + added versus updated. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + + + The name of the file to add or update. It should refer to a file in the + filesystem. The name of the file may be a relative path or a + fully-qualified path. + + + + Specifies a directory path to use to override any path in the + fileName. This path may, or may not, correspond to a real + directory in the current filesystem. If the files within the zip are + later extracted, this is the path used for the extracted file. Passing + null (Nothing in VB) will use the path on the + fileName, if any. Passing the empty string ("") will insert the + item at the root path within the archive. + + + + The ZipEntry corresponding to the File that was added or updated. + + + + + Add or update a directory in a zip archive. + + + + If the specified directory does not exist in the archive, then this method + is equivalent to calling AddDirectory(). If the specified + directory already exists in the archive, then this method updates any + existing entries, and adds any new entries. Any entries that are in the + zip archive but not in the specified directory, are left alone. In other + words, the contents of the zip file will be a union of the previous + contents and the new files. + + + + + + + + The path to the directory to be added to the zip archive, or updated in + the zip archive. + + + + The ZipEntry corresponding to the Directory that was added or updated. + + + + + Add or update a directory in the zip archive at the specified root + directory in the archive. + + + + If the specified directory does not exist in the archive, then this method + is equivalent to calling AddDirectory(). If the specified + directory already exists in the archive, then this method updates any + existing entries, and adds any new entries. Any entries that are in the + zip archive but not in the specified directory, are left alone. In other + words, the contents of the zip file will be a union of the previous + contents and the new files. + + + + + + + + The path to the directory to be added to the zip archive, or updated + in the zip archive. + + + + Specifies a directory path to use to override any path in the + directoryName. This path may, or may not, correspond to a real + directory in the current filesystem. If the files within the zip are + later extracted, this is the path used for the extracted file. Passing + null (Nothing in VB) will use the path on the + directoryName, if any. Passing the empty string ("") will insert + the item at the root path within the archive. + + + + The ZipEntry corresponding to the Directory that was added or updated. + + + + + Add or update a file or directory in the zip archive. + + + + + This is useful when the application is not sure or does not care if the + item to be added is a file or directory, and does not know or does not + care if the item already exists in the ZipFile. Calling this method + is equivalent to calling RemoveEntry() if an entry by the same name + already exists, followed calling by AddItem(). + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + + + the path to the file or directory to be added or updated. + + + + + Add or update a file or directory. + + + + + This method is useful when the application is not sure or does not care if + the item to be added is a file or directory, and does not know or does not + care if the item already exists in the ZipFile. Calling this method + is equivalent to calling RemoveEntry(), if an entry by that name + exists, and then calling AddItem(). + + + + This version of the method allows the caller to explicitly specify the + directory path to be used for the item being added to the archive. The + entry or entries that are added or updated will use the specified + DirectoryPathInArchive. Extracting the entry from the archive will + result in a file stored in that directory path. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + + + The path for the File or Directory to be added or updated. + + + Specifies a directory path to use to override any path in the + itemName. This path may, or may not, correspond to a real + directory in the current filesystem. If the files within the zip are + later extracted, this is the path used for the extracted file. Passing + null (Nothing in VB) will use the path on the + itemName, if any. Passing the empty string ("") will insert the + item at the root path within the archive. + + + + + Adds a named entry into the zip archive, taking content for the entry + from a string. + + + + Calling this method creates an entry using the given fileName and + directory path within the archive. There is no need for a file by the + given name to exist in the filesystem; the name is used within the zip + archive only. The content for the entry is encoded using the default text + encoding for the machine, or on Silverlight, using UTF-8. + + + + The content of the file, should it be extracted from the zip. + + + + The name, including any path, to use for the entry within the archive. + + + The ZipEntry added. + + + + This example shows how to add an entry to the zipfile, using a string as + content for that entry. + + + string Content = "This string will be the content of the Readme.txt file in the zip archive."; + using (ZipFile zip1 = new ZipFile()) + { + zip1.AddFile("MyDocuments\\Resume.doc", "files"); + zip1.AddEntry("Readme.txt", Content); + zip1.Comment = "This zip file was created at " + System.DateTime.Now.ToString("G"); + zip1.Save("Content.zip"); + } + + + + Public Sub Run() + Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive." + Using zip1 As ZipFile = New ZipFile + zip1.AddEntry("Readme.txt", Content) + zip1.AddFile("MyDocuments\Resume.doc", "files") + zip1.Comment = ("This zip file was created at " & DateTime.Now.ToString("G")) + zip1.Save("Content.zip") + End Using + End Sub + + + + + + Adds a named entry into the zip archive, taking content for the entry + from a string, and using the specified text encoding. + + + + + + Calling this method creates an entry using the given fileName and + directory path within the archive. There is no need for a file by the + given name to exist in the filesystem; the name is used within the zip + archive only. + + + + The content for the entry, a string value, is encoded using the given + text encoding. A BOM (byte-order-mark) is emitted into the file, if the + Encoding parameter is set for that. + + + + Most Encoding classes support a constructor that accepts a boolean, + indicating whether to emit a BOM or not. For example see . + + + + + + The name, including any path, to use within the archive for the entry. + + + + The content of the file, should it be extracted from the zip. + + + + The text encoding to use when encoding the string. Be aware: This is + distinct from the text encoding used to encode the fileName, as specified + in . + + + The ZipEntry added. + + + + + Create an entry in the ZipFile using the given Stream + as input. The entry will have the given filename. + + + + + + The application should provide an open, readable stream; in this case it + will be read during the call to or one of + its overloads. + + + + The passed stream will be read from its current position. If + necessary, callers should set the position in the stream before + calling AddEntry(). This might be appropriate when using this method + with a MemoryStream, for example. + + + + In cases where a large number of streams will be added to the + ZipFile, the application may wish to avoid maintaining all of the + streams open simultaneously. To handle this situation, the application + should use the + overload. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + This example adds a single entry to a ZipFile via a Stream. + + + + String zipToCreate = "Content.zip"; + String fileNameInArchive = "Content-From-Stream.bin"; + using (System.IO.Stream streamToRead = MyStreamOpener()) + { + using (ZipFile zip = new ZipFile()) + { + ZipEntry entry= zip.AddEntry(fileNameInArchive, streamToRead); + zip.AddFile("Readme.txt"); + zip.Save(zipToCreate); // the stream is read implicitly here + } + } + + + + Dim zipToCreate As String = "Content.zip" + Dim fileNameInArchive As String = "Content-From-Stream.bin" + Using streamToRead as System.IO.Stream = MyStreamOpener() + Using zip As ZipFile = New ZipFile() + Dim entry as ZipEntry = zip.AddEntry(fileNameInArchive, streamToRead) + zip.AddFile("Readme.txt") + zip.Save(zipToCreate) '' the stream is read implicitly, here + End Using + End Using + + + + + + + The name, including any path, which is shown in the zip file for the added + entry. + + + The input stream from which to grab content for the file + + The ZipEntry added. + + + + Add a ZipEntry for which content is written directly by the application. + + + + + When the application needs to write the zip entry data, use this + method to add the ZipEntry. For example, in the case that the + application wishes to write the XML representation of a DataSet into + a ZipEntry, the application can use this method to do so. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + About progress events: When using the WriteDelegate, DotNetZip does + not issue any SaveProgress events with EventType = + Saving_EntryBytesRead. (This is because it is the + application's code that runs in WriteDelegate - there's no way for + DotNetZip to know when to issue a EntryBytesRead event.) + Applications that want to update a progress bar or similar status + indicator should do so from within the WriteDelegate + itself. DotNetZip will issue the other SaveProgress events, + including + Saving_Started, + + Saving_BeforeWriteEntry, and + Saving_AfterWriteEntry. + + + + Note: When you use PKZip encryption, it's normally necessary to + compute the CRC of the content to be encrypted, before compressing or + encrypting it. Therefore, when using PKZip encryption with a + WriteDelegate, the WriteDelegate CAN BE called twice: once to compute + the CRC, and the second time to potentially compress and + encrypt. Surprising, but true. This is because PKWARE specified that + the encryption initialization data depends on the CRC. + If this happens, for each call of the delegate, your + application must stream the same entry data in its entirety. If your + application writes different data during the second call, it will + result in a corrupt zip file. + + + + The double-read behavior happens with all types of entries, not only + those that use WriteDelegate. It happens if you add an entry from a + filesystem file, or using a string, or a stream, or an opener/closer + pair. But in those cases, DotNetZip takes care of reading twice; in + the case of the WriteDelegate, the application code gets invoked + twice. Be aware. + + + + As you can imagine, this can cause performance problems for large + streams, and it can lead to correctness problems when you use a + WriteDelegate. This is a pretty big pitfall. There are two + ways to avoid it. First, and most preferred: don't use PKZIP + encryption. If you use the WinZip AES encryption, this problem + doesn't occur, because the encryption protocol doesn't require the CRC + up front. Second: if you do choose to use PKZIP encryption, write out + to a non-seekable stream (like standard output, or the + Response.OutputStream in an ASP.NET application). In this case, + DotNetZip will use an alternative encryption protocol that does not + rely on the CRC of the content. This also implies setting bit 3 in + the zip entry, which still presents problems for some zip tools. + + + + In the future I may modify DotNetZip to *always* use bit 3 when PKZIP + encryption is in use. This seems like a win overall, but there will + be some work involved. If you feel strongly about it, visit the + DotNetZip forums and vote up the Workitem + tracking this issue. + + + + + the name of the entry to add + the delegate which will write the entry content + the ZipEntry added + + + + This example shows an application filling a DataSet, then saving the + contents of that DataSet as XML, into a ZipEntry in a ZipFile, using an + anonymous delegate in C#. The DataSet XML is never saved to a disk file. + + + var c1= new System.Data.SqlClient.SqlConnection(connstring1); + var da = new System.Data.SqlClient.SqlDataAdapter() + { + SelectCommand= new System.Data.SqlClient.SqlCommand(strSelect, c1) + }; + + DataSet ds1 = new DataSet(); + da.Fill(ds1, "Invoices"); + + using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) + { + zip.AddEntry(zipEntryName, (name,stream) => ds1.WriteXml(stream) ); + zip.Save(zipFileName); + } + + + + + + This example uses an anonymous method in C# as the WriteDelegate to provide + the data for the ZipEntry. The example is a bit contrived - the + AddFile() method is a simpler way to insert the contents of a file + into an entry in a zip file. On the other hand, if there is some sort of + processing or transformation of the file contents required before writing, + the application could use the WriteDelegate to do it, in this way. + + + using (var input = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite )) + { + using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) + { + zip.AddEntry(zipEntryName, (name,output) => + { + byte[] buffer = new byte[BufferSize]; + int n; + while ((n = input.Read(buffer, 0, buffer.Length)) != 0) + { + // could transform the data here... + output.Write(buffer, 0, n); + // could update a progress bar here + } + }); + + zip.Save(zipFileName); + } + } + + + + + + This example uses a named delegate in VB to write data for the given + ZipEntry (VB9 does not have anonymous delegates). The example here is a bit + contrived - a simpler way to add the contents of a file to a ZipEntry is to + simply use the appropriate AddFile() method. The key scenario for + which the WriteDelegate makes sense is saving a DataSet, in XML + format, to the zip file. The DataSet can write XML to a stream, and the + WriteDelegate is the perfect place to write into the zip file. There may be + other data structures that can write to a stream, but cannot be read as a + stream. The WriteDelegate would be appropriate for those cases as + well. + + + Private Sub WriteEntry (ByVal name As String, ByVal output As Stream) + Using input As FileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite) + Dim n As Integer = -1 + Dim buffer As Byte() = New Byte(BufferSize){} + Do While n <> 0 + n = input.Read(buffer, 0, buffer.Length) + output.Write(buffer, 0, n) + Loop + End Using + End Sub + + Public Sub Run() + Using zip = New ZipFile + zip.AddEntry(zipEntryName, New WriteDelegate(AddressOf WriteEntry)) + zip.Save(zipFileName) + End Using + End Sub + + + + + + Add an entry, for which the application will provide a stream, + just-in-time. + + + + + In cases where the application wishes to open the stream that holds + the content for the ZipEntry, on a just-in-time basis, the application + can use this method and provide delegates to open and close the + stream. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + This example uses anonymous methods in C# to open and close the + source stream for the content for a zip entry. In a real + application, the logic for the OpenDelegate would probably be more + involved. + + + using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile()) + { + zip.AddEntry(zipEntryName, + (name) => File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ), + (name, stream) => stream.Close() + ); + + zip.Save(zipFileName); + } + + + + + + + This example uses delegates in VB.NET to open and close the + the source stream for the content for a zip entry. VB 9.0 lacks + support for "Sub" lambda expressions, and so the CloseDelegate must + be an actual, named Sub. + + + + Function MyStreamOpener(ByVal entryName As String) As Stream + '' This simply opens a file. You probably want to do somethinig + '' more involved here: open a stream to read from a database, + '' open a stream on an HTTP connection, and so on. + Return File.OpenRead(entryName) + End Function + + Sub MyStreamCloser(entryName As String, stream As Stream) + stream.Close() + End Sub + + Public Sub Run() + Dim dirToZip As String = "fodder" + Dim zipFileToCreate As String = "Archive.zip" + Dim opener As OpenDelegate = AddressOf MyStreamOpener + Dim closer As CloseDelegate = AddressOf MyStreamCloser + Dim numFilestoAdd As Int32 = 4 + Using zip As ZipFile = New ZipFile + Dim i As Integer + For i = 0 To numFilesToAdd - 1 + zip.AddEntry(String.Format("content-{0:000}.txt"), opener, closer) + Next i + zip.Save(zipFileToCreate) + End Using + End Sub + + + + + the name of the entry to add + + the delegate that will be invoked to open the stream + + + the delegate that will be invoked to close the stream + + the ZipEntry added + + + + + Updates the given entry in the ZipFile, using the given + string as content for the ZipEntry. + + + + + + Calling this method is equivalent to removing the ZipEntry for + the given file name and directory path, if it exists, and then calling + . See the documentation for + that method for further explanation. The string content is encoded + using the default encoding for the machine, or on Silverlight, using + UTF-8. This encoding is distinct from the encoding used for the + filename itself. See . + + + + + + The name, including any path, to use within the archive for the entry. + + + + The content of the file, should it be extracted from the zip. + + + The ZipEntry added. + + + + + Updates the given entry in the ZipFile, using the given string as + content for the ZipEntry. + + + + Calling this method is equivalent to removing the ZipEntry for the + given file name and directory path, if it exists, and then calling . See the + documentation for that method for further explanation. + + + + The name, including any path, to use within the archive for the entry. + + + + The content of the file, should it be extracted from the zip. + + + + The text encoding to use when encoding the string. Be aware: This is + distinct from the text encoding used to encode the filename. See . + + + The ZipEntry added. + + + + + Updates the given entry in the ZipFile, using the given delegate + as the source for content for the ZipEntry. + + + + Calling this method is equivalent to removing the ZipEntry for the + given file name and directory path, if it exists, and then calling . See the + documentation for that method for further explanation. + + + + The name, including any path, to use within the archive for the entry. + + + the delegate which will write the entry content. + + The ZipEntry added. + + + + + Updates the given entry in the ZipFile, using the given delegates + to open and close the stream that provides the content for the ZipEntry. + + + + Calling this method is equivalent to removing the ZipEntry for the + given file name and directory path, if it exists, and then calling . See the + documentation for that method for further explanation. + + + + The name, including any path, to use within the archive for the entry. + + + + the delegate that will be invoked to open the stream + + + the delegate that will be invoked to close the stream + + + The ZipEntry added or updated. + + + + + Updates the given entry in the ZipFile, using the given stream as + input, and the given filename and given directory Path. + + + + + Calling the method is equivalent to calling RemoveEntry() if an + entry by the same name already exists, and then calling AddEntry() + with the given fileName and stream. + + + + The stream must be open and readable during the call to + ZipFile.Save. You can dispense the stream on a just-in-time basis + using the property. Check the + documentation of that property for more information. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to the + ZipEntry added. + + + + + + + + + The name, including any path, to use within the archive for the entry. + + + The input stream from which to read file data. + The ZipEntry added. + + + + Add an entry into the zip archive using the given filename and + directory path within the archive, and the given content for the + file. No file is created in the filesystem. + + + The data to use for the entry. + + + The name, including any path, to use within the archive for the entry. + + + The ZipEntry added. + + + + Updates the given entry in the ZipFile, using the given byte + array as content for the entry. + + + + Calling this method is equivalent to removing the ZipEntry + for the given filename and directory path, if it exists, and then + calling . See the + documentation for that method for further explanation. + + + + The name, including any path, to use within the archive for the entry. + + + The content to use for the ZipEntry. + + The ZipEntry added. + + + + + Adds the contents of a filesystem directory to a Zip file archive. + + + + + + The name of the directory may be a relative path or a fully-qualified + path. Any files within the named directory are added to the archive. Any + subdirectories within the named directory are also added to the archive, + recursively. + + + + Top-level entries in the named directory will appear as top-level entries + in the zip archive. Entries in subdirectories in the named directory will + result in entries in subdirectories in the zip archive. + + + + If you want the entries to appear in a containing directory in the zip + archive itself, then you should call the AddDirectory() overload that + allows you to explicitly specify a directory path for use in the archive. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to each + ZipEntry added. + + + + + + + + + + This method has 2 overloads. + + The name of the directory to add. + The ZipEntry added. + + + + Adds the contents of a filesystem directory to a Zip file archive, + overriding the path to be used for entries in the archive. + + + + + The name of the directory may be a relative path or a fully-qualified + path. The add operation is recursive, so that any files or subdirectories + within the name directory are also added to the archive. + + + + Top-level entries in the named directory will appear as top-level entries + in the zip archive. Entries in subdirectories in the named directory will + result in entries in subdirectories in the zip archive. + + + + For ZipFile properties including , , , , , + , and , their + respective values at the time of this call will be applied to each + ZipEntry added. + + + + + + + In this code, calling the ZipUp() method with a value of "c:\reports" for + the directory parameter will result in a zip file structure in which all + entries are contained in a toplevel "reports" directory. + + + + public void ZipUp(string targetZip, string directory) + { + using (var zip = new ZipFile()) + { + zip.AddDirectory(directory, System.IO.Path.GetFileName(directory)); + zip.Save(targetZip); + } + } + + + + + + + + The name of the directory to add. + + + Specifies a directory path to use to override any path in the + DirectoryName. This path may, or may not, correspond to a real directory + in the current filesystem. If the zip is later extracted, this is the + path used for the extracted file or directory. Passing null + (Nothing in VB) or the empty string ("") will insert the items at + the root path within the archive. + + + The ZipEntry added. + + + + Creates a directory in the zip archive. + + + + + + Use this when you want to create a directory in the archive but there is + no corresponding filesystem representation for that directory. + + + + You will probably not need to do this in your code. One of the only times + you will want to do this is if you want an empty directory in the zip + archive. The reason: if you add a file to a zip archive that is stored + within a multi-level directory, all of the directory tree is implicitly + created in the zip archive. + + + + + + The name of the directory to create in the archive. + + The ZipEntry added. + + + + Checks a zip file to see if its directory is consistent. + + + + + + In cases of data error, the directory within a zip file can get out + of synch with the entries in the zip file. This method checks the + given zip file and returns true if this has occurred. + + + This method may take a long time to run for large zip files. + + + This method is not supported in the Reduced or Compact Framework + versions of DotNetZip. + + + + Developers using COM can use the ComHelper.CheckZip(String) + method. + + + + + The filename to of the zip file to check. + + true if the named zip file checks OK. Otherwise, false. + + + + + + + Checks a zip file to see if its directory is consistent, + and optionally fixes the directory if necessary. + + + + + + In cases of data error, the directory within a zip file can get out of + synch with the entries in the zip file. This method checks the given + zip file, and returns true if this has occurred. It also optionally + fixes the zipfile, saving the fixed copy in Name_Fixed.zip. + + + + This method may take a long time to run for large zip files. It + will take even longer if the file actually needs to be fixed, and if + fixIfNecessary is true. + + + + This method is not supported in the Reduced or Compact + Framework versions of DotNetZip. + + + + + The filename to of the zip file to check. + + If true, the method will fix the zip file if + necessary. + + + a TextWriter in which messages generated while checking will be written. + + + true if the named zip is OK; false if the file needs to be fixed. + + + + + + + Rewrite the directory within a zipfile. + + + + + + In cases of data error, the directory in a zip file can get out of + synch with the entries in the zip file. This method attempts to fix + the zip file if this has occurred. + + + This can take a long time for large zip files. + + This won't work if the zip file uses a non-standard + code page - neither IBM437 nor UTF-8. + + + This method is not supported in the Reduced or Compact Framework + versions of DotNetZip. + + + + Developers using COM can use the ComHelper.FixZipDirectory(String) + method. + + + + + The filename to of the zip file to fix. + + + + + + + Verify the password on a zip file. + + + + + Keep in mind that passwords in zipfiles are applied to + zip entries, not to the entire zip file. So testing a + zipfile for a particular password doesn't work in the + general case. On the other hand, it's often the case + that a single password will be used on all entries in a + zip file. This method works for that case. + + + There is no way to check a password without doing the + decryption. So this code decrypts and extracts the given + zipfile into + + + + The filename to of the zip file to fix. + + The password to check. + + a bool indicating whether the password matches. + + + + Returns true if an entry by the given name exists in the ZipFile. + + + the name of the entry to find + true if an entry with the given name exists; otherwise false. + + + + Provides a string representation of the instance. + a string representation of the instance. + + + + Creates a new ZipFile instance, using the specified filename. + + + + + Applications can use this constructor to create a new ZipFile for writing, + or to slurp in an existing zip archive for read and update purposes. + + + + To create a new zip archive, an application can call this constructor, + passing the name of a file that does not exist. The name may be a fully + qualified path. Then the application can add directories or files to the + ZipFile via AddDirectory(), AddFile(), AddItem() + and then write the zip archive to the disk by calling Save(). The + zip file is not actually opened and written to the disk until the + application calls ZipFile.Save(). At that point the new zip file + with the given name is created. + + + + If you won't know the name of the Zipfile until the time you call + ZipFile.Save(), or if you plan to save to a stream (which has no + name), then you should use the no-argument constructor. + + + + The application can also call this constructor to read an existing zip + archive. passing the name of a valid zip file that does exist. But, it's + better form to use the static method, + passing the name of the zip file, because using ZipFile.Read() in + your code communicates very clearly what you are doing. In either case, + the file is then read into the ZipFile instance. The app can then + enumerate the entries or can modify the zip file, for example adding + entries, removing entries, changing comments, and so on. + + + + One advantage to this parameterized constructor: it allows applications to + use the same code to add items to a zip archive, regardless of whether the + zip file exists. + + + + Instances of the ZipFile class are not multi-thread safe. You may + not party on a single instance with multiple threads. You may have + multiple threads that each use a distinct ZipFile instance, or you + can synchronize multi-thread access to a single instance. + + + + By the way, since DotNetZip is so easy to use, don't you think you should + donate $5 or $10? + + + + + + Thrown if name refers to an existing file that is not a valid zip file. + + + + This example shows how to create a zipfile, and add a few files into it. + + String ZipFileToCreate = "archive1.zip"; + String DirectoryToZip = "c:\\reports"; + using (ZipFile zip = new ZipFile()) + { + // Store all files found in the top level directory, into the zip archive. + String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip); + zip.AddFiles(filenames, "files"); + zip.Save(ZipFileToCreate); + } + + + + Dim ZipFileToCreate As String = "archive1.zip" + Dim DirectoryToZip As String = "c:\reports" + Using zip As ZipFile = New ZipFile() + Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip) + zip.AddFiles(filenames, "files") + zip.Save(ZipFileToCreate) + End Using + + + + The filename to use for the new zip archive. + + + + + Creates a new ZipFile instance, using the specified name for the + filename, and the specified Encoding. + + + + + See the documentation on the ZipFile + constructor that accepts a single string argument for basic + information on all the ZipFile constructors. + + + + The Encoding is used as the default alternate encoding for entries with + filenames or comments that cannot be encoded with the IBM437 code page. + This is equivalent to setting the property on the ZipFile + instance after construction. + + + + Instances of the ZipFile class are not multi-thread safe. You may + not party on a single instance with multiple threads. You may have + multiple threads that each use a distinct ZipFile instance, or you + can synchronize multi-thread access to a single instance. + + + + + + Thrown if name refers to an existing file that is not a valid zip file. + + + The filename to use for the new zip archive. + The Encoding is used as the default alternate + encoding for entries with filenames or comments that cannot be encoded + with the IBM437 code page. + + + + Create a zip file, without specifying a target filename or stream to save to. + + + + + See the documentation on the ZipFile + constructor that accepts a single string argument for basic + information on all the ZipFile constructors. + + + + After instantiating with this constructor and adding entries to the + archive, the application should call or + to save to a file or a + stream, respectively. The application can also set the + property and then call the no-argument method. (This + is the preferred approach for applications that use the library through + COM interop.) If you call the no-argument method + without having set the Name of the ZipFile, either through + the parameterized constructor or through the explicit property , the + Save() will throw, because there is no place to save the file. + + + Instances of the ZipFile class are not multi-thread safe. You may + have multiple threads that each use a distinct ZipFile instance, or + you can synchronize multi-thread access to a single instance. + + + + + This example creates a Zip archive called Backup.zip, containing all the files + in the directory DirectoryToZip. Files within subdirectories are not zipped up. + + using (ZipFile zip = new ZipFile()) + { + // Store all files found in the top level directory, into the zip archive. + // note: this code does not recurse subdirectories! + String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip); + zip.AddFiles(filenames, "files"); + zip.Save("Backup.zip"); + } + + + + Using zip As New ZipFile + ' Store all files found in the top level directory, into the zip archive. + ' note: this code does not recurse subdirectories! + Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip) + zip.AddFiles(filenames, "files") + zip.Save("Backup.zip") + End Using + + + + + + Create a zip file, specifying a text Encoding, but without specifying a + target filename or stream to save to. + + + + + See the documentation on the ZipFile + constructor that accepts a single string argument for basic + information on all the ZipFile constructors. + + + + + + The Encoding is used as the default alternate encoding for entries with + filenames or comments that cannot be encoded with the IBM437 code page. + + + + + Creates a new ZipFile instance, using the specified name for the + filename, and the specified status message writer. + + + + + See the documentation on the ZipFile + constructor that accepts a single string argument for basic + information on all the ZipFile constructors. + + + + This version of the constructor allows the caller to pass in a TextWriter, + to which verbose messages will be written during extraction or creation of + the zip archive. A console application may wish to pass + System.Console.Out to get messages on the Console. A graphical or headless + application may wish to capture the messages in a different + TextWriter, for example, a StringWriter, and then display + the messages in a TextBox, or generate an audit log of ZipFile operations. + + + + To encrypt the data for the files added to the ZipFile instance, + set the Password property after creating the ZipFile instance. + + + + Instances of the ZipFile class are not multi-thread safe. You may + not party on a single instance with multiple threads. You may have + multiple threads that each use a distinct ZipFile instance, or you + can synchronize multi-thread access to a single instance. + + + + + + Thrown if name refers to an existing file that is not a valid zip file. + + + + + using (ZipFile zip = new ZipFile("Backup.zip", Console.Out)) + { + // Store all files found in the top level directory, into the zip archive. + // note: this code does not recurse subdirectories! + // Status messages will be written to Console.Out + String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip); + zip.AddFiles(filenames); + zip.Save(); + } + + + + Using zip As New ZipFile("Backup.zip", Console.Out) + ' Store all files found in the top level directory, into the zip archive. + ' note: this code does not recurse subdirectories! + ' Status messages will be written to Console.Out + Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip) + zip.AddFiles(filenames) + zip.Save() + End Using + + + + The filename to use for the new zip archive. + A TextWriter to use for writing + verbose status messages. + + + + Creates a new ZipFile instance, using the specified name for the + filename, the specified status message writer, and the specified Encoding. + + + + + This constructor works like the ZipFile + constructor that accepts a single string argument. See that + reference for detail on what this constructor does. + + + + This version of the constructor allows the caller to pass in a + TextWriter, and an Encoding. The TextWriter will collect + verbose messages that are generated by the library during extraction or + creation of the zip archive. A console application may wish to pass + System.Console.Out to get messages on the Console. A graphical or + headless application may wish to capture the messages in a different + TextWriter, for example, a StringWriter, and then display + the messages in a TextBox, or generate an audit log of + ZipFile operations. + + + + The Encoding is used as the default alternate encoding for entries + with filenames or comments that cannot be encoded with the IBM437 code + page. This is a equivalent to setting the property on the ZipFile + instance after construction. + + + + To encrypt the data for the files added to the ZipFile instance, + set the Password property after creating the ZipFile + instance. + + + + Instances of the ZipFile class are not multi-thread safe. You may + not party on a single instance with multiple threads. You may have + multiple threads that each use a distinct ZipFile instance, or you + can synchronize multi-thread access to a single instance. + + + + + + Thrown if fileName refers to an existing file that is not a valid zip file. + + + The filename to use for the new zip archive. + A TextWriter to use for writing verbose + status messages. + + The Encoding is used as the default alternate encoding for entries with + filenames or comments that cannot be encoded with the IBM437 code page. + + + + + Initialize a ZipFile instance by reading in a zip file. + + + + + + This method is primarily useful from COM Automation environments, when + reading or extracting zip files. In COM, it is not possible to invoke + parameterized constructors for a class. A COM Automation application can + update a zip file by using the default (no argument) + constructor, then calling Initialize() to read the contents + of an on-disk zip archive into the ZipFile instance. + + + + .NET applications are encouraged to use the ZipFile.Read() methods + for better clarity. + + + + the name of the existing zip file to read in. + + + + Removes the given ZipEntry from the zip archive. + + + + + After calling RemoveEntry, the application must call Save to + make the changes permanent. + + + + + Thrown if the specified ZipEntry does not exist in the ZipFile. + + + + In this example, all entries in the zip archive dating from before + December 31st, 2007, are removed from the archive. This is actually much + easier if you use the RemoveSelectedEntries method. But I needed an + example for RemoveEntry, so here it is. + + String ZipFileToRead = "ArchiveToModify.zip"; + System.DateTime Threshold = new System.DateTime(2007,12,31); + using (ZipFile zip = ZipFile.Read(ZipFileToRead)) + { + var EntriesToRemove = new System.Collections.Generic.List<ZipEntry>(); + foreach (ZipEntry e in zip) + { + if (e.LastModified < Threshold) + { + // We cannot remove the entry from the list, within the context of + // an enumeration of said list. + // So we add the doomed entry to a list to be removed later. + EntriesToRemove.Add(e); + } + } + + // actually remove the doomed entries. + foreach (ZipEntry zombie in EntriesToRemove) + zip.RemoveEntry(zombie); + + zip.Comment= String.Format("This zip archive was updated at {0}.", + System.DateTime.Now.ToString("G")); + + // save with a different name + zip.Save("Archive-Updated.zip"); + } + + + + Dim ZipFileToRead As String = "ArchiveToModify.zip" + Dim Threshold As New DateTime(2007, 12, 31) + Using zip As ZipFile = ZipFile.Read(ZipFileToRead) + Dim EntriesToRemove As New System.Collections.Generic.List(Of ZipEntry) + Dim e As ZipEntry + For Each e In zip + If (e.LastModified < Threshold) Then + ' We cannot remove the entry from the list, within the context of + ' an enumeration of said list. + ' So we add the doomed entry to a list to be removed later. + EntriesToRemove.Add(e) + End If + Next + + ' actually remove the doomed entries. + Dim zombie As ZipEntry + For Each zombie In EntriesToRemove + zip.RemoveEntry(zombie) + Next + zip.Comment = String.Format("This zip archive was updated at {0}.", DateTime.Now.ToString("G")) + 'save as a different name + zip.Save("Archive-Updated.zip") + End Using + + + + + The ZipEntry to remove from the zip. + + + + + + + + Removes the ZipEntry with the given filename from the zip archive. + + + + + After calling RemoveEntry, the application must call Save to + make the changes permanent. + + + + + + Thrown if the ZipFile is not updatable. + + + + Thrown if a ZipEntry with the specified filename does not exist in + the ZipFile. + + + + + This example shows one way to remove an entry with a given filename from + an existing zip archive. + + + String zipFileToRead= "PackedDocuments.zip"; + string candidate = "DatedMaterial.xps"; + using (ZipFile zip = ZipFile.Read(zipFileToRead)) + { + if (zip.EntryFilenames.Contains(candidate)) + { + zip.RemoveEntry(candidate); + zip.Comment= String.Format("The file '{0}' has been removed from this archive.", + Candidate); + zip.Save(); + } + } + + + Dim zipFileToRead As String = "PackedDocuments.zip" + Dim candidate As String = "DatedMaterial.xps" + Using zip As ZipFile = ZipFile.Read(zipFileToRead) + If zip.EntryFilenames.Contains(candidate) Then + zip.RemoveEntry(candidate) + zip.Comment = String.Format("The file '{0}' has been removed from this archive.", Candidate) + zip.Save + End If + End Using + + + + + The name of the file, including any directory path, to remove from the zip. + The filename match is not case-sensitive by default; you can use the + CaseSensitiveRetrieval property to change this behavior. The + pathname can use forward-slashes or backward slashes. + + + + + + Closes the read and write streams associated + to the ZipFile, if necessary. + + + + The Dispose() method is generally employed implicitly, via a using(..) {..} + statement. (Using...End Using in VB) If you do not employ a using + statement, insure that your application calls Dispose() explicitly. For + example, in a Powershell application, or an application that uses the COM + interop interface, you must call Dispose() explicitly. + + + + This example extracts an entry selected by name, from the Zip file to the + Console. + + using (ZipFile zip = ZipFile.Read(zipfile)) + { + foreach (ZipEntry e in zip) + { + if (WantThisEntry(e.FileName)) + zip.Extract(e.FileName, Console.OpenStandardOutput()); + } + } // Dispose() is called implicitly here. + + + + Using zip As ZipFile = ZipFile.Read(zipfile) + Dim e As ZipEntry + For Each e In zip + If WantThisEntry(e.FileName) Then + zip.Extract(e.FileName, Console.OpenStandardOutput()) + End If + Next + End Using ' Dispose is implicity called here + + + + + + Disposes any managed resources, if the flag is set, then marks the + instance disposed. This method is typically not called explicitly from + application code. + + + + Applications should call the no-arg Dispose method. + + + + indicates whether the method should dispose streams or not. + + + + + Default size of the buffer used for IO. + + + + + Extracts all of the items in the zip archive, to the specified path in the + filesystem. The path can be relative or fully-qualified. + + + + + This method will extract all entries in the ZipFile to the + specified path. + + + + If an extraction of a file from the zip archive would overwrite an + existing file in the filesystem, the action taken is dictated by the + ExtractExistingFile property, which overrides any setting you may have + made on individual ZipEntry instances. By default, if you have not + set that property on the ZipFile instance, the entry will not + be extracted, the existing file will not be overwritten and an + exception will be thrown. To change this, set the property, or use the + overload that allows you to + specify an ExtractExistingFileAction parameter. + + + + The action to take when an extract would overwrite an existing file + applies to all entries. If you want to set this on a per-entry basis, + then you must use one of the ZipEntry.Extract methods. + + + + This method will send verbose output messages to the , if it is set on the ZipFile + instance. + + + + You may wish to take advantage of the ExtractProgress event. + + + + About timestamps: When extracting a file entry from a zip archive, the + extracted file gets the last modified time of the entry as stored in + the archive. The archive may also store extended file timestamp + information, including last accessed and created times. If these are + present in the ZipEntry, then the extracted file will also get + these times. + + + + A Directory entry is somewhat different. It will get the times as + described for a file entry, but, if there are file entries in the zip + archive that, when extracted, appear in the just-created directory, + then when those file entries are extracted, the last modified and last + accessed times of the directory will change, as a side effect. The + result is that after an extraction of a directory and a number of + files within the directory, the last modified and last accessed + timestamps on the directory will reflect the time that the last file + was extracted into the directory, rather than the time stored in the + zip archive for the directory. + + + + To compensate, when extracting an archive with ExtractAll, + DotNetZip will extract all the file and directory entries as described + above, but it will then make a second pass on the directories, and + reset the times on the directories to reflect what is stored in the + zip archive. + + + + This compensation is performed only within the context of an + ExtractAll. If you call ZipEntry.Extract on a directory + entry, the timestamps on directory in the filesystem will reflect the + times stored in the zip. If you then call ZipEntry.Extract on + a file entry, which is extracted into the directory, the timestamps on + the directory will be updated to the current time. + + + + + This example extracts all the entries in a zip archive file, to the + specified target directory. The extraction will overwrite any + existing files silently. + + + String TargetDirectory= "unpack"; + using(ZipFile zip= ZipFile.Read(ZipFileToExtract)) + { + zip.ExtractExistingFile= ExtractExistingFileAction.OverwriteSilently; + zip.ExtractAll(TargetDirectory); + } + + + + Dim TargetDirectory As String = "unpack" + Using zip As ZipFile = ZipFile.Read(ZipFileToExtract) + zip.ExtractExistingFile= ExtractExistingFileAction.OverwriteSilently + zip.ExtractAll(TargetDirectory) + End Using + + + + + + + + The path to which the contents of the zipfile will be extracted. + The path can be relative or fully-qualified. + + + + + + Extracts all of the items in the zip archive, to the specified path in the + filesystem, using the specified behavior when extraction would overwrite an + existing file. + + + + + + This method will extract all entries in the ZipFile to the specified + path. For an extraction that would overwrite an existing file, the behavior + is dictated by , which overrides any + setting you may have made on individual ZipEntry instances. + + + + The action to take when an extract would overwrite an existing file + applies to all entries. If you want to set this on a per-entry basis, + then you must use or one of the similar methods. + + + + Calling this method is equivalent to setting the property and then calling . + + + + This method will send verbose output messages to the + , if it is set on the ZipFile instance. + + + + + This example extracts all the entries in a zip archive file, to the + specified target directory. It does not overwrite any existing files. + + String TargetDirectory= "c:\\unpack"; + using(ZipFile zip= ZipFile.Read(ZipFileToExtract)) + { + zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite); + } + + + + Dim TargetDirectory As String = "c:\unpack" + Using zip As ZipFile = ZipFile.Read(ZipFileToExtract) + zip.ExtractAll(TargetDirectory, ExtractExistingFileAction.DontOverwrite) + End Using + + + + + The path to which the contents of the zipfile will be extracted. + The path can be relative or fully-qualified. + + + + The action to take if extraction would overwrite an existing file. + + + + + + Reads a zip file archive and returns the instance. + + + + + The stream is read using the default System.Text.Encoding, which is the + IBM437 codepage. + + + + + Thrown if the ZipFile cannot be read. The implementation of this method + relies on System.IO.File.OpenRead, which can throw a variety of exceptions, + including specific exceptions if a file is not found, an unauthorized access + exception, exceptions for poorly formatted filenames, and so on. + + + + The name of the zip archive to open. This can be a fully-qualified or relative + pathname. + + + . + + The instance read from the zip archive. + + + + + Reads a zip file archive from the named filesystem file using the + specified options. + + + + + This version of the Read() method allows the caller to pass + in a TextWriter an Encoding, via an instance of the + ReadOptions class. The ZipFile is read in using the + specified encoding for entries where UTF-8 encoding is not + explicitly specified. + + + + + + + This example shows how to read a zip file using the Big-5 Chinese + code page (950), and extract each entry in the zip file, while + sending status messages out to the Console. + + + + For this code to work as intended, the zipfile must have been + created using the big5 code page (CP950). This is typical, for + example, when using WinRar on a machine with CP950 set as the + default code page. In that case, the names of entries within the + Zip archive will be stored in that code page, and reading the zip + archive must be done using that code page. If the application did + not use the correct code page in ZipFile.Read(), then names of + entries within the zip archive would not be correctly retrieved. + + + + string zipToExtract = "MyArchive.zip"; + string extractDirectory = "extract"; + var options = new ReadOptions + { + StatusMessageWriter = System.Console.Out, + Encoding = System.Text.Encoding.GetEncoding(950) + }; + using (ZipFile zip = ZipFile.Read(zipToExtract, options)) + { + foreach (ZipEntry e in zip) + { + e.Extract(extractDirectory); + } + } + + + + + Dim zipToExtract as String = "MyArchive.zip" + Dim extractDirectory as String = "extract" + Dim options as New ReadOptions + options.Encoding = System.Text.Encoding.GetEncoding(950) + options.StatusMessageWriter = System.Console.Out + Using zip As ZipFile = ZipFile.Read(zipToExtract, options) + Dim e As ZipEntry + For Each e In zip + e.Extract(extractDirectory) + Next + End Using + + + + + + + + This example shows how to read a zip file using the default + code page, to remove entries that have a modified date before a given threshold, + sending status messages out to a StringWriter. + + + + var options = new ReadOptions + { + StatusMessageWriter = new System.IO.StringWriter() + }; + using (ZipFile zip = ZipFile.Read("PackedDocuments.zip", options)) + { + var Threshold = new DateTime(2007,7,4); + // We cannot remove the entry from the list, within the context of + // an enumeration of said list. + // So we add the doomed entry to a list to be removed later. + // pass 1: mark the entries for removal + var MarkedEntries = new System.Collections.Generic.List<ZipEntry>(); + foreach (ZipEntry e in zip) + { + if (e.LastModified < Threshold) + MarkedEntries.Add(e); + } + // pass 2: actually remove the entry. + foreach (ZipEntry zombie in MarkedEntries) + zip.RemoveEntry(zombie); + zip.Comment = "This archive has been updated."; + zip.Save(); + } + // can now use contents of sw, eg store in an audit log + + + + Dim options as New ReadOptions + options.StatusMessageWriter = New System.IO.StringWriter + Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip", options) + Dim Threshold As New DateTime(2007, 7, 4) + ' We cannot remove the entry from the list, within the context of + ' an enumeration of said list. + ' So we add the doomed entry to a list to be removed later. + ' pass 1: mark the entries for removal + Dim MarkedEntries As New System.Collections.Generic.List(Of ZipEntry) + Dim e As ZipEntry + For Each e In zip + If (e.LastModified < Threshold) Then + MarkedEntries.Add(e) + End If + Next + ' pass 2: actually remove the entry. + Dim zombie As ZipEntry + For Each zombie In MarkedEntries + zip.RemoveEntry(zombie) + Next + zip.Comment = "This archive has been updated." + zip.Save + End Using + ' can now use contents of sw, eg store in an audit log + + + + + Thrown if the zipfile cannot be read. The implementation of + this method relies on System.IO.File.OpenRead, which + can throw a variety of exceptions, including specific + exceptions if a file is not found, an unauthorized access + exception, exceptions for poorly formatted filenames, and so + on. + + + + The name of the zip archive to open. + This can be a fully-qualified or relative pathname. + + + + The set of options to use when reading the zip file. + + + The ZipFile instance read from the zip archive. + + + + + + + Reads a zip file archive using the specified text encoding, the specified + TextWriter for status messages, and the specified ReadProgress event handler, + and returns the instance. + + + + The name of the zip archive to open. + This can be a fully-qualified or relative pathname. + + + + An event handler for Read operations. + + + + The System.IO.TextWriter to use for writing verbose status messages + during operations on the zip archive. A console application may wish to + pass System.Console.Out to get messages on the Console. A graphical + or headless application may wish to capture the messages in a different + TextWriter, such as a System.IO.StringWriter. + + + + The System.Text.Encoding to use when reading in the zip archive. Be + careful specifying the encoding. If the value you use here is not the same + as the Encoding used when the zip archive was created (possibly by a + different archiver) you will get unexpected results and possibly exceptions. + + + The instance read from the zip archive. + + + + + Reads a zip archive from a stream. + + + + + + When reading from a file, it's probably easier to just use + ZipFile.Read(String, ReadOptions). This + overload is useful when when the zip archive content is + available from an already-open stream. The stream must be + open and readable and seekable when calling this method. The + stream is left open when the reading is completed. + + + + Using this overload, the stream is read using the default + System.Text.Encoding, which is the IBM437 + codepage. If you want to specify the encoding to use when + reading the zipfile content, see + ZipFile.Read(Stream, ReadOptions). This + + + + Reading of zip content begins at the current position in the + stream. This means if you have a stream that concatenates + regular data and zip data, if you position the open, readable + stream at the start of the zip data, you will be able to read + the zip archive using this constructor, or any of the ZipFile + constructors that accept a as + input. Some examples of where this might be useful: the zip + content is concatenated at the end of a regular EXE file, as + some self-extracting archives do. (Note: SFX files produced + by DotNetZip do not work this way; they can be read as normal + ZIP files). Another example might be a stream being read from + a database, where the zip content is embedded within an + aggregate stream of data. + + + + + + + This example shows how to Read zip content from a stream, and + extract one entry into a different stream. In this example, + the filename "NameOfEntryInArchive.doc", refers only to the + name of the entry within the zip archive. A file by that + name is not created in the filesystem. The I/O is done + strictly with the given streams. + + + + using (ZipFile zip = ZipFile.Read(InputStream)) + { + zip.Extract("NameOfEntryInArchive.doc", OutputStream); + } + + + + Using zip as ZipFile = ZipFile.Read(InputStream) + zip.Extract("NameOfEntryInArchive.doc", OutputStream) + End Using + + + + the stream containing the zip data. + + The ZipFile instance read from the stream + + + + + Reads a zip file archive from the given stream using the + specified options. + + + + + + When reading from a file, it's probably easier to just use + ZipFile.Read(String, ReadOptions). This + overload is useful when when the zip archive content is + available from an already-open stream. The stream must be + open and readable and seekable when calling this method. The + stream is left open when the reading is completed. + + + + Reading of zip content begins at the current position in the + stream. This means if you have a stream that concatenates + regular data and zip data, if you position the open, readable + stream at the start of the zip data, you will be able to read + the zip archive using this constructor, or any of the ZipFile + constructors that accept a as + input. Some examples of where this might be useful: the zip + content is concatenated at the end of a regular EXE file, as + some self-extracting archives do. (Note: SFX files produced + by DotNetZip do not work this way; they can be read as normal + ZIP files). Another example might be a stream being read from + a database, where the zip content is embedded within an + aggregate stream of data. + + + + the stream containing the zip data. + + + The set of options to use when reading the zip file. + + + + Thrown if the zip archive cannot be read. + + + The ZipFile instance read from the stream. + + + + + + + Reads a zip archive from a stream, using the specified text Encoding, the + specified TextWriter for status messages, + and the specified ReadProgress event handler. + + + + + Reading of zip content begins at the current position in the stream. This + means if you have a stream that concatenates regular data and zip data, if + you position the open, readable stream at the start of the zip data, you + will be able to read the zip archive using this constructor, or any of the + ZipFile constructors that accept a as + input. Some examples of where this might be useful: the zip content is + concatenated at the end of a regular EXE file, as some self-extracting + archives do. (Note: SFX files produced by DotNetZip do not work this + way). Another example might be a stream being read from a database, where + the zip content is embedded within an aggregate stream of data. + + + + the stream containing the zip data. + + + The System.IO.TextWriter to which verbose status messages are written + during operations on the ZipFile. For example, in a console + application, System.Console.Out works, and will get a message for each entry + added to the ZipFile. If the TextWriter is null, no verbose messages + are written. + + + + The text encoding to use when reading entries that do not have the UTF-8 + encoding bit set. Be careful specifying the encoding. If the value you use + here is not the same as the Encoding used when the zip archive was created + (possibly by a different archiver) you will get unexpected results and + possibly exceptions. See the + property for more information. + + + + An event handler for Read operations. + + + an instance of ZipFile + + + + Checks the given file to see if it appears to be a valid zip file. + + + + + Calling this method is equivalent to calling with the testExtract parameter set to false. + + + + The file to check. + true if the file appears to be a zip file. + + + + Checks a file to see if it is a valid zip file. + + + + + This method opens the specified zip file, reads in the zip archive, + verifying the ZIP metadata as it reads. + + + + If everything succeeds, then the method returns true. If anything fails - + for example if an incorrect signature or CRC is found, indicating a + corrupt file, the the method returns false. This method also returns + false for a file that does not exist. + + + + If is true, as part of its check, this + method reads in the content for each entry, expands it, and checks CRCs. + This provides an additional check beyond verifying the zip header and + directory data. + + + + If is true, and if any of the zip entries + are protected with a password, this method will return false. If you want + to verify a ZipFile that has entries which are protected with a + password, you will need to do that manually. + + + + + The zip file to check. + true if the caller wants to extract each entry. + true if the file contains a valid zip file. + + + + Checks a stream to see if it contains a valid zip archive. + + + + + This method reads the zip archive contained in the specified stream, verifying + the ZIP metadata as it reads. If testExtract is true, this method also extracts + each entry in the archive, dumping all the bits into . + + + + If everything succeeds, then the method returns true. If anything fails - + for example if an incorrect signature or CRC is found, indicating a corrupt + file, the the method returns false. This method also returns false for a + file that does not exist. + + + + If testExtract is true, this method reads in the content for each + entry, expands it, and checks CRCs. This provides an additional check + beyond verifying the zip header data. + + + + If testExtract is true, and if any of the zip entries are protected + with a password, this method will return false. If you want to verify a + ZipFile that has entries which are protected with a password, you will need + to do that manually. + + + + + + The stream to check. + true if the caller wants to extract each entry. + true if the stream contains a valid zip archive. + + + + Delete file with retry on UnauthorizedAccessException. + + + + + When calling File.Delete() on a file that has been "recently" + created, the call sometimes fails with + UnauthorizedAccessException. This method simply retries the Delete 3 + times with a sleep between tries. + + + + the name of the file to be deleted + + + + Saves the Zip archive to a file, specified by the Name property of the + ZipFile. + + + + + The ZipFile instance is written to storage, typically a zip file + in a filesystem, only when the caller calls Save. In the typical + case, the Save operation writes the zip content to a temporary file, and + then renames the temporary file to the desired name. If necessary, this + method will delete a pre-existing file before the rename. + + + + The property is specified either explicitly, + or implicitly using one of the parameterized ZipFile constructors. For + COM Automation clients, the Name property must be set explicitly, + because COM Automation clients cannot call parameterized constructors. + + + + When using a filesystem file for the Zip output, it is possible to call + Save multiple times on the ZipFile instance. With each + call the zip content is re-written to the same output file. + + + + Data for entries that have been added to the ZipFile instance is + written to the output when the Save method is called. This means + that the input streams for those entries must be available at the time + the application calls Save. If, for example, the application + adds entries with AddEntry using a dynamically-allocated + MemoryStream, the memory stream must not have been disposed + before the call to Save. See the property for more discussion of the + availability requirements of the input stream for an entry, and an + approach for providing just-in-time stream lifecycle management. + + + + + + + + Thrown if you haven't specified a location or stream for saving the zip, + either in the constructor or by setting the Name property, or if you try + to save a regular zip archive to a filename with a .exe extension. + + + + Thrown if is non-zero, and the number + of segments that would be generated for the spanned zip file during the + save operation exceeds 99. If this happens, you need to increase the + segment size. + + + + + + Save the file to a new zipfile, with the given name. + + + + + This method allows the application to explicitly specify the name of the zip + file when saving. Use this when creating a new zip file, or when + updating a zip archive. + + + + An application can also save a zip archive in several places by calling this + method multiple times in succession, with different filenames. + + + + The ZipFile instance is written to storage, typically a zip file in a + filesystem, only when the caller calls Save. The Save operation writes + the zip content to a temporary file, and then renames the temporary file + to the desired name. If necessary, this method will delete a pre-existing file + before the rename. + + + + + + Thrown if you specify a directory for the filename. + + + + The name of the zip archive to save to. Existing files will + be overwritten with great prejudice. + + + + This example shows how to create and Save a zip file. + + using (ZipFile zip = new ZipFile()) + { + zip.AddDirectory(@"c:\reports\January"); + zip.Save("January.zip"); + } + + + + Using zip As New ZipFile() + zip.AddDirectory("c:\reports\January") + zip.Save("January.zip") + End Using + + + + + + This example shows how to update a zip file. + + using (ZipFile zip = ZipFile.Read("ExistingArchive.zip")) + { + zip.AddFile("NewData.csv"); + zip.Save("UpdatedArchive.zip"); + } + + + + Using zip As ZipFile = ZipFile.Read("ExistingArchive.zip") + zip.AddFile("NewData.csv") + zip.Save("UpdatedArchive.zip") + End Using + + + + + + + Save the zip archive to the specified stream. + + + + + The ZipFile instance is written to storage - typically a zip file + in a filesystem, but using this overload, the storage can be anything + accessible via a writable stream - only when the caller calls Save. + + + + Use this method to save the zip content to a stream directly. A common + scenario is an ASP.NET application that dynamically generates a zip file + and allows the browser to download it. The application can call + Save(Response.OutputStream) to write a zipfile directly to the + output stream, without creating a zip file on the disk on the ASP.NET + server. + + + + Be careful when saving a file to a non-seekable stream, including + Response.OutputStream. When DotNetZip writes to a non-seekable + stream, the zip archive is formatted in such a way that may not be + compatible with all zip tools on all platforms. It's a perfectly legal + and compliant zip file, but some people have reported problems opening + files produced this way using the Mac OS archive utility. + + + + + + + This example saves the zipfile content into a MemoryStream, and + then gets the array of bytes from that MemoryStream. + + + using (var zip = new Ionic.Zip.ZipFile()) + { + zip.CompressionLevel= Ionic.Zlib.CompressionLevel.BestCompression; + zip.Password = "VerySecret."; + zip.Encryption = EncryptionAlgorithm.WinZipAes128; + zip.AddFile(sourceFileName); + MemoryStream output = new MemoryStream(); + zip.Save(output); + + byte[] zipbytes = output.ToArray(); + } + + + + + + This example shows a pitfall you should avoid. DO NOT read + from a stream, then try to save to the same stream. DO + NOT DO THIS: + + + + using (var fs = new FileSteeam(filename, FileMode.Open)) + { + using (var zip = Ionic.Zip.ZipFile.Read(inputStream)) + { + zip.AddEntry("Name1.txt", "this is the content"); + zip.Save(inputStream); // NO NO NO!! + } + } + + + + Better like this: + + + + using (var zip = Ionic.Zip.ZipFile.Read(filename)) + { + zip.AddEntry("Name1.txt", "this is the content"); + zip.Save(); // YES! + } + + + + + + The System.IO.Stream to write to. It must be + writable. If you created the ZipFile instanct by calling + ZipFile.Read(), this stream must not be the same stream + you passed to ZipFile.Read(). + + + + + Adds to the ZipFile a set of files from the current working directory on + disk, that conform to the specified criteria. + + + + + This method selects files from the the current working directory matching + the specified criteria, and adds them to the ZipFile. + + + + Specify the criteria in statements of 3 elements: a noun, an operator, and + a value. Consider the string "name != *.doc" . The noun is "name". The + operator is "!=", implying "Not Equal". The value is "*.doc". That + criterion, in English, says "all files with a name that does not end in + the .doc extension." + + + + Supported nouns include "name" (or "filename") for the filename; "atime", + "mtime", and "ctime" for last access time, last modfied time, and created + time of the file, respectively; "attributes" (or "attrs") for the file + attributes; "size" (or "length") for the file length (uncompressed), and + "type" for the type of object, either a file or a directory. The + "attributes", "name" and "type" nouns both support = and != as operators. + The "size", "atime", "mtime", and "ctime" nouns support = and !=, and + >, >=, <, <= as well. The times are taken to be expressed in + local time. + + + + Specify values for the file attributes as a string with one or more of the + characters H,R,S,A,I,L in any order, implying file attributes of Hidden, + ReadOnly, System, Archive, NotContextIndexed, and ReparsePoint (symbolic + link) respectively. + + + + To specify a time, use YYYY-MM-DD-HH:mm:ss or YYYY/MM/DD-HH:mm:ss as the + format. If you omit the HH:mm:ss portion, it is assumed to be 00:00:00 + (midnight). + + + + The value for a size criterion is expressed in integer quantities of bytes, + kilobytes (use k or kb after the number), megabytes (m or mb), or gigabytes + (g or gb). + + + + The value for a name is a pattern to match against the filename, potentially + including wildcards. The pattern follows CMD.exe glob rules: * implies one + or more of any character, while ? implies one character. If the name + pattern contains any slashes, it is matched to the entire filename, + including the path; otherwise, it is matched against only the filename + without the path. This means a pattern of "*\*.*" matches all files one + directory level deep, while a pattern of "*.*" matches all files in all + directories. + + + + To specify a name pattern that includes spaces, use single quotes around the + pattern. A pattern of "'* *.*'" will match all files that have spaces in + the filename. The full criteria string for that would be "name = '* *.*'" . + + + + The value for a type criterion is either F (implying a file) or D (implying + a directory). + + + + Some examples: + + + + + criteria + Files retrieved + + + + name != *.xls + any file with an extension that is not .xls + + + + + name = *.mp3 + any file with a .mp3 extension. + + + + + *.mp3 + (same as above) any file with a .mp3 extension. + + + + + attributes = A + all files whose attributes include the Archive bit. + + + + + attributes != H + all files whose attributes do not include the Hidden bit. + + + + + mtime > 2009-01-01 + all files with a last modified time after January 1st, 2009. + + + + + size > 2gb + all files whose uncompressed size is greater than 2gb. + + + + + type = D + all directories in the filesystem. + + + + + + You can combine criteria with the conjunctions AND or OR. Using a string + like "name = *.txt AND size >= 100k" for the selectionCriteria retrieves + entries whose names end in .txt, and whose uncompressed size is greater than + or equal to 100 kilobytes. + + + + For more complex combinations of criteria, you can use parenthesis to group + clauses in the boolean logic. Without parenthesis, the precedence of the + criterion atoms is determined by order of appearance. Unlike the C# + language, the AND conjunction does not take precendence over the logical OR. + This is important only in strings that contain 3 or more criterion atoms. + In other words, "name = *.txt and size > 1000 or attributes = H" implies + "((name = *.txt AND size > 1000) OR attributes = H)" while "attributes = + H OR name = *.txt and size > 1000" evaluates to "((attributes = H OR name + = *.txt) AND size > 1000)". When in doubt, use parenthesis. + + + + Using time properties requires some extra care. If you want to retrieve all + entries that were last updated on 2009 February 14, specify a time range + like so:"mtime >= 2009-02-14 AND mtime < 2009-02-15". Read this to + say: all files updated after 12:00am on February 14th, until 12:00am on + February 15th. You can use the same bracketing approach to specify any time + period - a year, a month, a week, and so on. + + + + The syntax allows one special case: if you provide a string with no spaces, it is + treated as a pattern to match for the filename. Therefore a string like "*.xls" + will be equivalent to specifying "name = *.xls". + + + + There is no logic in this method that insures that the file inclusion + criteria are internally consistent. For example, it's possible to specify + criteria that says the file must have a size of less than 100 bytes, as well + as a size that is greater than 1000 bytes. Obviously no file will ever + satisfy such criteria, but this method does not detect such logical + inconsistencies. The caller is responsible for insuring the criteria are + sensible. + + + + Using this method, the file selection does not recurse into + subdirectories, and the full path of the selected files is included in the + entries added into the zip archive. If you don't like these behaviors, + see the other overloads of this method. + + + + + This example zips up all *.csv files in the current working directory. + + using (ZipFile zip = new ZipFile()) + { + // To just match on filename wildcards, + // use the shorthand form of the selectionCriteria string. + zip.AddSelectedFiles("*.csv"); + zip.Save(PathToZipArchive); + } + + + Using zip As ZipFile = New ZipFile() + zip.AddSelectedFiles("*.csv") + zip.Save(PathToZipArchive) + End Using + + + + The criteria for file selection + + + + Adds to the ZipFile a set of files from the disk that conform to the + specified criteria, optionally recursing into subdirectories. + + + + + This method selects files from the the current working directory matching + the specified criteria, and adds them to the ZipFile. If + recurseDirectories is true, files are also selected from + subdirectories, and the directory structure in the filesystem is + reproduced in the zip archive, rooted at the current working directory. + + + + Using this method, the full path of the selected files is included in the + entries added into the zip archive. If you don't want this behavior, use + one of the overloads of this method that allows the specification of a + directoryInArchive. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + + + + This example zips up all *.xml files in the current working directory, or any + subdirectory, that are larger than 1mb. + + + using (ZipFile zip = new ZipFile()) + { + // Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.xml and size > 1024kb", true); + zip.Save(PathToZipArchive); + } + + + Using zip As ZipFile = New ZipFile() + ' Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.xml and size > 1024kb", true) + zip.Save(PathToZipArchive) + End Using + + + + The criteria for file selection + + + If true, the file selection will recurse into subdirectories. + + + + + Adds to the ZipFile a set of files from a specified directory in the + filesystem, that conform to the specified criteria. + + + + + This method selects files that conform to the specified criteria, from the + the specified directory on disk, and adds them to the ZipFile. The search + does not recurse into subdirectores. + + + + Using this method, the full filesystem path of the files on disk is + reproduced on the entries added to the zip file. If you don't want this + behavior, use one of the other overloads of this method. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + + + + This example zips up all *.xml files larger than 1mb in the directory + given by "d:\rawdata". + + + using (ZipFile zip = new ZipFile()) + { + // Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.xml and size > 1024kb", "d:\\rawdata"); + zip.Save(PathToZipArchive); + } + + + + Using zip As ZipFile = New ZipFile() + ' Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.xml and size > 1024kb", "d:\rawdata) + zip.Save(PathToZipArchive) + End Using + + + + The criteria for file selection + + + The name of the directory on the disk from which to select files. + + + + + Adds to the ZipFile a set of files from the specified directory on disk, + that conform to the specified criteria. + + + + + + This method selects files from the the specified disk directory matching + the specified selection criteria, and adds them to the ZipFile. If + recurseDirectories is true, files are also selected from + subdirectories. + + + + The full directory structure in the filesystem is reproduced on the + entries added to the zip archive. If you don't want this behavior, use + one of the overloads of this method that allows the specification of a + directoryInArchive. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + + + This example zips up all *.csv files in the "files" directory, or any + subdirectory, that have been saved since 2009 February 14th. + + + using (ZipFile zip = new ZipFile()) + { + // Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.csv and mtime > 2009-02-14", "files", true); + zip.Save(PathToZipArchive); + } + + + Using zip As ZipFile = New ZipFile() + ' Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.csv and mtime > 2009-02-14", "files", true) + zip.Save(PathToZipArchive) + End Using + + + + + This example zips up all files in the current working + directory, and all its child directories, except those in + the excludethis subdirectory. + + Using Zip As ZipFile = New ZipFile(zipfile) + Zip.AddSelectedFfiles("name != 'excludethis\*.*'", datapath, True) + Zip.Save() + End Using + + + + The criteria for file selection + + + The filesystem path from which to select files. + + + + If true, the file selection will recurse into subdirectories. + + + + + Adds to the ZipFile a selection of files from the specified directory on + disk, that conform to the specified criteria, and using a specified root + path for entries added to the zip archive. + + + + + This method selects files from the specified disk directory matching the + specified selection criteria, and adds those files to the ZipFile, using + the specified directory path in the archive. The search does not recurse + into subdirectories. For details on the syntax for the selectionCriteria + parameter, see . + + + + + + + This example zips up all *.psd files in the "photos" directory that have + been saved since 2009 February 14th, and puts them all in a zip file, + using the directory name of "content" in the zip archive itself. When the + zip archive is unzipped, the folder containing the .psd files will be + named "content". + + + using (ZipFile zip = new ZipFile()) + { + // Use a compound expression in the selectionCriteria string. + zip.AddSelectedFiles("name = *.psd and mtime > 2009-02-14", "photos", "content"); + zip.Save(PathToZipArchive); + } + + + Using zip As ZipFile = New ZipFile + zip.AddSelectedFiles("name = *.psd and mtime > 2009-02-14", "photos", "content") + zip.Save(PathToZipArchive) + End Using + + + + + The criteria for selection of files to add to the ZipFile. + + + + The path to the directory in the filesystem from which to select files. + + + + Specifies a directory path to use to in place of the + directoryOnDisk. This path may, or may not, correspond to a real + directory in the current filesystem. If the files within the zip are + later extracted, this is the path used for the extracted file. Passing + null (nothing in VB) will use the path on the file name, if any; in other + words it would use directoryOnDisk, plus any subdirectory. Passing + the empty string ("") will insert the item at the root path within the + archive. + + + + + Adds to the ZipFile a selection of files from the specified directory on + disk, that conform to the specified criteria, optionally recursing through + subdirectories, and using a specified root path for entries added to the + zip archive. + + + + This method selects files from the specified disk directory that match the + specified selection criteria, and adds those files to the ZipFile, using + the specified directory path in the archive. If recurseDirectories + is true, files are also selected from subdirectories, and the directory + structure in the filesystem is reproduced in the zip archive, rooted at + the directory specified by directoryOnDisk. For details on the + syntax for the selectionCriteria parameter, see . + + + + + This example zips up all files that are NOT *.pst files, in the current + working directory and any subdirectories. + + + using (ZipFile zip = new ZipFile()) + { + zip.AddSelectedFiles("name != *.pst", SourceDirectory, "backup", true); + zip.Save(PathToZipArchive); + } + + + Using zip As ZipFile = New ZipFile + zip.AddSelectedFiles("name != *.pst", SourceDirectory, "backup", true) + zip.Save(PathToZipArchive) + End Using + + + + + The criteria for selection of files to add to the ZipFile. + + + + The path to the directory in the filesystem from which to select files. + + + + Specifies a directory path to use to in place of the + directoryOnDisk. This path may, or may not, correspond to a real + directory in the current filesystem. If the files within the zip are + later extracted, this is the path used for the extracted file. Passing + null (nothing in VB) will use the path on the file name, if any; in other + words it would use directoryOnDisk, plus any subdirectory. Passing + the empty string ("") will insert the item at the root path within the + archive. + + + + If true, the method also scans subdirectories for files matching the + criteria. + + + + + Updates the ZipFile with a selection of files from the disk that conform + to the specified criteria. + + + + This method selects files from the specified disk directory that match the + specified selection criteria, and Updates the ZipFile with those + files, using the specified directory path in the archive. If + recurseDirectories is true, files are also selected from + subdirectories, and the directory structure in the filesystem is + reproduced in the zip archive, rooted at the directory specified by + directoryOnDisk. For details on the syntax for the + selectionCriteria parameter, see . + + + + The criteria for selection of files to add to the ZipFile. + + + + The path to the directory in the filesystem from which to select files. + + + + Specifies a directory path to use to in place of the + directoryOnDisk. This path may, or may not, correspond to a + real directory in the current filesystem. If the files within the zip + are later extracted, this is the path used for the extracted file. + Passing null (nothing in VB) will use the path on the file name, if + any; in other words it would use directoryOnDisk, plus any + subdirectory. Passing the empty string ("") will insert the item at + the root path within the archive. + + + + If true, the method also scans subdirectories for files matching the criteria. + + + + + + + Retrieve entries from the zipfile by specified criteria. + + + + + This method allows callers to retrieve the collection of entries from the zipfile + that fit the specified criteria. The criteria are described in a string format, and + can include patterns for the filename; constraints on the size of the entry; + constraints on the last modified, created, or last accessed time for the file + described by the entry; or the attributes of the entry. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + This method is intended for use with a ZipFile that has been read from storage. + When creating a new ZipFile, this method will work only after the ZipArchive has + been Saved to the disk (the ZipFile class subsequently and implicitly reads the Zip + archive from storage.) Calling SelectEntries on a ZipFile that has not yet been + saved will deliver undefined results. + + + + + Thrown if selectionCriteria has an invalid syntax. + + + + This example selects all the PhotoShop files from within an archive, and extracts them + to the current working directory. + + using (ZipFile zip1 = ZipFile.Read(ZipFileName)) + { + var PhotoShopFiles = zip1.SelectEntries("*.psd"); + foreach (ZipEntry psd in PhotoShopFiles) + { + psd.Extract(); + } + } + + + Using zip1 As ZipFile = ZipFile.Read(ZipFileName) + Dim PhotoShopFiles as ICollection(Of ZipEntry) + PhotoShopFiles = zip1.SelectEntries("*.psd") + Dim psd As ZipEntry + For Each psd In PhotoShopFiles + psd.Extract + Next + End Using + + + the string that specifies which entries to select + a collection of ZipEntry objects that conform to the inclusion spec + + + + Retrieve entries from the zipfile by specified criteria. + + + + + This method allows callers to retrieve the collection of entries from the zipfile + that fit the specified criteria. The criteria are described in a string format, and + can include patterns for the filename; constraints on the size of the entry; + constraints on the last modified, created, or last accessed time for the file + described by the entry; or the attributes of the entry. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + This method is intended for use with a ZipFile that has been read from storage. + When creating a new ZipFile, this method will work only after the ZipArchive has + been Saved to the disk (the ZipFile class subsequently and implicitly reads the Zip + archive from storage.) Calling SelectEntries on a ZipFile that has not yet been + saved will deliver undefined results. + + + + + Thrown if selectionCriteria has an invalid syntax. + + + + + using (ZipFile zip1 = ZipFile.Read(ZipFileName)) + { + var UpdatedPhotoShopFiles = zip1.SelectEntries("*.psd", "UpdatedFiles"); + foreach (ZipEntry e in UpdatedPhotoShopFiles) + { + // prompt for extract here + if (WantExtract(e.FileName)) + e.Extract(); + } + } + + + Using zip1 As ZipFile = ZipFile.Read(ZipFileName) + Dim UpdatedPhotoShopFiles As ICollection(Of ZipEntry) = zip1.SelectEntries("*.psd", "UpdatedFiles") + Dim e As ZipEntry + For Each e In UpdatedPhotoShopFiles + ' prompt for extract here + If Me.WantExtract(e.FileName) Then + e.Extract + End If + Next + End Using + + + the string that specifies which entries to select + + + the directory in the archive from which to select entries. If null, then + all directories in the archive are used. + + + a collection of ZipEntry objects that conform to the inclusion spec + + + + Remove entries from the zipfile by specified criteria. + + + + + This method allows callers to remove the collection of entries from the zipfile + that fit the specified criteria. The criteria are described in a string format, and + can include patterns for the filename; constraints on the size of the entry; + constraints on the last modified, created, or last accessed time for the file + described by the entry; or the attributes of the entry. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + This method is intended for use with a ZipFile that has been read from storage. + When creating a new ZipFile, this method will work only after the ZipArchive has + been Saved to the disk (the ZipFile class subsequently and implicitly reads the Zip + archive from storage.) Calling SelectEntries on a ZipFile that has not yet been + saved will deliver undefined results. + + + + + Thrown if selectionCriteria has an invalid syntax. + + + + This example removes all entries in a zip file that were modified prior to January 1st, 2008. + + using (ZipFile zip1 = ZipFile.Read(ZipFileName)) + { + // remove all entries from prior to Jan 1, 2008 + zip1.RemoveEntries("mtime < 2008-01-01"); + // don't forget to save the archive! + zip1.Save(); + } + + + Using zip As ZipFile = ZipFile.Read(ZipFileName) + ' remove all entries from prior to Jan 1, 2008 + zip1.RemoveEntries("mtime < 2008-01-01") + ' do not forget to save the archive! + zip1.Save + End Using + + + the string that specifies which entries to select + the number of entries removed + + + + Remove entries from the zipfile by specified criteria, and within the specified + path in the archive. + + + + + This method allows callers to remove the collection of entries from the zipfile + that fit the specified criteria. The criteria are described in a string format, and + can include patterns for the filename; constraints on the size of the entry; + constraints on the last modified, created, or last accessed time for the file + described by the entry; or the attributes of the entry. + + + + For details on the syntax for the selectionCriteria parameter, see . + + + + This method is intended for use with a ZipFile that has been read from storage. + When creating a new ZipFile, this method will work only after the ZipArchive has + been Saved to the disk (the ZipFile class subsequently and implicitly reads the Zip + archive from storage.) Calling SelectEntries on a ZipFile that has not yet been + saved will deliver undefined results. + + + + + Thrown if selectionCriteria has an invalid syntax. + + + + + using (ZipFile zip1 = ZipFile.Read(ZipFileName)) + { + // remove all entries from prior to Jan 1, 2008 + zip1.RemoveEntries("mtime < 2008-01-01", "documents"); + // a call to ZipFile.Save will make the modifications permanent + zip1.Save(); + } + + + Using zip As ZipFile = ZipFile.Read(ZipFileName) + ' remove all entries from prior to Jan 1, 2008 + zip1.RemoveEntries("mtime < 2008-01-01", "documents") + ' a call to ZipFile.Save will make the modifications permanent + zip1.Save + End Using + + + + the string that specifies which entries to select + + the directory in the archive from which to select entries. If null, then + all directories in the archive are used. + + the number of entries removed + + + + Selects and Extracts a set of Entries from the ZipFile. + + + + + The entries are extracted into the current working directory. + + + + If any of the files to be extracted already exist, then the action taken is as + specified in the property on the + corresponding ZipEntry instance. By default, the action taken in this case is to + throw an exception. + + + + For information on the syntax of the selectionCriteria string, + see . + + + + + This example shows how extract all XML files modified after 15 January 2009. + + using (ZipFile zip = ZipFile.Read(zipArchiveName)) + { + zip.ExtractSelectedEntries("name = *.xml and mtime > 2009-01-15"); + } + + + the selection criteria for entries to extract. + + + + + + Selects and Extracts a set of Entries from the ZipFile. + + + + + The entries are extracted into the current working directory. When extraction would would + overwrite an existing filesystem file, the action taken is as specified in the + parameter. + + + + For information on the syntax of the string describing the entry selection criteria, + see . + + + + + This example shows how extract all XML files modified after 15 January 2009, + overwriting any existing files. + + using (ZipFile zip = ZipFile.Read(zipArchiveName)) + { + zip.ExtractSelectedEntries("name = *.xml and mtime > 2009-01-15", + ExtractExistingFileAction.OverwriteSilently); + } + + + + the selection criteria for entries to extract. + + + The action to take if extraction would overwrite an existing file. + + + + + Selects and Extracts a set of Entries from the ZipFile. + + + + + The entries are selected from the specified directory within the archive, and then + extracted into the current working directory. + + + + If any of the files to be extracted already exist, then the action taken is as + specified in the property on the + corresponding ZipEntry instance. By default, the action taken in this case is to + throw an exception. + + + + For information on the syntax of the string describing the entry selection criteria, + see . + + + + + This example shows how extract all XML files modified after 15 January 2009, + and writes them to the "unpack" directory. + + using (ZipFile zip = ZipFile.Read(zipArchiveName)) + { + zip.ExtractSelectedEntries("name = *.xml and mtime > 2009-01-15","unpack"); + } + + + + the selection criteria for entries to extract. + + + the directory in the archive from which to select entries. If null, then + all directories in the archive are used. + + + + + + + Selects and Extracts a set of Entries from the ZipFile. + + + + + The entries are extracted into the specified directory. If any of the files to be + extracted already exist, an exception will be thrown. + + + For information on the syntax of the string describing the entry selection criteria, + see . + + + + the selection criteria for entries to extract. + + + the directory in the archive from which to select entries. If null, then + all directories in the archive are used. + + + + the directory on the disk into which to extract. It will be created + if it does not exist. + + + + + Selects and Extracts a set of Entries from the ZipFile. + + + + + The entries are extracted into the specified directory. When extraction would would + overwrite an existing filesystem file, the action taken is as specified in the + parameter. + + + + For information on the syntax of the string describing the entry selection criteria, + see . + + + + + This example shows how extract all files with an XML extension or with a size larger than 100,000 bytes, + and puts them in the unpack directory. For any files that already exist in + that destination directory, they will not be overwritten. + + using (ZipFile zip = ZipFile.Read(zipArchiveName)) + { + zip.ExtractSelectedEntries("name = *.xml or size > 100000", + null, + "unpack", + ExtractExistingFileAction.DontOverwrite); + } + + + + the selection criteria for entries to extract. + + + The directory on the disk into which to extract. It will be created if it does not exist. + + + + The directory in the archive from which to select entries. If null, then + all directories in the archive are used. + + + + The action to take if extraction would overwrite an existing file. + + + + + + Saves the ZipFile instance to a self-extracting zip archive. + + + + + + The generated exe image will execute on any machine that has the .NET + Framework 2.0 installed on it. The generated exe image is also a + valid ZIP file, readable with DotNetZip or another Zip library or tool + such as WinZip. + + + + There are two "flavors" of self-extracting archive. The + WinFormsApplication version will pop up a GUI and allow the + user to select a target directory into which to extract. There's also + a checkbox allowing the user to specify to overwrite existing files, + and another checkbox to allow the user to request that Explorer be + opened to see the extracted files after extraction. The other flavor + is ConsoleApplication. A self-extractor generated with that + flavor setting will run from the command line. It accepts command-line + options to set the overwrite behavior, and to specify the target + extraction directory. + + + + There are a few temporary files created during the saving to a + self-extracting zip. These files are created in the directory pointed + to by , which defaults to . These temporary files are + removed upon successful completion of this method. + + + + When a user runs the WinForms SFX, the user's personal directory (Environment.SpecialFolder.Personal) + will be used as the default extract location. If you want to set the + default extract location, you should use the other overload of + SaveSelfExtractor()/ The user who runs the SFX will have the + opportunity to change the extract directory before extracting. When + the user runs the Command-Line SFX, the user must explicitly specify + the directory to which to extract. The .NET Framework 2.0 is required + on the computer when the self-extracting archive is run. + + + + NB: This method is not available in the version of DotNetZip build for + the .NET Compact Framework, nor in the "Reduced" DotNetZip library. + + + + + + + string DirectoryPath = "c:\\Documents\\Project7"; + using (ZipFile zip = new ZipFile()) + { + zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath)); + zip.Comment = "This will be embedded into a self-extracting console-based exe"; + zip.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.ConsoleApplication); + } + + + Dim DirectoryPath As String = "c:\Documents\Project7" + Using zip As New ZipFile() + zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath)) + zip.Comment = "This will be embedded into a self-extracting console-based exe" + zip.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.ConsoleApplication) + End Using + + + + + a pathname, possibly fully qualified, to be created. Typically it + will end in an .exe extension. + + Indicates whether a Winforms or Console self-extractor is + desired. + + + + Saves the ZipFile instance to a self-extracting zip archive, using + the specified save options. + + + + + This method saves a self extracting archive, using the specified save + options. These options include the flavor of the SFX, the default extract + directory, the icon file, and so on. See the documentation + for for more + details. + + + + The user who runs the SFX will have the opportunity to change the extract + directory before extracting. If at the time of extraction, the specified + directory does not exist, the SFX will create the directory before + extracting the files. + + + + + + This example saves a WinForms-based self-extracting archive EXE that + will use c:\ExtractHere as the default extract location. The C# code + shows syntax for .NET 3.0, which uses an object initializer for + the SelfExtractorOptions object. + + string DirectoryPath = "c:\\Documents\\Project7"; + using (ZipFile zip = new ZipFile()) + { + zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath)); + zip.Comment = "This will be embedded into a self-extracting WinForms-based exe"; + var options = new SelfExtractorOptions + { + Flavor = SelfExtractorFlavor.WinFormsApplication, + DefaultExtractDirectory = "%USERPROFILE%\\ExtractHere", + PostExtractCommandLine = ExeToRunAfterExtract, + SfxExeWindowTitle = "My Custom Window Title", + RemoveUnpackedFilesAfterExecute = true + }; + zip.SaveSelfExtractor("archive.exe", options); + } + + + Dim DirectoryPath As String = "c:\Documents\Project7" + Using zip As New ZipFile() + zip.AddDirectory(DirectoryPath, System.IO.Path.GetFileName(DirectoryPath)) + zip.Comment = "This will be embedded into a self-extracting console-based exe" + Dim options As New SelfExtractorOptions() + options.Flavor = SelfExtractorFlavor.WinFormsApplication + options.DefaultExtractDirectory = "%USERPROFILE%\\ExtractHere" + options.PostExtractCommandLine = ExeToRunAfterExtract + options.SfxExeWindowTitle = "My Custom Window Title" + options.RemoveUnpackedFilesAfterExecute = True + zip.SaveSelfExtractor("archive.exe", options) + End Using + + + + The name of the EXE to generate. + provides the options for creating the + Self-extracting archive. + + + + Generic IEnumerator support, for use of a ZipFile in an enumeration. + + + + You probably do not want to call GetEnumerator explicitly. Instead + it is implicitly called when you use a loop in C#, or a + For Each loop in VB.NET. + + + + This example reads a zipfile of a given name, then enumerates the + entries in that zip file, and displays the information about each + entry on the Console. + + using (ZipFile zip = ZipFile.Read(zipfile)) + { + bool header = true; + foreach (ZipEntry e in zip) + { + if (header) + { + System.Console.WriteLine("Zipfile: {0}", zip.Name); + System.Console.WriteLine("Version Needed: 0x{0:X2}", e.VersionNeeded); + System.Console.WriteLine("BitField: 0x{0:X2}", e.BitField); + System.Console.WriteLine("Compression Method: 0x{0:X2}", e.CompressionMethod); + System.Console.WriteLine("\n{1,-22} {2,-6} {3,4} {4,-8} {0}", + "Filename", "Modified", "Size", "Ratio", "Packed"); + System.Console.WriteLine(new System.String('-', 72)); + header = false; + } + + System.Console.WriteLine("{1,-22} {2,-6} {3,4:F0}% {4,-8} {0}", + e.FileName, + e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"), + e.UncompressedSize, + e.CompressionRatio, + e.CompressedSize); + + e.Extract(); + } + } + + + + Dim ZipFileToExtract As String = "c:\foo.zip" + Using zip As ZipFile = ZipFile.Read(ZipFileToExtract) + Dim header As Boolean = True + Dim e As ZipEntry + For Each e In zip + If header Then + Console.WriteLine("Zipfile: {0}", zip.Name) + Console.WriteLine("Version Needed: 0x{0:X2}", e.VersionNeeded) + Console.WriteLine("BitField: 0x{0:X2}", e.BitField) + Console.WriteLine("Compression Method: 0x{0:X2}", e.CompressionMethod) + Console.WriteLine(ChrW(10) & "{1,-22} {2,-6} {3,4} {4,-8} {0}", _ + "Filename", "Modified", "Size", "Ratio", "Packed" ) + Console.WriteLine(New String("-"c, 72)) + header = False + End If + Console.WriteLine("{1,-22} {2,-6} {3,4:F0}% {4,-8} {0}", _ + e.FileName, _ + e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"), _ + e.UncompressedSize, _ + e.CompressionRatio, _ + e.CompressedSize ) + e.Extract + Next + End Using + + + + A generic enumerator suitable for use within a foreach loop. + + + + An IEnumerator, for use of a ZipFile in a foreach construct. + + + + This method is included for COM support. An application generally does not call + this method directly. It is called implicitly by COM clients when enumerating + the entries in the ZipFile instance. In VBScript, this is done with a For Each + statement. In Javascript, this is done with new Enumerator(zipfile). + + + + The IEnumerator over the entries in the ZipFile. + + + + + Provides a human-readable string with information about the ZipFile. + + + + + The information string contains 10 lines or so, about each ZipEntry, + describing whether encryption is in use, the compressed and uncompressed + length of the entry, the offset of the entry, and so on. As a result the + information string can be very long for zip files that contain many + entries. + + + This information is mostly useful for diagnostic purposes. + + + + + + Indicates whether to perform a full scan of the zip file when reading it. + + + + + + You almost never want to use this property. + + + + When reading a zip file, if this flag is true (True in + VB), the entire zip archive will be scanned and searched for entries. + For large archives, this can take a very, long time. The much more + efficient default behavior is to read the zip directory, which is + stored at the end of the zip file. But, in some cases the directory is + corrupted and you need to perform a full scan of the zip file to + determine the contents of the zip file. This property lets you do + that, when necessary. + + + + This flag is effective only when calling . Normally you would read a ZipFile with the + static ZipFile.Read + method. But you can't set the FullScan property on the + ZipFile instance when you use a static factory method like + ZipFile.Read. + + + + + + + This example shows how to read a zip file using the full scan approach, + and then save it, thereby producing a corrected zip file. + + + using (var zip = new ZipFile()) + { + zip.FullScan = true; + zip.Initialize(zipFileName); + zip.Save(newName); + } + + + + Using zip As New ZipFile + zip.FullScan = True + zip.Initialize(zipFileName) + zip.Save(newName) + End Using + + + + + + + Whether to sort the ZipEntries before saving the file. + + + + The default is false. If you have a large number of zip entries, the sort + alone can consume significant time. + + + + + using (var zip = new ZipFile()) + { + zip.AddFiles(filesToAdd); + zip.SortEntriesBeforeSaving = true; + zip.Save(name); + } + + + + Using zip As New ZipFile + zip.AddFiles(filesToAdd) + zip.SortEntriesBeforeSaving = True + zip.Save(name) + End Using + + + + + + + Indicates whether NTFS Reparse Points, like junctions, should be + traversed during calls to AddDirectory(). + + + + By default, calls to AddDirectory() will traverse NTFS reparse + points, like mounted volumes, and directory junctions. An example + of a junction is the "My Music" directory in Windows Vista. In some + cases you may not want DotNetZip to traverse those directories. In + that case, set this property to false. + + + + + using (var zip = new ZipFile()) + { + zip.AddDirectoryWillTraverseReparsePoints = false; + zip.AddDirectory(dirToZip,"fodder"); + zip.Save(zipFileToCreate); + } + + + + + + Size of the IO buffer used while saving. + + + + + + First, let me say that you really don't need to bother with this. It is + here to allow for optimizations that you probably won't make! It will work + fine if you don't set or get this property at all. Ok? + + + + Now that we have that out of the way, the fine print: This + property affects the size of the buffer that is used for I/O for each + entry contained in the zip file. When a file is read in to be compressed, + it uses a buffer given by the size here. When you update a zip file, the + data for unmodified entries is copied from the first zip file to the + other, through a buffer given by the size here. + + + + Changing the buffer size affects a few things: first, for larger buffer + sizes, the memory used by the ZipFile, obviously, will be larger + during I/O operations. This may make operations faster for very much + larger files. Last, for any given entry, when you use a larger buffer + there will be fewer progress events during I/O operations, because there's + one progress event generated for each time the buffer is filled and then + emptied. + + + + The default buffer size is 8k. Increasing the buffer size may speed + things up as you compress larger files. But there are no hard-and-fast + rules here, eh? You won't know til you test it. And there will be a + limit where ever larger buffers actually slow things down. So as I said + in the beginning, it's probably best if you don't set or get this property + at all. + + + + + + This example shows how you might set a large buffer size for efficiency when + dealing with zip entries that are larger than 1gb. + + using (ZipFile zip = new ZipFile()) + { + zip.SaveProgress += this.zip1_SaveProgress; + zip.AddDirectory(directoryToZip, ""); + zip.UseZip64WhenSaving = Zip64Option.Always; + zip.BufferSize = 65536*8; // 65536 * 8 = 512k + zip.Save(ZipFileToCreate); + } + + + + + + Size of the work buffer to use for the ZLIB codec during compression. + + + + + When doing ZLIB or Deflate compression, the library fills a buffer, + then passes it to the compressor for compression. Then the library + reads out the compressed bytes. This happens repeatedly until there + is no more uncompressed data to compress. This property sets the + size of the buffer that will be used for chunk-wise compression. In + order for the setting to take effect, your application needs to set + this property before calling one of the ZipFile.Save() + overloads. + + + Setting this affects the performance and memory efficiency of + compression and decompression. For larger files, setting this to a + larger size may improve compression performance, but the exact + numbers vary depending on available memory, the size of the streams + you are compressing, and a bunch of other variables. I don't have + good firm recommendations on how to set it. You'll have to test it + yourself. Or just leave it alone and accept the default. + + + + + + Indicates whether extracted files should keep their paths as + stored in the zip archive. + + + + + This property affects Extraction. It is not used when creating zip + archives. + + + + With this property set to false, the default, extracting entries + from a zip file will create files in the filesystem that have the full + path associated to the entry within the zip file. With this property set + to true, extracting entries from the zip file results in files + with no path: the folders are "flattened." + + + + An example: suppose the zip file contains entries /directory1/file1.txt and + /directory2/file2.txt. With FlattenFoldersOnExtract set to false, + the files created will be \directory1\file1.txt and \directory2\file2.txt. + With the property set to true, the files created are file1.txt and file2.txt. + + + + + + + The compression strategy to use for all entries. + + + + Set the Strategy used by the ZLIB-compatible compressor, when + compressing entries using the DEFLATE method. Different compression + strategies work better on different sorts of data. The strategy + parameter can affect the compression ratio and the speed of + compression but not the correctness of the compresssion. For more + information see Ionic.Zlib.CompressionStrategy. + + + + + The name of the ZipFile, on disk. + + + + + + When the ZipFile instance was created by reading an archive using + one of the ZipFile.Read methods, this property represents the name + of the zip file that was read. When the ZipFile instance was + created by using the no-argument constructor, this value is null + (Nothing in VB). + + + + If you use the no-argument constructor, and you then explicitly set this + property, when you call , this name will + specify the name of the zip file created. Doing so is equivalent to + calling . When instantiating a + ZipFile by reading from a stream or byte array, the Name + property remains null. When saving to a stream, the Name + property is implicitly set to null. + + + + + + Sets the compression level to be used for entries subsequently added to + the zip archive. + + + + + Varying the compression level used on entries can affect the + size-vs-speed tradeoff when compression and decompressing data streams + or files. + + + + As with some other properties on the ZipFile class, like , , and , setting this property on a ZipFile + instance will cause the specified CompressionLevel to be used on all + items that are subsequently added to the + ZipFile instance. If you set this property after you have added + items to the ZipFile, but before you have called Save(), + those items will not use the specified compression level. + + + + If you do not set this property, the default compression level is used, + which normally gives a good balance of compression efficiency and + compression speed. In some tests, using BestCompression can + double the time it takes to compress, while delivering just a small + increase in compression efficiency. This behavior will vary with the + type of data you compress. If you are in doubt, just leave this setting + alone, and accept the default. + + + + + + The compression method for the zipfile. + + + + By default, the compression method is CompressionMethod.Deflate. + + + + + + + A comment attached to the zip archive. + + + + + + This property is read/write. It allows the application to specify a + comment for the ZipFile, or read the comment for the + ZipFile. After setting this property, changes are only made + permanent when you call a Save() method. + + + + According to PKWARE's + zip specification, the comment is not encrypted, even if there is a + password set on the zip file. + + + + The specification does not describe how to indicate the encoding used + on a comment string. Many "compliant" zip tools and libraries use + IBM437 as the code page for comments; DotNetZip, too, follows that + practice. On the other hand, there are situations where you want a + Comment to be encoded with something else, for example using code page + 950 "Big-5 Chinese". To fill that need, DotNetZip will encode the + comment following the same procedure it follows for encoding + filenames: (a) if is + Never, it uses the default encoding (IBM437). (b) if is Always, it always uses the + alternate encoding (). (c) if is AsNecessary, it uses the + alternate encoding only if the default encoding is not sufficient for + encoding the comment - in other words if decoding the result does not + produce the original string. This decision is taken at the time of + the call to ZipFile.Save(). + + + + When creating a zip archive using this library, it is possible to change + the value of between each + entry you add, and between adding entries and the call to + Save(). Don't do this. It will likely result in a zip file that is + not readable by any tool or application. For best interoperability, leave + alone, or specify it only + once, before adding any entries to the ZipFile instance. + + + + + + + Specifies whether the Creation, Access, and Modified times for entries + added to the zip file will be emitted in “Windows format” + when the zip archive is saved. + + + + + An application creating a zip archive can use this flag to explicitly + specify that the file times for the entries should or should not be stored + in the zip archive in the format used by Windows. By default this flag is + true, meaning the Windows-format times are stored in the zip + archive. + + + + When adding an entry from a file or directory, the Creation (), Access (), and Modified () times for the given entry are + automatically set from the filesystem values. When adding an entry from a + stream or string, all three values are implicitly set to + DateTime.Now. Applications can also explicitly set those times by + calling . + + + + PKWARE's + zip specification describes multiple ways to format these times in a + zip file. One is the format Windows applications normally use: 100ns ticks + since January 1, 1601 UTC. The other is a format Unix applications typically + use: seconds since January 1, 1970 UTC. Each format can be stored in an + "extra field" in the zip entry when saving the zip archive. The former + uses an extra field with a Header Id of 0x000A, while the latter uses a + header ID of 0x5455, although you probably don't need to know that. + + + + Not all tools and libraries can interpret these fields. Windows + compressed folders is one that can read the Windows Format timestamps, + while I believe the Infozip + tools can read the Unix format timestamps. Some tools and libraries + may be able to read only one or the other. DotNetZip can read or write + times in either or both formats. + + + + The times stored are taken from , , and . + + + + The value set here applies to all entries subsequently added to the + ZipFile. + + + + This property is not mutually exclusive of the property. It is possible and + legal and valid to produce a zip file that contains timestamps encoded in + the Unix format as well as in the Windows format, in addition to the LastModified time attached to each + entry in the archive, a time that is always stored in "DOS format". And, + notwithstanding the names PKWare uses for these time formats, any of them + can be read and written by any computer, on any operating system. But, + there are no guarantees that a program running on Mac or Linux will + gracefully handle a zip file with "Windows" formatted times, or that an + application that does not use DotNetZip but runs on Windows will be able to + handle file times in Unix format. + + + + When in doubt, test. Sorry, I haven't got a complete list of tools and + which sort of timestamps they can use and will tolerate. If you get any + good information and would like to pass it on, please do so and I will + include that information in this documentation. + + + + + This example shows how to save a zip file that contains file timestamps + in a format normally used by Unix. + + using (var zip = new ZipFile()) + { + // produce a zip file the Mac will like + zip.EmitTimesInWindowsFormatWhenSaving = false; + zip.EmitTimesInUnixFormatWhenSaving = true; + zip.AddDirectory(directoryToZip, "files"); + zip.Save(outputFile); + } + + + + Using zip As New ZipFile + '' produce a zip file the Mac will like + zip.EmitTimesInWindowsFormatWhenSaving = False + zip.EmitTimesInUnixFormatWhenSaving = True + zip.AddDirectory(directoryToZip, "files") + zip.Save(outputFile) + End Using + + + + + + + + + Specifies whether the Creation, Access, and Modified times + for entries added to the zip file will be emitted in "Unix(tm) + format" when the zip archive is saved. + + + + + An application creating a zip archive can use this flag to explicitly + specify that the file times for the entries should or should not be stored + in the zip archive in the format used by Unix. By default this flag is + false, meaning the Unix-format times are not stored in the zip + archive. + + + + When adding an entry from a file or directory, the Creation (), Access (), and Modified () times for the given entry are + automatically set from the filesystem values. When adding an entry from a + stream or string, all three values are implicitly set to DateTime.Now. + Applications can also explicitly set those times by calling . + + + + PKWARE's + zip specification describes multiple ways to format these times in a + zip file. One is the format Windows applications normally use: 100ns ticks + since January 1, 1601 UTC. The other is a format Unix applications + typically use: seconds since January 1, 1970 UTC. Each format can be + stored in an "extra field" in the zip entry when saving the zip + archive. The former uses an extra field with a Header Id of 0x000A, while + the latter uses a header ID of 0x5455, although you probably don't need to + know that. + + + + Not all tools and libraries can interpret these fields. Windows + compressed folders is one that can read the Windows Format timestamps, + while I believe the Infozip + tools can read the Unix format timestamps. Some tools and libraries may be + able to read only one or the other. DotNetZip can read or write times in + either or both formats. + + + + The times stored are taken from , , and . + + + + This property is not mutually exclusive of the property. It is possible and + legal and valid to produce a zip file that contains timestamps encoded in + the Unix format as well as in the Windows format, in addition to the LastModified time attached to each + entry in the zip archive, a time that is always stored in "DOS + format". And, notwithstanding the names PKWare uses for these time + formats, any of them can be read and written by any computer, on any + operating system. But, there are no guarantees that a program running on + Mac or Linux will gracefully handle a zip file with "Windows" formatted + times, or that an application that does not use DotNetZip but runs on + Windows will be able to handle file times in Unix format. + + + + When in doubt, test. Sorry, I haven't got a complete list of tools and + which sort of timestamps they can use and will tolerate. If you get any + good information and would like to pass it on, please do so and I will + include that information in this documentation. + + + + + + + + + Indicates whether verbose output is sent to the during AddXxx() and + ReadXxx() operations. + + + + This is a synthetic property. It returns true if the is non-null. + + + + + Indicates whether to perform case-sensitive matching on the filename when + retrieving entries in the zipfile via the string-based indexer. + + + + The default value is false, which means don't do case-sensitive + matching. In other words, retrieving zip["ReadMe.Txt"] is the same as + zip["readme.txt"]. It really makes sense to set this to true only + if you are not running on Windows, which has case-insensitive + filenames. But since this library is not built for non-Windows platforms, + in most cases you should just leave this property alone. + + + + + Indicates whether to encode entry filenames and entry comments using Unicode + (UTF-8). + + + + + The + PKWare zip specification provides for encoding file names and file + comments in either the IBM437 code page, or in UTF-8. This flag selects + the encoding according to that specification. By default, this flag is + false, and filenames and comments are encoded into the zip file in the + IBM437 codepage. Setting this flag to true will specify that filenames + and comments that cannot be encoded with IBM437 will be encoded with + UTF-8. + + + + Zip files created with strict adherence to the PKWare specification with + respect to UTF-8 encoding can contain entries with filenames containing + any combination of Unicode characters, including the full range of + characters from Chinese, Latin, Hebrew, Greek, Cyrillic, and many other + alphabets. However, because at this time, the UTF-8 portion of the PKWare + specification is not broadly supported by other zip libraries and + utilities, such zip files may not be readable by your favorite zip tool or + archiver. In other words, interoperability will decrease if you set this + flag to true. + + + + In particular, Zip files created with strict adherence to the PKWare + specification with respect to UTF-8 encoding will not work well with + Explorer in Windows XP or Windows Vista, because Windows compressed + folders, as far as I know, do not support UTF-8 in zip files. Vista can + read the zip files, but shows the filenames incorrectly. Unpacking from + Windows Vista Explorer will result in filenames that have rubbish + characters in place of the high-order UTF-8 bytes. + + + + Also, zip files that use UTF-8 encoding will not work well with Java + applications that use the java.util.zip classes, as of v5.0 of the Java + runtime. The Java runtime does not correctly implement the PKWare + specification in this regard. + + + + As a result, we have the unfortunate situation that "correct" behavior by + the DotNetZip library with regard to Unicode encoding of filenames during + zip creation will result in zip files that are readable by strictly + compliant and current tools (for example the most recent release of the + commercial WinZip tool); but these zip files will not be readable by + various other tools or libraries, including Windows Explorer. + + + + The DotNetZip library can read and write zip files with UTF8-encoded + entries, according to the PKware spec. If you use DotNetZip for both + creating and reading the zip file, and you use UTF-8, there will be no + loss of information in the filenames. For example, using a self-extractor + created by this library will allow you to unpack files correctly with no + loss of information in the filenames. + + + + If you do not set this flag, it will remain false. If this flag is false, + your ZipFile will encode all filenames and comments using the + IBM437 codepage. This can cause "loss of information" on some filenames, + but the resulting zipfile will be more interoperable with other + utilities. As an example of the loss of information, diacritics can be + lost. The o-tilde character will be down-coded to plain o. The c with a + cedilla (Unicode 0xE7) used in Portugese will be downcoded to a c. + Likewise, the O-stroke character (Unicode 248), used in Danish and + Norwegian, will be down-coded to plain o. Chinese characters cannot be + represented in codepage IBM437; when using the default encoding, Chinese + characters in filenames will be represented as ?. These are all examples + of "information loss". + + + + The loss of information associated to the use of the IBM437 encoding is + inconvenient, and can also lead to runtime errors. For example, using + IBM437, any sequence of 4 Chinese characters will be encoded as ????. If + your application creates a ZipFile, then adds two files, each with + names of four Chinese characters each, this will result in a duplicate + filename exception. In the case where you add a single file with a name + containing four Chinese characters, calling Extract() on the entry that + has question marks in the filename will result in an exception, because + the question mark is not legal for use within filenames on Windows. These + are just a few examples of the problems associated to loss of information. + + + + This flag is independent of the encoding of the content within the entries + in the zip file. Think of the zip file as a container - it supports an + encoding. Within the container are other "containers" - the file entries + themselves. The encoding within those entries is independent of the + encoding of the zip archive container for those entries. + + + + Rather than specify the encoding in a binary fashion using this flag, an + application can specify an arbitrary encoding via the property. Setting the encoding + explicitly when creating zip archives will result in non-compliant zip + files that, curiously, are fairly interoperable. The challenge is, the + PKWare specification does not provide for a way to specify that an entry + in a zip archive uses a code page that is neither IBM437 nor UTF-8. + Therefore if you set the encoding explicitly when creating a zip archive, + you must take care upon reading the zip archive to use the same code page. + If you get it wrong, the behavior is undefined and may result in incorrect + filenames, exceptions, stomach upset, hair loss, and acne. + + + + + + + Specify whether to use ZIP64 extensions when saving a zip archive. + + + + + + When creating a zip file, the default value for the property is . is + safest, in the sense that you will not get an Exception if a pre-ZIP64 + limit is exceeded. + + + + You may set the property at any time before calling Save(). + + + + When reading a zip file via the Zipfile.Read() method, DotNetZip + will properly read ZIP64-endowed zip archives, regardless of the value of + this property. DotNetZip will always read ZIP64 archives. This property + governs only whether DotNetZip will write them. Therefore, when updating + archives, be careful about setting this property after reading an archive + that may use ZIP64 extensions. + + + + An interesting question is, if you have set this property to + AsNecessary, and then successfully saved, does the resulting + archive use ZIP64 extensions or not? To learn this, check the property, after calling Save(). + + + + Have you thought about + donating? + + + + + + + + Indicates whether the archive requires ZIP64 extensions. + + + + + + This property is null (or Nothing in VB) if the archive has + not been saved, and there are fewer than 65334 ZipEntry items + contained in the archive. + + + + The Value is true if any of the following four conditions holds: + the uncompressed size of any entry is larger than 0xFFFFFFFF; the + compressed size of any entry is larger than 0xFFFFFFFF; the relative + offset of any entry within the zip archive is larger than 0xFFFFFFFF; or + there are more than 65534 entries in the archive. (0xFFFFFFFF = + 4,294,967,295). The result may not be known until a Save() is attempted + on the zip archive. The Value of this + property may be set only AFTER one of the Save() methods has been called. + + + + If none of the four conditions holds, and the archive has been saved, then + the Value is false. + + + + A Value of false does not indicate that the zip archive, as saved, + does not use ZIP64. It merely indicates that ZIP64 is not required. An + archive may use ZIP64 even when not required if the property is set to , or if the property is set to and the output stream was not + seekable. Use the property to determine if + the most recent Save() method resulted in an archive that utilized + the ZIP64 extensions. + + + + + + + + + Indicates whether the most recent Save() operation used ZIP64 extensions. + + + + + The use of ZIP64 extensions within an archive is not always necessary, and + for interoperability concerns, it may be desired to NOT use ZIP64 if + possible. The property can be + set to use ZIP64 extensions only when necessary. In those cases, + Sometimes applications want to know whether a Save() actually used ZIP64 + extensions. Applications can query this read-only property to learn + whether ZIP64 has been used in a just-saved ZipFile. + + + + The value is null (or Nothing in VB) if the archive has not + been saved. + + + + Non-null values (HasValue is true) indicate whether ZIP64 + extensions were used during the most recent Save() operation. The + ZIP64 extensions may have been used as required by any particular entry + because of its uncompressed or compressed size, or because the archive is + larger than 4294967295 bytes, or because there are more than 65534 entries + in the archive, or because the UseZip64WhenSaving property was set + to , or because the + UseZip64WhenSaving property was set to and the output stream was not seekable. + The value of this property does not indicate the reason the ZIP64 + extensions were used. + + + + + + + + + Indicates whether the most recent Read() operation read a zip file that uses + ZIP64 extensions. + + + + This property will return null (Nothing in VB) if you've added an entry after reading + the zip file. + + + + + The text encoding to use when writing new entries to the ZipFile, + for those entries that cannot be encoded with the default (IBM437) + encoding; or, the text encoding that was used when reading the entries + from the ZipFile. + + + + + In its + zip specification, PKWare describes two options for encoding + filenames and comments: using IBM437 or UTF-8. But, some archiving tools + or libraries do not follow the specification, and instead encode + characters using the system default code page. For example, WinRAR when + run on a machine in Shanghai may encode filenames with the Big-5 Chinese + (950) code page. This behavior is contrary to the Zip specification, but + it occurs anyway. + + + + When using DotNetZip to write zip archives that will be read by one of + these other archivers, set this property to specify the code page to use + when encoding the and for each ZipEntry in the zip file, for + values that cannot be encoded with the default codepage for zip files, + IBM437. This is why this property is "provisional". In all cases, IBM437 + is used where possible, in other words, where no loss of data would + result. It is possible, therefore, to have a given entry with a + Comment encoded in IBM437 and a FileName encoded with the + specified "provisional" codepage. + + + + Be aware that a zip file created after you've explicitly set the property to a value other than + IBM437 may not be compliant to the PKWare specification, and may not be + readable by compliant archivers. On the other hand, many (most?) + archivers are non-compliant and can read zip files created in arbitrary + code pages. The trick is to use or specify the proper codepage when + reading the zip. + + + + When creating a zip archive using this library, it is possible to change + the value of between each + entry you add, and between adding entries and the call to + Save(). Don't do this. It will likely result in a zipfile that is + not readable. For best interoperability, either leave alone, or specify it only once, + before adding any entries to the ZipFile instance. There is one + exception to this recommendation, described later. + + + + When using an arbitrary, non-UTF8 code page for encoding, there is no + standard way for the creator application - whether DotNetZip, WinZip, + WinRar, or something else - to formally specify in the zip file which + codepage has been used for the entries. As a result, readers of zip files + are not able to inspect the zip file and determine the codepage that was + used for the entries contained within it. It is left to the application + or user to determine the necessary codepage when reading zip files encoded + this way. In other words, if you explicitly specify the codepage when you + create the zipfile, you must explicitly specify the same codepage when + reading the zipfile. + + + + The way you specify the code page to use when reading a zip file varies + depending on the tool or library you use to read the zip. In DotNetZip, + you use a ZipFile.Read() method that accepts an encoding parameter. It + isn't possible with Windows Explorer, as far as I know, to specify an + explicit codepage to use when reading a zip. If you use an incorrect + codepage when reading a zipfile, you will get entries with filenames that + are incorrect, and the incorrect filenames may even contain characters + that are not legal for use within filenames in Windows. Extracting entries + with illegal characters in the filenames will lead to exceptions. It's too + bad, but this is just the way things are with code pages in zip + files. Caveat Emptor. + + + + Example: Suppose you create a zipfile that contains entries with + filenames that have Danish characters. If you use equal to "iso-8859-1" (cp 28591), + the filenames will be correctly encoded in the zip. But, to read that + zipfile correctly, you have to specify the same codepage at the time you + read it. If try to read that zip file with Windows Explorer or another + application that is not flexible with respect to the codepage used to + decode filenames in zipfiles, you will get a filename like "Inf°.txt". + + + + When using DotNetZip to read a zip archive, and the zip archive uses an + arbitrary code page, you must specify the encoding to use before or when + the Zipfile is READ. This means you must use a ZipFile.Read() + method that allows you to specify a System.Text.Encoding parameter. Setting + the ProvisionalAlternateEncoding property after your application has read in + the zip archive will not affect the entry names of entries that have already + been read in. + + + + And now, the exception to the rule described above. One strategy for + specifying the code page for a given zip file is to describe the code page + in a human-readable form in the Zip comment. For example, the comment may + read "Entries in this archive are encoded in the Big5 code page". For + maximum interoperability, the zip comment in this case should be encoded + in the default, IBM437 code page. In this case, the zip comment is + encoded using a different page than the filenames. To do this, Specify + ProvisionalAlternateEncoding to your desired region-specific code + page, once before adding any entries, and then reset + ProvisionalAlternateEncoding to IBM437 before setting the property and calling Save(). + + + + + This example shows how to read a zip file using the Big-5 Chinese code page + (950), and extract each entry in the zip file. For this code to work as + desired, the Zipfile must have been created using the big5 code page + (CP950). This is typical, for example, when using WinRar on a machine with + CP950 set as the default code page. In that case, the names of entries + within the Zip archive will be stored in that code page, and reading the zip + archive must be done using that code page. If the application did not use + the correct code page in ZipFile.Read(), then names of entries within the + zip archive would not be correctly retrieved. + + using (var zip = ZipFile.Read(zipFileName, System.Text.Encoding.GetEncoding("big5"))) + { + // retrieve and extract an entry using a name encoded with CP950 + zip[MyDesiredEntry].Extract("unpack"); + } + + + + Using zip As ZipFile = ZipFile.Read(ZipToExtract, System.Text.Encoding.GetEncoding("big5")) + ' retrieve and extract an entry using a name encoded with CP950 + zip(MyDesiredEntry).Extract("unpack") + End Using + + + + DefaultEncoding + + + + A Text Encoding to use when encoding the filenames and comments for + all the ZipEntry items, during a ZipFile.Save() operation. + + + + Whether the encoding specified here is used during the save depends + on . + + + + + + A flag that tells if and when this instance should apply + AlternateEncoding to encode the filenames and comments associated to + of ZipEntry objects contained within this instance. + + + + + The default text encoding used in zip archives. It is numeric 437, also + known as IBM437. + + + + + + Gets or sets the TextWriter to which status messages are delivered + for the instance. + + + + If the TextWriter is set to a non-null value, then verbose output is sent + to the TextWriter during Add, Read, Save and + Extract operations. Typically, console applications might use + Console.Out and graphical or headless applications might use a + System.IO.StringWriter. The output of this is suitable for viewing + by humans. + + + + + In this example, a console application instantiates a ZipFile, then + sets the StatusMessageTextWriter to Console.Out. At that + point, all verbose status messages for that ZipFile are sent to the + console. + + + + using (ZipFile zip= ZipFile.Read(FilePath)) + { + zip.StatusMessageTextWriter= System.Console.Out; + // messages are sent to the console during extraction + zip.ExtractAll(); + } + + + + Using zip As ZipFile = ZipFile.Read(FilePath) + zip.StatusMessageTextWriter= System.Console.Out + 'Status Messages will be sent to the console during extraction + zip.ExtractAll() + End Using + + + + In this example, a Windows Forms application instantiates a + ZipFile, then sets the StatusMessageTextWriter to a + StringWriter. At that point, all verbose status messages for that + ZipFile are sent to the StringWriter. + + + + var sw = new System.IO.StringWriter(); + using (ZipFile zip= ZipFile.Read(FilePath)) + { + zip.StatusMessageTextWriter= sw; + zip.ExtractAll(); + } + Console.WriteLine("{0}", sw.ToString()); + + + + Dim sw as New System.IO.StringWriter + Using zip As ZipFile = ZipFile.Read(FilePath) + zip.StatusMessageTextWriter= sw + zip.ExtractAll() + End Using + 'Status Messages are now available in sw + + + + + + + Gets or sets the name for the folder to store the temporary file + this library writes when saving a zip archive. + + + + + This library will create a temporary file when saving a Zip archive to a + file. This file is written when calling one of the Save() methods + that does not save to a stream, or one of the SaveSelfExtractor() + methods. + + + + By default, the library will create the temporary file in the directory + specified for the file itself, via the property or via + the method. + + + + Setting this property allows applications to override this default + behavior, so that the library will create the temporary file in the + specified folder. For example, to have the library create the temporary + file in the current working directory, regardless where the ZipFile + is saved, specfy ".". To revert to the default behavior, set this + property to null (Nothing in VB). + + + + When setting the property to a non-null value, the folder specified must + exist; if it does not an exception is thrown. The application should have + write and delete permissions on the folder. The permissions are not + explicitly checked ahead of time; if the application does not have the + appropriate rights, an exception will be thrown at the time Save() + is called. + + + + There is no temporary file created when reading a zip archive. When + saving to a Stream, there is no temporary file created. For example, if + the application is an ASP.NET application and calls Save() + specifying the Response.OutputStream as the output stream, there is + no temporary file created. + + + + + Thrown when setting the property if the directory does not exist. + + + + + + Sets the password to be used on the ZipFile instance. + + + + + + When writing a zip archive, this password is applied to the entries, not + to the zip archive itself. It applies to any ZipEntry subsequently + added to the ZipFile, using one of the AddFile, + AddDirectory, AddEntry, or AddItem methods, etc. + When reading a zip archive, this property applies to any entry + subsequently extracted from the ZipFile using one of the Extract + methods on the ZipFile class. + + + + When writing a zip archive, keep this in mind: though the password is set + on the ZipFile object, according to the Zip spec, the "directory" of the + archive - in other words the list of entries or files contained in the archive - is + not encrypted with the password, or protected in any way. If you set the + Password property, the password actually applies to individual entries + that are added to the archive, subsequent to the setting of this property. + The list of filenames in the archive that is eventually created will + appear in clear text, but the contents of the individual files are + encrypted. This is how Zip encryption works. + + + + One simple way around this limitation is to simply double-wrap sensitive + filenames: Store the files in a zip file, and then store that zip file + within a second, "outer" zip file. If you apply a password to the outer + zip file, then readers will be able to see that the outer zip file + contains an inner zip file. But readers will not be able to read the + directory or file list of the inner zip file. + + + + If you set the password on the ZipFile, and then add a set of files + to the archive, then each entry is encrypted with that password. You may + also want to change the password between adding different entries. If you + set the password, add an entry, then set the password to null + (Nothing in VB), and add another entry, the first entry is + encrypted and the second is not. If you call AddFile(), then set + the Password property, then call ZipFile.Save, the file + added will not be password-protected, and no warning will be generated. + + + + When setting the Password, you may also want to explicitly set the property, to specify how to encrypt the entries added + to the ZipFile. If you set the Password to a non-null value and do not + set , then PKZip 2.0 ("Weak") encryption is used. + This encryption is relatively weak but is very interoperable. If you set + the password to a null value (Nothing in VB), Encryption is + reset to None. + + + + All of the preceding applies to writing zip archives, in other words when + you use one of the Save methods. To use this property when reading or an + existing ZipFile, do the following: set the Password property on the + ZipFile, then call one of the Extract() overloads on the . In this case, the entry is extracted using the + Password that is specified on the ZipFile instance. If you + have not set the Password property, then the password is + null, and the entry is extracted with no password. + + + + If you set the Password property on the ZipFile, then call + Extract() an entry that has not been encrypted with a password, the + password is not used for that entry, and the ZipEntry is extracted + as normal. In other words, the password is used only if necessary. + + + + The class also has a Password property. It takes precedence + over this property on the ZipFile. Typically, you would use the + per-entry Password when most entries in the zip archive use one password, + and a few entries use a different password. If all entries in the zip + file use the same password, then it is simpler to just set this property + on the ZipFile itself, whether creating a zip archive or extracting + a zip archive. + + + + + + + This example creates a zip file, using password protection for the + entries, and then extracts the entries from the zip file. When creating + the zip file, the Readme.txt file is not protected with a password, but + the other two are password-protected as they are saved. During extraction, + each file is extracted with the appropriate password. + + + // create a file with encryption + using (ZipFile zip = new ZipFile()) + { + zip.AddFile("ReadMe.txt"); + zip.Password= "!Secret1"; + zip.AddFile("MapToTheSite-7440-N49th.png"); + zip.AddFile("2008-Regional-Sales-Report.pdf"); + zip.Save("EncryptedArchive.zip"); + } + + // extract entries that use encryption + using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip")) + { + zip.Password= "!Secret1"; + zip.ExtractAll("extractDir"); + } + + + + + Using zip As New ZipFile + zip.AddFile("ReadMe.txt") + zip.Password = "123456!" + zip.AddFile("MapToTheSite-7440-N49th.png") + zip.Password= "!Secret1"; + zip.AddFile("2008-Regional-Sales-Report.pdf") + zip.Save("EncryptedArchive.zip") + End Using + + + ' extract entries that use encryption + Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip")) + zip.Password= "!Secret1" + zip.ExtractAll("extractDir") + End Using + + + + + + ZipFile.Encryption + ZipEntry.Password + + + + The action the library should take when extracting a file that already + exists. + + + + + This property affects the behavior of the Extract methods (one of the + Extract() or ExtractWithPassword() overloads), when + extraction would would overwrite an existing filesystem file. If you do + not set this property, the library throws an exception when extracting an + entry would overwrite an existing file. + + + + This property has no effect when extracting to a stream, or when the file + to be extracted does not already exist. + + + + + + + The action the library should take when an error is encountered while + opening or reading files as they are saved into a zip archive. + + + + + Errors can occur as a file is being saved to the zip archive. For + example, the File.Open may fail, or a File.Read may fail, because of + lock conflicts or other reasons. + + + + The first problem might occur after having called AddDirectory() on a + directory that contains a Clipper .dbf file; the file is locked by + Clipper and cannot be opened for read by another process. An example of + the second problem might occur when trying to zip a .pst file that is in + use by Microsoft Outlook. Outlook locks a range on the file, which allows + other processes to open the file, but not read it in its entirety. + + + + This property tells DotNetZip what you would like to do in the case of + these errors. The primary options are: ZipErrorAction.Throw to + throw an exception (this is the default behavior if you don't set this + property); ZipErrorAction.Skip to Skip the file for which there + was an error and continue saving; ZipErrorAction.Retry to Retry + the entry that caused the problem; or + ZipErrorAction.InvokeErrorEvent to invoke an event handler. + + + + This property is implicitly set to ZipErrorAction.InvokeErrorEvent + if you add a handler to the event. If you set + this property to something other than + ZipErrorAction.InvokeErrorEvent, then the ZipError + event is implicitly cleared. What it means is you can set one or the + other (or neither), depending on what you want, but you never need to set + both. + + + + As with some other properties on the ZipFile class, like , , and , setting this property on a ZipFile + instance will cause the specified ZipErrorAction to be used on all + items that are subsequently added to the + ZipFile instance. If you set this property after you have added + items to the ZipFile, but before you have called Save(), + those items will not use the specified error handling action. + + + + If you want to handle any errors that occur with any entry in the zip + file in the same way, then set this property once, before adding any + entries to the zip archive. + + + + If you set this property to ZipErrorAction.Skip and you'd like to + learn which files may have been skipped after a Save(), you can + set the on the ZipFile before + calling Save(). A message will be emitted into that writer for + each skipped file, if any. + + + + + + This example shows how to tell DotNetZip to skip any files for which an + error is generated during the Save(). + + Public Sub SaveZipFile() + Dim SourceFolder As String = "fodder" + Dim DestFile As String = "eHandler.zip" + Dim sw as New StringWriter + Using zipArchive As ZipFile = New ZipFile + ' Tell DotNetZip to skip any files for which it encounters an error + zipArchive.ZipErrorAction = ZipErrorAction.Skip + zipArchive.StatusMessageTextWriter = sw + zipArchive.AddDirectory(SourceFolder) + zipArchive.Save(DestFile) + End Using + ' examine sw here to see any messages + End Sub + + + + + + + + + + The Encryption to use for entries added to the ZipFile. + + + + + Set this when creating a zip archive, or when updating a zip archive. The + specified Encryption is applied to the entries subsequently added to the + ZipFile instance. Applications do not need to set the + Encryption property when reading or extracting a zip archive. + + + + If you set this to something other than EncryptionAlgorithm.None, you + will also need to set the . + + + + As with some other properties on the ZipFile class, like and , setting this + property on a ZipFile instance will cause the specified + EncryptionAlgorithm to be used on all items + that are subsequently added to the ZipFile instance. In other + words, if you set this property after you have added items to the + ZipFile, but before you have called Save(), those items will + not be encrypted or protected with a password in the resulting zip + archive. To get a zip archive with encrypted entries, set this property, + along with the property, before calling + AddFile, AddItem, or AddDirectory (etc.) on the + ZipFile instance. + + + + If you read a ZipFile, you can modify the Encryption on an + encrypted entry, only by setting the Encryption property on the + ZipEntry itself. Setting the Encryption property on the + ZipFile, once it has been created via a call to + ZipFile.Read(), does not affect entries that were previously read. + + + + For example, suppose you read a ZipFile, and there is an encrypted + entry. Setting the Encryption property on that ZipFile and + then calling Save() on the ZipFile does not update the + Encryption used for the entries in the archive. Neither is an + exception thrown. Instead, what happens during the Save() is that + all previously existing entries are copied through to the new zip archive, + with whatever encryption and password that was used when originally + creating the zip archive. Upon re-reading that archive, to extract + entries, applications should use the original password or passwords, if + any. + + + + Suppose an application reads a ZipFile, and there is an encrypted + entry. Setting the Encryption property on that ZipFile and + then adding new entries (via AddFile(), AddEntry(), etc) + and then calling Save() on the ZipFile does not update the + Encryption on any of the entries that had previously been in the + ZipFile. The Encryption property applies only to the + newly-added entries. + + + + + + + This example creates a zip archive that uses encryption, and then extracts + entries from the archive. When creating the zip archive, the ReadMe.txt + file is zipped without using a password or encryption. The other files + use encryption. + + + + // Create a zip archive with AES Encryption. + using (ZipFile zip = new ZipFile()) + { + zip.AddFile("ReadMe.txt"); + zip.Encryption= EncryptionAlgorithm.WinZipAes256; + zip.Password= "Top.Secret.No.Peeking!"; + zip.AddFile("7440-N49th.png"); + zip.AddFile("2008-Regional-Sales-Report.pdf"); + zip.Save("EncryptedArchive.zip"); + } + + // Extract a zip archive that uses AES Encryption. + // You do not need to specify the algorithm during extraction. + using (ZipFile zip = ZipFile.Read("EncryptedArchive.zip")) + { + zip.Password= "Top.Secret.No.Peeking!"; + zip.ExtractAll("extractDirectory"); + } + + + + ' Create a zip that uses Encryption. + Using zip As New ZipFile() + zip.Encryption= EncryptionAlgorithm.WinZipAes256 + zip.Password= "Top.Secret.No.Peeking!" + zip.AddFile("ReadMe.txt") + zip.AddFile("7440-N49th.png") + zip.AddFile("2008-Regional-Sales-Report.pdf") + zip.Save("EncryptedArchive.zip") + End Using + + ' Extract a zip archive that uses AES Encryption. + ' You do not need to specify the algorithm during extraction. + Using (zip as ZipFile = ZipFile.Read("EncryptedArchive.zip")) + zip.Password= "Top.Secret.No.Peeking!" + zip.ExtractAll("extractDirectory") + End Using + + + + + ZipFile.Password + ZipEntry.Encryption + + + + A callback that allows the application to specify the compression level + to use for entries subsequently added to the zip archive. + + + + + + With this callback, the DotNetZip library allows the application to + determine whether compression will be used, at the time of the + Save. This may be useful if the application wants to favor + speed over size, and wants to defer the decision until the time of + Save. + + + + Typically applications set the property on + the ZipFile or on each ZipEntry to determine the level of + compression used. This is done at the time the entry is added to the + ZipFile. Setting the property to + Ionic.Zlib.CompressionLevel.None means no compression will be used. + + + + This callback allows the application to defer the decision on the + CompressionLevel to use, until the time of the call to + ZipFile.Save(). The callback is invoked once per ZipEntry, + at the time the data for the entry is being written out as part of a + Save() operation. The application can use whatever criteria it + likes in determining the level to return. For example, an application may + wish that no .mp3 files should be compressed, because they are already + compressed and the extra compression is not worth the CPU time incurred, + and so can return None for all .mp3 entries. + + + + The library determines whether compression will be attempted for an entry + this way: If the entry is a zero length file, or a directory, no + compression is used. Otherwise, if this callback is set, it is invoked + and the CompressionLevel is set to the return value. If this + callback has not been set, then the previously set value for + CompressionLevel is used. + + + + + + + The maximum size of an output segment, when saving a split Zip file. + + + + Set this to a non-zero value before calling or to specify that the ZipFile should be saved as a + split archive, also sometimes called a spanned archive. Some also + call them multi-file archives. + + + + A split zip archive is saved in a set of discrete filesystem files, + rather than in a single file. This is handy when transmitting the + archive in email or some other mechanism that has a limit to the size of + each file. The first file in a split archive will be named + basename.z01, the second will be named basename.z02, and + so on. The final file is named basename.zip. According to the zip + specification from PKWare, the minimum value is 65536, for a 64k segment + size. The maximum number of segments allows in a split archive is 99. + + + + The value of this property determines the maximum size of a split + segment when writing a split archive. For example, suppose you have a + ZipFile that would save to a single file of 200k. If you set the + MaxOutputSegmentSize to 65536 before calling Save(), you + will get four distinct output files. On the other hand if you set this + property to 256k, then you will get a single-file archive for that + ZipFile. + + + + The size of each split output file will be as large as possible, up to + the maximum size set here. The zip specification requires that some data + fields in a zip archive may not span a split boundary, and an output + segment may be smaller than the maximum if necessary to avoid that + problem. Also, obviously the final segment of the archive may be smaller + than the maximum segment size. Segments will never be larger than the + value set with this property. + + + + You can save a split Zip file only when saving to a regular filesystem + file. It's not possible to save a split zip file as a self-extracting + archive, nor is it possible to save a split zip file to a stream. When + saving to a SFX or to a Stream, this property is ignored. + + + + About interoperability: Split or spanned zip files produced by DotNetZip + can be read by WinZip or PKZip, and vice-versa. Segmented zip files may + not be readable by other tools, if those other tools don't support zip + spanning or splitting. When in doubt, test. I don't believe Windows + Explorer can extract a split archive. + + + + This property has no effect when reading a split archive. You can read + a split archive in the normal way with DotNetZip. + + + + When saving a zip file, if you want a regular zip file rather than a + split zip file, don't set this property, or set it to Zero. + + + + If you read a split archive, with and + then subsequently call ZipFile.Save(), unless you set this + property before calling Save(), you will get a normal, + single-file archive. + + + + + + + + Returns the number of segments used in the most recent Save() operation. + + + + This is normally zero, unless you have set the property. If you have set , and then you save a file, after the call to + Save() completes, you can read this value to learn the number of segments that + were created. + + + If you call Save("Archive.zip"), and it creates 5 segments, then you + will have filesystem files named Archive.z01, Archive.z02, Archive.z03, + Archive.z04, and Archive.zip, and the value of this property will be 5. + + + + + + + The size threshold for an entry, above which a parallel deflate is used. + + + + + + DotNetZip will use multiple threads to compress any ZipEntry, + if the entry is larger than the given size. Zero means "always + use parallel deflate", while -1 means "never use parallel + deflate". The default value for this property is 512k. Aside + from the special values of 0 and 1, the minimum value is 65536. + + + + If the entry size cannot be known before compression, as with a + read-forward stream, then Parallel deflate will never be + performed, unless the value of this property is zero. + + + + A parallel deflate operations will speed up the compression of + large files, on computers with multiple CPUs or multiple CPU + cores. For files above 1mb, on a dual core or dual-cpu (2p) + machine, the time required to compress the file can be 70% of the + single-threaded deflate. For very large files on 4p machines the + compression can be done in 30% of the normal time. The downside + is that parallel deflate consumes extra memory during the deflate, + and the deflation is not as effective. + + + + Parallel deflate tends to yield slightly less compression when + compared to as single-threaded deflate; this is because the original + data stream is split into multiple independent buffers, each of which + is compressed in parallel. But because they are treated + independently, there is no opportunity to share compression + dictionaries. For that reason, a deflated stream may be slightly + larger when compressed using parallel deflate, as compared to a + traditional single-threaded deflate. Sometimes the increase over the + normal deflate is as much as 5% of the total compressed size. For + larger files it can be as small as 0.1%. + + + + Multi-threaded compression does not give as much an advantage when + using Encryption. This is primarily because encryption tends to slow + down the entire pipeline. Also, multi-threaded compression gives less + of an advantage when using lower compression levels, for example . You may have to + perform some tests to determine the best approach for your situation. + + + + + + + + + + The maximum number of buffer pairs to use when performing + parallel compression. + + + + + This property sets an upper limit on the number of memory + buffer pairs to create when performing parallel + compression. The implementation of the parallel + compression stream allocates multiple buffers to + facilitate parallel compression. As each buffer fills up, + the stream uses + ThreadPool.QueueUserWorkItem() to compress those + buffers in a background threadpool thread. After a buffer + is compressed, it is re-ordered and written to the output + stream. + + + + A higher number of buffer pairs enables a higher degree of + parallelism, which tends to increase the speed of compression on + multi-cpu computers. On the other hand, a higher number of buffer + pairs also implies a larger memory consumption, more active worker + threads, and a higher cpu utilization for any compression. This + property enables the application to limit its memory consumption and + CPU utilization behavior depending on requirements. + + + + For each compression "task" that occurs in parallel, there are 2 + buffers allocated: one for input and one for output. This property + sets a limit for the number of pairs. The total amount of storage + space allocated for buffering will then be (N*S*2), where N is the + number of buffer pairs, S is the size of each buffer (). By default, DotNetZip allocates 4 buffer + pairs per CPU core, so if your machine has 4 cores, and you retain + the default buffer size of 128k, then the + ParallelDeflateOutputStream will use 4 * 4 * 2 * 128kb of buffer + memory in total, or 4mb, in blocks of 128kb. If you then set this + property to 8, then the number will be 8 * 2 * 128kb of buffer + memory, or 2mb. + + + + CPU utilization will also go up with additional buffers, because a + larger number of buffer pairs allows a larger number of background + threads to compress in parallel. If you find that parallel + compression is consuming too much memory or CPU, you can adjust this + value downward. + + + + The default value is 16. Different values may deliver better or + worse results, depending on your priorities and the dynamic + performance characteristics of your storage and compute resources. + + + + This property is not the number of buffer pairs to use; it is an + upper limit. An illustration: Suppose you have an application that + uses the default value of this property (which is 16), and it runs + on a machine with 2 CPU cores. In that case, DotNetZip will allocate + 4 buffer pairs per CPU core, for a total of 8 pairs. The upper + limit specified by this property has no effect. + + + + The application can set this value at any time + before calling ZipFile.Save(). + + + + + + + + + Returns the version number on the DotNetZip assembly. + + + + + This property is exposed as a convenience. Callers could also get the + version value by retrieving GetName().Version on the + System.Reflection.Assembly object pointing to the DotNetZip + assembly. But sometimes it is not clear which assembly is being loaded. + This property makes it clear. + + + This static property is primarily useful for diagnostic purposes. + + + + + + This is an integer indexer into the Zip archive. + + + + + This property is read-only. + + + + Internally, the ZipEntry instances that belong to the + ZipFile are stored in a Dictionary. When you use this + indexer the first time, it creates a read-only + List<ZipEntry> from the Dictionary.Values Collection. + If at any time you modify the set of entries in the ZipFile, + either by adding an entry, removing an entry, or renaming an + entry, a new List will be created, and the numeric indexes for the + remaining entries may be different. + + + + This means you cannot rename any ZipEntry from + inside an enumeration of the zip file. + + + + The index value. + + + + + + The ZipEntry within the Zip archive at the specified index. If the + entry does not exist in the archive, this indexer throws. + + + + + + This is a name-based indexer into the Zip archive. + + + + + This property is read-only. + + + + The property on the ZipFile + determines whether retrieval via this indexer is done via case-sensitive + comparisons. By default, retrieval is not case sensitive. This makes + sense on Windows, in which filesystems are not case sensitive. + + + + Regardless of case-sensitivity, it is not always the case that + this[value].FileName == value. In other words, the FileName + property of the ZipEntry retrieved with this indexer, may or may + not be equal to the index value. + + + + This is because DotNetZip performs a normalization of filenames passed to + this indexer, before attempting to retrieve the item. That normalization + includes: removal of a volume letter and colon, swapping backward slashes + for forward slashes. So, zip["dir1\\entry1.txt"].FileName == + "dir1/entry.txt". + + + + Directory entries in the zip file may be retrieved via this indexer only + with names that have a trailing slash. DotNetZip automatically appends a + trailing slash to the names of any directory entries added to a zip. + + + + + + This example extracts only the entries in a zip file that are .txt files. + + using (ZipFile zip = ZipFile.Read("PackedDocuments.zip")) + { + foreach (string s1 in zip.EntryFilenames) + { + if (s1.EndsWith(".txt")) + zip[s1].Extract("textfiles"); + } + } + + + Using zip As ZipFile = ZipFile.Read("PackedDocuments.zip") + Dim s1 As String + For Each s1 In zip.EntryFilenames + If s1.EndsWith(".txt") Then + zip(s1).Extract("textfiles") + End If + Next + End Using + + + + + + Thrown if the caller attempts to assign a non-null value to the indexer. + + + + The name of the file, including any directory path, to retrieve from the + zip. The filename match is not case-sensitive by default; you can use the + property to change this behavior. The + pathname can use forward-slashes or backward slashes. + + + + The ZipEntry within the Zip archive, given by the specified + filename. If the named entry does not exist in the archive, this indexer + returns null (Nothing in VB). + + + + + + The list of filenames for the entries contained within the zip archive. + + + + According to the ZIP specification, the names of the entries use forward + slashes in pathnames. If you are scanning through the list, you may have + to swap forward slashes for backslashes. + + + + + + This example shows one way to test if a filename is already contained + within a zip archive. + + String zipFileToRead= "PackedDocuments.zip"; + string candidate = "DatedMaterial.xps"; + using (ZipFile zip = new ZipFile(zipFileToRead)) + { + if (zip.EntryFilenames.Contains(candidate)) + Console.WriteLine("The file '{0}' exists in the zip archive '{1}'", + candidate, + zipFileName); + else + Console.WriteLine("The file, '{0}', does not exist in the zip archive '{1}'", + candidate, + zipFileName); + Console.WriteLine(); + } + + + Dim zipFileToRead As String = "PackedDocuments.zip" + Dim candidate As String = "DatedMaterial.xps" + Using zip As ZipFile.Read(ZipFileToRead) + If zip.EntryFilenames.Contains(candidate) Then + Console.WriteLine("The file '{0}' exists in the zip archive '{1}'", _ + candidate, _ + zipFileName) + Else + Console.WriteLine("The file, '{0}', does not exist in the zip archive '{1}'", _ + candidate, _ + zipFileName) + End If + Console.WriteLine + End Using + + + + + The list of strings for the filenames contained within the Zip archive. + + + + + + Returns the readonly collection of entries in the Zip archive. + + + + + + If there are no entries in the current ZipFile, the value returned is a + non-null zero-element collection. If there are entries in the zip file, + the elements are returned in no particular order. + + + This is the implied enumerator on the ZipFile class. If you use a + ZipFile instance in a context that expects an enumerator, you will + get this collection. + + + + + + + Returns a readonly collection of entries in the Zip archive, sorted by FileName. + + + + If there are no entries in the current ZipFile, the value returned + is a non-null zero-element collection. If there are entries in the zip + file, the elements are returned sorted by the name of the entry. + + + + + This example fills a Windows Forms ListView with the entries in a zip file. + + + using (ZipFile zip = ZipFile.Read(zipFile)) + { + foreach (ZipEntry entry in zip.EntriesSorted) + { + ListViewItem item = new ListViewItem(n.ToString()); + n++; + string[] subitems = new string[] { + entry.FileName.Replace("/","\\"), + entry.LastModified.ToString("yyyy-MM-dd HH:mm:ss"), + entry.UncompressedSize.ToString(), + String.Format("{0,5:F0}%", entry.CompressionRatio), + entry.CompressedSize.ToString(), + (entry.UsesEncryption) ? "Y" : "N", + String.Format("{0:X8}", entry.Crc)}; + + foreach (String s in subitems) + { + ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem(); + subitem.Text = s; + item.SubItems.Add(subitem); + } + + this.listView1.Items.Add(item); + } + } + + + + + + + + Returns the number of entries in the Zip archive. + + + + + An event handler invoked when a Save() starts, before and after each + entry has been written to the archive, when a Save() completes, and + during other Save events. + + + + + Depending on the particular event, different properties on the parameter are set. The following + table summarizes the available EventTypes and the conditions under + which this event handler is invoked with a + SaveProgressEventArgs with the given EventType. + + + + + value of EntryType + Meaning and conditions + + + + ZipProgressEventType.Saving_Started + Fired when ZipFile.Save() begins. + + + + + ZipProgressEventType.Saving_BeforeSaveEntry + + Fired within ZipFile.Save(), just before writing data for each + particular entry. + + + + + ZipProgressEventType.Saving_AfterSaveEntry + + Fired within ZipFile.Save(), just after having finished writing data + for each particular entry. + + + + + ZipProgressEventType.Saving_Completed + Fired when ZipFile.Save() has completed. + + + + + ZipProgressEventType.Saving_AfterSaveTempArchive + + Fired after the temporary file has been created. This happens only + when saving to a disk file. This event will not be invoked when + saving to a stream. + + + + + ZipProgressEventType.Saving_BeforeRenameTempArchive + + Fired just before renaming the temporary file to the permanent + location. This happens only when saving to a disk file. This event + will not be invoked when saving to a stream. + + + + + ZipProgressEventType.Saving_AfterRenameTempArchive + + Fired just after renaming the temporary file to the permanent + location. This happens only when saving to a disk file. This event + will not be invoked when saving to a stream. + + + + + ZipProgressEventType.Saving_AfterCompileSelfExtractor + + Fired after a self-extracting archive has finished compiling. This + EventType is used only within SaveSelfExtractor(). + + + + + ZipProgressEventType.Saving_BytesRead + + Set during the save of a particular entry, to update progress of the + Save(). When this EventType is set, the BytesTransferred is the + number of bytes that have been read from the source stream. The + TotalBytesToTransfer is the number of bytes in the uncompressed + file. + + + + + + + + + This example uses an anonymous method to handle the + SaveProgress event, by updating a progress bar. + + + progressBar1.Value = 0; + progressBar1.Max = listbox1.Items.Count; + using (ZipFile zip = new ZipFile()) + { + // listbox1 contains a list of filenames + zip.AddFiles(listbox1.Items); + + // do the progress bar: + zip.SaveProgress += (sender, e) => { + if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry) { + progressBar1.PerformStep(); + } + }; + + zip.Save(fs); + } + + + + + This example uses a named method as the + SaveProgress event handler, to update the user, in a + console-based application. + + + static bool justHadByteUpdate= false; + public static void SaveProgress(object sender, SaveProgressEventArgs e) + { + if (e.EventType == ZipProgressEventType.Saving_Started) + Console.WriteLine("Saving: {0}", e.ArchiveName); + + else if (e.EventType == ZipProgressEventType.Saving_Completed) + { + justHadByteUpdate= false; + Console.WriteLine(); + Console.WriteLine("Done: {0}", e.ArchiveName); + } + + else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry) + { + if (justHadByteUpdate) + Console.WriteLine(); + Console.WriteLine(" Writing: {0} ({1}/{2})", + e.CurrentEntry.FileName, e.EntriesSaved, e.EntriesTotal); + justHadByteUpdate= false; + } + + else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead) + { + if (justHadByteUpdate) + Console.SetCursorPosition(0, Console.CursorTop); + Console.Write(" {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer, + e.BytesTransferred / (0.01 * e.TotalBytesToTransfer )); + justHadByteUpdate= true; + } + } + + public static ZipUp(string targetZip, string directory) + { + using (var zip = new ZipFile()) { + zip.SaveProgress += SaveProgress; + zip.AddDirectory(directory); + zip.Save(targetZip); + } + } + + + + + Public Sub ZipUp(ByVal targetZip As String, ByVal directory As String) + Using zip As ZipFile = New ZipFile + AddHandler zip.SaveProgress, AddressOf MySaveProgress + zip.AddDirectory(directory) + zip.Save(targetZip) + End Using + End Sub + + Private Shared justHadByteUpdate As Boolean = False + + Public Shared Sub MySaveProgress(ByVal sender As Object, ByVal e As SaveProgressEventArgs) + If (e.EventType Is ZipProgressEventType.Saving_Started) Then + Console.WriteLine("Saving: {0}", e.ArchiveName) + + ElseIf (e.EventType Is ZipProgressEventType.Saving_Completed) Then + justHadByteUpdate = False + Console.WriteLine + Console.WriteLine("Done: {0}", e.ArchiveName) + + ElseIf (e.EventType Is ZipProgressEventType.Saving_BeforeWriteEntry) Then + If justHadByteUpdate Then + Console.WriteLine + End If + Console.WriteLine(" Writing: {0} ({1}/{2})", e.CurrentEntry.FileName, e.EntriesSaved, e.EntriesTotal) + justHadByteUpdate = False + + ElseIf (e.EventType Is ZipProgressEventType.Saving_EntryBytesRead) Then + If justHadByteUpdate Then + Console.SetCursorPosition(0, Console.CursorTop) + End If + Console.Write(" {0}/{1} ({2:N0}%)", e.BytesTransferred, _ + e.TotalBytesToTransfer, _ + (CDbl(e.BytesTransferred) / (0.01 * e.TotalBytesToTransfer))) + justHadByteUpdate = True + End If + End Sub + + + + + + This is a more complete example of using the SaveProgress + events in a Windows Forms application, with a + Thread object. + + + delegate void SaveEntryProgress(SaveProgressEventArgs e); + delegate void ButtonClick(object sender, EventArgs e); + + public class WorkerOptions + { + public string ZipName; + public string Folder; + public string Encoding; + public string Comment; + public int ZipFlavor; + public Zip64Option Zip64; + } + + private int _progress2MaxFactor; + private bool _saveCanceled; + private long _totalBytesBeforeCompress; + private long _totalBytesAfterCompress; + private Thread _workerThread; + + + private void btnZipup_Click(object sender, EventArgs e) + { + KickoffZipup(); + } + + private void btnCancel_Click(object sender, EventArgs e) + { + if (this.lblStatus.InvokeRequired) + { + this.lblStatus.Invoke(new ButtonClick(this.btnCancel_Click), new object[] { sender, e }); + } + else + { + _saveCanceled = true; + lblStatus.Text = "Canceled..."; + ResetState(); + } + } + + private void KickoffZipup() + { + _folderName = tbDirName.Text; + + if (_folderName == null || _folderName == "") return; + if (this.tbZipName.Text == null || this.tbZipName.Text == "") return; + + // check for existence of the zip file: + if (System.IO.File.Exists(this.tbZipName.Text)) + { + var dlgResult = MessageBox.Show(String.Format("The file you have specified ({0}) already exists." + + " Do you want to overwrite this file?", this.tbZipName.Text), + "Confirmation is Required", MessageBoxButtons.YesNo, MessageBoxIcon.Question); + if (dlgResult != DialogResult.Yes) return; + System.IO.File.Delete(this.tbZipName.Text); + } + + _saveCanceled = false; + _nFilesCompleted = 0; + _totalBytesAfterCompress = 0; + _totalBytesBeforeCompress = 0; + this.btnOk.Enabled = false; + this.btnOk.Text = "Zipping..."; + this.btnCancel.Enabled = true; + lblStatus.Text = "Zipping..."; + + var options = new WorkerOptions + { + ZipName = this.tbZipName.Text, + Folder = _folderName, + Encoding = "ibm437" + }; + + if (this.comboBox1.SelectedIndex != 0) + { + options.Encoding = this.comboBox1.SelectedItem.ToString(); + } + + if (this.radioFlavorSfxCmd.Checked) + options.ZipFlavor = 2; + else if (this.radioFlavorSfxGui.Checked) + options.ZipFlavor = 1; + else options.ZipFlavor = 0; + + if (this.radioZip64AsNecessary.Checked) + options.Zip64 = Zip64Option.AsNecessary; + else if (this.radioZip64Always.Checked) + options.Zip64 = Zip64Option.Always; + else options.Zip64 = Zip64Option.Never; + + options.Comment = String.Format("Encoding:{0} || Flavor:{1} || ZIP64:{2}\r\nCreated at {3} || {4}\r\n", + options.Encoding, + FlavorToString(options.ZipFlavor), + options.Zip64.ToString(), + System.DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"), + this.Text); + + if (this.tbComment.Text != TB_COMMENT_NOTE) + options.Comment += this.tbComment.Text; + + _workerThread = new Thread(this.DoSave); + _workerThread.Name = "Zip Saver thread"; + _workerThread.Start(options); + this.Cursor = Cursors.WaitCursor; + } + + + private void DoSave(Object p) + { + WorkerOptions options = p as WorkerOptions; + try + { + using (var zip1 = new ZipFile()) + { + zip1.ProvisionalAlternateEncoding = System.Text.Encoding.GetEncoding(options.Encoding); + zip1.Comment = options.Comment; + zip1.AddDirectory(options.Folder); + _entriesToZip = zip1.EntryFileNames.Count; + SetProgressBars(); + zip1.SaveProgress += this.zip1_SaveProgress; + + zip1.UseZip64WhenSaving = options.Zip64; + + if (options.ZipFlavor == 1) + zip1.SaveSelfExtractor(options.ZipName, SelfExtractorFlavor.WinFormsApplication); + else if (options.ZipFlavor == 2) + zip1.SaveSelfExtractor(options.ZipName, SelfExtractorFlavor.ConsoleApplication); + else + zip1.Save(options.ZipName); + } + } + catch (System.Exception exc1) + { + MessageBox.Show(String.Format("Exception while zipping: {0}", exc1.Message)); + btnCancel_Click(null, null); + } + } + + + + void zip1_SaveProgress(object sender, SaveProgressEventArgs e) + { + switch (e.EventType) + { + case ZipProgressEventType.Saving_AfterWriteEntry: + StepArchiveProgress(e); + break; + case ZipProgressEventType.Saving_EntryBytesRead: + StepEntryProgress(e); + break; + case ZipProgressEventType.Saving_Completed: + SaveCompleted(); + break; + case ZipProgressEventType.Saving_AfterSaveTempArchive: + // this event only occurs when saving an SFX file + TempArchiveSaved(); + break; + } + if (_saveCanceled) + e.Cancel = true; + } + + + + private void StepArchiveProgress(SaveProgressEventArgs e) + { + if (this.progressBar1.InvokeRequired) + { + this.progressBar1.Invoke(new SaveEntryProgress(this.StepArchiveProgress), new object[] { e }); + } + else + { + if (!_saveCanceled) + { + _nFilesCompleted++; + this.progressBar1.PerformStep(); + _totalBytesAfterCompress += e.CurrentEntry.CompressedSize; + _totalBytesBeforeCompress += e.CurrentEntry.UncompressedSize; + + // reset the progress bar for the entry: + this.progressBar2.Value = this.progressBar2.Maximum = 1; + + this.Update(); + } + } + } + + + private void StepEntryProgress(SaveProgressEventArgs e) + { + if (this.progressBar2.InvokeRequired) + { + this.progressBar2.Invoke(new SaveEntryProgress(this.StepEntryProgress), new object[] { e }); + } + else + { + if (!_saveCanceled) + { + if (this.progressBar2.Maximum == 1) + { + // reset + Int64 max = e.TotalBytesToTransfer; + _progress2MaxFactor = 0; + while (max > System.Int32.MaxValue) + { + max /= 2; + _progress2MaxFactor++; + } + this.progressBar2.Maximum = (int)max; + lblStatus.Text = String.Format("{0} of {1} files...({2})", + _nFilesCompleted + 1, _entriesToZip, e.CurrentEntry.FileName); + } + + int xferred = e.BytesTransferred >> _progress2MaxFactor; + + this.progressBar2.Value = (xferred >= this.progressBar2.Maximum) + ? this.progressBar2.Maximum + : xferred; + + this.Update(); + } + } + } + + private void SaveCompleted() + { + if (this.lblStatus.InvokeRequired) + { + this.lblStatus.Invoke(new MethodInvoker(this.SaveCompleted)); + } + else + { + lblStatus.Text = String.Format("Done, Compressed {0} files, {1:N0}% of original.", + _nFilesCompleted, (100.00 * _totalBytesAfterCompress) / _totalBytesBeforeCompress); + ResetState(); + } + } + + private void ResetState() + { + this.btnCancel.Enabled = false; + this.btnOk.Enabled = true; + this.btnOk.Text = "Zip it!"; + this.progressBar1.Value = 0; + this.progressBar2.Value = 0; + this.Cursor = Cursors.Default; + if (!_workerThread.IsAlive) + _workerThread.Join(); + } + + + + + + + + + + + An event handler invoked before, during, and after the reading of a zip archive. + + + + + Depending on the particular event being signaled, different properties on the + parameter are set. The following table + summarizes the available EventTypes and the conditions under which this + event handler is invoked with a ReadProgressEventArgs with the given EventType. + + + + + value of EntryType + Meaning and conditions + + + + ZipProgressEventType.Reading_Started + Fired just as ZipFile.Read() begins. Meaningful properties: ArchiveName. + + + + + ZipProgressEventType.Reading_Completed + Fired when ZipFile.Read() has completed. Meaningful properties: ArchiveName. + + + + + ZipProgressEventType.Reading_ArchiveBytesRead + Fired while reading, updates the number of bytes read for the entire archive. + Meaningful properties: ArchiveName, CurrentEntry, BytesTransferred, TotalBytesToTransfer. + + + + + ZipProgressEventType.Reading_BeforeReadEntry + Indicates an entry is about to be read from the archive. + Meaningful properties: ArchiveName, EntriesTotal. + + + + + ZipProgressEventType.Reading_AfterReadEntry + Indicates an entry has just been read from the archive. + Meaningful properties: ArchiveName, EntriesTotal, CurrentEntry. + + + + + + + + + + + + + An event handler invoked before, during, and after extraction of + entries in the zip archive. + + + + + Depending on the particular event, different properties on the parameter are set. The following + table summarizes the available EventTypes and the conditions under + which this event handler is invoked with a + ExtractProgressEventArgs with the given EventType. + + + + + value of EntryType + Meaning and conditions + + + + ZipProgressEventType.Extracting_BeforeExtractAll + + Set when ExtractAll() begins. The ArchiveName, Overwrite, and + ExtractLocation properties are meaningful. + + + + ZipProgressEventType.Extracting_AfterExtractAll + + Set when ExtractAll() has completed. The ArchiveName, Overwrite, + and ExtractLocation properties are meaningful. + + + + + ZipProgressEventType.Extracting_BeforeExtractEntry + + Set when an Extract() on an entry in the ZipFile has begun. + Properties that are meaningful: ArchiveName, EntriesTotal, + CurrentEntry, Overwrite, ExtractLocation, EntriesExtracted. + + + + + ZipProgressEventType.Extracting_AfterExtractEntry + + Set when an Extract() on an entry in the ZipFile has completed. + Properties that are meaningful: ArchiveName, EntriesTotal, + CurrentEntry, Overwrite, ExtractLocation, EntriesExtracted. + + + + + ZipProgressEventType.Extracting_EntryBytesWritten + + Set within a call to Extract() on an entry in the ZipFile, as data + is extracted for the entry. Properties that are meaningful: + ArchiveName, CurrentEntry, BytesTransferred, TotalBytesToTransfer. + + + + + ZipProgressEventType.Extracting_ExtractEntryWouldOverwrite + + Set within a call to Extract() on an entry in the ZipFile, when the + extraction would overwrite an existing file. This event type is used + only when ExtractExistingFileAction on the ZipFile or + ZipEntry is set to InvokeExtractProgressEvent. + + + + + + + + + + private static bool justHadByteUpdate = false; + public static void ExtractProgress(object sender, ExtractProgressEventArgs e) + { + if(e.EventType == ZipProgressEventType.Extracting_EntryBytesWritten) + { + if (justHadByteUpdate) + Console.SetCursorPosition(0, Console.CursorTop); + + Console.Write(" {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer, + e.BytesTransferred / (0.01 * e.TotalBytesToTransfer )); + justHadByteUpdate = true; + } + else if(e.EventType == ZipProgressEventType.Extracting_BeforeExtractEntry) + { + if (justHadByteUpdate) + Console.WriteLine(); + Console.WriteLine("Extracting: {0}", e.CurrentEntry.FileName); + justHadByteUpdate= false; + } + } + + public static ExtractZip(string zipToExtract, string directory) + { + string TargetDirectory= "extract"; + using (var zip = ZipFile.Read(zipToExtract)) { + zip.ExtractProgress += ExtractProgress; + foreach (var e in zip1) + { + e.Extract(TargetDirectory, true); + } + } + } + + + + Public Shared Sub Main(ByVal args As String()) + Dim ZipToUnpack As String = "C1P3SML.zip" + Dim TargetDir As String = "ExtractTest_Extract" + Console.WriteLine("Extracting file {0} to {1}", ZipToUnpack, TargetDir) + Using zip1 As ZipFile = ZipFile.Read(ZipToUnpack) + AddHandler zip1.ExtractProgress, AddressOf MyExtractProgress + Dim e As ZipEntry + For Each e In zip1 + e.Extract(TargetDir, True) + Next + End Using + End Sub + + Private Shared justHadByteUpdate As Boolean = False + + Public Shared Sub MyExtractProgress(ByVal sender As Object, ByVal e As ExtractProgressEventArgs) + If (e.EventType = ZipProgressEventType.Extracting_EntryBytesWritten) Then + If ExtractTest.justHadByteUpdate Then + Console.SetCursorPosition(0, Console.CursorTop) + End If + Console.Write(" {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer, (CDbl(e.BytesTransferred) / (0.01 * e.TotalBytesToTransfer))) + ExtractTest.justHadByteUpdate = True + ElseIf (e.EventType = ZipProgressEventType.Extracting_BeforeExtractEntry) Then + If ExtractTest.justHadByteUpdate Then + Console.WriteLine + End If + Console.WriteLine("Extracting: {0}", e.CurrentEntry.FileName) + ExtractTest.justHadByteUpdate = False + End If + End Sub + + + + + + + + + + An event handler invoked before, during, and after Adding entries to a zip archive. + + + + Adding a large number of entries to a zip file can take a long + time. For example, when calling on a + directory that contains 50,000 files, it could take 3 minutes or so. + This event handler allws an application to track the progress of the Add + operation, and to optionally cancel a lengthy Add operation. + + + + + + int _numEntriesToAdd= 0; + int _numEntriesAdded= 0; + void AddProgressHandler(object sender, AddProgressEventArgs e) + { + switch (e.EventType) + { + case ZipProgressEventType.Adding_Started: + Console.WriteLine("Adding files to the zip..."); + break; + case ZipProgressEventType.Adding_AfterAddEntry: + _numEntriesAdded++; + Console.WriteLine(String.Format("Adding file {0}/{1} :: {2}", + _numEntriesAdded, _numEntriesToAdd, e.CurrentEntry.FileName)); + break; + case ZipProgressEventType.Adding_Completed: + Console.WriteLine("Added all files"); + break; + } + } + + void CreateTheZip() + { + using (ZipFile zip = new ZipFile()) + { + zip.AddProgress += AddProgressHandler; + zip.AddDirectory(System.IO.Path.GetFileName(DirToZip)); + zip.Save(ZipFileToCreate); + } + } + + + + + + Private Sub AddProgressHandler(ByVal sender As Object, ByVal e As AddProgressEventArgs) + Select Case e.EventType + Case ZipProgressEventType.Adding_Started + Console.WriteLine("Adding files to the zip...") + Exit Select + Case ZipProgressEventType.Adding_AfterAddEntry + Console.WriteLine(String.Format("Adding file {0}", e.CurrentEntry.FileName)) + Exit Select + Case ZipProgressEventType.Adding_Completed + Console.WriteLine("Added all files") + Exit Select + End Select + End Sub + + Sub CreateTheZip() + Using zip as ZipFile = New ZipFile + AddHandler zip.AddProgress, AddressOf AddProgressHandler + zip.AddDirectory(System.IO.Path.GetFileName(DirToZip)) + zip.Save(ZipFileToCreate); + End Using + End Sub + + + + + + + + + + + + An event that is raised when an error occurs during open or read of files + while saving a zip archive. + + + + + Errors can occur as a file is being saved to the zip archive. For + example, the File.Open may fail, or a File.Read may fail, because of + lock conflicts or other reasons. If you add a handler to this event, + you can handle such errors in your own code. If you don't add a + handler, the library will throw an exception if it encounters an I/O + error during a call to Save(). + + + + Setting a handler implicitly sets to + ZipErrorAction.InvokeErrorEvent. + + + + The handler you add applies to all items that are + subsequently added to the ZipFile instance. If you set this + property after you have added items to the ZipFile, but before you + have called Save(), errors that occur while saving those items + will not cause the error handler to be invoked. + + + + If you want to handle any errors that occur with any entry in the zip + file using the same error handler, then add your error handler once, + before adding any entries to the zip archive. + + + + In the error handler method, you need to set the property on the + ZipErrorEventArgs.CurrentEntry. This communicates back to + DotNetZip what you would like to do with this particular error. Within + an error handler, if you set the ZipEntry.ZipErrorAction property + on the ZipEntry to ZipErrorAction.InvokeErrorEvent or if + you don't set it at all, the library will throw the exception. (It is the + same as if you had set the ZipEntry.ZipErrorAction property on the + ZipEntry to ZipErrorAction.Throw.) If you set the + ZipErrorEventArgs.Cancel to true, the entire Save() will be + canceled. + + + + In the case that you use ZipErrorAction.Skip, implying that + you want to skip the entry for which there's been an error, DotNetZip + tries to seek backwards in the output stream, and truncate all bytes + written on behalf of that particular entry. This works only if the + output stream is seekable. It will not work, for example, when using + ASPNET's Response.OutputStream. + + + + + + + This example shows how to use an event handler to handle + errors during save of the zip file. + + + public static void MyZipError(object sender, ZipErrorEventArgs e) + { + Console.WriteLine("Error saving {0}...", e.FileName); + Console.WriteLine(" Exception: {0}", e.exception); + ZipEntry entry = e.CurrentEntry; + string response = null; + // Ask the user whether he wants to skip this error or not + do + { + Console.Write("Retry, Skip, Throw, or Cancel ? (R/S/T/C) "); + response = Console.ReadLine(); + Console.WriteLine(); + + } while (response != null && + response[0]!='S' && response[0]!='s' && + response[0]!='R' && response[0]!='r' && + response[0]!='T' && response[0]!='t' && + response[0]!='C' && response[0]!='c'); + + e.Cancel = (response[0]=='C' || response[0]=='c'); + + if (response[0]=='S' || response[0]=='s') + entry.ZipErrorAction = ZipErrorAction.Skip; + else if (response[0]=='R' || response[0]=='r') + entry.ZipErrorAction = ZipErrorAction.Retry; + else if (response[0]=='T' || response[0]=='t') + entry.ZipErrorAction = ZipErrorAction.Throw; + } + + public void SaveTheFile() + { + string directoryToZip = "fodder"; + string directoryInArchive = "files"; + string zipFileToCreate = "Archive.zip"; + using (var zip = new ZipFile()) + { + // set the event handler before adding any entries + zip.ZipError += MyZipError; + zip.AddDirectory(directoryToZip, directoryInArchive); + zip.Save(zipFileToCreate); + } + } + + + + Private Sub MyZipError(ByVal sender As Object, ByVal e As Ionic.Zip.ZipErrorEventArgs) + ' At this point, the application could prompt the user for an action to take. + ' But in this case, this application will simply automatically skip the file, in case of error. + Console.WriteLine("Zip Error, entry {0}", e.CurrentEntry.FileName) + Console.WriteLine(" Exception: {0}", e.exception) + ' set the desired ZipErrorAction on the CurrentEntry to communicate that to DotNetZip + e.CurrentEntry.ZipErrorAction = Zip.ZipErrorAction.Skip + End Sub + + Public Sub SaveTheFile() + Dim directoryToZip As String = "fodder" + Dim directoryInArchive As String = "files" + Dim zipFileToCreate as String = "Archive.zip" + Using zipArchive As ZipFile = New ZipFile + ' set the event handler before adding any entries + AddHandler zipArchive.ZipError, AddressOf MyZipError + zipArchive.AddDirectory(directoryToZip, directoryInArchive) + zipArchive.Save(zipFileToCreate) + End Using + End Sub + + + + + + + + + Options for using ZIP64 extensions when saving zip archives. + + + + + + Designed many years ago, the original zip + specification from PKWARE allowed for 32-bit quantities for the + compressed and uncompressed sizes of zip entries, as well as a 32-bit quantity + for specifying the length of the zip archive itself, and a maximum of 65535 + entries. These limits are now regularly exceeded in many backup and archival + scenarios. Recently, PKWare added extensions to the original zip spec, called + "ZIP64 extensions", to raise those limitations. This property governs whether + DotNetZip will use those extensions when writing zip archives. The use of + these extensions is optional and explicit in DotNetZip because, despite the + status of ZIP64 as a bona fide standard, many other zip tools and libraries do + not support ZIP64, and therefore a zip file with ZIP64 extensions may be + unreadable by some of those other tools. + + + + Set this property to to always use ZIP64 + extensions when saving, regardless of whether your zip archive needs it. + Suppose you add 5 files, each under 100k, to a ZipFile. If you specify Always + for this flag, you will get a ZIP64 archive, though the archive does not need + to use ZIP64 because none of the original zip limits had been exceeded. + + + + Set this property to to tell the DotNetZip + library to never use ZIP64 extensions. This is useful for maximum + compatibility and interoperability, at the expense of the capability of + handling large files or large archives. NB: Windows Explorer in Windows XP + and Windows Vista cannot currently extract files from a zip64 archive, so if + you want to guarantee that a zip archive produced by this library will work in + Windows Explorer, use Never. If you set this property to , and your application creates a zip that would + exceed one of the Zip limits, the library will throw an exception while saving + the zip file. + + + + Set this property to to tell the + DotNetZip library to use the ZIP64 extensions when required by the + entry. After the file is compressed, the original and compressed sizes are + checked, and if they exceed the limits described above, then zip64 can be + used. That is the general idea, but there is an additional wrinkle when saving + to a non-seekable device, like the ASP.NET Response.OutputStream, or + Console.Out. When using non-seekable streams for output, the entry + header - which indicates whether zip64 is in use - is emitted before it is + known if zip64 is necessary. It is only after all entries have been saved + that it can be known if ZIP64 will be required. On seekable output streams, + after saving all entries, the library can seek backward and re-emit the zip + file header to be consistent with the actual ZIP64 requirement. But using a + non-seekable output stream, the library cannot seek backward, so the header + can never be changed. In other words, the archive's use of ZIP64 extensions is + not alterable after the header is emitted. Therefore, when saving to + non-seekable streams, using is the same + as using : it will always produce a zip + archive that uses ZIP64 extensions. + + + + + + + The default behavior, which is "Never". + (For COM clients, this is a 0 (zero).) + + + + + Do not use ZIP64 extensions when writing zip archives. + (For COM clients, this is a 0 (zero).) + + + + + Use ZIP64 extensions when writing zip archives, as necessary. + For example, when a single entry exceeds 0xFFFFFFFF in size, or when the archive as a whole + exceeds 0xFFFFFFFF in size, or when there are more than 65535 entries in an archive. + (For COM clients, this is a 1.) + + + + + Always use ZIP64 extensions when writing zip archives, even when unnecessary. + (For COM clients, this is a 2.) + + + + + An enum representing the values on a three-way toggle switch + for various options in the library. This might be used to + specify whether to employ a particular text encoding, or to use + ZIP64 extensions, or some other option. + + + + + The default behavior. This is the same as "Never". + (For COM clients, this is a 0 (zero).) + + + + + Never use the associated option. + (For COM clients, this is a 0 (zero).) + + + + + Use the associated behavior "as necessary." + (For COM clients, this is a 1.) + + + + + Use the associated behavior Always, whether necessary or not. + (For COM clients, this is a 2.) + + + + + A class for collecting the various options that can be used when + Reading zip files for extraction or update. + + + + + When reading a zip file, there are several options an + application can set, to modify how the file is read, or what + the library does while reading. This class collects those + options into one container. + + + + Pass an instance of the ReadOptions class into the + ZipFile.Read() method. + + + . + . + + + + + An event handler for Read operations. When opening large zip + archives, you may want to display a progress bar or other + indicator of status progress while reading. This parameter + allows you to specify a ReadProgress Event Handler directly. + When you call Read(), the progress event is invoked as + necessary. + + + + + The System.IO.TextWriter to use for writing verbose status messages + during operations on the zip archive. A console application may wish to + pass System.Console.Out to get messages on the Console. A graphical + or headless application may wish to capture the messages in a different + TextWriter, such as a System.IO.StringWriter. + + + + + The System.Text.Encoding to use when reading in the zip archive. Be + careful specifying the encoding. If the value you use here is not the same + as the Encoding used when the zip archive was created (possibly by a + different archiver) you will get unexpected results and possibly exceptions. + + + + + + + + An enum that provides the different self-extractor flavors + + + + + A self-extracting zip archive that runs from the console or + command line. + + + + + A self-extracting zip archive that presents a graphical user + interface when it is executed. + + + + + The options for generating a self-extracting archive. + + + + + The type of SFX to create. + + + + + The command to run after extraction. + + + + + This is optional. Leave it empty (null in C# or Nothing in + VB) to run no command after extraction. + + + + If it is non-empty, the SFX will execute the command specified in this + string on the user's machine, and using the extract directory as the + working directory for the process, after unpacking the archive. The + program to execute can include a path, if you like. If you want to execute + a program that accepts arguments, specify the program name, followed by a + space, and then the arguments for the program, each separated by a space, + just as you would on a normal command line. Example: program.exe arg1 + arg2. The string prior to the first space will be taken as the + program name, and the string following the first space specifies the + arguments to the program. + + + + If you want to execute a program that has a space in the name or path of + the file, surround the program name in double-quotes. The first character + of the command line should be a double-quote character, and there must be + a matching double-quote following the end of the program file name. Any + optional arguments to the program follow that, separated by + spaces. Example: "c:\project files\program name.exe" arg1 arg2. + + + + If the flavor of the SFX is SelfExtractorFlavor.ConsoleApplication, + then the SFX starts a new process, using this string as the post-extract + command line. The SFX waits for the process to exit. The exit code of + the post-extract command line is returned as the exit code of the + command-line self-extractor exe. A non-zero exit code is typically used to + indicated a failure by the program. In the case of an SFX, a non-zero exit + code may indicate a failure during extraction, OR, it may indicate a + failure of the run-after-extract program if specified, OR, it may indicate + the run-after-extract program could not be fuond. There is no way to + distinguish these conditions from the calling shell, aside from parsing + the output of the SFX. If you have Quiet set to true, you may not + see error messages, if a problem occurs. + + + + If the flavor of the SFX is + SelfExtractorFlavor.WinFormsApplication, then the SFX starts a new + process, using this string as the post-extract command line, and using the + extract directory as the working directory for the process. The SFX does + not wait for the command to complete, and does not check the exit code of + the program. If the run-after-extract program cannot be fuond, a message + box is displayed indicating that fact. + + + + You can specify environment variables within this string, with a format like + %NAME%. The value of these variables will be expanded at the time + the SFX is run. Example: %WINDIR%\system32\xcopy.exe may expand at + runtime to c:\Windows\System32\xcopy.exe. + + + + By combining this with the RemoveUnpackedFilesAfterExecute + flag, you can create an SFX that extracts itself, runs a file that + was extracted, then deletes all the files that were extracted. If + you want it to run "invisibly" then set Flavor to + SelfExtractorFlavor.ConsoleApplication, and set Quiet + to true. The user running such an EXE will see a console window + appear, then disappear quickly. You may also want to specify the + default extract location, with DefaultExtractDirectory. + + + + If you set Flavor to + SelfExtractorFlavor.WinFormsApplication, and set Quiet to + true, then a GUI with progressbars is displayed, but it is + "non-interactive" - it accepts no input from the user. Instead the SFX + just automatically unpacks and exits. + + + + + + + The default extract directory the user will see when + running the self-extracting archive. + + + + + Passing null (or Nothing in VB) here will cause the Self Extractor to use + the the user's personal directory () for the default extract + location. + + + + This is only a default location. The actual extract location will be + settable on the command line when the SFX is executed. + + + + You can specify environment variables within this string, + with %NAME%. The value of these variables will be + expanded at the time the SFX is run. Example: + %USERPROFILE%\Documents\unpack may expand at runtime to + c:\users\melvin\Documents\unpack. + + + + + + The name of an .ico file in the filesystem to use for the application icon + for the generated SFX. + + + + + Normally, DotNetZip will embed an "zipped folder" icon into the generated + SFX. If you prefer to use a different icon, you can specify it here. It + should be a .ico file. This file is passed as the /win32icon + option to the csc.exe compiler when constructing the SFX file. + + + + + + + Whether the ConsoleApplication SFX will be quiet during extraction. + + + + + This option affects the way the generated SFX runs. By default it is + false. When you set it to true,... + + + + + Flavor + Behavior + + + + ConsoleApplication + no messages will be emitted during successful + operation. Double-clicking the SFX in Windows + Explorer or as an attachment in an email will cause a console + window to appear briefly, before it disappears. If you run the + ConsoleApplication SFX from the cmd.exe prompt, it runs as a + normal console app; by default, because it is quiet, it displays + no messages to the console. If you pass the -v+ command line + argument to the Console SFX when you run it, you will get verbose + messages to the console. + + + + + WinFormsApplication + the SFX extracts automatically when the application + is launched, with no additional user input. + + + + + + + When you set it to false,... + + + + + Flavor + Behavior + + + + ConsoleApplication + the extractor will emit a + message to the console for each entry extracted. + + When double-clicking to launch the SFX, the console window will + remain, and the SFX will emit a message for each file as it + extracts. The messages fly by quickly, they won't be easily + readable, unless the extracted files are fairly large. + + + + + + WinFormsApplication + the SFX presents a forms UI and allows the user to select + options before extracting. + + + + + + + + + + Specify what the self-extractor will do when extracting an entry + would overwrite an existing file. + + + + The default behavvior is to Throw. + + + + + + Whether to remove the files that have been unpacked, after executing the + PostExtractCommandLine. + + + + + If true, and if there is a + PostExtractCommandLine, and if the command runs successfully, + then the files that the SFX unpacked will be removed, afterwards. If + the command does not complete successfully (non-zero return code), + that is interpreted as a failure, and the extracted files will not be + removed. + + + + Setting this flag, and setting Flavor to + SelfExtractorFlavor.ConsoleApplication, and setting Quiet to + true, results in an SFX that extracts itself, runs a file that was + extracted, then deletes all the files that were extracted, with no + intervention by the user. You may also want to specify the default + extract location, with DefaultExtractDirectory. + + + + + + + The file version number to embed into the generated EXE. It will show up, for + example, during a mouseover in Windows Explorer. + + + + + + The product version to embed into the generated EXE. It will show up, for + example, during a mouseover in Windows Explorer. + + + + You can use any arbitrary string, but a human-readable version number is + recommended. For example "v1.2 alpha" or "v4.2 RC2". If you specify nothing, + then there is no product version embedded into the EXE. + + + + + + The copyright notice, if any, to embed into the generated EXE. + + + + It will show up, for example, while viewing properties of the file in + Windows Explorer. You can use any arbitrary string, but typically you + want something like "Copyright © Dino Chiesa 2011". + + + + + + The description to embed into the generated EXE. + + + + Use any arbitrary string. This text will be displayed during a + mouseover in Windows Explorer. If you specify nothing, then the string + "DotNetZip SFX Archive" is embedded into the EXE as the description. + + + + + + The product name to embed into the generated EXE. + + + + Use any arbitrary string. This text will be displayed + while viewing properties of the EXE file in + Windows Explorer. + + + + + + The title to display in the Window of a GUI SFX, while it extracts. + + + + + By default the title show in the GUI window of a self-extractor + is "DotNetZip Self-extractor (http://DotNetZip.codeplex.com/)". + You can change that by setting this property before saving the SFX. + + + + This property has an effect only when producing a Self-extractor + of flavor SelfExtractorFlavor.WinFormsApplication. + + + + + + + Additional options for the csc.exe compiler, when producing the SFX + EXE. + + + + + + Reset the BitWriter. + + + + This is useful when the BitWriter writes into a MemoryStream, and + is used by a BZip2Compressor, which itself is re-used for multiple + distinct data blocks. + + + + + + Write some number of bits from the given value, into the output. + + + + The nbits value should be a max of 25, for safety. For performance + reasons, this method does not check! + + + + + + Write a full 8-bit byte into the output. + + + + + Write four 8-bit bytes into the output. + + + + + Write all available byte-aligned bytes. + + + + This method writes no new output, but flushes any accumulated + bits. At completion, the accumulator may contain up to 7 + bits. + + + This is necessary when re-assembling output from N independent + compressors, one for each of N blocks. The output of any + particular compressor will in general have some fragment of a byte + remaining. This fragment needs to be accumulated into the + parent BZip2OutputStream. + + + + + + Writes all available bytes, and emits padding for the final byte as + necessary. This must be the last method invoked on an instance of + BitWriter. + + + + + Delivers the remaining bits, left-aligned, in a byte. + + + + This is valid only if NumRemainingBits is less than 8; + in other words it is valid only after a call to Flush(). + + + + + Knuth's increments seem to work better than Incerpi-Sedgewick here. + Possibly because the number of elems to sort is usually small, typically + <= 20. + + + + BZip2Compressor writes its compressed data out via a BitWriter. This + is necessary because BZip2 does byte shredding. + + + + + Accept new bytes into the compressor data buffer + + + + This method does the first-level (cheap) run-length encoding, and + stores the encoded data into the rle block. + + + + + + Process one input byte into the block. + + + + + To "process" the byte means to do the run-length encoding. + There are 3 possible return values: + + 0 - the byte was not written, in other words, not + encoded into the block. This happens when the + byte b would require the start of a new run, and + the block has no more room for new runs. + + 1 - the byte was written, and the block is not full. + + 2 - the byte was written, and the block is full. + + + + 0 if the byte was not written, non-zero if written. + + + + Append one run to the output block. + + + + + This compressor does run-length-encoding before BWT and etc. This + method simply appends a run to the output block. The append always + succeeds. The return value indicates whether the block is full: + false (not full) implies that at least one additional run could be + processed. + + + true if the block is now full; otherwise false. + + + + Compress the data that has been placed (Run-length-encoded) into the + block. The compressed data goes into the CompressedBytes array. + + + + Side effects: 1. fills the CompressedBytes array. 2. sets the + AvailableBytesOut property. + + + + + This is the most hammered method of this class. + +

+ This is the version using unrolled loops. +

+
+ + Method "mainQSort3", file "blocksort.c", BZip2 1.0.2 + + + + The number of uncompressed bytes being held in the buffer. + + + + I am thinking this may be useful in a Stream that uses this + compressor class. In the Close() method on the stream it could + check this value to see if anything has been written at all. You + may think the stream could easily track the number of bytes it + wrote, which would eliminate the need for this. But, there is the + case where the stream writes a complete block, and it is full, and + then writes no more. In that case the stream may want to check. + + + + + Array instance identical to sfmap, both are used only + temporarily and independently, so we do not need to allocate + additional memory. + + + + A read-only decorator stream that performs BZip2 decompression on Read. + + + + + Create a BZip2InputStream, wrapping it around the given input Stream. + + + + The input stream will be closed when the BZip2InputStream is closed. + + + The stream from which to read compressed data + + + + Create a BZip2InputStream with the given stream, and + specifying whether to leave the wrapped stream open when + the BZip2InputStream is closed. + + The stream from which to read compressed data + + Whether to leave the input stream open, when the BZip2InputStream closes. + + + + + This example reads a bzip2-compressed file, decompresses it, + and writes the decompressed data into a newly created file. + + + var fname = "logfile.log.bz2"; + using (var fs = File.OpenRead(fname)) + { + using (var decompressor = new Ionic.BZip2.BZip2InputStream(fs)) + { + var outFname = fname + ".decompressed"; + using (var output = File.Create(outFname)) + { + byte[] buffer = new byte[2048]; + int n; + while ((n = decompressor.Read(buffer, 0, buffer.Length)) > 0) + { + output.Write(buffer, 0, n); + } + } + } + } + + + + + + Read data from the stream. + + + + + To decompress a BZip2 data stream, create a BZip2InputStream, + providing a stream that reads compressed data. Then call Read() on + that BZip2InputStream, and the data read will be decompressed + as you read. + + + + A BZip2InputStream can be used only for Read(), not for Write(). + + + + The buffer into which the read data should be placed. + the offset within that data array to put the first byte read. + the number of bytes to read. + the number of bytes actually read + + + + Read a single byte from the stream. + + the byte read from the stream, or -1 if EOF + + + + Flush the stream. + + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + this is irrelevant, since it will always throw! + irrelevant! + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + + + + Calling this method always throws a . + + this parameter is never used + this parameter is never used + this parameter is never used + + + + Dispose the stream. + + + indicates whether the Dispose method was invoked by user code. + + + + + Close the stream. + + + + + Read n bits from input, right justifying the result. + + + + For example, if you read 1 bit, the result is either 0 + or 1. + + + + The number of bits to read, always between 1 and 32. + + + + Called by createHuffmanDecodingTables() exclusively. + + + Called by recvDecodingTables() exclusively. + + + + Indicates whether the stream can be read. + + + The return value depends on whether the captive stream supports reading. + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream can be written. + + + The return value depends on whether the captive stream supports writing. + + + + + Reading this property always throws a . + + + + + The position of the stream pointer. + + + + Setting this property always throws a . Reading will return the + total number of uncompressed bytes read in. + + + + + Compressor State + + + + Freq table collected to save a pass over the data during + decompression. + + + Initializes the tt array. + + This method is called when the required length of the array is known. + I don't initialize it at construction time to avoid unneccessary + memory allocation when compressing small files. + + + + A write-only decorator stream that compresses data as it is + written using the BZip2 algorithm. + + + + + Constructs a new BZip2OutputStream, that sends its + compressed output to the given output stream. + + + + The destination stream, to which compressed output will be sent. + + + + + This example reads a file, then compresses it with bzip2 file, + and writes the compressed data into a newly created file. + + + var fname = "logfile.log"; + using (var fs = File.OpenRead(fname)) + { + var outFname = fname + ".bz2"; + using (var output = File.Create(outFname)) + { + using (var compressor = new Ionic.BZip2.BZip2OutputStream(output)) + { + byte[] buffer = new byte[2048]; + int n; + while ((n = fs.Read(buffer, 0, buffer.Length)) > 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + + + + Constructs a new BZip2OutputStream with specified blocksize. + + the destination stream. + + The blockSize in units of 100000 bytes. + The valid range is 1..9. + + + + + Constructs a new BZip2OutputStream. + + the destination stream. + + whether to leave the captive stream open upon closing this stream. + + + + + Constructs a new BZip2OutputStream with specified blocksize, + and explicitly specifies whether to leave the wrapped stream open. + + + the destination stream. + + The blockSize in units of 100000 bytes. + The valid range is 1..9. + + + whether to leave the captive stream open upon closing this stream. + + + + + Close the stream. + + + + This may or may not close the underlying stream. Check the + constructors that accept a bool value. + + + + + + Flush the stream. + + + + + Write data to the stream. + + + + + Use the BZip2OutputStream to compress data while writing: + create a BZip2OutputStream with a writable output stream. + Then call Write() on that BZip2OutputStream, providing + uncompressed data as input. The data sent to the output stream will + be the compressed form of the input data. + + + + A BZip2OutputStream can be used only for Write() not for Read(). + + + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + this is irrelevant, since it will always throw! + irrelevant! + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + + + + Calling this method always throws a . + + this parameter is never used + this parameter is never used + this parameter is never used + never returns anything; always throws + + + + The blocksize parameter specified at construction time. + + + + + Indicates whether the stream can be read. + + + The return value is always false. + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream can be written. + + + The return value should always be true, unless and until the + object is disposed and closed. + + + + + Reading this property always throws a . + + + + + The position of the stream pointer. + + + + Setting this property always throws a . Reading will return the + total number of uncompressed bytes written through. + + + + + A write-only decorator stream that compresses data as it is + written using the BZip2 algorithm. This stream compresses by + block using multiple threads. + + + This class performs BZIP2 compression through writing. For + more information on the BZIP2 algorithm, see + . + + + + This class is similar to , + except that this implementation uses an approach that employs multiple + worker threads to perform the compression. On a multi-cpu or multi-core + computer, the performance of this class can be significantly higher than + the single-threaded BZip2OutputStream, particularly for larger streams. + How large? Anything over 10mb is a good candidate for parallel + compression. + + + + The tradeoff is that this class uses more memory and more CPU than the + vanilla BZip2OutputStream. Also, for small files, the + ParallelBZip2OutputStream can be much slower than the vanilla + BZip2OutputStream, because of the overhead associated to using the + thread pool. + + + + + + + Constructs a new ParallelBZip2OutputStream, that sends its + compressed output to the given output stream. + + + + The destination stream, to which compressed output will be sent. + + + + + This example reads a file, then compresses it with bzip2 file, + and writes the compressed data into a newly created file. + + + var fname = "logfile.log"; + using (var fs = File.OpenRead(fname)) + { + var outFname = fname + ".bz2"; + using (var output = File.Create(outFname)) + { + using (var compressor = new Ionic.BZip2.ParallelBZip2OutputStream(output)) + { + byte[] buffer = new byte[2048]; + int n; + while ((n = fs.Read(buffer, 0, buffer.Length)) > 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + + + + Constructs a new ParallelBZip2OutputStream with specified blocksize. + + the destination stream. + + The blockSize in units of 100000 bytes. + The valid range is 1..9. + + + + + Constructs a new ParallelBZip2OutputStream. + + the destination stream. + + whether to leave the captive stream open upon closing this stream. + + + + + Constructs a new ParallelBZip2OutputStream with specified blocksize, + and explicitly specifies whether to leave the wrapped stream open. + + + the destination stream. + + The blockSize in units of 100000 bytes. + The valid range is 1..9. + + + whether to leave the captive stream open upon closing this stream. + + + + + Close the stream. + + + + This may or may not close the underlying stream. Check the + constructors that accept a bool value. + + + + + + Flush the stream. + + + + + Write data to the stream. + + + + + Use the ParallelBZip2OutputStream to compress data while + writing: create a ParallelBZip2OutputStream with a writable + output stream. Then call Write() on that + ParallelBZip2OutputStream, providing uncompressed data as + input. The data sent to the output stream will be the compressed + form of the input data. + + + + A ParallelBZip2OutputStream can be used only for + Write() not for Read(). + + + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + this is irrelevant, since it will always throw! + irrelevant! + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + + + + Calling this method always throws a . + + this parameter is never used + this parameter is never used + this parameter is never used + never returns anything; always throws + + + + The maximum number of concurrent compression worker threads to use. + + + + + This property sets an upper limit on the number of concurrent worker + threads to employ for compression. The implementation of this stream + employs multiple threads from the .NET thread pool, via + ThreadPool.QueueUserWorkItem(), to compress the incoming data by + block. As each block of data is compressed, this stream re-orders the + compressed blocks and writes them to the output stream. + + + + A higher number of workers enables a higher degree of + parallelism, which tends to increase the speed of compression on + multi-cpu computers. On the other hand, a higher number of buffer + pairs also implies a larger memory consumption, more active worker + threads, and a higher cpu utilization for any compression. This + property enables the application to limit its memory consumption and + CPU utilization behavior depending on requirements. + + + + By default, DotNetZip allocates 4 workers per CPU core, subject to the + upper limit specified in this property. For example, suppose the + application sets this property to 16. Then, on a machine with 2 + cores, DotNetZip will use 8 workers; that number does not exceed the + upper limit specified by this property, so the actual number of + workers used will be 4 * 2 = 8. On a machine with 4 cores, DotNetZip + will use 16 workers; again, the limit does not apply. On a machine + with 8 cores, DotNetZip will use 16 workers, because of the limit. + + + + For each compression "worker thread" that occurs in parallel, there is + up to 2mb of memory allocated, for buffering and processing. The + actual number depends on the property. + + + + CPU utilization will also go up with additional workers, because a + larger number of buffer pairs allows a larger number of background + threads to compress in parallel. If you find that parallel + compression is consuming too much memory or CPU, you can adjust this + value downward. + + + + The default value is 16. Different values may deliver better or + worse results, depending on your priorities and the dynamic + performance characteristics of your storage and compute resources. + + + + The application can set this value at any time, but it is effective + only before the first call to Write(), which is when the buffers are + allocated. + + + + + + The blocksize parameter specified at construction time. + + + + + Indicates whether the stream can be read. + + + The return value is always false. + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream can be written. + + + The return value depends on whether the captive stream supports writing. + + + + + Reading this property always throws a . + + + + + The position of the stream pointer. + + + + Setting this property always throws a . Reading will return the + total number of uncompressed bytes written through. + + + + + The total number of bytes written out by the stream. + + + This value is meaningful only after a call to Close(). + + + + + Returns the "random" number at a specific index. + + the index + the random number + + + + A class for compressing and decompressing streams using the Deflate algorithm. + + + + + + The DeflateStream is a Decorator on a . It adds DEFLATE compression or decompression to any + stream. + + + + Using this stream, applications can compress or decompress data via stream + Read and Write operations. Either compresssion or decompression + can occur through either reading or writing. The compression format used is + DEFLATE, which is documented in IETF RFC 1951, "DEFLATE + Compressed Data Format Specification version 1.3.". + + + + This class is similar to , except that + ZlibStream adds the RFC + 1950 - ZLIB framing bytes to a compressed stream when compressing, or + expects the RFC1950 framing bytes when decompressing. The DeflateStream + does not. + + + + + + + + + + Create a DeflateStream using the specified CompressionMode. + + + + When mode is CompressionMode.Compress, the DeflateStream will use + the default compression level. The "captive" stream will be closed when + the DeflateStream is closed. + + + + This example uses a DeflateStream to compress data from a file, and writes + the compressed data to another file. + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(fileToCompress + ".deflated")) + { + using (Stream compressor = new DeflateStream(raw, CompressionMode.Compress)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(fileToCompress & ".deflated") + Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + The stream which will be read or written. + Indicates whether the DeflateStream will compress or decompress. + + + + Create a DeflateStream using the specified CompressionMode and the specified CompressionLevel. + + + + + + When mode is CompressionMode.Decompress, the level parameter is + ignored. The "captive" stream will be closed when the DeflateStream is + closed. + + + + + + + This example uses a DeflateStream to compress data from a file, and writes + the compressed data to another file. + + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(fileToCompress + ".deflated")) + { + using (Stream compressor = new DeflateStream(raw, + CompressionMode.Compress, + CompressionLevel.BestCompression)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n= -1; + while (n != 0) + { + if (n > 0) + compressor.Write(buffer, 0, n); + n= input.Read(buffer, 0, buffer.Length); + } + } + } + } + + + + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(fileToCompress & ".deflated") + Using compressor As Stream = New DeflateStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + The stream to be read or written while deflating or inflating. + Indicates whether the DeflateStream will compress or decompress. + A tuning knob to trade speed for effectiveness. + + + + Create a DeflateStream using the specified + CompressionMode, and explicitly specify whether the + stream should be left open after Deflation or Inflation. + + + + + + This constructor allows the application to request that the captive stream + remain open after the deflation or inflation occurs. By default, after + Close() is called on the stream, the captive stream is also + closed. In some cases this is not desired, for example if the stream is a + memory stream that will be re-read after compression. Specify true for + the parameter to leave the stream open. + + + + The DeflateStream will use the default compression level. + + + + See the other overloads of this constructor for example code. + + + + + The stream which will be read or written. This is called the + "captive" stream in other places in this documentation. + + + + Indicates whether the DeflateStream will compress or decompress. + + + true if the application would like the stream to + remain open after inflation/deflation. + + + + Create a DeflateStream using the specified CompressionMode + and the specified CompressionLevel, and explicitly specify whether + the stream should be left open after Deflation or Inflation. + + + + + + When mode is CompressionMode.Decompress, the level parameter is ignored. + + + + This constructor allows the application to request that the captive stream + remain open after the deflation or inflation occurs. By default, after + Close() is called on the stream, the captive stream is also + closed. In some cases this is not desired, for example if the stream is a + that will be re-read after + compression. Specify true for the parameter + to leave the stream open. + + + + + + + This example shows how to use a DeflateStream to compress data from + a file, and store the compressed data into another file. + + + using (var output = System.IO.File.Create(fileToCompress + ".deflated")) + { + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (Stream compressor = new DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n= -1; + while (n != 0) + { + if (n > 0) + compressor.Write(buffer, 0, n); + n= input.Read(buffer, 0, buffer.Length); + } + } + } + // can write additional data to the output stream here + } + + + + Using output As FileStream = File.Create(fileToCompress & ".deflated") + Using input As Stream = File.OpenRead(fileToCompress) + Using compressor As Stream = New DeflateStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + ' can write additional data to the output stream here. + End Using + + + The stream which will be read or written. + Indicates whether the DeflateStream will compress or decompress. + true if the application would like the stream to remain open after inflation/deflation. + A tuning knob to trade speed for effectiveness. + + + + Dispose the stream. + + + + This may or may not result in a Close() call on the captive + stream. See the constructors that have a leaveOpen parameter + for more information. + + + Application code won't call this code directly. This method may be + invoked in two distinct scenarios. If disposing == true, the method + has been called directly or indirectly by a user's code, for example + via the public Dispose() method. In this case, both managed and + unmanaged resources can be referenced and disposed. If disposing == + false, the method has been called by the runtime from inside the + object finalizer and this method should not reference other objects; + in that case only unmanaged resources must be referenced or + disposed. + + + + true if the Dispose method was invoked by user code. + + + + + Flush the stream. + + + + + Read data from the stream. + + + + + If you wish to use the DeflateStream to compress data while + reading, you can create a DeflateStream with + CompressionMode.Compress, providing an uncompressed data stream. + Then call Read() on that DeflateStream, and the data read will be + compressed as you read. If you wish to use the DeflateStream to + decompress data while reading, you can create a DeflateStream with + CompressionMode.Decompress, providing a readable compressed data + stream. Then call Read() on that DeflateStream, and the data read + will be decompressed as you read. + + + + A DeflateStream can be used for Read() or Write(), but not both. + + + + The buffer into which the read data should be placed. + the offset within that data array to put the first byte read. + the number of bytes to read. + the number of bytes actually read + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + this is irrelevant, since it will always throw! + irrelevant! + + + + Calling this method always throws a . + + this is irrelevant, since it will always throw! + + + + Write data to the stream. + + + + + If you wish to use the DeflateStream to compress data while + writing, you can create a DeflateStream with + CompressionMode.Compress, and a writable output stream. Then call + Write() on that DeflateStream, providing uncompressed data + as input. The data sent to the output stream will be the compressed form + of the data written. If you wish to use the DeflateStream to + decompress data while writing, you can create a DeflateStream with + CompressionMode.Decompress, and a writable output stream. Then + call Write() on that stream, providing previously compressed + data. The data sent to the output stream will be the decompressed form of + the data written. + + + + A DeflateStream can be used for Read() or Write(), + but not both. + + + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Compress a string into a byte array using DEFLATE (RFC 1951). + + + + Uncompress it with . + + + DeflateStream.UncompressString(byte[]) + DeflateStream.CompressBuffer(byte[]) + GZipStream.CompressString(string) + ZlibStream.CompressString(string) + + + A string to compress. The string will first be encoded + using UTF8, then compressed. + + + The string in compressed form + + + + Compress a byte array into a new byte array using DEFLATE. + + + + Uncompress it with . + + + DeflateStream.CompressString(string) + DeflateStream.UncompressBuffer(byte[]) + GZipStream.CompressBuffer(byte[]) + ZlibStream.CompressBuffer(byte[]) + + + A buffer to compress. + + + The data in compressed form + + + + Uncompress a DEFLATE'd byte array into a single string. + + + DeflateStream.CompressString(String) + DeflateStream.UncompressBuffer(byte[]) + GZipStream.UncompressString(byte[]) + ZlibStream.UncompressString(byte[]) + + + A buffer containing DEFLATE-compressed data. + + + The uncompressed string + + + + Uncompress a DEFLATE'd byte array into a byte array. + + + DeflateStream.CompressBuffer(byte[]) + DeflateStream.UncompressString(byte[]) + GZipStream.UncompressBuffer(byte[]) + ZlibStream.UncompressBuffer(byte[]) + + + A buffer containing data that has been compressed with DEFLATE. + + + The data in uncompressed form + + + + This property sets the flush behavior on the stream. + + See the ZLIB documentation for the meaning of the flush behavior. + + + + + The size of the working buffer for the compression codec. + + + + + The working buffer is used for all stream operations. The default size is + 1024 bytes. The minimum size is 128 bytes. You may get better performance + with a larger buffer. Then again, you might not. You would have to test + it. + + + + Set this before the first call to Read() or Write() on the + stream. If you try to set it afterwards, it will throw. + + + + + + The ZLIB strategy to be used during compression. + + + + By tweaking this parameter, you may be able to optimize the compression for + data with particular characteristics. + + + + Returns the total number of bytes input so far. + + + Returns the total number of bytes output so far. + + + + Indicates whether the stream can be read. + + + The return value depends on whether the captive stream supports reading. + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream can be written. + + + The return value depends on whether the captive stream supports writing. + + + + + Reading this property always throws a . + + + + + The position of the stream pointer. + + + + Setting this property always throws a . Reading will return the total bytes + written out, if used in writing, or the total bytes read in, if used in + reading. The count may refer to compressed bytes or uncompressed bytes, + depending on how you've used the stream. + + + + + A class for compressing and decompressing GZIP streams. + + + + + The GZipStream is a Decorator on a + . It adds GZIP compression or decompression to any + stream. + + + + Like the System.IO.Compression.GZipStream in the .NET Base Class Library, the + Ionic.Zlib.GZipStream can compress while writing, or decompress while + reading, but not vice versa. The compression method used is GZIP, which is + documented in IETF RFC + 1952, "GZIP file format specification version 4.3". + + + A GZipStream can be used to decompress data (through Read()) or + to compress data (through Write()), but not both. + + + + If you wish to use the GZipStream to compress data, you must wrap it + around a write-able stream. As you call Write() on the GZipStream, the + data will be compressed into the GZIP format. If you want to decompress data, + you must wrap the GZipStream around a readable stream that contains an + IETF RFC 1952-compliant stream. The data will be decompressed as you call + Read() on the GZipStream. + + + + Though the GZIP format allows data from multiple files to be concatenated + together, this stream handles only a single segment of GZIP format, typically + representing a single file. + + + + This class is similar to and . + ZlibStream handles RFC1950-compliant streams. + handles RFC1951-compliant streams. This class handles RFC1952-compliant streams. + + + + + + + + + + The last modified time for the GZIP stream. + + + + GZIP allows the storage of a last modified time with each GZIP entry. + When compressing data, you can set this before the first call to + Write(). When decompressing, you can retrieve this value any time + after the first call to Read(). + + + + + Create a GZipStream using the specified CompressionMode. + + + + + When mode is CompressionMode.Compress, the GZipStream will use the + default compression level. + + + + As noted in the class documentation, the CompressionMode (Compress + or Decompress) also establishes the "direction" of the stream. A + GZipStream with CompressionMode.Compress works only through + Write(). A GZipStream with + CompressionMode.Decompress works only through Read(). + + + + + + This example shows how to use a GZipStream to compress data. + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(outputFile)) + { + using (Stream compressor = new GZipStream(raw, CompressionMode.Compress)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + Dim outputFile As String = (fileToCompress & ".compressed") + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(outputFile) + Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + + + This example shows how to use a GZipStream to uncompress a file. + + private void GunZipFile(string filename) + { + if (!filename.EndsWith(".gz)) + throw new ArgumentException("filename"); + var DecompressedFile = filename.Substring(0,filename.Length-3); + byte[] working = new byte[WORKING_BUFFER_SIZE]; + int n= 1; + using (System.IO.Stream input = System.IO.File.OpenRead(filename)) + { + using (Stream decompressor= new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, true)) + { + using (var output = System.IO.File.Create(DecompressedFile)) + { + while (n !=0) + { + n= decompressor.Read(working, 0, working.Length); + if (n > 0) + { + output.Write(working, 0, n); + } + } + } + } + } + } + + + + Private Sub GunZipFile(ByVal filename as String) + If Not (filename.EndsWith(".gz)) Then + Throw New ArgumentException("filename") + End If + Dim DecompressedFile as String = filename.Substring(0,filename.Length-3) + Dim working(WORKING_BUFFER_SIZE) as Byte + Dim n As Integer = 1 + Using input As Stream = File.OpenRead(filename) + Using decompressor As Stream = new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, True) + Using output As Stream = File.Create(UncompressedFile) + Do + n= decompressor.Read(working, 0, working.Length) + If n > 0 Then + output.Write(working, 0, n) + End IF + Loop While (n > 0) + End Using + End Using + End Using + End Sub + + + + The stream which will be read or written. + Indicates whether the GZipStream will compress or decompress. + + + + Create a GZipStream using the specified CompressionMode and + the specified CompressionLevel. + + + + + The CompressionMode (Compress or Decompress) also establishes the + "direction" of the stream. A GZipStream with + CompressionMode.Compress works only through Write(). A + GZipStream with CompressionMode.Decompress works only + through Read(). + + + + + + + This example shows how to use a GZipStream to compress a file into a .gz file. + + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(fileToCompress + ".gz")) + { + using (Stream compressor = new GZipStream(raw, + CompressionMode.Compress, + CompressionLevel.BestCompression)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(fileToCompress & ".gz") + Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + The stream to be read or written while deflating or inflating. + Indicates whether the GZipStream will compress or decompress. + A tuning knob to trade speed for effectiveness. + + + + Create a GZipStream using the specified CompressionMode, and + explicitly specify whether the stream should be left open after Deflation + or Inflation. + + + + + This constructor allows the application to request that the captive stream + remain open after the deflation or inflation occurs. By default, after + Close() is called on the stream, the captive stream is also + closed. In some cases this is not desired, for example if the stream is a + memory stream that will be re-read after compressed data has been written + to it. Specify true for the parameter to leave + the stream open. + + + + The (Compress or Decompress) also + establishes the "direction" of the stream. A GZipStream with + CompressionMode.Compress works only through Write(). A GZipStream + with CompressionMode.Decompress works only through Read(). + + + + The GZipStream will use the default compression level. If you want + to specify the compression level, see . + + + + See the other overloads of this constructor for example code. + + + + + + The stream which will be read or written. This is called the "captive" + stream in other places in this documentation. + + + Indicates whether the GZipStream will compress or decompress. + + + + true if the application would like the base stream to remain open after + inflation/deflation. + + + + + Create a GZipStream using the specified CompressionMode and the + specified CompressionLevel, and explicitly specify whether the + stream should be left open after Deflation or Inflation. + + + + + + This constructor allows the application to request that the captive stream + remain open after the deflation or inflation occurs. By default, after + Close() is called on the stream, the captive stream is also + closed. In some cases this is not desired, for example if the stream is a + memory stream that will be re-read after compressed data has been written + to it. Specify true for the parameter to + leave the stream open. + + + + As noted in the class documentation, the CompressionMode (Compress + or Decompress) also establishes the "direction" of the stream. A + GZipStream with CompressionMode.Compress works only through + Write(). A GZipStream with CompressionMode.Decompress works only + through Read(). + + + + + + This example shows how to use a GZipStream to compress data. + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(outputFile)) + { + using (Stream compressor = new GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, true)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + Dim outputFile As String = (fileToCompress & ".compressed") + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(outputFile) + Using compressor As Stream = New GZipStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression, True) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + The stream which will be read or written. + Indicates whether the GZipStream will compress or decompress. + true if the application would like the stream to remain open after inflation/deflation. + A tuning knob to trade speed for effectiveness. + + + + Dispose the stream. + + + + This may or may not result in a Close() call on the captive + stream. See the constructors that have a leaveOpen parameter + for more information. + + + This method may be invoked in two distinct scenarios. If disposing + == true, the method has been called directly or indirectly by a + user's code, for example via the public Dispose() method. In this + case, both managed and unmanaged resources can be referenced and + disposed. If disposing == false, the method has been called by the + runtime from inside the object finalizer and this method should not + reference other objects; in that case only unmanaged resources must + be referenced or disposed. + + + + indicates whether the Dispose method was invoked by user code. + + + + + Flush the stream. + + + + + Read and decompress data from the source stream. + + + + With a GZipStream, decompression is done through reading. + + + + + byte[] working = new byte[WORKING_BUFFER_SIZE]; + using (System.IO.Stream input = System.IO.File.OpenRead(_CompressedFile)) + { + using (Stream decompressor= new Ionic.Zlib.GZipStream(input, CompressionMode.Decompress, true)) + { + using (var output = System.IO.File.Create(_DecompressedFile)) + { + int n; + while ((n= decompressor.Read(working, 0, working.Length)) !=0) + { + output.Write(working, 0, n); + } + } + } + } + + + The buffer into which the decompressed data should be placed. + the offset within that data array to put the first byte read. + the number of bytes to read. + the number of bytes actually read + + + + Calling this method always throws a . + + irrelevant; it will always throw! + irrelevant; it will always throw! + irrelevant! + + + + Calling this method always throws a . + + irrelevant; this method will always throw! + + + + Write data to the stream. + + + + + If you wish to use the GZipStream to compress data while writing, + you can create a GZipStream with CompressionMode.Compress, and a + writable output stream. Then call Write() on that GZipStream, + providing uncompressed data as input. The data sent to the output stream + will be the compressed form of the data written. + + + + A GZipStream can be used for Read() or Write(), but not + both. Writing implies compression. Reading implies decompression. + + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Compress a string into a byte array using GZip. + + + + Uncompress it with . + + + + + + + A string to compress. The string will first be encoded + using UTF8, then compressed. + + + The string in compressed form + + + + Compress a byte array into a new byte array using GZip. + + + + Uncompress it with . + + + + + + + A buffer to compress. + + + The data in compressed form + + + + Uncompress a GZip'ed byte array into a single string. + + + + + + + A buffer containing GZIP-compressed data. + + + The uncompressed string + + + + Uncompress a GZip'ed byte array into a byte array. + + + + + + + A buffer containing data that has been compressed with GZip. + + + The data in uncompressed form + + + + The comment on the GZIP stream. + + + + + The GZIP format allows for each file to optionally have an associated + comment stored with the file. The comment is encoded with the ISO-8859-1 + code page. To include a comment in a GZIP stream you create, set this + property before calling Write() for the first time on the + GZipStream. + + + + When using GZipStream to decompress, you can retrieve this property + after the first call to Read(). If no comment has been set in the + GZIP bytestream, the Comment property will return null + (Nothing in VB). + + + + + + The FileName for the GZIP stream. + + + + + + The GZIP format optionally allows each file to have an associated + filename. When compressing data (through Write()), set this + FileName before calling Write() the first time on the GZipStream. + The actual filename is encoded into the GZIP bytestream with the + ISO-8859-1 code page, according to RFC 1952. It is the application's + responsibility to insure that the FileName can be encoded and decoded + correctly with this code page. + + + + When decompressing (through Read()), you can retrieve this value + any time after the first Read(). In the case where there was no filename + encoded into the GZIP bytestream, the property will return null (Nothing + in VB). + + + + + + The CRC on the GZIP stream. + + + This is used for internal error checking. You probably don't need to look at this property. + + + + + This property sets the flush behavior on the stream. + + + + + The size of the working buffer for the compression codec. + + + + + The working buffer is used for all stream operations. The default size is + 1024 bytes. The minimum size is 128 bytes. You may get better performance + with a larger buffer. Then again, you might not. You would have to test + it. + + + + Set this before the first call to Read() or Write() on the + stream. If you try to set it afterwards, it will throw. + + + + + Returns the total number of bytes input so far. + + + Returns the total number of bytes output so far. + + + + Indicates whether the stream can be read. + + + The return value depends on whether the captive stream supports reading. + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream can be written. + + + The return value depends on whether the captive stream supports writing. + + + + + Reading this property always throws a . + + + + + The position of the stream pointer. + + + + Setting this property always throws a . Reading will return the total bytes + written out, if used in writing, or the total bytes read in, if used in + reading. The count may refer to compressed bytes or uncompressed bytes, + depending on how you've used the stream. + + + + + A class for compressing streams using the + Deflate algorithm with multiple threads. + + + + + This class performs DEFLATE compression through writing. For + more information on the Deflate algorithm, see IETF RFC 1951, + "DEFLATE Compressed Data Format Specification version 1.3." + + + + This class is similar to , except + that this class is for compression only, and this implementation uses an + approach that employs multiple worker threads to perform the DEFLATE. On + a multi-cpu or multi-core computer, the performance of this class can be + significantly higher than the single-threaded DeflateStream, particularly + for larger streams. How large? Anything over 10mb is a good candidate + for parallel compression. + + + + The tradeoff is that this class uses more memory and more CPU than the + vanilla DeflateStream, and also is less efficient as a compressor. For + large files the size of the compressed data stream can be less than 1% + larger than the size of a compressed data stream from the vanialla + DeflateStream. For smaller files the difference can be larger. The + difference will also be larger if you set the BufferSize to be lower than + the default value. Your mileage may vary. Finally, for small files, the + ParallelDeflateOutputStream can be much slower than the vanilla + DeflateStream, because of the overhead associated to using the thread + pool. + + + + + + + + Create a ParallelDeflateOutputStream. + + + + + This stream compresses data written into it via the DEFLATE + algorithm (see RFC 1951), and writes out the compressed byte stream. + + + + The instance will use the default compression level, the default + buffer sizes and the default number of threads and buffers per + thread. + + + + This class is similar to , + except that this implementation uses an approach that employs + multiple worker threads to perform the DEFLATE. On a multi-cpu or + multi-core computer, the performance of this class can be + significantly higher than the single-threaded DeflateStream, + particularly for larger streams. How large? Anything over 10mb is + a good candidate for parallel compression. + + + + + + + This example shows how to use a ParallelDeflateOutputStream to compress + data. It reads a file, compresses it, and writes the compressed data to + a second, output file. + + + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n= -1; + String outputFile = fileToCompress + ".compressed"; + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(outputFile)) + { + using (Stream compressor = new ParallelDeflateOutputStream(raw)) + { + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Dim outputFile As String = (fileToCompress & ".compressed") + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(outputFile) + Using compressor As Stream = New ParallelDeflateOutputStream(raw) + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + The stream to which compressed data will be written. + + + + Create a ParallelDeflateOutputStream using the specified CompressionLevel. + + + See the + constructor for example code. + + The stream to which compressed data will be written. + A tuning knob to trade speed for effectiveness. + + + + Create a ParallelDeflateOutputStream and specify whether to leave the captive stream open + when the ParallelDeflateOutputStream is closed. + + + See the + constructor for example code. + + The stream to which compressed data will be written. + + true if the application would like the stream to remain open after inflation/deflation. + + + + + Create a ParallelDeflateOutputStream and specify whether to leave the captive stream open + when the ParallelDeflateOutputStream is closed. + + + See the + constructor for example code. + + The stream to which compressed data will be written. + A tuning knob to trade speed for effectiveness. + + true if the application would like the stream to remain open after inflation/deflation. + + + + + Create a ParallelDeflateOutputStream using the specified + CompressionLevel and CompressionStrategy, and specifying whether to + leave the captive stream open when the ParallelDeflateOutputStream is + closed. + + + See the + constructor for example code. + + The stream to which compressed data will be written. + A tuning knob to trade speed for effectiveness. + + By tweaking this parameter, you may be able to optimize the compression for + data with particular characteristics. + + + true if the application would like the stream to remain open after inflation/deflation. + + + + + Write data to the stream. + + + + + + To use the ParallelDeflateOutputStream to compress data, create a + ParallelDeflateOutputStream with CompressionMode.Compress, passing a + writable output stream. Then call Write() on that + ParallelDeflateOutputStream, providing uncompressed data as input. The + data sent to the output stream will be the compressed form of the data + written. + + + + To decompress data, use the class. + + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Flush the stream. + + + + + Close the stream. + + + You must call Close on the stream to guarantee that all of the data written in has + been compressed, and the compressed data has been written out. + + + + Dispose the object + + + Because ParallelDeflateOutputStream is IDisposable, the + application must call this method when finished using the instance. + + + This method is generally called implicitly upon exit from + a using scope in C# (Using in VB). + + + + + The Dispose method + + indicates whether the Dispose method was invoked by user code. + + + + + Resets the stream for use with another stream. + + + Because the ParallelDeflateOutputStream is expensive to create, it + has been designed so that it can be recycled and re-used. You have + to call Close() on the stream first, then you can call Reset() on + it, to use it again on another stream. + + + + The new output stream for this era. + + + + + ParallelDeflateOutputStream deflater = null; + foreach (var inputFile in listOfFiles) + { + string outputFile = inputFile + ".compressed"; + using (System.IO.Stream input = System.IO.File.OpenRead(inputFile)) + { + using (var outStream = System.IO.File.Create(outputFile)) + { + if (deflater == null) + deflater = new ParallelDeflateOutputStream(outStream, + CompressionLevel.Best, + CompressionStrategy.Default, + true); + deflater.Reset(outStream); + + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + deflater.Write(buffer, 0, n); + } + } + } + } + + + + + + This method always throws a NotSupportedException. + + + The buffer into which data would be read, IF THIS METHOD + ACTUALLY DID ANYTHING. + + + The offset within that data array at which to insert the + data that is read, IF THIS METHOD ACTUALLY DID + ANYTHING. + + + The number of bytes to write, IF THIS METHOD ACTUALLY DID + ANYTHING. + + nothing. + + + + This method always throws a NotSupportedException. + + + The offset to seek to.... + IF THIS METHOD ACTUALLY DID ANYTHING. + + + The reference specifying how to apply the offset.... IF + THIS METHOD ACTUALLY DID ANYTHING. + + nothing. It always throws. + + + + This method always throws a NotSupportedException. + + + The new value for the stream length.... IF + THIS METHOD ACTUALLY DID ANYTHING. + + + + + The ZLIB strategy to be used during compression. + + + + + + The maximum number of buffer pairs to use. + + + + + This property sets an upper limit on the number of memory buffer + pairs to create. The implementation of this stream allocates + multiple buffers to facilitate parallel compression. As each buffer + fills up, this stream uses + ThreadPool.QueueUserWorkItem() + to compress those buffers in a background threadpool thread. After a + buffer is compressed, it is re-ordered and written to the output + stream. + + + + A higher number of buffer pairs enables a higher degree of + parallelism, which tends to increase the speed of compression on + multi-cpu computers. On the other hand, a higher number of buffer + pairs also implies a larger memory consumption, more active worker + threads, and a higher cpu utilization for any compression. This + property enables the application to limit its memory consumption and + CPU utilization behavior depending on requirements. + + + + For each compression "task" that occurs in parallel, there are 2 + buffers allocated: one for input and one for output. This property + sets a limit for the number of pairs. The total amount of storage + space allocated for buffering will then be (N*S*2), where N is the + number of buffer pairs, S is the size of each buffer (). By default, DotNetZip allocates 4 buffer + pairs per CPU core, so if your machine has 4 cores, and you retain + the default buffer size of 128k, then the + ParallelDeflateOutputStream will use 4 * 4 * 2 * 128kb of buffer + memory in total, or 4mb, in blocks of 128kb. If you then set this + property to 8, then the number will be 8 * 2 * 128kb of buffer + memory, or 2mb. + + + + CPU utilization will also go up with additional buffers, because a + larger number of buffer pairs allows a larger number of background + threads to compress in parallel. If you find that parallel + compression is consuming too much memory or CPU, you can adjust this + value downward. + + + + The default value is 16. Different values may deliver better or + worse results, depending on your priorities and the dynamic + performance characteristics of your storage and compute resources. + + + + This property is not the number of buffer pairs to use; it is an + upper limit. An illustration: Suppose you have an application that + uses the default value of this property (which is 16), and it runs + on a machine with 2 CPU cores. In that case, DotNetZip will allocate + 4 buffer pairs per CPU core, for a total of 8 pairs. The upper + limit specified by this property has no effect. + + + + The application can set this value at any time, but it is effective + only before the first call to Write(), which is when the buffers are + allocated. + + + + + + The size of the buffers used by the compressor threads. + + + + + The default buffer size is 128k. The application can set this value + at any time, but it is effective only before the first Write(). + + + + Larger buffer sizes implies larger memory consumption but allows + more efficient compression. Using smaller buffer sizes consumes less + memory but may result in less effective compression. For example, + using the default buffer size of 128k, the compression delivered is + within 1% of the compression delivered by the single-threaded . On the other hand, using a + BufferSize of 8k can result in a compressed data stream that is 5% + larger than that delivered by the single-threaded + DeflateStream. Excessively small buffer sizes can also cause + the speed of the ParallelDeflateOutputStream to drop, because of + larger thread scheduling overhead dealing with many many small + buffers. + + + + The total amount of storage space allocated for buffering will be + (N*S*2), where N is the number of buffer pairs, and S is the size of + each buffer (this property). There are 2 buffers used by the + compressor, one for input and one for output. By default, DotNetZip + allocates 4 buffer pairs per CPU core, so if your machine has 4 + cores, then the number of buffer pairs used will be 16. If you + accept the default value of this property, 128k, then the + ParallelDeflateOutputStream will use 16 * 2 * 128kb of buffer memory + in total, or 4mb, in blocks of 128kb. If you set this property to + 64kb, then the number will be 16 * 2 * 64kb of buffer memory, or + 2mb. + + + + + + + The CRC32 for the data that was written out, prior to compression. + + + This value is meaningful only after a call to Close(). + + + + + The total number of uncompressed bytes processed by the ParallelDeflateOutputStream. + + + This value is meaningful only after a call to Close(). + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream supports Read operations. + + + Always returns false. + + + + + Indicates whether the stream supports Write operations. + + + Returns true if the provided stream is writable. + + + + + Reading this property always throws a NotSupportedException. + + + + + Returns the current position of the output stream. + + + + Because the output gets written by a background thread, + the value may change asynchronously. Setting this + property always throws a NotSupportedException. + + + + + + Map from a distance to a distance code. + + + No side effects. _dist_code[256] and _dist_code[257] are never used. + + + + + Describes how to flush the current deflate operation. + + + The different FlushType values are useful when using a Deflate in a streaming application. + + + + No flush at all. + + + Closes the current block, but doesn't flush it to + the output. Used internally only in hypothetical + scenarios. This was supposed to be removed by Zlib, but it is + still in use in some edge cases. + + + + + Use this during compression to specify that all pending output should be + flushed to the output buffer and the output should be aligned on a byte + boundary. You might use this in a streaming communication scenario, so that + the decompressor can get all input data available so far. When using this + with a ZlibCodec, AvailableBytesIn will be zero after the call if + enough output space has been provided before the call. Flushing will + degrade compression and so it should be used only when necessary. + + + + + Use this during compression to specify that all output should be flushed, as + with FlushType.Sync, but also, the compression state should be reset + so that decompression can restart from this point if previous compressed + data has been damaged or if random access is desired. Using + FlushType.Full too often can significantly degrade the compression. + + + + Signals the end of the compression/decompression stream. + + + + The compression level to be used when using a DeflateStream or ZlibStream with CompressionMode.Compress. + + + + + None means that the data will be simply stored, with no change at all. + If you are producing ZIPs for use on Mac OSX, be aware that archives produced with CompressionLevel.None + cannot be opened with the default zip reader. Use a different CompressionLevel. + + + + + Same as None. + + + + + The fastest but least effective compression. + + + + + A synonym for BestSpeed. + + + + + A little slower, but better, than level 1. + + + + + A little slower, but better, than level 2. + + + + + A little slower, but better, than level 3. + + + + + A little slower than level 4, but with better compression. + + + + + The default compression level, with a good balance of speed and compression efficiency. + + + + + A synonym for Default. + + + + + Pretty good compression! + + + + + Better compression than Level7! + + + + + The "best" compression, where best means greatest reduction in size of the input data stream. + This is also the slowest compression. + + + + + A synonym for BestCompression. + + + + + Describes options for how the compression algorithm is executed. Different strategies + work better on different sorts of data. The strategy parameter can affect the compression + ratio and the speed of compression but not the correctness of the compresssion. + + + + + The default strategy is probably the best for normal data. + + + + + The Filtered strategy is intended to be used most effectively with data produced by a + filter or predictor. By this definition, filtered data consists mostly of small + values with a somewhat random distribution. In this case, the compression algorithm + is tuned to compress them better. The effect of Filtered is to force more Huffman + coding and less string matching; it is a half-step between Default and HuffmanOnly. + + + + + Using HuffmanOnly will force the compressor to do Huffman encoding only, with no + string matching. + + + + + An enum to specify the direction of transcoding - whether to compress or decompress. + + + + + Used to specify that the stream should compress the data. + + + + + Used to specify that the stream should decompress the data. + + + + + A general purpose exception class for exceptions in the Zlib library. + + + + + The ZlibException class captures exception information generated + by the Zlib library. + + + + + This ctor collects a message attached to the exception. + + the message for the exception. + + + + Performs an unsigned bitwise right shift with the specified number + + Number to operate on + Ammount of bits to shift + The resulting number from the shift operation + + + + Reads a number of characters from the current source TextReader and writes + the data to the target array at the specified index. + + + The source TextReader to read from + Contains the array of characteres read from the source TextReader. + The starting index of the target array. + The maximum number of characters to read from the source TextReader. + + + The number of characters read. The number will be less than or equal to + count depending on the data available in the source TextReader. Returns -1 + if the end of the stream is reached. + + + + + Computes an Adler-32 checksum. + + + The Adler checksum is similar to a CRC checksum, but faster to compute, though less + reliable. It is used in producing RFC1950 compressed streams. The Adler checksum + is a required part of the "ZLIB" standard. Applications will almost never need to + use this class directly. + + + + + + + Calculates the Adler32 checksum. + + + + This is used within ZLIB. You probably don't need to use this directly. + + + + To compute an Adler32 checksum on a byte array: + + var adler = Adler.Adler32(0, null, 0, 0); + adler = Adler.Adler32(adler, buffer, index, length); + + + + + + Encoder and Decoder for ZLIB and DEFLATE (IETF RFC1950 and RFC1951). + + + + This class compresses and decompresses data according to the Deflate algorithm + and optionally, the ZLIB format, as documented in RFC 1950 - ZLIB and RFC 1951 - DEFLATE. + + + + + The buffer from which data is taken. + + + + + An index into the InputBuffer array, indicating where to start reading. + + + + + The number of bytes available in the InputBuffer, starting at NextIn. + + + Generally you should set this to InputBuffer.Length before the first Inflate() or Deflate() call. + The class will update this number as calls to Inflate/Deflate are made. + + + + + Total number of bytes read so far, through all calls to Inflate()/Deflate(). + + + + + Buffer to store output data. + + + + + An index into the OutputBuffer array, indicating where to start writing. + + + + + The number of bytes available in the OutputBuffer, starting at NextOut. + + + Generally you should set this to OutputBuffer.Length before the first Inflate() or Deflate() call. + The class will update this number as calls to Inflate/Deflate are made. + + + + + Total number of bytes written to the output so far, through all calls to Inflate()/Deflate(). + + + + + used for diagnostics, when something goes wrong! + + + + + The compression level to use in this codec. Useful only in compression mode. + + + + + The number of Window Bits to use. + + + This gauges the size of the sliding window, and hence the + compression effectiveness as well as memory consumption. It's best to just leave this + setting alone if you don't know what it is. The maximum value is 15 bits, which implies + a 32k window. + + + + + The compression strategy to use. + + + This is only effective in compression. The theory offered by ZLIB is that different + strategies could potentially produce significant differences in compression behavior + for different data sets. Unfortunately I don't have any good recommendations for how + to set it differently. When I tested changing the strategy I got minimally different + compression performance. It's best to leave this property alone if you don't have a + good feel for it. Or, you may want to produce a test harness that runs through the + different strategy options and evaluates them on different file types. If you do that, + let me know your results. + + + + + Create a ZlibCodec. + + + If you use this default constructor, you will later have to explicitly call + InitializeInflate() or InitializeDeflate() before using the ZlibCodec to compress + or decompress. + + + + + Create a ZlibCodec that either compresses or decompresses. + + + Indicates whether the codec should compress (deflate) or decompress (inflate). + + + + + Initialize the inflation state. + + + It is not necessary to call this before using the ZlibCodec to inflate data; + It is implicitly called when you call the constructor. + + Z_OK if everything goes well. + + + + Initialize the inflation state with an explicit flag to + govern the handling of RFC1950 header bytes. + + + + By default, the ZLIB header defined in RFC 1950 is expected. If + you want to read a zlib stream you should specify true for + expectRfc1950Header. If you have a deflate stream, you will want to specify + false. It is only necessary to invoke this initializer explicitly if you + want to specify false. + + + whether to expect an RFC1950 header byte + pair when reading the stream of data to be inflated. + + Z_OK if everything goes well. + + + + Initialize the ZlibCodec for inflation, with the specified number of window bits. + + The number of window bits to use. If you need to ask what that is, + then you shouldn't be calling this initializer. + Z_OK if all goes well. + + + + Initialize the inflation state with an explicit flag to govern the handling of + RFC1950 header bytes. + + + + If you want to read a zlib stream you should specify true for + expectRfc1950Header. In this case, the library will expect to find a ZLIB + header, as defined in RFC + 1950, in the compressed stream. If you will be reading a DEFLATE or + GZIP stream, which does not have such a header, you will want to specify + false. + + + whether to expect an RFC1950 header byte pair when reading + the stream of data to be inflated. + The number of window bits to use. If you need to ask what that is, + then you shouldn't be calling this initializer. + Z_OK if everything goes well. + + + + Inflate the data in the InputBuffer, placing the result in the OutputBuffer. + + + You must have set InputBuffer and OutputBuffer, NextIn and NextOut, and AvailableBytesIn and + AvailableBytesOut before calling this method. + + + + private void InflateBuffer() + { + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + ZlibCodec decompressor = new ZlibCodec(); + + Console.WriteLine("\n============================================"); + Console.WriteLine("Size of Buffer to Inflate: {0} bytes.", CompressedBytes.Length); + MemoryStream ms = new MemoryStream(DecompressedBytes); + + int rc = decompressor.InitializeInflate(); + + decompressor.InputBuffer = CompressedBytes; + decompressor.NextIn = 0; + decompressor.AvailableBytesIn = CompressedBytes.Length; + + decompressor.OutputBuffer = buffer; + + // pass 1: inflate + do + { + decompressor.NextOut = 0; + decompressor.AvailableBytesOut = buffer.Length; + rc = decompressor.Inflate(FlushType.None); + + if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) + throw new Exception("inflating: " + decompressor.Message); + + ms.Write(decompressor.OutputBuffer, 0, buffer.Length - decompressor.AvailableBytesOut); + } + while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); + + // pass 2: finish and flush + do + { + decompressor.NextOut = 0; + decompressor.AvailableBytesOut = buffer.Length; + rc = decompressor.Inflate(FlushType.Finish); + + if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) + throw new Exception("inflating: " + decompressor.Message); + + if (buffer.Length - decompressor.AvailableBytesOut > 0) + ms.Write(buffer, 0, buffer.Length - decompressor.AvailableBytesOut); + } + while (decompressor.AvailableBytesIn > 0 || decompressor.AvailableBytesOut == 0); + + decompressor.EndInflate(); + } + + + + The flush to use when inflating. + Z_OK if everything goes well. + + + + Ends an inflation session. + + + Call this after successively calling Inflate(). This will cause all buffers to be flushed. + After calling this you cannot call Inflate() without a intervening call to one of the + InitializeInflate() overloads. + + Z_OK if everything goes well. + + + + I don't know what this does! + + Z_OK if everything goes well. + + + + Initialize the ZlibCodec for deflation operation. + + + The codec will use the MAX window bits and the default level of compression. + + + + int bufferSize = 40000; + byte[] CompressedBytes = new byte[bufferSize]; + byte[] DecompressedBytes = new byte[bufferSize]; + + ZlibCodec compressor = new ZlibCodec(); + + compressor.InitializeDeflate(CompressionLevel.Default); + + compressor.InputBuffer = System.Text.ASCIIEncoding.ASCII.GetBytes(TextToCompress); + compressor.NextIn = 0; + compressor.AvailableBytesIn = compressor.InputBuffer.Length; + + compressor.OutputBuffer = CompressedBytes; + compressor.NextOut = 0; + compressor.AvailableBytesOut = CompressedBytes.Length; + + while (compressor.TotalBytesIn != TextToCompress.Length && compressor.TotalBytesOut < bufferSize) + { + compressor.Deflate(FlushType.None); + } + + while (true) + { + int rc= compressor.Deflate(FlushType.Finish); + if (rc == ZlibConstants.Z_STREAM_END) break; + } + + compressor.EndDeflate(); + + + + Z_OK if all goes well. You generally don't need to check the return code. + + + + Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel. + + + The codec will use the maximum window bits (15) and the specified + CompressionLevel. It will emit a ZLIB stream as it compresses. + + The compression level for the codec. + Z_OK if all goes well. + + + + Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, + and the explicit flag governing whether to emit an RFC1950 header byte pair. + + + The codec will use the maximum window bits (15) and the specified CompressionLevel. + If you want to generate a zlib stream, you should specify true for + wantRfc1950Header. In this case, the library will emit a ZLIB + header, as defined in RFC + 1950, in the compressed stream. + + The compression level for the codec. + whether to emit an initial RFC1950 byte pair in the compressed stream. + Z_OK if all goes well. + + + + Initialize the ZlibCodec for deflation operation, using the specified CompressionLevel, + and the specified number of window bits. + + + The codec will use the specified number of window bits and the specified CompressionLevel. + + The compression level for the codec. + the number of window bits to use. If you don't know what this means, don't use this method. + Z_OK if all goes well. + + + + Initialize the ZlibCodec for deflation operation, using the specified + CompressionLevel, the specified number of window bits, and the explicit flag + governing whether to emit an RFC1950 header byte pair. + + + The compression level for the codec. + whether to emit an initial RFC1950 byte pair in the compressed stream. + the number of window bits to use. If you don't know what this means, don't use this method. + Z_OK if all goes well. + + + + Deflate one batch of data. + + + You must have set InputBuffer and OutputBuffer before calling this method. + + + + private void DeflateBuffer(CompressionLevel level) + { + int bufferSize = 1024; + byte[] buffer = new byte[bufferSize]; + ZlibCodec compressor = new ZlibCodec(); + + Console.WriteLine("\n============================================"); + Console.WriteLine("Size of Buffer to Deflate: {0} bytes.", UncompressedBytes.Length); + MemoryStream ms = new MemoryStream(); + + int rc = compressor.InitializeDeflate(level); + + compressor.InputBuffer = UncompressedBytes; + compressor.NextIn = 0; + compressor.AvailableBytesIn = UncompressedBytes.Length; + + compressor.OutputBuffer = buffer; + + // pass 1: deflate + do + { + compressor.NextOut = 0; + compressor.AvailableBytesOut = buffer.Length; + rc = compressor.Deflate(FlushType.None); + + if (rc != ZlibConstants.Z_OK && rc != ZlibConstants.Z_STREAM_END) + throw new Exception("deflating: " + compressor.Message); + + ms.Write(compressor.OutputBuffer, 0, buffer.Length - compressor.AvailableBytesOut); + } + while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); + + // pass 2: finish and flush + do + { + compressor.NextOut = 0; + compressor.AvailableBytesOut = buffer.Length; + rc = compressor.Deflate(FlushType.Finish); + + if (rc != ZlibConstants.Z_STREAM_END && rc != ZlibConstants.Z_OK) + throw new Exception("deflating: " + compressor.Message); + + if (buffer.Length - compressor.AvailableBytesOut > 0) + ms.Write(buffer, 0, buffer.Length - compressor.AvailableBytesOut); + } + while (compressor.AvailableBytesIn > 0 || compressor.AvailableBytesOut == 0); + + compressor.EndDeflate(); + + ms.Seek(0, SeekOrigin.Begin); + CompressedBytes = new byte[compressor.TotalBytesOut]; + ms.Read(CompressedBytes, 0, CompressedBytes.Length); + } + + + whether to flush all data as you deflate. Generally you will want to + use Z_NO_FLUSH here, in a series of calls to Deflate(), and then call EndDeflate() to + flush everything. + + Z_OK if all goes well. + + + + End a deflation session. + + + Call this after making a series of one or more calls to Deflate(). All buffers are flushed. + + Z_OK if all goes well. + + + + Reset a codec for another deflation session. + + + Call this to reset the deflation state. For example if a thread is deflating + non-consecutive blocks, you can call Reset() after the Deflate(Sync) of the first + block and before the next Deflate(None) of the second block. + + Z_OK if all goes well. + + + + Set the CompressionStrategy and CompressionLevel for a deflation session. + + the level of compression to use. + the strategy to use for compression. + Z_OK if all goes well. + + + + Set the dictionary to be used for either Inflation or Deflation. + + The dictionary bytes to use. + Z_OK if all goes well. + + + + The Adler32 checksum on the data transferred through the codec so far. You probably don't need to look at this. + + + + + A bunch of constants used in the Zlib interface. + + + + + The maximum number of window bits for the Deflate algorithm. + + + + + The default number of window bits for the Deflate algorithm. + + + + + indicates everything is A-OK + + + + + Indicates that the last operation reached the end of the stream. + + + + + The operation ended in need of a dictionary. + + + + + There was an error with the stream - not enough data, not open and readable, etc. + + + + + There was an error with the data - not enough data, bad data, etc. + + + + + There was an error with the working buffer. + + + + + The size of the working buffer used in the ZlibCodec class. Defaults to 8192 bytes. + + + + + The minimum size of the working buffer used in the ZlibCodec class. Currently it is 128 bytes. + + + + + Represents a Zlib stream for compression or decompression. + + + + + The ZlibStream is a Decorator on a . It adds ZLIB compression or decompression to any + stream. + + + Using this stream, applications can compress or decompress data via + stream Read() and Write() operations. Either compresssion or + decompression can occur through either reading or writing. The compression + format used is ZLIB, which is documented in IETF RFC 1950, "ZLIB Compressed + Data Format Specification version 3.3". This implementation of ZLIB always uses + DEFLATE as the compression method. (see IETF RFC 1951, "DEFLATE + Compressed Data Format Specification version 1.3.") + + + The ZLIB format allows for varying compression methods, window sizes, and dictionaries. + This implementation always uses the DEFLATE compression method, a preset dictionary, + and 15 window bits by default. + + + + This class is similar to , except that it adds the + RFC1950 header and trailer bytes to a compressed stream when compressing, or expects + the RFC1950 header and trailer bytes when decompressing. It is also similar to the + . + + + + + + + + Create a ZlibStream using the specified CompressionMode. + + + + + When mode is CompressionMode.Compress, the ZlibStream + will use the default compression level. The "captive" stream will be + closed when the ZlibStream is closed. + + + + + + This example uses a ZlibStream to compress a file, and writes the + compressed data to another file. + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(fileToCompress + ".zlib")) + { + using (Stream compressor = new ZlibStream(raw, CompressionMode.Compress)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(fileToCompress & ".zlib") + Using compressor As Stream = New ZlibStream(raw, CompressionMode.Compress) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + + The stream which will be read or written. + Indicates whether the ZlibStream will compress or decompress. + + + + Create a ZlibStream using the specified CompressionMode and + the specified CompressionLevel. + + + + + + When mode is CompressionMode.Decompress, the level parameter is ignored. + The "captive" stream will be closed when the ZlibStream is closed. + + + + + + This example uses a ZlibStream to compress data from a file, and writes the + compressed data to another file. + + + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (var raw = System.IO.File.Create(fileToCompress + ".zlib")) + { + using (Stream compressor = new ZlibStream(raw, + CompressionMode.Compress, + CompressionLevel.BestCompression)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + } + + + + Using input As Stream = File.OpenRead(fileToCompress) + Using raw As FileStream = File.Create(fileToCompress & ".zlib") + Using compressor As Stream = New ZlibStream(raw, CompressionMode.Compress, CompressionLevel.BestCompression) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + End Using + + + + The stream to be read or written while deflating or inflating. + Indicates whether the ZlibStream will compress or decompress. + A tuning knob to trade speed for effectiveness. + + + + Create a ZlibStream using the specified CompressionMode, and + explicitly specify whether the captive stream should be left open after + Deflation or Inflation. + + + + + + When mode is CompressionMode.Compress, the ZlibStream will use + the default compression level. + + + + This constructor allows the application to request that the captive stream + remain open after the deflation or inflation occurs. By default, after + Close() is called on the stream, the captive stream is also + closed. In some cases this is not desired, for example if the stream is a + that will be re-read after + compression. Specify true for the parameter to leave the stream + open. + + + + See the other overloads of this constructor for example code. + + + + + The stream which will be read or written. This is called the + "captive" stream in other places in this documentation. + Indicates whether the ZlibStream will compress or decompress. + true if the application would like the stream to remain + open after inflation/deflation. + + + + Create a ZlibStream using the specified CompressionMode + and the specified CompressionLevel, and explicitly specify + whether the stream should be left open after Deflation or Inflation. + + + + + + This constructor allows the application to request that the captive + stream remain open after the deflation or inflation occurs. By + default, after Close() is called on the stream, the captive + stream is also closed. In some cases this is not desired, for example + if the stream is a that will be + re-read after compression. Specify true for the parameter to leave the stream open. + + + + When mode is CompressionMode.Decompress, the level parameter is + ignored. + + + + + + + This example shows how to use a ZlibStream to compress the data from a file, + and store the result into another file. The filestream remains open to allow + additional data to be written to it. + + + using (var output = System.IO.File.Create(fileToCompress + ".zlib")) + { + using (System.IO.Stream input = System.IO.File.OpenRead(fileToCompress)) + { + using (Stream compressor = new ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, true)) + { + byte[] buffer = new byte[WORKING_BUFFER_SIZE]; + int n; + while ((n= input.Read(buffer, 0, buffer.Length)) != 0) + { + compressor.Write(buffer, 0, n); + } + } + } + // can write additional data to the output stream here + } + + + Using output As FileStream = File.Create(fileToCompress & ".zlib") + Using input As Stream = File.OpenRead(fileToCompress) + Using compressor As Stream = New ZlibStream(output, CompressionMode.Compress, CompressionLevel.BestCompression, True) + Dim buffer As Byte() = New Byte(4096) {} + Dim n As Integer = -1 + Do While (n <> 0) + If (n > 0) Then + compressor.Write(buffer, 0, n) + End If + n = input.Read(buffer, 0, buffer.Length) + Loop + End Using + End Using + ' can write additional data to the output stream here. + End Using + + + + The stream which will be read or written. + + Indicates whether the ZlibStream will compress or decompress. + + + true if the application would like the stream to remain open after + inflation/deflation. + + + + A tuning knob to trade speed for effectiveness. This parameter is + effective only when mode is CompressionMode.Compress. + + + + + Dispose the stream. + + + + This may or may not result in a Close() call on the captive + stream. See the constructors that have a leaveOpen parameter + for more information. + + + This method may be invoked in two distinct scenarios. If disposing + == true, the method has been called directly or indirectly by a + user's code, for example via the public Dispose() method. In this + case, both managed and unmanaged resources can be referenced and + disposed. If disposing == false, the method has been called by the + runtime from inside the object finalizer and this method should not + reference other objects; in that case only unmanaged resources must + be referenced or disposed. + + + + indicates whether the Dispose method was invoked by user code. + + + + + Flush the stream. + + + + + Read data from the stream. + + + + + + If you wish to use the ZlibStream to compress data while reading, + you can create a ZlibStream with CompressionMode.Compress, + providing an uncompressed data stream. Then call Read() on that + ZlibStream, and the data read will be compressed. If you wish to + use the ZlibStream to decompress data while reading, you can create + a ZlibStream with CompressionMode.Decompress, providing a + readable compressed data stream. Then call Read() on that + ZlibStream, and the data will be decompressed as it is read. + + + + A ZlibStream can be used for Read() or Write(), but + not both. + + + + + + The buffer into which the read data should be placed. + + + the offset within that data array to put the first byte read. + + the number of bytes to read. + + the number of bytes read + + + + Calling this method always throws a . + + + The offset to seek to.... + IF THIS METHOD ACTUALLY DID ANYTHING. + + + The reference specifying how to apply the offset.... IF + THIS METHOD ACTUALLY DID ANYTHING. + + + nothing. This method always throws. + + + + Calling this method always throws a . + + + The new value for the stream length.... IF + THIS METHOD ACTUALLY DID ANYTHING. + + + + + Write data to the stream. + + + + + + If you wish to use the ZlibStream to compress data while writing, + you can create a ZlibStream with CompressionMode.Compress, + and a writable output stream. Then call Write() on that + ZlibStream, providing uncompressed data as input. The data sent to + the output stream will be the compressed form of the data written. If you + wish to use the ZlibStream to decompress data while writing, you + can create a ZlibStream with CompressionMode.Decompress, and a + writable output stream. Then call Write() on that stream, + providing previously compressed data. The data sent to the output stream + will be the decompressed form of the data written. + + + + A ZlibStream can be used for Read() or Write(), but not both. + + + The buffer holding data to write to the stream. + the offset within that data array to find the first byte to write. + the number of bytes to write. + + + + Compress a string into a byte array using ZLIB. + + + + Uncompress it with . + + + + + + + + A string to compress. The string will first be encoded + using UTF8, then compressed. + + + The string in compressed form + + + + Compress a byte array into a new byte array using ZLIB. + + + + Uncompress it with . + + + + + + + A buffer to compress. + + + The data in compressed form + + + + Uncompress a ZLIB-compressed byte array into a single string. + + + + + + + A buffer containing ZLIB-compressed data. + + + The uncompressed string + + + + Uncompress a ZLIB-compressed byte array into a byte array. + + + + + + + A buffer containing ZLIB-compressed data. + + + The data in uncompressed form + + + + This property sets the flush behavior on the stream. + Sorry, though, not sure exactly how to describe all the various settings. + + + + + The size of the working buffer for the compression codec. + + + + + The working buffer is used for all stream operations. The default size is + 1024 bytes. The minimum size is 128 bytes. You may get better performance + with a larger buffer. Then again, you might not. You would have to test + it. + + + + Set this before the first call to Read() or Write() on the + stream. If you try to set it afterwards, it will throw. + + + + + Returns the total number of bytes input so far. + + + Returns the total number of bytes output so far. + + + + Indicates whether the stream can be read. + + + The return value depends on whether the captive stream supports reading. + + + + + Indicates whether the stream supports Seek operations. + + + Always returns false. + + + + + Indicates whether the stream can be written. + + + The return value depends on whether the captive stream supports writing. + + + + + Reading this property always throws a . + + + + + The position of the stream pointer. + + + + Setting this property always throws a . Reading will return the total bytes + written out, if used in writing, or the total bytes read in, if used in + reading. The count may refer to compressed bytes or uncompressed bytes, + depending on how you've used the stream. + + + + + Computes a CRC-32. The CRC-32 algorithm is parameterized - you + can set the polynomial and enable or disable bit + reversal. This can be used for GZIP, BZip2, or ZIP. + + + This type is used internally by DotNetZip; it is generally not used + directly by applications wishing to create, read, or manipulate zip + archive files. + + + + + Returns the CRC32 for the specified stream. + + The stream over which to calculate the CRC32 + the CRC32 calculation + + + + Returns the CRC32 for the specified stream, and writes the input into the + output stream. + + The stream over which to calculate the CRC32 + The stream into which to deflate the input + the CRC32 calculation + + + + Get the CRC32 for the given (word,byte) combo. This is a + computation defined by PKzip for PKZIP 2.0 (weak) encryption. + + The word to start with. + The byte to combine it with. + The CRC-ized result. + + + + Update the value for the running CRC32 using the given block of bytes. + This is useful when using the CRC32() class in a Stream. + + block of bytes to slurp + starting point in the block + how many bytes within the block to slurp + + + + Process one byte in the CRC. + + the byte to include into the CRC . + + + + Process a run of N identical bytes into the CRC. + + + + This method serves as an optimization for updating the CRC when a + run of identical bytes is found. Rather than passing in a buffer of + length n, containing all identical bytes b, this method accepts the + byte value and the length of the (virtual) buffer - the length of + the run. + + + the byte to include into the CRC. + the number of times that byte should be repeated. + + + + Combines the given CRC32 value with the current running total. + + + This is useful when using a divide-and-conquer approach to + calculating a CRC. Multiple threads can each calculate a + CRC32 on a segment of the data, and then combine the + individual CRC32 values at the end. + + the crc value to be combined with this one + the length of data the CRC value was calculated on + + + + Create an instance of the CRC32 class using the default settings: no + bit reversal, and a polynomial of 0xEDB88320. + + + + + Create an instance of the CRC32 class, specifying whether to reverse + data bits or not. + + + specify true if the instance should reverse data bits. + + + + In the CRC-32 used by BZip2, the bits are reversed. Therefore if you + want a CRC32 with compatibility with BZip2, you should pass true + here. In the CRC-32 used by GZIP and PKZIP, the bits are not + reversed; Therefore if you want a CRC32 with compatibility with + those, you should pass false. + + + + + + Create an instance of the CRC32 class, specifying the polynomial and + whether to reverse data bits or not. + + + The polynomial to use for the CRC, expressed in the reversed (LSB) + format: the highest ordered bit in the polynomial value is the + coefficient of the 0th power; the second-highest order bit is the + coefficient of the 1 power, and so on. Expressed this way, the + polynomial for the CRC-32C used in IEEE 802.3, is 0xEDB88320. + + + specify true if the instance should reverse data bits. + + + + + In the CRC-32 used by BZip2, the bits are reversed. Therefore if you + want a CRC32 with compatibility with BZip2, you should pass true + here for the reverseBits parameter. In the CRC-32 used by + GZIP and PKZIP, the bits are not reversed; Therefore if you want a + CRC32 with compatibility with those, you should pass false for the + reverseBits parameter. + + + + + + Reset the CRC-32 class - clear the CRC "remainder register." + + + + Use this when employing a single instance of this class to compute + multiple, distinct CRCs on multiple, distinct data blocks. + + + + + + Indicates the total number of bytes applied to the CRC. + + + + + Indicates the current CRC for all blocks slurped in. + + + + + A Stream that calculates a CRC32 (a checksum) on all bytes read, + or on all bytes written. + + + + + This class can be used to verify the CRC of a ZipEntry when + reading from a stream, or to calculate a CRC when writing to a + stream. The stream should be used to either read, or write, but + not both. If you intermix reads and writes, the results are not + defined. + + + + This class is intended primarily for use internally by the + DotNetZip library. + + + + + + The default constructor. + + + + Instances returned from this constructor will leave the underlying + stream open upon Close(). The stream uses the default CRC32 + algorithm, which implies a polynomial of 0xEDB88320. + + + The underlying stream + + + + The constructor allows the caller to specify how to handle the + underlying stream at close. + + + + The stream uses the default CRC32 algorithm, which implies a + polynomial of 0xEDB88320. + + + The underlying stream + true to leave the underlying stream + open upon close of the CrcCalculatorStream; false otherwise. + + + + A constructor allowing the specification of the length of the stream + to read. + + + + The stream uses the default CRC32 algorithm, which implies a + polynomial of 0xEDB88320. + + + Instances returned from this constructor will leave the underlying + stream open upon Close(). + + + The underlying stream + The length of the stream to slurp + + + + A constructor allowing the specification of the length of the stream + to read, as well as whether to keep the underlying stream open upon + Close(). + + + + The stream uses the default CRC32 algorithm, which implies a + polynomial of 0xEDB88320. + + + The underlying stream + The length of the stream to slurp + true to leave the underlying stream + open upon close of the CrcCalculatorStream; false otherwise. + + + + A constructor allowing the specification of the length of the stream + to read, as well as whether to keep the underlying stream open upon + Close(), and the CRC32 instance to use. + + + + The stream uses the specified CRC32 instance, which allows the + application to specify how the CRC gets calculated. + + + The underlying stream + The length of the stream to slurp + true to leave the underlying stream + open upon close of the CrcCalculatorStream; false otherwise. + the CRC32 instance to use to calculate the CRC32 + + + + Read from the stream + + the buffer to read + the offset at which to start + the number of bytes to read + the number of bytes actually read + + + + Write to the stream. + + the buffer from which to write + the offset at which to start writing + the number of bytes to write + + + + Flush the stream. + + + + + Seeking is not supported on this stream. This method always throws + + + N/A + N/A + N/A + + + + This method always throws + + + N/A + + + + Closes the stream. + + + + + Gets the total number of bytes run through the CRC32 calculator. + + + + This is either the total number of bytes read, or the total number of + bytes written, depending on the direction of this stream. + + + + + Provides the current CRC for all blocks slurped in. + + + + The running total of the CRC is kept as data is written or read + through the stream. read this property after all reads or writes to + get an accurate CRC for the entire stream. + + + + + + Indicates whether the underlying stream will be left open when the + CrcCalculatorStream is Closed. + + + + Set this at any point before calling . + + + + + + Indicates whether the stream supports reading. + + + + + Indicates whether the stream supports seeking. + + + + Always returns false. + + + + + + Indicates whether the stream supports writing. + + + + + Returns the length of the underlying stream. + + + + + The getter for this property returns the total bytes read. + If you use the setter, it will throw + . + + +
+
diff --git a/src/Xamarin.Android.Build.Tasks/Linker/LinkModes.cs b/src/Xamarin.Android.Build.Tasks/Linker/LinkModes.cs new file mode 100644 index 00000000000..9ef60b5096b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/LinkModes.cs @@ -0,0 +1,11 @@ +using System; + +namespace Xamarin.Android.Tasks +{ + public enum LinkModes + { + None, + SdkOnly, + Full + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs b/src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs new file mode 100644 index 00000000000..8e588247eb9 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/Mobile.Tuner/MobileProfile.cs @@ -0,0 +1,97 @@ +using System; +using System.Collections.Generic; + +using Mono.Cecil; + +using Mono.Tuner; + +namespace Mobile.Tuner { + + public abstract class MobileProfile : Profile { + + static readonly HashSet Sdk = new HashSet { + "mscorlib", + "System", + "System.ComponentModel.Composition", + "System.ComponentModel.DataAnnotations", + "System.Core", + "System.Data", + "System.Data.Services.Client", + "System.IO.Compression.FileSystem", + "System.IO.Compression", + "System.Json", + "System.Net", + "System.Net.Http", + "System.Numerics", + "System.Runtime.Serialization", + "System.ServiceModel", + "System.ServiceModel.Web", + "System.Transactions", + "System.Web.Services", + "System.Windows", + "System.Xml", + "System.Xml.Linq", + "System.Xml.Serialization", + "Microsoft.CSharp", + "Microsoft.VisualBasic", + "Mono.CSharp", + "Mono.Cairo", + "Mono.CompilerServices.SymbolWriter", + "Mono.Data.Tds", + "Mono.Data.Sqlite", + "Mono.Security", + "OpenTK", + "OpenTK-1.0", + // Facades assemblies (PCL) + "System.Collections.Concurrent", + "System.Collections", + "System.ComponentModel.Annotations", + "System.ComponentModel.EventBasedAsync", + "System.ComponentModel", + "System.Diagnostics.Contracts", + "System.Diagnostics.Debug", + "System.Diagnostics.Tools", + "System.Dynamic.Runtime", + "System.EnterpriseServices", + "System.Globalization", + "System.IO", + "System.Linq.Expressions", + "System.Linq.Parallel", + "System.Linq.Queryable", + "System.Linq", + "System.Net.NetworkInformation", + "System.Net.Primitives", + "System.Net.Requests", + "System.ObjectModel", + "System.Reflection.Extensions", + "System.Reflection.Primitives", + "System.Reflection", + "System.Resources.ResourceManager", + "System.Runtime.Extensions", + "System.Runtime.InteropServices", + "System.Runtime.InteropServices.WindowsRuntime", + "System.Runtime.Numerics", + "System.Runtime.Serialization.Json", + "System.Runtime.Serialization.Primitives", + "System.Runtime.Serialization.Xml", + "System.Runtime", + "System.Security.Principal", + "System.ServiceModel.Http", + "System.ServiceModel.Primitives", + "System.Text.Encoding.Extensions", + "System.Text.Encoding", + "System.Text.RegularExpressions", + "System.Threading.Tasks.Parallel", + "System.Threading.Tasks", + "System.Threading", + "System.Xml.ReaderWriter", + "System.Xml.XDocument", + "System.Xml.XmlSerializer", + }; + + protected override bool IsSdk (string assemblyName) + { + return Sdk.Contains (assemblyName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/ApplyPreserveAttribute.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/ApplyPreserveAttribute.cs new file mode 100644 index 00000000000..5e956b6d7a3 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/ApplyPreserveAttribute.cs @@ -0,0 +1,122 @@ +// Copyright 2011, 2015 Xamarin Inc. All rights reserved. + +using System; + +using Mono.Linker; +using Mono.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + public class ApplyPreserveAttribute : ApplyPreserveAttributeBase { + + // System.ServiceModeldll is an SDK assembly but it does contain types with [DataMember] attributes + public override bool IsActiveFor (AssemblyDefinition assembly) + { + if (Profile.IsSdkAssembly (assembly)) { + if (assembly.Name.Name != "System.ServiceModel") + return false; + } + return Annotations.GetAction (assembly) == AssemblyAction.Link; + } + + protected override bool IsPreservedAttribute (ICustomAttributeProvider provider, CustomAttribute attribute, out bool removeAttribute) + { + removeAttribute = false; + TypeReference type = attribute.Constructor.DeclaringType; + + switch (type.Namespace) { + case "Android.Runtime": + // there's no need to keep the [Preserve] attribute in the assembly once it was processed + if (type.Name == "PreserveAttribute") { + removeAttribute = true; + return true; + } + break; + case "System.Runtime.Serialization": + bool srs = false; + // http://bugzilla.xamarin.com/show_bug.cgi?id=1415 + // http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx + if (provider is PropertyDefinition || provider is FieldDefinition || provider is EventDefinition) + srs = (type.Name == "DataMemberAttribute"); + else if (provider is TypeDefinition) + srs = (type.Name == "DataContractAttribute"); + + if (srs) { + MarkDefautConstructor (provider); + return true; + } + break; + case "System.Xml.Serialization": + // http://msdn.microsoft.com/en-us/library/83y7df3e.aspx + string name = type.Name; + if ((name.StartsWith ("Xml", StringComparison.Ordinal) && name.EndsWith ("Attribute", StringComparison.Ordinal))) { + // but we do not have to keep things that XML serialization will ignore anyway! + if (name != "XmlIgnoreAttribute") { + // the default constructor of the type *being used* is needed + MarkDefautConstructor (provider); + return true; + } + } + break; + default: + if (type.Name == "PreserveAttribute") { + // there's no need to keep the [Preserve] attribute in the assembly once it was processed + removeAttribute = true; + return true; + } + break; + } + // keep them (provider and attribute) + return false; + } + + // xml serialization requires the default .ctor to be present + void MarkDefautConstructor (ICustomAttributeProvider provider) + { + TypeDefinition td = (provider as TypeDefinition); + if (td == null) { + PropertyDefinition pd = (provider as PropertyDefinition); + if (pd != null) { + MarkDefautConstructor (pd.DeclaringType); + MarkGenericType (pd.PropertyType as GenericInstanceType); + td = pd.PropertyType.Resolve (); + } else { + FieldDefinition fd = (provider as FieldDefinition); + if (fd != null) { + MarkDefautConstructor (fd.DeclaringType); + MarkGenericType (fd.FieldType as GenericInstanceType); + td = (fd.FieldType as TypeReference).Resolve (); + } + } + } + + // e.g. property (see bug #5543) or field (see linkall unit tests) + if (td != null) + MarkDefautConstructor (td); + } + + void MarkGenericType (GenericInstanceType git) + { + if (git == null || !git.HasGenericArguments) + return; + + foreach (TypeReference tr in git.GenericArguments) + MarkDefautConstructor (tr.Resolve ()); + } + + void MarkDefautConstructor (TypeDefinition type) + { + if ((type == null) || !type.HasMethods) + return; + + foreach (MethodDefinition ctor in type.Methods) { + if (!ctor.IsConstructor || ctor.IsStatic || ctor.HasParameters) + continue; + + Annotations.AddPreservedMethod (type, ctor); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs new file mode 100644 index 00000000000..02add26c787 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Extensions.cs @@ -0,0 +1,101 @@ +using System; +using System.Collections.Generic; + +using Mono.Cecil; + +using Mono.Linker; + +using Mono.Tuner; + +namespace MonoDroid.Tuner { + + static class Extensions { + + const string JavaObject = "Java.Lang.Object"; + const string IJavaObject = "Android.Runtime.IJavaObject"; + const string JavaThrowable = "Java.Lang.Throwable"; + + public static bool IsJavaObject (this TypeDefinition type) + { + return type.Inherits (JavaObject); + } + + public static bool IsJavaException (this TypeDefinition type) + { + return type.Inherits (JavaThrowable); + } + + public static bool ImplementsIJavaObject (this TypeDefinition type) + { + return type.Implements (IJavaObject); + } + + public static object GetSettableValue (this CustomAttributeArgument arg) + { + TypeReference tr = arg.Value as TypeReference; + TypeDefinition td = tr != null ? tr.Resolve () : null; + return td != null ? td.FullName + "," + td.Module.Assembly.FullName : arg.Value; + } + + public static bool Implements (this TypeReference self, string interfaceName) + { + if (interfaceName == null) + throw new ArgumentNullException ("interfaceName"); + if (self == null) + return false; + + TypeDefinition type = self.Resolve (); + if (type == null) + return false; // not enough information available + + // special case, check if we implement ourselves + if (type.IsInterface && (type.FullName == interfaceName)) + return true; + + return Implements (type, interfaceName, (interfaceName.IndexOf ('`') >= 0)); + } + + public static bool Implements (TypeDefinition type, string interfaceName, bool generic) + { + while (type != null) { + // does the type implements it itself + if (type.HasInterfaces) { + foreach (TypeReference iface in type.Interfaces) { + string fullname = (generic) ? iface.GetElementType ().FullName : iface.FullName; + if (fullname == interfaceName) + return true; + //if not, then maybe one of its parent interfaces does + if (Implements (iface.Resolve (), interfaceName, generic)) + return true; + } + } + + type = type.BaseType != null ? type.BaseType.Resolve () : null; + } + return false; + } + + public static bool Inherits (this TypeReference self, string className) + { + if (className == null) + throw new ArgumentNullException ("className"); + if (self == null) + return false; + + TypeReference current = self.Resolve (); + while (current != null) { + string fullname = current.FullName; + if (fullname == className) + return true; + if (fullname == "System.Object") + return false; + + TypeDefinition td = current.Resolve (); + if (td == null) + return false; // could not resolve type + current = td.BaseType; + } + return false; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs new file mode 100644 index 00000000000..2e3df969d06 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/FixAbstractMethodsStep.cs @@ -0,0 +1,225 @@ +using System; +using System.Collections.Generic; + +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Mono.Linker; +using Mono.Linker.Steps; +using Mono.Tuner; + +namespace MonoDroid.Tuner +{ + public class FixAbstractMethodsStep : BaseStep + { + protected override void ProcessAssembly (AssemblyDefinition assembly) + { + if (!Annotations.HasAction (assembly)) + Annotations.SetAction (assembly, AssemblyAction.Skip); + + if (Profile.IsSdkAssembly (assembly) || Profile.IsProductAssembly (assembly)) + return; + + bool changed = false; + foreach (var type in assembly.MainModule.Types) { + if (MightNeedFix (type)) + changed |= FixAbstractMethods (type); + } + + if (changed) { + AssemblyAction action = Annotations.HasAction (assembly) ? Annotations.GetAction (assembly) : AssemblyAction.Skip; + if (action == AssemblyAction.Skip || action == AssemblyAction.Copy || action == AssemblyAction.Delete) + Annotations.SetAction (assembly, AssemblyAction.Save); + var td = AbstractMethodErrorConstructor.DeclaringType.Resolve (); + Annotations.Mark (td); + Annotations.SetPreserve (td, TypePreserve.Nothing); + Annotations.AddPreservedMethod (td, AbstractMethodErrorConstructor.Resolve ()); + } + } + + bool MightNeedFix (TypeDefinition type) + { + return !type.IsAbstract && type.IsSubclassOf ("Java.Lang.Object"); + } + + static bool CompareTypes (TypeReference iType, TypeReference tType) + { + if (iType.IsGenericParameter) + return true; + + if (iType.IsArray) { + if (!tType.IsArray) + return false; + return CompareTypes (iType.GetElementType (), tType.GetElementType ()); + } + + if (iType.IsByReference) { + if (!tType.IsByReference) + return false; + return CompareTypes (iType.GetElementType (), tType.GetElementType ()); + } + + if (iType.Name != tType.Name) + return false; + + if (iType.Namespace != tType.Namespace) + return false; + + TypeDefinition iTypeDef = iType.Resolve (); + if (iTypeDef == null) + return false; + + TypeDefinition tTypeDef = tType.Resolve (); + if (tTypeDef == null) + return false; + + if (iTypeDef.Module.FullyQualifiedName != tTypeDef.Module.FullyQualifiedName) + return false; + + if (iType is Mono.Cecil.GenericInstanceType && tType is Mono.Cecil.GenericInstanceType) { + GenericInstanceType iGType = iType as GenericInstanceType; + GenericInstanceType tGType = tType as GenericInstanceType; + + if (iGType.GenericArguments.Count != tGType.GenericArguments.Count) + return false; + for (int i = 0; i < iGType.GenericArguments.Count; i++) { + if (iGType.GenericArguments [i].IsGenericParameter) + continue; + if (!CompareTypes (iGType.GenericArguments [i], tGType.GenericArguments [i])) + return false; + } + } + + return true; + } + + bool HaveSameSignature (TypeReference iface, MethodDefinition iMethod, MethodDefinition tMethod) + { + if (iMethod.Name != tMethod.Name && (iMethod.DeclaringType == null || (iMethod.DeclaringType.DeclaringType == null ? (string.Format ("{0}.{1}", iface.FullName, iMethod.Name) != tMethod.Name) : (string.Format ("{0}.{1}.{2}", iMethod.DeclaringType.DeclaringType, iface.Name, iMethod.Name) != tMethod.Name)))) + return false; + + if (!CompareTypes (iMethod.MethodReturnType.ReturnType, tMethod.MethodReturnType.ReturnType)) + return false; + + if (iMethod.Parameters.Count != tMethod.Parameters.Count || iMethod.GenericParameters.Count != tMethod.GenericParameters.Count) + return false; + + if (iMethod.HasParameters) { + List m1p = new List (iMethod.Parameters); + List m2p = new List (tMethod.Parameters); + + for (int i = 0; i < m1p.Count; i++) { + if (!CompareTypes (m1p [i].ParameterType, m2p [i].ParameterType)) + return false; + } + } + + if (iMethod.HasGenericParameters) { + List m1p = new List (iMethod.GenericParameters); + List m2p = new List (tMethod.GenericParameters); + + for (int i = 0; i < m1p.Count; i++) + if (!CompareTypes (m1p [i], m2p [i])) + return false; + } + + return true; + } + + bool FixAbstractMethods (TypeDefinition type) + { + if (!type.HasInterfaces) + return false; + + bool rv = false; + List typeMethods = new List (type.Methods); + foreach (var baseType in type.GetBaseTypes ()) + typeMethods.AddRange (baseType.Methods); + + foreach (var iface in type.Interfaces) { + var ifaceDef = iface.Resolve (); + if (ifaceDef == null) { + if (Context.LogInternalExceptions) + Console.WriteLine ("Unable to unresolve interface: {0}", iface.FullName); + continue; + } + if (ifaceDef.HasGenericParameters) + continue; + + foreach (var iMethod in ifaceDef.Methods) { + bool exists = false; + + foreach (var tMethod in typeMethods) { + if (HaveSameSignature (iface, iMethod, tMethod)) { + exists = true; + break; + } + } + + if (!exists) { + AddNewExceptionMethod (type, iMethod); + rv = true; + } + } + } + + return rv; + } + + TypeReference TryImportType (TypeDefinition declaringType, TypeReference type) + { + if (type.IsGenericParameter) + return type; + + return declaringType.Module.Import (type); + } + + void AddNewExceptionMethod (TypeDefinition type, MethodDefinition method) + { + var newMethod = new MethodDefinition (method.Name, (method.Attributes | MethodAttributes.Final) & ~MethodAttributes.Abstract, TryImportType (type, method.ReturnType)); + + foreach (var paramater in method.Parameters) + newMethod.Parameters.Add (new ParameterDefinition (paramater.Name, paramater.Attributes, TryImportType (type, paramater.ParameterType))); + + var ilP = newMethod.Body.GetILProcessor (); + + ilP.Append (ilP.Create (Mono.Cecil.Cil.OpCodes.Newobj, type.Module.Import (AbstractMethodErrorConstructor))); + ilP.Append (ilP.Create (Mono.Cecil.Cil.OpCodes.Throw)); + + type.Methods.Add (newMethod); + + if (Context.LogInternalExceptions) + Console.WriteLine ("Added method: {0} to type: {1} scope: {2}", method, type.FullName, type.Scope); + } + + MethodReference abstractMethodErrorConstructor; + + MethodReference AbstractMethodErrorConstructor { + get { + if (abstractMethodErrorConstructor != null) + return abstractMethodErrorConstructor; + + foreach (var assembly in Context.GetAssemblies ()) { + if (assembly.Name.Name != "Mono.Android") + continue; + + var errorException = assembly.MainModule.GetType ("Java.Lang.AbstractMethodError"); + if (errorException == null) + break; + + foreach (var method in errorException.Methods) + if (method.Name == ".ctor" && !method.HasParameters) { + abstractMethodErrorConstructor = method; + break; + } + } + + if (abstractMethodErrorConstructor == null) + throw new Exception ("Unable to find Java.Lang.AbstractMethodError constructor in Mono.Android assembly"); + + return abstractMethodErrorConstructor; + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs new file mode 100644 index 00000000000..5c36b272765 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs @@ -0,0 +1,87 @@ +// +// GenerateProguardConfiguration.cs +// +// Author: +// Atsushi Eno +// +// (C) 2014 Xamarin Inc. (http://www.xamarin.com) +// + +using System; +using System.IO; +using System.Linq; + +using Mono.Cecil; + +namespace Mono.Linker.Steps { + public class GenerateProguardConfiguration : BaseStep + { + public GenerateProguardConfiguration (string outputFileName) + { + this.filename = outputFileName; + } + + string filename; + TextWriter writer; + + protected override void Process () + { + writer = File.CreateText (filename); + } + + protected override void EndProcess () + { + writer.Close (); + } + + protected override void ProcessAssembly (AssemblyDefinition assembly) + { + // Those assemblies that do not reference Mono.Android.dll (such as System.* + // assemblies and Mono.Android.dll itself) can be skipped. + // (Mono.Android.dll is special; android.jar is not part of classes.dex). + // + // FIXME: Those non-embedded jar bindings could visit here too, and they don't have to + // be part of proguard configuration. But they don't break (they will be NOTEd though). + if (!assembly.MainModule.AssemblyReferences.Any (r => r.Name == "Mono.Android")) + return; + + writer.WriteLine ("# ACW for " + assembly.Name.Name); + foreach (var type in assembly.MainModule.Types) + ProcessType (type); + } + + void ProcessType (TypeDefinition type) + { + foreach (var nt in type.NestedTypes) + ProcessType (nt); + if (!type.IsClass) + return; + var ra = type.CustomAttributes.FirstOrDefault (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute"); + if (ra == null) + return; + var jtype = ra.ConstructorArguments.First ().Value.ToString ().Replace ('/', '.'); + writer.WriteLine ("-keep class " + jtype); + writer.WriteLine ("-keepclassmembers class " + jtype + " {"); + foreach (var m in type.Methods) + ProcessMethod (m); + writer.WriteLine ("}"); + writer.WriteLine (); + } + + void ProcessMethod (MethodDefinition method) + { + var ra = method.CustomAttributes.FirstOrDefault (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute"); + if (ra == null) + return; + var jname = ra.ConstructorArguments.First ().Value.ToString (); + var jargs = ra.ConstructorArguments [1].Value.ToString (); + var pargs = jargs.StartsWith ("()") ? string.Empty : "***"; + // FIXME: do not preserve all overroads. + if (jname == ".ctor") + writer.WriteLine (" (...);", pargs); + else + writer.WriteLine (" *** {0}(...);", jname, pargs); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Linker.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Linker.cs new file mode 100644 index 00000000000..3f5996c63a0 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/Linker.cs @@ -0,0 +1,156 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Xml.XPath; +using Mono.Linker; +using Mono.Linker.Steps; +using Mono.Cecil.Mdb; +using Mono.Tuner; +using Mono.Cecil; +using MonoTouch.Tuner; + +namespace MonoDroid.Tuner +{ + class Linker + { + public static void Process (LinkerOptions options, out LinkContext context) + { + var pipeline = CreatePipeline (options); + + pipeline.PrependStep (new ResolveFromAssemblyStep (options.MainAssembly)); + if (options.RetainAssemblies != null) + foreach (var ass in options.RetainAssemblies) + pipeline.PrependStep (new ResolveFromAssemblyStep (ass)); + + context = CreateLinkContext (options, pipeline); + context.Resolver.AddSearchDirectory (options.OutputDirectory); + + Run (pipeline, context); + } + + static void Run (Pipeline pipeline, LinkContext context) + { + pipeline.Process (context); + } + + static LinkContext CreateLinkContext (LinkerOptions options, Pipeline pipeline) + { + var context = new LinkContext (pipeline, options.Resolver); + if (options.DumpDependencies) { + var prepareDependenciesDump = context.Annotations.GetType ().GetMethod ("PrepareDependenciesDump", new Type[] {}); + if (prepareDependenciesDump != null) + prepareDependenciesDump.Invoke (context.Annotations, null); + } + context.LogInternalExceptions = Xamarin.Android.Tasks.MonoAndroidHelper.LogInternalExceptions; + context.CoreAction = AssemblyAction.Link; + context.LinkSymbols = true; + context.SymbolReaderProvider = new MdbReaderProvider (); + context.SymbolWriterProvider = new MdbWriterProvider (); + context.OutputDirectory = options.OutputDirectory; + return context; + } + + static Pipeline CreatePipeline (LinkerOptions options) + { + var pipeline = new Pipeline (); + + if (options.LinkNone) { + pipeline.AppendStep (new FixAbstractMethodsStep ()); + pipeline.AppendStep (new OutputStep ()); + return pipeline; + } + + pipeline.AppendStep (new LoadReferencesStep ()); + + if (options.I18nAssemblies != I18nAssemblies.None) + pipeline.AppendStep (new LoadI18nAssemblies (options.I18nAssemblies)); + + pipeline.AppendStep (new BlacklistStep ()); + + foreach (var desc in options.LinkDescriptions) + pipeline.AppendStep (new ResolveFromXmlStep (new XPathDocument (desc))); + + pipeline.AppendStep (new CustomizeActions (options.LinkSdkOnly, options.SkippedAssemblies)); + + pipeline.AppendStep (new TypeMapStep ()); + + // monodroid tuner steps + pipeline.AppendStep (new SubStepDispatcher { + new ApplyPreserveAttribute (), + new PreserveExportedTypes (), + new RemoveSecurity (), + new MarkJavaObjects (), + new PreserveJavaExceptions (), + new PreserveJavaTypeRegistrations (), + new PreserveApplications (), + new RemoveAttributes (), + new PreserveDynamicTypes (), + new PreserveSoapHttpClients (), + new PreserveTypeConverters (), + new PreserveLinqExpressions (), + new PreserveRuntimeSerialization (), + }); + + pipeline.AppendStep (new PreserveCrypto ()); + pipeline.AppendStep (new PreserveCode ()); + + pipeline.AppendStep (new RemoveLibraryResourceZip ()); + pipeline.AppendStep (new RemoveResources (options.I18nAssemblies)); // remove collation tables + // end monodroid specific + + pipeline.AppendStep (new FixAbstractMethodsStep ()); + pipeline.AppendStep (new MonoDroidMarkStep ()); + pipeline.AppendStep (new SweepStep ()); + pipeline.AppendStep (new CleanStep ()); + // monodroid tuner steps + if (!string.IsNullOrWhiteSpace (options.ProguardConfiguration)) + pipeline.AppendStep (new GenerateProguardConfiguration (options.ProguardConfiguration)); + // end monodroid specific + pipeline.AppendStep (new RegenerateGuidStep ()); + pipeline.AppendStep (new OutputStep ()); + + return pipeline; + } + + static List ListAssemblies (LinkContext context) + { + var list = new List (); + foreach (var assembly in context.GetAssemblies ()) { + if (context.Annotations.GetAction (assembly) == AssemblyAction.Delete) + continue; + + list.Add (GetFullyQualifiedName (assembly)); + } + + return list; + } + + static string GetFullyQualifiedName (AssemblyDefinition assembly) + { + return assembly.MainModule.FullyQualifiedName; + } + + public static I18nAssemblies ParseI18nAssemblies (string i18n) + { + if (string.IsNullOrWhiteSpace (i18n)) + return I18nAssemblies.None; + + var assemblies = I18nAssemblies.None; + + foreach (var part in i18n.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)) { + var assembly = part.Trim (); + if (string.IsNullOrEmpty (assembly)) + continue; + + try { + assemblies |= (I18nAssemblies) Enum.Parse (typeof (I18nAssemblies), assembly, true); + } catch { + throw new FormatException ("Unknown value for i18n: " + assembly); + } + } + + return assemblies; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/LinkerOptions.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/LinkerOptions.cs new file mode 100644 index 00000000000..be092f7435f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/LinkerOptions.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Mono.Cecil; +using Mono.Linker; + +namespace MonoDroid.Tuner +{ + class LinkerOptions + { + public AssemblyDefinition MainAssembly { get; set; } + public IEnumerable RetainAssemblies { get; set; } + public string OutputDirectory { get; set; } + public bool LinkSdkOnly { get; set; } + public bool LinkNone { get; set; } + public string [] LinkDescriptions { get; set; } + public AssemblyResolver Resolver { get; set; } + public IEnumerable SkippedAssemblies { get; set; } + public I18nAssemblies I18nAssemblies { get; set; } + public string ProguardConfiguration { get; set; } + public bool DumpDependencies { get; set; } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MarkJavaObjects.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MarkJavaObjects.cs new file mode 100644 index 00000000000..e0ca761832a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MarkJavaObjects.cs @@ -0,0 +1,255 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Mono.Cecil; +using Mono.Tuner; +using Xamarin.Android.Tasks; + +namespace MonoDroid.Tuner { + + public class MarkJavaObjects : BaseSubStep { + Dictionary> module_types = new Dictionary> (); + + public override SubStepTargets Targets { + get { return SubStepTargets.Type; } + } + + public override void ProcessType (TypeDefinition type) + { + // If this isn't a JLO or IJavaObject implementer, + // then we don't need to MarkJavaObjects + if (!type.ImplementsIJavaObject ()) + return; + + PreserveJavaObjectImplementation (type); + + if (IsImplementor (type)) + PreserveImplementor (type); + + // If a user overrode a method, we need to preserve it, + // because it won't be referenced anywhere, but it will + // be called from Java + if (IsUserType (type) && type.HasMethods) { + foreach (var method in type.Methods.Where (m => m.Overrides != null)) + PreserveMethod (type, method); + } + } + + void PreserveJavaObjectImplementation (TypeDefinition type) + { + PreserveIntPtrConstructor (type); + PreserveAttributeSetConstructor (type); + PreserveAdapter (type); + PreserveInvoker (type); + } + + void PreserveAttributeSetConstructor (TypeDefinition type) + { + if (!type.HasMethods) + return; + + foreach (var constructor in GetAttributeSetConstructors (type)) + PreserveMethod (type, constructor); + } + + static IEnumerable GetAttributeSetConstructors (TypeDefinition type) + { + foreach (MethodDefinition constructor in type.Methods.Where (m => m.IsConstructor)) { + if (!constructor.HasParameters) + continue; + + var parameters = constructor.Parameters; + + if (parameters.Count < 2 || parameters.Count > 3) + continue; + + if (parameters [0].ParameterType.FullName != "Android.Content.Context") + continue; + + if (parameters [1].ParameterType.FullName != "Android.Util.IAttributeSet") + continue; + + if (parameters.Count == 3 && parameters [2].ParameterType.FullName != "System.Int32") + continue; + + yield return constructor; + } + } + + void PreserveIntPtrConstructor (TypeDefinition type) + { + var constructor = GetIntPtrConstructor (type); + + if (constructor != null) + PreserveMethod (type, constructor); + + var constructor2 = GetNewIntPtrConstructor (type); + + if (constructor2 != null) + PreserveMethod (type, constructor2); + } + + static MethodDefinition GetIntPtrConstructor (TypeDefinition type) + { + if (!type.HasMethods) + return null; + + foreach (MethodDefinition constructor in type.Methods.Where (m => m.IsConstructor)) { + if (!constructor.HasParameters) + continue; + + if (constructor.Parameters.Count != 1 || constructor.Parameters[0].ParameterType.FullName != "System.IntPtr") + continue; + + return constructor; + } + + return null; + } + + static MethodDefinition GetNewIntPtrConstructor (TypeDefinition type) + { + if (!type.HasMethods) + return null; + + foreach (MethodDefinition constructor in type.Methods.Where (m => m.IsConstructor)) { + if (!constructor.HasParameters) + continue; + + if (constructor.Parameters.Count != 2 || constructor.Parameters[0].ParameterType.FullName != "System.IntPtr" + || constructor.Parameters[1].ParameterType.FullName != "Android.Runtime.JniHandleOwnership") + continue; + + return constructor; + } + + return null; + } + + void PreserveMethod (TypeDefinition type, MethodDefinition method) + { + Annotations.AddPreservedMethod (type, method); + } + + void PreserveAdapter (TypeDefinition type) + { + var adapter = PreserveHelperType (type, "Adapter"); + + if (adapter == null || !adapter.HasMethods) + return; + + foreach (MethodDefinition method in adapter.Methods) { + if (method.Name != "GetObject") + continue; + + if (method.Parameters.Count != 2) + continue; + + PreserveMethod (type, method); + } + } + + void PreserveInvoker (TypeDefinition type) + { + var invoker = PreserveHelperType (type, "Invoker"); + if (invoker == null) + return; + + PreserveIntPtrConstructor (invoker); + } + + TypeDefinition PreserveHelperType (TypeDefinition type, string suffix) + { + var helper = GetHelperType (type, suffix); + if (helper != null) + PreserveConstructors (type, helper); + + return helper; + } + + TypeDefinition GetHelperType (TypeDefinition type, string suffix) + { + string fullname = type.FullName; + + if (type.HasGenericParameters) { + var pos = fullname.IndexOf ('`'); + if (pos == -1) + throw new ArgumentException (); + + fullname = fullname.Substring (0, pos) + suffix + fullname.Substring (pos); + } else + fullname = fullname + suffix; + + return FindType (type, fullname); + } + + // Keep a dictionary cache of all types in a module rather than + // looping through them on every lookup. + TypeDefinition FindType (TypeDefinition type, string fullname) + { + Dictionary types; + + if (!module_types.TryGetValue (type.Module, out types)) { + types = GetTypesInModule (type.Module); + module_types.Add (type.Module, types); + } + + TypeDefinition helper; + + if (types.TryGetValue (fullname, out helper)) + return helper; + + return null; + } + + static Dictionary GetTypesInModule (ModuleDefinition module) + { + var types = module.Types.ToDictionary (p => p.FullName); + + foreach (var t in module.Types) + AddNestedTypes (types, t); + + return types; + } + + static void AddNestedTypes (Dictionary types, TypeDefinition type) + { + if (!type.HasNestedTypes) + return; + + foreach (var t in type.NestedTypes) { + types.Add (t.FullName, t); + AddNestedTypes (types, t); + } + } + + void PreserveConstructors (TypeDefinition type, TypeDefinition helper) + { + if (!helper.HasMethods) + return; + + foreach (MethodDefinition ctor in helper.Methods.Where (m => m.IsConstructor)) + PreserveMethod (type, ctor); + } + + static bool IsImplementor (TypeDefinition type) + { + return type.Name.EndsWith ("Implementor") && type.Inherits ("Java.Lang.Object"); + } + + static bool IsUserType (TypeDefinition type) + { + return !MonoAndroidHelper.IsFrameworkAssembly (type.Module.Assembly.Name.Name + ".dll"); + } + + void PreserveImplementor (TypeDefinition type) + { + if (!type.HasMethods) + return; + + foreach (MethodDefinition method in type.Methods) + if (method.Name.EndsWith ("Handler")) + PreserveMethod (type, method); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs new file mode 100644 index 00000000000..b194bf8618d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidMarkStep.cs @@ -0,0 +1,170 @@ +using System; +using System.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Mono.Linker.Steps; + +namespace MonoDroid.Tuner +{ + class MonoDroidMarkStep : MarkStep + { + const string RegisterAttribute = "Android.Runtime.RegisterAttribute"; + + // If this is one of our infrastructure methods that has [Register], like: + // [Register ("hasWindowFocus", "()Z", "GetHasWindowFocusHandler")], + // we need to preserve the "GetHasWindowFocusHandler" method as well. + protected override void DoAdditionalMethodProcessing (MethodDefinition method) + { + string member; + + if (!TryGetRegisterMember (method, out member)) + return; + + PreserveRegisteredMethod (method.DeclaringType, member); + } + + protected override void DoAdditionalTypeProcessing (TypeDefinition type) + { + // If we are preserving a Mono.Android interface, + // preserve all members on the interface. + if (!type.IsInterface) + return; + + // Mono.Android interfaces will always inherit IJavaObject + if (!type.ImplementsIJavaObject ()) + return; + + foreach (MethodReference method in type.Methods) + MarkMethod (method); + + // We also need to preserve the interface adapter + string adapter; + + if (!TryGetRegisterMember (type, out adapter)) + return; + + // This is a fast path for finding the adapter type + var adapter_type = type.DeclaringType.GetNestedType (adapter); + + // It wasn't in the same containing type, look through the whole assembly + if (adapter_type == null) + adapter_type = type.Module.FindType (adapter); + + if (adapter_type == null) + return; + + MarkType (adapter_type); + + // We also need to preserve every member on the interface adapter + foreach (MethodReference m in adapter_type.Methods) + MarkMethod (m); + } + + private void PreserveRegisteredMethod (TypeDefinition type, string member) + { + var type_ptr = type; + var pos = member.IndexOf (':'); + + if (pos > 0) { + var type_name = member.Substring (pos + 1); + member = member.Substring (0, pos); + type_ptr = type_ptr.Module.Types.FirstOrDefault (t => t.FullName == type_name); + } + + if (type_ptr == null) + return; + + while (MarkNamedMethod (type, member) == 0 && type.BaseType != null) + type = type.BaseType.Resolve (); + } + + private static bool TryGetRegisterMember (ICustomAttributeProvider provider, out string method) + { + CustomAttribute register; + method = null; + + if (!TryGetRegisterAttribute (provider, out register)) + return false; + + if (register.ConstructorArguments.Count != 3) + return false; + + method = (string) register.ConstructorArguments [2].Value; + + if (string.IsNullOrEmpty (method)) + return false; + + return true; + } + + private static bool TryGetRegisterAttribute (ICustomAttributeProvider provider, out CustomAttribute register) + { + register = null; + + if (!provider.HasCustomAttributes) + return false; + + foreach (CustomAttribute attribute in provider.CustomAttributes) { + if (!IsRegisterAttribute (attribute)) + continue; + + register = attribute; + return true; + } + + return false; + } + + private static bool IsRegisterAttribute (CustomAttribute attribute) + { + var constructor = attribute.Constructor; + + if (constructor.DeclaringType.FullName != RegisterAttribute) + return false; + + if (!constructor.HasParameters) + return false; + + if (constructor.Parameters.Count != 3) + return false; + + return true; + } + + protected override TypeDefinition MarkType (TypeReference reference) + { + TypeDefinition type = base.MarkType (reference); + if (type == null) + return null; + + if (type.Module.Assembly.Name.Name == "System.Core") + ProcessSystemCore (type); + + return type; + } + + void ProcessSystemCore (TypeDefinition type) + { + switch (type.Namespace) { + case "System.Linq.Expressions": + switch (type.Name) { + case "LambdaExpression": + var expr_t = type.Module.GetType ("System.Linq.Expressions.Expression`1"); + if (expr_t != null) + MarkNamedMethod (expr_t, "Create"); + break; + } + break; + case "System.Linq.Expressions.Compiler": + switch (type.Name) { + case "LambdaCompiler": + MarkNamedMethod (type.Module.GetType ("System.Runtime.CompilerServices.RuntimeOps"), "Quote"); + break; + } + break; + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs new file mode 100644 index 00000000000..833c67c94f8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/MonoDroidProfile.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + class MonoDroidProfile : MobileProfile { + + protected override bool IsProduct (string assemblyName) + { + return assemblyName == "Mono.Android"; + } + + protected override bool IsSdk (string assemblyName) + { + return assemblyName.Equals ("Java.Interop", StringComparison.Ordinal) || base.IsSdk (assemblyName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs new file mode 100644 index 00000000000..20db776258e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveApplications.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + public class PreserveApplications : BaseSubStep { + + public override SubStepTargets Targets { + get { return SubStepTargets.Type + | SubStepTargets.Assembly; + } + } + + public override bool IsActiveFor (AssemblyDefinition assembly) + { + return Annotations.GetAction (assembly) == AssemblyAction.Link; + } + + public override void ProcessAssembly (AssemblyDefinition assembly) + { + ProcessAttributeProvider (assembly); + } + + public override void ProcessType (TypeDefinition type) + { + if (!type.Inherits ("Android.App.Application")) + return; + + ProcessAttributeProvider (type); + } + + void ProcessAttributeProvider (ICustomAttributeProvider provider) + { + if (!provider.HasCustomAttributes) + return; + + const string ApplicationAttribute = "Android.App.ApplicationAttribute"; + + foreach (CustomAttribute attribute in provider.CustomAttributes) + if (attribute.Constructor.DeclaringType.FullName == ApplicationAttribute) + PreserveApplicationAttribute (attribute); + } + + void PreserveApplicationAttribute (CustomAttribute attribute) + { + PreserveTypeProperty (attribute, "BackupAgent"); + PreserveTypeProperty (attribute, "ManageSpaceActivity"); + } + + void PreserveTypeProperty (CustomAttribute attribute, string property) + { + if (!attribute.HasProperties) + return; + + var type_ref = (TypeReference) attribute.Properties.First (p => p.Name == property).Argument.Value; + if (type_ref == null) + return; + + var type = type_ref.Resolve (); + if (type == null) + return; + + PreserveDefaultConstructor (type); + } + + void PreserveDefaultConstructor (TypeDefinition type) + { + if (!type.HasMethods) + return; + + foreach (MethodDefinition ctor in type.Methods.Where (t => t.IsConstructor)) { + if (!ctor.IsStatic && !ctor.HasParameters) { + PreserveMethod (type, ctor); + break; + } + } + } + + void PreserveMethod (TypeDefinition type, MethodDefinition method) + { + Annotations.AddPreservedMethod (type, method); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveCode.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveCode.cs new file mode 100644 index 00000000000..b54c84de76e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveCode.cs @@ -0,0 +1,76 @@ +using System; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoTouch.Tuner { + + public class PreserveCode : IStep { + + LinkContext context; + + public void Process (LinkContext context) + { + this.context = context; + + PreserveDictionaryConstructor (); + PreserveQueryableEnumerable (); + PreserveResourceSet (); + } + + void PreserveDictionaryConstructor () + { + AssemblyDefinition corlib; + if (!context.TryGetLinkedAssembly ("mscorlib", out corlib)) + return; + + var dictionary = corlib.MainModule.Types.FirstOrDefault (t => t.FullName == "System.Collections.Generic.Dictionary`2"); + if (dictionary == null || !dictionary.Methods.Any (m => m.IsConstructor)) + return; + + foreach (MethodDefinition ctor in dictionary.Methods.Where (m => m.IsConstructor)) + if (ctor.HasParameters && ctor.Parameters [0].ParameterType.FullName == "System.Int32") + context.Annotations.AddPreservedMethod (dictionary, ctor); + } + + void PreserveQueryableEnumerable () + { + AssemblyDefinition core; + if (!context.TryGetLinkedAssembly ("System.Core", out core)) + return; + + var enumerable = core.MainModule.Types.FirstOrDefault (t => t.FullName == "System.Linq.Enumerable"); + + var queryable_enumerable = core.MainModule.Types.FirstOrDefault (t => t.FullName == "System.Linq.QueryableEnumerable`1"); + + if (enumerable != null && queryable_enumerable != null) + foreach (MethodDefinition method in enumerable.Methods) + context.Annotations.AddPreservedMethod (queryable_enumerable, method); + + if (queryable_enumerable != null) + foreach (MethodDefinition method in queryable_enumerable.Methods.Where (m => m.IsConstructor)) + context.Annotations.AddPreservedMethod (queryable_enumerable, method); + } + + void PreserveResourceSet () + { + AssemblyDefinition corlib; + if (!context.TryGetLinkedAssembly ("mscorlib", out corlib)) + return; + + var resource_set = corlib.MainModule.Types.FirstOrDefault (t => t.FullName == "System.Resources.RuntimeResourceSet"); + if (resource_set == null || !resource_set.Methods.Any (m => m.IsConstructor)) + return; + + foreach (MethodDefinition ctor in resource_set.Methods.Where (m => m.IsConstructor)) + context.Annotations.AddPreservedMethod (resource_set, ctor); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveDynamicTypes.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveDynamicTypes.cs new file mode 100644 index 00000000000..577395e6ac0 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveDynamicTypes.cs @@ -0,0 +1,42 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Cecil; + +namespace Mono.Tuner { + + public class PreserveDynamicTypes : BaseSubStep { + + public override SubStepTargets Targets { + get { return SubStepTargets.Assembly ; } + } + + bool preserve_dynamic; + List saved = new List (); + + public override void ProcessAssembly (AssemblyDefinition assembly) + { + // Preserve dynamic dependencies only when Microsoft.CSharp is referenced. + switch (assembly.Name.Name) { + case "Microsoft.CSharp": + preserve_dynamic = true; + foreach (var ass in saved) + ResolveFromAssemblyStep.ProcessLibrary (context, ass); + ResolveFromAssemblyStep.ProcessLibrary (context, assembly); + break; + case "Mono.CSharp": + case "System.Core": + if (preserve_dynamic) + ResolveFromAssemblyStep.ProcessLibrary (context, assembly); + else + saved.Add (assembly); + break; + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveExportedTypes.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveExportedTypes.cs new file mode 100644 index 00000000000..afdb49d3baf --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveExportedTypes.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Cecil; + +namespace Mono.Tuner { + + public class PreserveExportedTypes : BaseSubStep { + + public override SubStepTargets Targets { + get { + return SubStepTargets.Field + | SubStepTargets.Method + | SubStepTargets.Property + ; + } + } + + public override bool IsActiveFor (AssemblyDefinition assembly) + { + return !Profile.IsSdkAssembly (assembly); + } + + public override void ProcessField (FieldDefinition field) + { + ProcessExports (field); + } + + public override void ProcessMethod (MethodDefinition method) + { + ProcessExports (method); + } + + public override void ProcessProperty (PropertyDefinition property) + { + ProcessExports (property.GetMethod); + ProcessExports (property.SetMethod); + } + + public override void ProcessEvent (EventDefinition @event) + { + } + + void ProcessExports (ICustomAttributeProvider provider) + { + if (provider == null) + return; + if (!provider.HasCustomAttributes) + return; + + var attributes = provider.CustomAttributes; + + for (int i = 0; i < attributes.Count; i++) { + var attribute = attributes [i]; + switch (attribute.Constructor.DeclaringType.FullName) { + case "Java.Interop.ExportAttribute": + Annotations.Mark (provider); + if (!attribute.HasProperties) + break; + var throwsAtt = attribute.Properties.FirstOrDefault (p => p.Name == "Throws"); + var thrownTypesArgs = throwsAtt.Argument.Value != null ? (CustomAttributeArgument []) throwsAtt.Argument.Value : null; + if (thrownTypesArgs != null) + foreach (var attArg in thrownTypesArgs) + Annotations.Mark (((TypeReference) attArg.Value).Resolve ()); + break; + case "Java.Interop.ExportFieldAttribute": + Annotations.Mark (provider); + break; + default: + continue; + } + if (provider is MemberReference) + Annotations.Mark (((MemberReference) provider).DeclaringType.Resolve ()); + if (provider is MethodDefinition) + Annotations.SetAction (((MethodDefinition) provider), MethodAction.ForceParse); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaExceptions.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaExceptions.cs new file mode 100644 index 00000000000..2356bdd4407 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaExceptions.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + public class PreserveJavaExceptions : BaseSubStep { + + public override SubStepTargets Targets { + get { return SubStepTargets.Type; } + } + + public override void ProcessType (TypeDefinition type) + { + if (type.IsJavaException ()) + PreserveJavaException (type); + } + + void PreserveJavaException (TypeDefinition type) + { + PreserveStringConstructor (type); + } + + void PreserveStringConstructor (TypeDefinition type) + { + var constructor = GetStringConstructor (type); + if (constructor == null) + return; + + PreserveMethod (type, constructor); + } + + MethodDefinition GetStringConstructor (TypeDefinition type) + { + if (!type.HasMethods) + return null; + + foreach (MethodDefinition constructor in type.Methods.Where (m => m.IsConstructor)) { + if (!constructor.HasParameters) + continue; + + if (constructor.Parameters.Count != 1 || constructor.Parameters [0].ParameterType.FullName != "System.String") + continue; + + return constructor; + } + + return null; + } + + void PreserveMethod (TypeDefinition type, MethodDefinition method) + { + Annotations.AddPreservedMethod (type, method); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaTypeRegistrations.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaTypeRegistrations.cs new file mode 100644 index 00000000000..7444aab5a1f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveJavaTypeRegistrations.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + public class PreserveJavaTypeRegistrations : BaseSubStep { + + public override SubStepTargets Targets { + get { return SubStepTargets.Type; } + } + + public override void ProcessType (TypeDefinition type) + { + if (type.FullName == "Java.Interop.__TypeRegistrations") + PreserveJavaTypeRegistration (type); + } + + void PreserveJavaTypeRegistration (TypeDefinition type) + { + Annotations.Mark (type); + Annotations.SetPreserve (type, TypePreserve.All); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveLinqExpressions.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveLinqExpressions.cs new file mode 100644 index 00000000000..0603e983ca1 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveLinqExpressions.cs @@ -0,0 +1,52 @@ +using System; +using System.Linq; + +using Mono.Linker; + +using Mono.Cecil; + +namespace Mono.Tuner { + + // WARNING: this link substep is placed after marking all required stuff. + public class PreserveLinqExpressions : BaseSubStep { + + public override SubStepTargets Targets { + get { return SubStepTargets.Type; } + } + + public override bool IsActiveFor (AssemblyDefinition assembly) + { + // scan all user assemblies even if it is not linked. + return !Xamarin.Android.Tasks.MonoAndroidHelper.IsFrameworkAssembly (assembly.Name.Name); + } + + bool already_marked; + + public override void ProcessType (TypeDefinition type) + { + if (already_marked) + return; + if (IsLinqProvider (type)) { + already_marked = true; + AssemblyDefinition core; + if (!context.TryGetLinkedAssembly ("System.Core", out core)) + return; + foreach (var t in core.MainModule.Types.Where (t => t.Namespace == "System.Linq" || t.Namespace == "System.Linq.Expressions")) + Annotations.SetPreserve (t, TypePreserve.All); + } + } + + bool IsLinqProvider (TypeDefinition type) + { + // skip unmarked types. + if (!Annotations.IsMarked (type)) + return false; + if (type.Namespace == "System.Linq") + return false; // we are not looking for default system types. + if (!type.HasInterfaces) + return false; + return type.Interfaces.Any (i => i.FullName == "System.Linq.IQueryProvider") + || type.Interfaces.Select (t => t.Resolve ()).Any (t => t != null && IsLinqProvider (t)); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveRuntimeSerialization.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveRuntimeSerialization.cs new file mode 100644 index 00000000000..86cbdb907d6 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveRuntimeSerialization.cs @@ -0,0 +1,98 @@ +using System; +using Mono.Tuner; +using Mono.Cecil; + +namespace MonoDroid.Tuner +{ + public class PreserveRuntimeSerialization : BaseSubStep + { + public override SubStepTargets Targets { + get { return SubStepTargets.Type; } + } + + public override bool IsActiveFor (Mono.Cecil.AssemblyDefinition assembly) + { + return assembly.Name.Name == "System.Runtime.Serialization"; + } + + public override void ProcessType (Mono.Cecil.TypeDefinition type) + { + switch (type.Namespace) { + case "System.Runtime.Serialization.Json": + switch (type.Name) { + case "JsonFormatWriterInterpreter": + TypeDefinition jwd = GetType ("System.Runtime.Serialization", "System.Runtime.Serialization.Json.JsonWriterDelegator"); + PreserveMethods (jwd); + break; + } + break; + case "System.Runtime.Serialization": + // MS referencesource use reflection to call the required methods to serialize each PrimitiveDataContract subclasses + // this goes thru XmlFormatGeneratorStatics and it's a better candidate (than PrimitiveDataContract) as there are other callers + switch (type.Name) { + case "XmlFormatGeneratorStatics": + TypeDefinition xwd = GetType ("System.Runtime.Serialization", "System.Runtime.Serialization.XmlWriterDelegator"); + PreserveMethods (xwd); + TypeDefinition xoswc = GetType ("System.Runtime.Serialization", "System.Runtime.Serialization.XmlObjectSerializerWriteContext"); + PreserveMethods (xoswc); + TypeDefinition xosrc = GetType ("System.Runtime.Serialization", "System.Runtime.Serialization.XmlObjectSerializerReadContext"); + PreserveMethods (xosrc); + TypeDefinition xrd = GetType ("System.Runtime.Serialization", "System.Runtime.Serialization.XmlReaderDelegator"); + PreserveMethods (xrd); + break; + case "CollectionDataContract": + // ensure the nested type, DictionaryEnumerator and GenericDictionaryEnumerator`2, can be created thru reflection + foreach (var nt in type.NestedTypes) + PreserveConstructors (nt); + break; + } + break; + } + } + + protected void PreserveConstructors (TypeDefinition type) + { + if ((type == null) || !type.HasMethods) + return; + foreach (MethodDefinition ctor in type.Methods) { + if (ctor.IsConstructor) + PreserveMethod (type, ctor); + } + } + + protected AssemblyDefinition GetAssembly (string assemblyName) + { + AssemblyDefinition ad; + context.TryGetLinkedAssembly (assemblyName, out ad); + return ad; + } + + protected TypeDefinition GetType (string assemblyName, string typeName) + { + AssemblyDefinition ad = GetAssembly (assemblyName); + return ad == null ? null : GetType (ad, typeName); + } + + protected TypeDefinition GetType (AssemblyDefinition assembly, string typeName) + { + return assembly.MainModule.GetType (typeName); + } + + void PreserveMethods (TypeDefinition type) + { + if (type == null) + return; + + if (!type.HasMethods) + return; + + foreach (MethodDefinition method in type.Methods) + PreserveMethod (type, method); + } + + void PreserveMethod (TypeDefinition type, MethodDefinition method) + { + Annotations.AddPreservedMethod (type, method); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveTypeConverters.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveTypeConverters.cs new file mode 100644 index 00000000000..5aebdeb809e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/PreserveTypeConverters.cs @@ -0,0 +1,46 @@ +using System; +using System.Linq; + +using Mono.Linker; + +using Mono.Cecil; + +namespace Mono.Tuner { + + public class PreserveTypeConverters : BaseSubStep { + + public override SubStepTargets Targets { + get { return SubStepTargets.Type; } + } + + public override bool IsActiveFor (AssemblyDefinition assembly) + { + return Annotations.GetAction (assembly) == AssemblyAction.Link; + } + + public override void ProcessType (TypeDefinition type) + { + if (IsTypeConverter (type)) + PreserveTypeConverter (type); + } + + void PreserveTypeConverter (TypeDefinition type) + { + if (!type.HasMethods) + return; + + foreach (MethodDefinition ctor in type.Methods.Where (m => m.IsConstructor)) { + // We only care about ctors with 0 or 1 params. + if (ctor.HasParameters && ctor.Parameters.Count > 1) + continue; + + Annotations.AddPreservedMethod (type, ctor); + } + } + + static bool IsTypeConverter (TypeDefinition type) + { + return type.Inherits ("System.ComponentModel", "TypeConverter"); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveAttributes.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveAttributes.cs new file mode 100644 index 00000000000..ae5045ea7c5 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveAttributes.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + public class RemoveAttributes : RemoveAttributesBase { + + protected virtual bool DebugBuild { + get { return context.LinkSymbols; } + } + + protected override bool IsRemovedAttribute (CustomAttribute attribute) + { + // note: this also avoid calling FullName (which allocates a string) + var attr_type = attribute.Constructor.DeclaringType; + switch (attr_type.Name) { + case "ObsoleteAttribute": + // System.Mono*Attribute from mono/mcs/build/common/MonoTODOAttribute.cs + case "MonoDocumentationNoteAttribute": + case "MonoExtensionAttribute": + case "MonoInternalNoteAttribute": + case "MonoLimitationAttribute": + case "MonoNotSupportedAttribute": + case "MonoTODOAttribute": + return attr_type.Namespace == "System"; + case "MonoFIXAttribute": + return attr_type.Namespace == "System.Xml"; + // remove debugging-related attributes if we're not linking symbols (i.e. we're building release builds) + case "DebuggableAttribute": + case "DebuggerBrowsableAttribute": + case "DebuggerDisplayAttribute": + case "DebuggerHiddenAttribute": + case "DebuggerNonUserCodeAttribute": + case "DebuggerStepperBoundaryAttribute": + case "DebuggerStepThroughAttribute": + case "DebuggerTypeProxyAttribute": + case "DebuggerVisualizerAttribute": + return !DebugBuild && attr_type.Namespace == "System.Diagnostics"; + default: + return false; + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveLibraryResourceZip.cs b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveLibraryResourceZip.cs new file mode 100644 index 00000000000..03cb6a2a303 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/RemoveLibraryResourceZip.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +using Mono.Linker; +using Mono.Linker.Steps; + +using Mono.Tuner; +using Mobile.Tuner; + +using Mono.Cecil; + +namespace MonoDroid.Tuner { + + public class RemoveLibraryResourceZip : BaseStep { + protected override void ProcessAssembly (AssemblyDefinition assembly) + { + foreach (var mod in assembly.Modules) { + var lres = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidLibraryProjects__.zip"); + if (lres != null) + mod.Resources.Remove (lres); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/Mono.Android.xml b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/Mono.Android.xml new file mode 100644 index 00000000000..c6da83bffdf --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/Mono.Android.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/OpenTK.xml b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/OpenTK.xml new file mode 100644 index 00000000000..9628ddfb3d7 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/OpenTK.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.Data.Services.Client.xml b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.Data.Services.Client.xml new file mode 100644 index 00000000000..680504f600a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.Data.Services.Client.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.ServiceModel.xml b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.ServiceModel.xml new file mode 100644 index 00000000000..122a4f7d7db --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.ServiceModel.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.xml b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.xml new file mode 100644 index 00000000000..d94321536a4 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/System.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/mscorlib.xml b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/mscorlib.xml new file mode 100644 index 00000000000..29c7037964b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Linker/PreserveLists/mscorlib.xml @@ -0,0 +1,405 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/ActivityAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/ActivityAttribute.Partial.cs new file mode 100644 index 00000000000..8fbf4abef98 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/ActivityAttribute.Partial.cs @@ -0,0 +1,192 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +using Android.Content.PM; +using Android.Views; + +namespace Android.App { + + partial class ActivityAttribute { + + bool _AllowEmbedded; + bool _HardwareAccelerated; + string _ParentActivity; + LayoutDirection _LayoutDirection; + UiOptions _UiOptions; + bool _Immersive; + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("activity") { + { + "AllowEmbedded", + "allowEmbedded", + self => self._AllowEmbedded, + (self, value) => self._AllowEmbedded = (bool) value + }, { + "AllowTaskReparenting", + "allowTaskReparenting", + self => self.AllowTaskReparenting, + (self, value) => self.AllowTaskReparenting = (bool) value + }, { + "AlwaysRetainTaskState", + "alwaysRetainTaskState", + self => self.AlwaysRetainTaskState, + (self, value) => self.AlwaysRetainTaskState = (bool) value + }, { + "ClearTaskOnLaunch", + "clearTaskOnLaunch", + self => self.ClearTaskOnLaunch, + (self, value) => self.ClearTaskOnLaunch = (bool) value + }, { + "ConfigurationChanges", + "configChanges", + self => self.ConfigurationChanges, + (self, value) => self.ConfigurationChanges = (ConfigChanges) value + }, { + "Enabled", + "enabled", + self => self.Enabled, + (self, value) => self.Enabled = (bool) value + }, { + "ExcludeFromRecents", + "excludeFromRecents", + self => self.ExcludeFromRecents, + (self, value) => self.ExcludeFromRecents = (bool) value + }, { + "Exported", + "exported", + self => self.Exported, + (self, value) => self.Exported = (bool) value + }, { + "FinishOnTaskLaunch", + "finishOnTaskLaunch", + self => self.FinishOnTaskLaunch, + (self, value) => self.FinishOnTaskLaunch = (bool) value + }, { + "HardwareAccelerated", + "hardwareAccelerated", + self => self._HardwareAccelerated, + (self, value) => self._HardwareAccelerated = (bool) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "Immersive", + "immersive", + self => self._Immersive, + (self, value) => self._Immersive = (bool) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "LaunchMode", + "launchMode", + self => self.LaunchMode, + (self, value) => self.LaunchMode = (LaunchMode) value + }, { + "LayoutDirection", + "layoutDirection", + self => self._LayoutDirection, + (self, value) => self._LayoutDirection = (LayoutDirection) value + }, { + "MainLauncher", + null, + null, + (self, value) => self.MainLauncher = (bool) value + }, { + "MultiProcess", + "multiprocess", + self => self.MultiProcess, + (self, value) => self.MultiProcess = (bool) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "NoHistory", + "noHistory", + self => self.NoHistory, + (self, value) => self.NoHistory = (bool) value + }, { + "ParentActivity", + "parentActivityName", + self => self._ParentActivity, + (self, value) => self._ParentActivity = (string) value, + typeof (Type) + }, { + "Permission", + "permission", + self => self.Permission, + (self, value) => self.Permission = (string) value + }, { + "Process", + "process", + self => self.Process, + (self, value) => self.Process = (string) value + }, { + "ScreenOrientation", + "screenOrientation", + self => self.ScreenOrientation, + (self, value) => self.ScreenOrientation = (ScreenOrientation) value + }, { + "StateNotNeeded", + "stateNotNeeded", + self => self.StateNotNeeded, + (self, value) => self.StateNotNeeded = (bool) value + }, { + "TaskAffinity", + "taskAffinity", + self => self.TaskAffinity, + (self, value) => self.TaskAffinity = (string) value + }, { + "Theme", + "theme", + self => self.Theme, + (self, value) => self.Theme = (string) value + }, { + "UiOptions", + "uiOptions", + self => self._UiOptions, + (self, value) => self._UiOptions = (UiOptions) value + }, { + "WindowSoftInputMode", + "windowSoftInputMode", + self => self.WindowSoftInputMode, + (self, value) => self.WindowSoftInputMode = (SoftInput) value + }, + }; + + TypeDefinition type; + ICollection specified; + + public static ActivityAttribute FromTypeDefinition (TypeDefinition type) + { + CustomAttribute attr = type.GetCustomAttributes ("Android.App.ActivityAttribute") + .SingleOrDefault (); + if (attr == null) + return null; + ActivityAttribute self = new ActivityAttribute () { + type = type, + }; + self.specified = mapping.Load (self, attr); + return self; + } + + internal XElement ToElement (IAssemblyResolver resolver, string packageName, int targetSdkVersion) + { + return mapping.ToElement (this, specified, packageName, type, resolver, targetSdkVersion); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs new file mode 100644 index 00000000000..d66df95abc6 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/ApplicationAttribute.Partial.cs @@ -0,0 +1,232 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +using Mono.Cecil; + +using MonoDroid.Utils; + +using Java.Interop.Tools.Cecil; +using Java.Interop.Tools.TypeNameMappings; + +using Xamarin.Android.Manifest; + +using Android.Content.PM; + +namespace Android.App { + + partial class ApplicationAttribute { + + string _BackupAgent; + string _Banner; + string _Logo; + string _ManageSpaceActivity; + string _RequiredAccountType; + string _RestrictedAccountType; + bool _HardwareAccelerated; + bool _ExtractNativeLibs; + bool _FullBackupContent; + bool _LargeHeap; + UiOptions _UiOptions; + bool _SupportsRtl; + bool _VMSafeMode; + ICustomAttributeProvider provider; + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("application") { + { + "AllowBackup", + "allowBackup", + self => self.AllowBackup, + (self, value) => self.AllowBackup = (bool) value + }, { + "AllowClearUserData", + "allowClearUserData", + self => self.AllowClearUserData, + (self, value) => self.AllowClearUserData = (bool) value + }, { + "AllowTaskReparenting", + "allowTaskReparenting", + self => self.AllowTaskReparenting, + (self, value) => self.AllowTaskReparenting = (bool) value + }, { + "BackupAgent", + "backupAgent", + (self, value) => self._BackupAgent = (string) value, + (self, p, r) => { + var typeDef = ManifestDocumentElement.ResolveType (self._BackupAgent, p, r); + + if (!typeDef.IsSubclassOf ("Android.App.Backup.BackupAgent")) + throw new InvalidOperationException ( + string.Format ("The Type '{0}', referenced by the Android.App.ApplicationAttribute.BackupAgent property, must be a subclass of the type Android.App.Backup.BackupAgent.", + typeDef.FullName)); + + return ManifestDocumentElement.ToString (typeDef); + } + }, { + "Banner", + "banner", + self => self._Banner, + (self, value) => self._Banner = (string) value + }, { + "Debuggable", + "debuggable", + self => self.Debuggable, + (self, value) => self.Debuggable = (bool) value + }, { + "Description", + "description", + self => self.Description, + (self, value) => self.Description = (string) value + }, { + "Enabled", + "enabled", + self => self.Enabled, + (self, value) => self.Enabled = (bool) value + }, { + "ExtractNativeLibs", + "extractNativeLibs", + self => self._ExtractNativeLibs, + (self, value) => self._ExtractNativeLibs = (bool) value + }, { + "FullBackupContent", + "fullBackupContent", + self => self._FullBackupContent, + (self, value) => self._FullBackupContent = (bool) value + }, { + "HardwareAccelerated", + "hardwareAccelerated", + self => self._HardwareAccelerated, + (self, value) => self._HardwareAccelerated = (bool) value + }, { + "HasCode", + "hasCode", + self => self.HasCode, + (self, value) => self.HasCode = (bool) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "KillAfterRestore", + "killAfterRestore", + self => self.KillAfterRestore, + (self, value) => self.KillAfterRestore = (bool) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "LargeHeap", + "largeHeap", + self => self._LargeHeap, + (self, value) => self._LargeHeap = (bool) value + }, { + "Logo", + "logo", + self => self._Logo, + (self, value) => self._Logo = (string) value + }, { + "ManageSpaceActivity", + "manageSpaceActivity", + self => self._ManageSpaceActivity, + (self, value) => self._ManageSpaceActivity = (string) value, + typeof (Type) + }, { + "Name", + "name", + (self, value) => self.Name = (string) value, + ToNameAttribute + }, { + "Permission", + "permission", + self => self.Permission, + (self, value) => self.Permission = (string) value + }, { + "Persistent", + "persistent", + self => self.Persistent, + (self, value) => self.Persistent = (bool) value + }, { + "Process", + "process", + self => self.Process, + (self, value) => self.Process = (string) value + }, { + "RequiredAccountType", + "requiredAccountType", + self => self._RequiredAccountType, + (self, value) => self._RequiredAccountType = (string) value + }, { + "RestoreAnyVersion", + "restoreAnyVersion", + self => self.RestoreAnyVersion, + (self, value) => self.RestoreAnyVersion = (bool) value + }, { + "RestrictedAccountType", + "restrictedAccountType", + self => self._RestrictedAccountType, + (self, value) => self._RestrictedAccountType = (string) value + }, { + "SupportsRtl", + "supportsRtl", + self => self._SupportsRtl, + (self, value) => self._SupportsRtl = (bool) value + }, { + "TaskAffinity", + "taskAffinity", + self => self.TaskAffinity, + (self, value) => self.TaskAffinity = (string) value + }, { + "Theme", + "theme", + self => self.Theme, + (self, value) => self.Theme = (string) value + }, { + "UiOptions", + "uiOptions", + self => self._UiOptions, + (self, value) => self._UiOptions = (UiOptions) value + }, { + "VMSafeMode", + "vmSafeMode", + self => self._VMSafeMode, + (self, value) => self._VMSafeMode = (bool) value + }, + }; + + ICollection specified; + + public static ApplicationAttribute FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + CustomAttribute attr = provider.GetCustomAttributes ("Android.App.ApplicationAttribute") + .SingleOrDefault (); + if (attr == null) + return null; + ApplicationAttribute self = new ApplicationAttribute () { + provider = provider, + }; + self.specified = mapping.Load (self, attr); + if (provider is TypeDefinition) { + self.specified.Add ("Name"); + } + return self; + } + + internal XElement ToElement (IAssemblyResolver resolver, string packageName) + { + return mapping.ToElement (this, specified, packageName, provider, resolver); + } + + static string ToNameAttribute (ApplicationAttribute self) + { + var type = self.provider as TypeDefinition; + if (string.IsNullOrEmpty (self.Name) && type != null) + return JniType.ToJniName (type).Replace ('/', '.'); + + return self.Name; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/BroadcastReceiverAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/BroadcastReceiverAttribute.Partial.cs new file mode 100644 index 00000000000..24d4bfaeeac --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/BroadcastReceiverAttribute.Partial.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.Content { + + partial class BroadcastReceiverAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("receiver") { + { + "Enabled", + "enabled", + self => self.Enabled, + (self, value) => self.Enabled = (bool) value + }, { + "Exported", + "exported", + self => self.Exported, + (self, value) => self.Exported = (bool) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "Permission", + "permission", + self => self.Permission, + (self, value) => self.Permission = (string) value + }, { + "Process", + "process", + self => self.Process, + (self, value) => self.Process = (string) value + }, + }; + + + ICollection specified; + + public static BroadcastReceiverAttribute FromTypeDefinition (TypeDefinition type) + { + CustomAttribute attr = type.GetCustomAttributes ("Android.Content.BroadcastReceiverAttribute") + .SingleOrDefault (); + if (attr == null) + return null; + var self = new BroadcastReceiverAttribute (); + self.specified = mapping.Load (self, attr); + return self; + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/ContentProviderAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/ContentProviderAttribute.Partial.cs new file mode 100644 index 00000000000..386eb14f51b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/ContentProviderAttribute.Partial.cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.Content { + + partial class ContentProviderAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("provider") { + { + "Authorities", + "authorities", + (self, value) => self.Authorities = ToStringArray (value), + self => string.Join (";", self.Authorities) + }, { + "Enabled", + "enabled", + self => self.Enabled, + (self, value) => self.Enabled = (bool) value + }, { + "Exported", + "exported", + self => self.Exported, + (self, value) => self.Exported = (bool) value + }, { + "GrantUriPermissions", + "grantUriPermissions", + self => self.GrantUriPermissions, + (self, value) => self.GrantUriPermissions = (bool) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "InitOrder", + "initOrder", + self => self.InitOrder, + (self, value) => self.InitOrder = (int) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "MultiProcess", + "multiprocess", + self => self.MultiProcess, + (self, value) => self.MultiProcess = (bool) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "Permission", + "permission", + self => self.Permission, + (self, value) => self.Permission = (string) value + }, { + "Process", + "process", + self => self.Process, + (self, value) => self.Process = (string) value + }, { + "ReadPermission", + "readPermission", + self => self.ReadPermission, + (self, value) => self.ReadPermission = (string) value + }, { + "Syncable", + "syncable", + self => self.Syncable, + (self, value) => self.Syncable = (bool) value + }, { + "WritePermission", + "writePermission", + self => self.WritePermission, + (self, value) => self.WritePermission = (string) value + }, + }; + + static string[] ToStringArray (object value) + { + var values = (CustomAttributeArgument []) value; + return values.Select (v => (string) v.Value).ToArray (); + } + + ICollection specified; + + public static ContentProviderAttribute FromTypeDefinition (TypeDefinition type) + { + CustomAttribute attr = type.GetCustomAttributes ("Android.Content.ContentProviderAttribute") + .SingleOrDefault (); + if (attr == null) + return null; + var self = new ContentProviderAttribute (ToStringArray (attr.ConstructorArguments [0].Value)); + self.specified = mapping.Load (self, attr); + self.specified.Add ("Authorities"); + return self; + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/GrantUriPermissionAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/GrantUriPermissionAttribute.Partial.cs new file mode 100644 index 00000000000..bee41a98070 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/GrantUriPermissionAttribute.Partial.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.Content { + + partial class GrantUriPermissionAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("grant-uri-permission") { + { + "Path", + "path", + self => self.Path, + (self, value) => self.Path = (string) value + }, { + "PathPattern", + "pathPattern", + self => self.PathPattern, + (self, value) => self.PathPattern = (string) value + }, { + "PathPrefix", + "pathPrefix", + self => self.PathPrefix, + (self, value) => self.PathPrefix = (string) value + }, + }; + + ICollection specified; + + public static IEnumerable FromTypeDefinition (TypeDefinition type) + { + IEnumerable attrs = type.GetCustomAttributes ("Android.Content.GrantUriPermissionAttribute"); + if (!attrs.Any ()) + yield break; + foreach (CustomAttribute attr in attrs) { + var self = new GrantUriPermissionAttribute (); + self.specified = mapping.Load (self, attr); + yield return self; + } + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs new file mode 100644 index 00000000000..2cda53e0bd5 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/InstrumentationAttribute.Partial.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.App { + + partial class InstrumentationAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("instrumentation") { + { + "FunctionalTest", + "functionalTest", + self => self.FunctionalTest, + (self, value) => self.FunctionalTest = (bool) value + }, { + "HandleProfiling", + "handleProfiling", + self => self.HandleProfiling, + (self, value) => self.HandleProfiling = (bool) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "TargetPackage", + "targetPackage", + self => self.TargetPackage, + (self, value) => self.TargetPackage = (string) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + foreach (CustomAttribute attr in provider.GetCustomAttributes ("Android.App.InstrumentationAttribute")) { + InstrumentationAttribute self = new InstrumentationAttribute (); + self.specified = mapping.Load (self, attr); + yield return self; + } + } + + public void SetTargetPackage (string package) + { + TargetPackage = package; + specified.Add ("TargetPackage"); + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/IntentFilterAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/IntentFilterAttribute.Partial.cs new file mode 100644 index 00000000000..260b291eec8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/IntentFilterAttribute.Partial.cs @@ -0,0 +1,153 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; +using Monodroid; +using MonoDroid.Tuner; + +using Java.Interop.Tools.Cecil; + +namespace Android.App { + + partial class IntentFilterAttribute { + bool _AutoVerify; + + static readonly Dictionary attributeMappings = new Dictionary () { + { "Icon", "icon" }, + { "Label", "label" }, + { "Priority", "priority" }, + { "DataHost", "host" }, + { "DataMimeType", "mimeType" }, + { "DataPath", "path" }, + { "DataPathPattern", "pathPattern" }, + { "DataPathPrefix", "pathPrefix" }, + { "DataPort", "port" }, + { "DataScheme", "scheme" }, + { "AutoVerify", "autoVerify" }, + }; + + static readonly Dictionary> setters = new Dictionary> () { + { "Icon", (self, value) => self.Icon = (string) value }, + { "Label", (self, value) => self.Label = (string) value }, + { "Priority", (self, value) => self.Priority = (int) value }, + { "Categories", (self, value) => self.Categories = ToStringArray (value) }, + { "DataHost", (self, value) => self.DataHost = (string) value }, + { "DataMimeType", (self, value) => self.DataMimeType = (string) value }, + { "DataPath", (self, value) => self.DataPath = (string) value }, + { "DataPathPattern", (self, value) => self.DataPathPattern = (string) value }, + { "DataPathPrefix", (self, value) => self.DataPathPrefix = (string) value }, + { "DataPort", (self, value) => self.DataPort = (string) value }, + { "DataScheme", (self, value) => self.DataScheme = (string) value }, + { "DataHosts", (self, value) => self.DataHosts = ToStringArray (value) }, + { "DataMimeTypes", (self, value) => self.DataMimeTypes = ToStringArray (value) }, + { "DataPaths", (self, value) => self.DataPaths = ToStringArray (value) }, + { "DataPathPatterns", (self, value) => self.DataPathPatterns= ToStringArray (value) }, + { "DataPathPrefixes", (self, value) => self.DataPathPrefixes= ToStringArray (value) }, + { "DataPorts", (self, value) => self.DataPorts = ToStringArray (value) }, + { "DataSchemes", (self, value) => self.DataSchemes = ToStringArray (value) }, + { "AutoVerify", (self, value) => self._AutoVerify = (bool) value }, + }; + + static string[] ToStringArray (object value) + { + var values = (CustomAttributeArgument []) value; + return values.Select (v => (string) v.Value).ToArray (); + } + + HashSet specified = new HashSet (); + + public static IEnumerable FromTypeDefinition (TypeDefinition type) + { + IEnumerable attrs = type.GetCustomAttributes ("Android.App.IntentFilterAttribute"); + if (!attrs.Any ()) + yield break; + foreach (CustomAttribute attr in attrs) { + var self = new IntentFilterAttribute (ToStringArray (attr.ConstructorArguments [0].Value)); + foreach (var e in attr.Properties) { + self.specified.Add (e.Name); + setters [e.Name] (self, e.Argument.GetSettableValue ()); + } + yield return self; + } + } + + string ReplacePackage (string s, string packageName) + { + return s != null ? s.Replace ("@PACKAGE_NAME@", packageName) : null; + } + + public XElement ToElement (string packageName) + { + var r = new XElement ("intent-filter", + ToAttribute ("Icon", Icon), + ToAttribute ("Label", ReplacePackage (Label, packageName)), + ToAttribute ("Priority", Priority), + ToAttribute ("AutoVerify", _AutoVerify), + Actions.Select (a => new XElement ("action", new XAttribute (android + "name", ReplacePackage (a, packageName)))), + (Categories ?? new string[0]).Select (c => new XElement ("category", new XAttribute (android + "name", ReplacePackage (c, packageName)))), + GetData (packageName)); + AndroidResource.UpdateXmlResource (r); + return r; + } + + static readonly XNamespace android = "http://schemas.android.com/apk/res/android"; + + XAttribute ToAttribute (string name, bool value) + { + if (!specified.Contains (name)) + return null; + return new XAttribute (android + attributeMappings [name], value ? "true" : "false"); + } + + XAttribute ToAttribute (string name, int value) + { + if (!specified.Contains (name)) + return null; + return new XAttribute (android + attributeMappings [name], value); + } + + XAttribute ToAttribute (string name, string value) + { + if (value == null) + return null; + return new XAttribute (android + attributeMappings [name], value); + } + + IEnumerable GetData (string packageName) + { + Func toHost = v => ToAttribute ("DataHost", ReplacePackage (v, packageName)); + Func toMimeType = v => ToAttribute ("DataMimeType", ReplacePackage (v, packageName)); + Func toPath = v => ToAttribute ("DataPath", ReplacePackage (v, packageName)); + Func toPathPattern = v => ToAttribute ("DataPathPattern", ReplacePackage (v, packageName)); + Func toPathPrefix = v => ToAttribute ("DataPathPrefix", ReplacePackage (v, packageName)); + Func toPort = v => ToAttribute ("DataPort", ReplacePackage (v, packageName)); + Func toScheme = v => ToAttribute ("DataScheme", ReplacePackage (v, packageName)); + Func, string, XElement> toData = (f, s) => string.IsNullOrEmpty (s) ? null : new XElement ("data", f (s)); + var empty = new string [0]; + var dataList = Enumerable.Empty () + .Concat ((DataHosts ?? empty).Select (p => toData (toHost, p))) + .Concat ((DataMimeTypes ?? empty).Select (p => toData (toMimeType, p))) + .Concat ((DataPaths ?? empty).Select (p => toData (toPath, p))) + .Concat ((DataPathPatterns ?? empty).Select (p => toData (toPathPattern, p))) + .Concat ((DataPathPrefixes ?? empty).Select (p => toData (toPathPrefix, p))) + .Concat ((DataPorts ?? empty).Select (p => toData (toPort, p))) + .Concat ((DataSchemes ?? empty).Select (p => toData (toScheme, p))); + if (string.IsNullOrEmpty (DataHost) && string.IsNullOrEmpty (DataMimeType) && + string.IsNullOrEmpty (DataPath) && string.IsNullOrEmpty (DataPathPattern) && string.IsNullOrEmpty (DataPathPrefix) && + string.IsNullOrEmpty (DataPort) && string.IsNullOrEmpty (DataScheme) && + !dataList.Any ()) + return null; + return new XElement [] { + toData (toHost, DataHost), + toData (toMimeType, DataMimeType), + toData (toPath, DataPath), + toData (toPathPattern, DataPathPattern), + toData (toPathPrefix, DataPathPrefix), + toData (toPort, DataPort), + toData (toScheme, DataScheme) } + .Concat (dataList).Where (x => x != null); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/MetaDataAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/MetaDataAttribute.Partial.cs new file mode 100644 index 00000000000..16755af4e7d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/MetaDataAttribute.Partial.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.App { + + partial class MetaDataAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("meta-data") { + { + "Name", + "name", + self => self.Name, + null + }, { + "Resource", + "resource", + self => self.Resource, + (self, value) => self.Resource = (string) value + }, { + "Value", + "value", + self => self.Value, + (self, value) => self.Value = (string) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider type) + { + IEnumerable attrs = type.GetCustomAttributes ("Android.App.MetaDataAttribute"); + if (!attrs.Any ()) + yield break; + foreach (CustomAttribute attr in attrs) { + var self = new MetaDataAttribute ((string) attr.ConstructorArguments [0].Value); + self.specified = mapping.Load (self, attr); + self.specified.Add ("Name"); + yield return self; + } + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs new file mode 100644 index 00000000000..e791dfd3f30 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionAttribute.Partial.cs @@ -0,0 +1,94 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +using Mono.Cecil; + +using MonoDroid.Utils; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +using Android.Content.PM; + +namespace Android.App { + + partial class PermissionAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("permission") { + { + "Description", + "description", + self => self.Description, + (self, value) => self.Description = (string) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "PermissionGroup", + "permissionGroup", + self => self.PermissionGroup, + (self, value) => self.PermissionGroup = (string) value + }, { + "ProtectionLevel", + "protectionLevel", + self => self.ProtectionLevel, + (self, value) => self.ProtectionLevel = (Protection) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.PermissionAttribute"); + foreach (var attr in attrs) { + PermissionAttribute self = new PermissionAttribute (); + + self.specified = mapping.Load (self, attr); + + yield return self; + } + } + + internal XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + + internal class PermissionAttributeComparer : IEqualityComparer + { + #region IEqualityComparer Members + public bool Equals (PermissionAttribute x, PermissionAttribute y) + { + return + x.Description == y.Description && + x.Icon == y.Icon && + x.Label == y.Label && + x.Name == y.Name && + x.PermissionGroup == y.PermissionGroup && + x.ProtectionLevel == y.ProtectionLevel; + } + + public int GetHashCode (PermissionAttribute obj) + { + return (obj.Name ?? String.Empty).GetHashCode (); + } + #endregion + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs new file mode 100644 index 00000000000..a12c8b82c23 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionGroupAttribute.Partial.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +using Mono.Cecil; + +using MonoDroid.Utils; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +using Android.Content.PM; + +namespace Android.App { + + partial class PermissionGroupAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("permission") { + { + "Description", + "description", + self => self.Description, + (self, value) => self.Description = (string) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.PermissionGroupAttribute"); + foreach (var attr in attrs) { + PermissionGroupAttribute self = new PermissionGroupAttribute (); + + self.specified = mapping.Load (self, attr); + + yield return self; + } + } + + internal XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + + internal class PermissionGroupAttributeComparer : IEqualityComparer + { + #region IEqualityComparer Members + public bool Equals (PermissionGroupAttribute x, PermissionGroupAttribute y) + { + return + x.Description == y.Description && + x.Icon == y.Icon && + x.Label == y.Label && + x.Name == y.Name; + } + + public int GetHashCode (PermissionGroupAttribute obj) + { + return (obj.Name ?? String.Empty).GetHashCode (); + } + #endregion + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs new file mode 100644 index 00000000000..b7122205512 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/PermissionTreeAttribute.Partial.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +using Mono.Cecil; + +using MonoDroid.Utils; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +using Android.Content.PM; + +namespace Android.App { + + partial class PermissionTreeAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("permission") { + { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.PermissionTreeAttribute"); + foreach (var attr in attrs) { + PermissionTreeAttribute self = new PermissionTreeAttribute (); + + self.specified = mapping.Load (self, attr); + + yield return self; + } + } + + internal XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + + internal class PermissionTreeAttributeComparer : IEqualityComparer + { + #region IEqualityComparer Members + public bool Equals (PermissionTreeAttribute x, PermissionTreeAttribute y) + { + return + x.Icon == y.Icon && + x.Label == y.Label && + x.Name == y.Name; + } + + public int GetHashCode (PermissionTreeAttribute obj) + { + return (obj.Name ?? String.Empty).GetHashCode (); + } + #endregion + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/ServiceAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/ServiceAttribute.Partial.cs new file mode 100644 index 00000000000..668bb12040e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/ServiceAttribute.Partial.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.App { + + partial class ServiceAttribute { + + bool _IsolatedProcess; + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("service") { + { + "Enabled", + "enabled", + self => self.Enabled, + (self, value) => self.Enabled = (bool) value + }, { + "Exported", + "exported", + self => self.Exported, + (self, value) => self.Exported = (bool) value + }, { + "Icon", + "icon", + self => self.Icon, + (self, value) => self.Icon = (string) value + }, { + "IsolatedProcess", + "isolatedProcess", + self => self._IsolatedProcess, + (self, value) => self._IsolatedProcess = (bool) value + }, { + "Label", + "label", + self => self.Label, + (self, value) => self.Label = (string) value + }, { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "Permission", + "permission", + self => self.Permission, + (self, value) => self.Permission = (string) value + }, { + "Process", + "process", + self => self.Process, + (self, value) => self.Process = (string) value + }, + }; + + ICollection specified; + + public static ServiceAttribute FromTypeDefinition (TypeDefinition type) + { + CustomAttribute attr = type.GetCustomAttributes ("Android.App.ServiceAttribute") + .SingleOrDefault (); + if (attr == null) + return null; + ServiceAttribute self = new ServiceAttribute (); + self.specified = mapping.Load (self, attr); + return self; + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs new file mode 100644 index 00000000000..603b0d0bdc8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/SupportsGLTextureAttribute.Partial.cs @@ -0,0 +1,44 @@ +using System; +using System.Xml.Linq; +using System.Collections.Generic; + +using Mono.Cecil; +using Java.Interop.Tools.Cecil; +using Xamarin.Android.Manifest; + +namespace Android.App +{ + partial class SupportsGLTextureAttribute + { + static ManifestDocumentElement mapping = new ManifestDocumentElement ("supports-gl-texture") { + { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + } + }; + + + internal XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.SupportsGLTextureAttribute"); + foreach (var attr in attrs) { + if (attr.HasConstructorArguments && attr.ConstructorArguments.Count == 1) { + SupportsGLTextureAttribute self = new SupportsGLTextureAttribute((string)attr.ConstructorArguments[0].Value); + self.specified = mapping.Load (self, attr); + self.specified.Add("Name"); + yield return self; + } + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs new file mode 100644 index 00000000000..d13abece9dd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesFeatureAttribute.Partial.cs @@ -0,0 +1,78 @@ +using System; + +using System.Collections.Generic; +using Xamarin.Android.Manifest; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +namespace Android.App { + + partial class UsesFeatureAttribute { + + bool _Required; + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("uses-feature") { + { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "Required", + "required", + self => self._Required, + (self, value) => self._Required = (bool) value + }, { + "GLESVersion", + "glEsVersion", + self => self.GLESVesionAsString(), + (self, value) => self.GLESVersion = (int) value + } + }; + + internal string GLESVesionAsString () + { + return String.Format("0x{0}", GLESVersion.ToString("X8")); + } + + internal XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.UsesFeatureAttribute"); + foreach (var attr in attrs) { + + UsesFeatureAttribute self = new UsesFeatureAttribute (); + + if (attr.HasProperties) { + // handle the case where the user sets additional properties + self.specified = mapping.Load (self, attr); + if (self.specified.Contains("GLESVersion") && self.GLESVersion==0) { + throw new InvalidOperationException("Invalid value '0' for UsesFeatureAttribute.GLESVersion."); + } + } + if (attr.HasConstructorArguments) { + // in this case the user used one of the Consructors to pass arguments and possibly properties + // so we only create the specified list if its not been created already + if (self.specified == null) + self.specified = new List(); + foreach(var arg in attr.ConstructorArguments) { + if (arg.Value.GetType() == typeof(string)) { + self.specified.Add("Name"); + self.Name = (string)arg.Value; + } + } + } + yield return self; + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesLibraryAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesLibraryAttribute.Partial.cs new file mode 100644 index 00000000000..989efcfd5ee --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesLibraryAttribute.Partial.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.App { + + partial class UsesLibraryAttribute { + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("uses-library") { + { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "Required", + "required", + self => self.Required, + (self, value) => self.Required = (bool) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.UsesLibraryAttribute"); + foreach (var attr in attrs) { + UsesLibraryAttribute self; + + string[] extra = null; + if (attr.ConstructorArguments.Count == 1) { + self = new UsesLibraryAttribute ( + (string) attr.ConstructorArguments [0].Value); + extra = new[]{"Name"}; + } else if (attr.ConstructorArguments.Count == 2) { + self = new UsesLibraryAttribute ( + (string) attr.ConstructorArguments [0].Value, + (bool) attr.ConstructorArguments [1].Value); + extra = new[]{"Name", "Required"}; + } else { + self = new UsesLibraryAttribute (); + extra = new string[0]; + } + + self.specified = mapping.Load (self, attr); + + foreach (var e in extra) + self.specified.Add (e); + + yield return self; + } + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs new file mode 100644 index 00000000000..1870440dbb0 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Mono.Android/UsesPermissionAttribute.Partial.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; +using Mono.Cecil; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Manifest; + +namespace Android.App { + + partial class UsesPermissionAttribute { + + int _MaxSdkVersion; + + static ManifestDocumentElement mapping = new ManifestDocumentElement ("uses-permission") { + { + "Name", + "name", + self => self.Name, + (self, value) => self.Name = (string) value + }, { + "MaxSdkVersion", + "maxSdkVersion", + self => self._MaxSdkVersion, + (self, value) => self._MaxSdkVersion = (int) value + }, + }; + + ICollection specified; + + public static IEnumerable FromCustomAttributeProvider (ICustomAttributeProvider provider) + { + var attrs = provider.GetCustomAttributes ("Android.App.UsesPermissionAttribute"); + foreach (var attr in attrs) { + UsesPermissionAttribute self; + + string[] extra; + if (attr.ConstructorArguments.Count == 1) { + self = new UsesPermissionAttribute ((string)attr.ConstructorArguments[0].Value); + extra = new[]{"Name"}; + } else { + self = new UsesPermissionAttribute (); + extra = new string [0]; + } + + self.specified = mapping.Load (self, attr); + + foreach (var e in extra) + self.specified.Add (e); + + yield return self; + } + } + + public XElement ToElement (string packageName) + { + return mapping.ToElement (this, specified, packageName); + } + + internal class UsesPermissionComparer : IEqualityComparer + { + #region IEqualityComparer Members + public bool Equals (UsesPermissionAttribute x, UsesPermissionAttribute y) + { + return x.Name == y.Name; + } + + public int GetHashCode (UsesPermissionAttribute obj) + { + return (obj.Name ?? String.Empty).GetHashCode (); + } + #endregion + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Properties/AssemblyInfo.cs b/src/Xamarin.Android.Build.Tasks/Properties/AssemblyInfo.cs new file mode 100644 index 00000000000..677d87e3685 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Properties/AssemblyInfo.cs @@ -0,0 +1,38 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle ("Xamarin.Android.Build.Tasks")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Xamarin")] +[assembly: AssemblyProduct ("Xamarin.Android.Build.Tasks")] +[assembly: AssemblyCopyright ("Copyright © Novell 2010-2011 / Xamarin 2011-2012")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible (false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid ("42bb9aea-f4d8-4b43-8956-8e1fee857697")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion ("1.0.0.0")] +[assembly: AssemblyFileVersion ("1.0.0.0")] + +[assembly: InternalsVisibleTo ("AndroidMSBuildTests")] diff --git a/src/Xamarin.Android.Build.Tasks/Resources/ApplicationRegistration.java b/src/Xamarin.Android.Build.Tasks/Resources/ApplicationRegistration.java new file mode 100644 index 00000000000..5f86173d6cd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/ApplicationRegistration.java @@ -0,0 +1,9 @@ +package mono.android.app; + +public class ApplicationRegistration { + + public static void registerApplications () + { + // REGISTER_APPLICATION_AND_INSTRUMENTATION_CLASSES_HERE + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Resources/IncrementalClassLoader.java b/src/Xamarin.Android.Build.Tasks/Resources/IncrementalClassLoader.java new file mode 100644 index 00000000000..4b29fbfc52a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/IncrementalClassLoader.java @@ -0,0 +1,104 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mono.android.incrementaldeployment; + +import android.util.Log; + +import dalvik.system.BaseDexClassLoader; + +import java.io.File; +import java.lang.reflect.Field; +import java.util.List; + +/** + * A class loader that loads classes from any .dex file in a particular directory on the SD card. + * + *

Used to implement incremental deployment to Android phones. + */ +public class IncrementalClassLoader extends ClassLoader { + private final DelegateClassLoader delegateClassLoader; + + public IncrementalClassLoader(ClassLoader original, + String packageName, File codeCacheDir, String nativeLibDir, List dexes) { + super(original.getParent()); + + // TODO(bazel-team): For some mysterious reason, we need to use two class loaders so that + // everything works correctly. Investigate why that is the case so that the code can be + // simplified. + delegateClassLoader = createDelegateClassLoader(codeCacheDir, nativeLibDir, dexes, original); + } + + @Override + public Class findClass(String className) throws ClassNotFoundException { + return delegateClassLoader.findClass(className); + } + + /** + * A class loader whose only purpose is to make {@code findClass()} public. + */ + private static class DelegateClassLoader extends BaseDexClassLoader { + private DelegateClassLoader( + String dexPath, File optimizedDirectory, String libraryPath, ClassLoader parent) { + super(dexPath, optimizedDirectory, libraryPath, parent); + } + + @Override + public Class findClass(String name) throws ClassNotFoundException { + return super.findClass(name); + } + } + + private static DelegateClassLoader createDelegateClassLoader( + File codeCacheDir, String nativeLibDir, List dexes, ClassLoader original) { + StringBuilder pathBuilder = new StringBuilder(); + boolean first = true; + for (String dex : dexes) { + if (first) { + first = false; + } else { + pathBuilder.append(File.pathSeparator); + } + + pathBuilder.append(dex); + } + + Log.v("IncrementalClassLoader", "Incremental dex path is " + pathBuilder); + Log.v("IncrementalClassLoader", "Native lib dir is " + nativeLibDir); + return new DelegateClassLoader(pathBuilder.toString(), codeCacheDir, + nativeLibDir, original); + } + + private static void setParent(ClassLoader classLoader, ClassLoader newParent) { + try { + Field parent = ClassLoader.class.getDeclaredField("parent"); + parent.setAccessible(true); + parent.set(classLoader, newParent); + } catch (IllegalArgumentException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } + } + + public static void inject( + ClassLoader classLoader, String packageName, File codeCacheDir, + String nativeLibDir, List dexes) { + IncrementalClassLoader incrementalClassLoader = + new IncrementalClassLoader(classLoader, packageName, codeCacheDir, nativeLibDir, dexes); + setParent(classLoader, incrementalClassLoader); + } +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java new file mode 100644 index 00000000000..883c1b4a071 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.api4.java @@ -0,0 +1,78 @@ +package mono; + +import java.io.*; +import java.lang.String; +import java.util.Locale; +import java.util.HashSet; +import java.util.zip.*; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.res.AssetManager; +import android.util.Log; +import mono.android.Runtime; + +public class MonoPackageManager { + + static Object lock = new Object (); + static boolean initialized; + + static android.content.Context Context; + + public static void LoadApplication (Context context, ApplicationInfo runtimePackage, String[] apks) + { + synchronized (lock) { + if (!initialized) { + android.content.IntentFilter timezoneChangedFilter = new android.content.IntentFilter ( + android.content.Intent.ACTION_TIMEZONE_CHANGED + ); + context.registerReceiver (new mono.android.app.NotifyTimeZoneChanges (), timezoneChangedFilter); + Context = context; + + System.loadLibrary("monodroid"); + Locale locale = Locale.getDefault (); + String language = locale.getLanguage () + "-" + locale.getCountry (); + String filesDir = context.getFilesDir ().getAbsolutePath (); + String cacheDir = context.getCacheDir ().getAbsolutePath (); + String dataDir = context.getApplicationInfo ().dataDir + "/lib"; + ClassLoader loader = context.getClassLoader (); + + Runtime.init ( + language, + apks, + runtimePackage.dataDir + "/lib", + new String[]{ + filesDir, + cacheDir, + dataDir, + }, + loader, + new java.io.File ( + android.os.Environment.getExternalStorageDirectory (), + "Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (), + MonoPackageManager_Resources.Assemblies, + context.getPackageName ()); + + mono.android.app.ApplicationRegistration.registerApplications (); + + initialized = true; + } + } + } + + public static String[] getAssemblies () + { + return MonoPackageManager_Resources.Assemblies; + } + + public static String[] getDependencies () + { + return MonoPackageManager_Resources.Dependencies; + } + + public static String getApiPackageName () + { + return MonoPackageManager_Resources.ApiPackageName; + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java new file mode 100644 index 00000000000..4452e14c125 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/MonoPackageManager.java @@ -0,0 +1,90 @@ +package mono; + +import java.io.*; +import java.lang.String; +import java.util.Locale; +import java.util.HashSet; +import java.util.zip.*; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.res.AssetManager; +import android.util.Log; +import mono.android.Runtime; + +public class MonoPackageManager { + + static Object lock = new Object (); + static boolean initialized; + + static android.content.Context Context; + + public static void LoadApplication (Context context, ApplicationInfo runtimePackage, String[] apks) + { + synchronized (lock) { + if (!initialized) { + android.content.IntentFilter timezoneChangedFilter = new android.content.IntentFilter ( + android.content.Intent.ACTION_TIMEZONE_CHANGED + ); + context.registerReceiver (new mono.android.app.NotifyTimeZoneChanges (), timezoneChangedFilter); + Context = context; + + System.loadLibrary("monodroid"); + Locale locale = Locale.getDefault (); + String language = locale.getLanguage () + "-" + locale.getCountry (); + String filesDir = context.getFilesDir ().getAbsolutePath (); + String cacheDir = context.getCacheDir ().getAbsolutePath (); + String dataDir = getNativeLibraryPath (context); + ClassLoader loader = context.getClassLoader (); + + Runtime.init ( + language, + apks, + getNativeLibraryPath (runtimePackage), + new String[]{ + filesDir, + cacheDir, + dataDir, + }, + loader, + new java.io.File ( + android.os.Environment.getExternalStorageDirectory (), + "Android/data/" + context.getPackageName () + "/files/.__override__").getAbsolutePath (), + MonoPackageManager_Resources.Assemblies, + context.getPackageName ()); + + mono.android.app.ApplicationRegistration.registerApplications (); + + initialized = true; + } + } + } + + static String getNativeLibraryPath (Context context) + { + return getNativeLibraryPath (context.getApplicationInfo ()); + } + + static String getNativeLibraryPath (ApplicationInfo ainfo) + { + if (android.os.Build.VERSION.SDK_INT >= 9) + return ainfo.nativeLibraryDir; + return ainfo.dataDir + "/lib"; + } + + public static String[] getAssemblies () + { + return MonoPackageManager_Resources.Assemblies; + } + + public static String[] getDependencies () + { + return MonoPackageManager_Resources.Dependencies; + } + + public static String getApiPackageName () + { + return MonoPackageManager_Resources.ApiPackageName; + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Resources/NOTICE.txt b/src/Xamarin.Android.Build.Tasks/Resources/NOTICE.txt new file mode 100644 index 00000000000..54020cf67ce --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/NOTICE.txt @@ -0,0 +1,4 @@ +Xamarin built applications contain open source software. +For detailed attribution and licensing notices, please visit: + + http://xamarin.com/mobile-licensing diff --git a/src/Xamarin.Android.Build.Tasks/Resources/NotifyTimeZoneChanges.java b/src/Xamarin.Android.Build.Tasks/Resources/NotifyTimeZoneChanges.java new file mode 100644 index 00000000000..21896a0f130 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/NotifyTimeZoneChanges.java @@ -0,0 +1,8 @@ +package mono.android.app; + +public class NotifyTimeZoneChanges extends android.content.BroadcastReceiver { + @Override + public void onReceive (android.content.Context context, android.content.Intent intent) { + mono.android.Runtime.notifyTimeZoneChanged (); + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Resources/Placeholder.java b/src/Xamarin.Android.Build.Tasks/Resources/Placeholder.java new file mode 100644 index 00000000000..e17e9037910 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/Placeholder.java @@ -0,0 +1,25 @@ +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mono.android.incrementaldeployment; + +/** + * A dummy class. + * + *

This class exists because Android L requires that a .dex file be present in a main .apk. + * We do not want to put any class in there so that we can replace them without reinstalling the + * complete app, thus, we put this tiny little class in there. + */ +public final class Placeholder { +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Resources/Seppuku.java b/src/Xamarin.Android.Build.Tasks/Resources/Seppuku.java new file mode 100644 index 00000000000..539c87426c7 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/Seppuku.java @@ -0,0 +1,19 @@ +package mono.android; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +public class Seppuku extends BroadcastReceiver { + @Override + public void onReceive (Context context, Intent intent) + { + Intent startMain = new Intent (Intent.ACTION_MAIN); + startMain.addCategory (Intent.CATEGORY_HOME); + startMain.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity (startMain); + + java.lang.Runtime.getRuntime ().exit (-1); + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Resources/StubApplication.java b/src/Xamarin.Android.Build.Tasks/Resources/StubApplication.java new file mode 100644 index 00000000000..c83b7dc4608 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/StubApplication.java @@ -0,0 +1,447 @@ +// +// This is a modified version of Bazel source code. Its copyright lines follow below. +// + +// Copyright 2015 Xamarin Inc. All rights reserved. + +// +// Copyright 2014 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mono.android.incrementaldeployment; + +import android.app.Application; +import android.content.Context; +import android.content.ContextWrapper; +import android.content.res.AssetManager; +import android.content.res.Resources; +import android.util.Log; + +import java.io.BufferedReader; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileReader; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.lang.ref.WeakReference; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * A stub application that patches the class loader, then replaces itself with the real application + * by applying a liberal amount of reflection on Android internals. + * + *

This is, of course, terribly error-prone. Most of this code was tested with API versions + * 8, 10, 14, 15, 16, 17, 18, 19 and 21 on the Android emulator, a Nexus 5 running Lollipop LRX22C + * and a Samsung GT-I5800 running Froyo XWJPE. The exception is {@code monkeyPatchAssetManagers}, + * which only works on Kitkat and Lollipop. + * + *

Note that due to a bug in Dalvik, this only works on Kitkat if ART is the Java runtime. + * + *

Unfortunately, if this does not work, we don't have a fallback mechanism: as soon as we + * build the APK with this class as the Application, we are committed to going through with it. + * + *

This class should use as few other classes as possible before the class loader is patched + * because any class loaded before it cannot be incrementally deployed. + */ +public class StubApplication extends Application { + + public static boolean real_application_not_instantiated = true; + + private static String mIncrementalDeploymentDir; + + private static String getIncrementalDeploymentDir (Context context) + { + // For initial setup by Seppuku, it needs to create the dex deployment directory at app bootstrap. + // dex is special, important for mono runtime bootstrap. + String dir = new File ( + android.os.Environment.getExternalStorageDirectory (), + "Android/data/" + context.getPackageName ()).getAbsolutePath (); + dir = new File (dir).exists () ? + dir + "/files" : + "/data/data/" + context.getPackageName () + "/files"; + String dexDir = dir + "/.__override__/dexes"; + if (!new File (dexDir).exists ()) + new File (dexDir).mkdirs (); + return dir; + } + + private static final FilenameFilter SO = new FilenameFilter() { + public boolean accept(File dir, String name) { + return name.endsWith(".so"); + } + }; + + private final String realClassName; + private final String packageName; + + private String externalResourceFile; + private Application realApplication; + + public StubApplication() { + String[] stubApplicationData = getResourceAsString("stub_application_data.txt").split("\n"); + realClassName = stubApplicationData[0]; + packageName = stubApplicationData[1]; + + Log.v("StubApplication", String.format( + "StubApplication created. Android package is %s, real application class is %s.", + packageName, realClassName)); + } + + private String getExternalResourceFile() { + String base = mIncrementalDeploymentDir; + String resourceFile = base + ".__override__/packaged_resources"; + if (!(new File(resourceFile).isFile())) { + resourceFile = base + ".__override__/resources"; + if (!(new File(resourceFile).isDirectory())) { + Log.v("StubApplication", "Cannot find external resources, not patching them in"); + return null; + } + } + + Log.v("StubApplication", "Found external resources at " + resourceFile); + return resourceFile; + } + + private List getDexList(String packageName) { + List result = new ArrayList(); + String dexDirectory = mIncrementalDeploymentDir + ".__override__/dexes"; + File[] dexes = new File(dexDirectory).listFiles(); + // It is not illegal state when it was launched to start Seppuku + if (dexes == null) { + real_application_not_instantiated = true; + return null; + } else { + for (File dex : dexes) { + if (dex.getName().endsWith(".dex")) { + result.add(dex.getPath()); + } + } + } + + return result; + } + + private String getResourceAsString(String resource) { + InputStream resourceStream = null; + // try-with-resources would be much nicer, but that requires SDK level 19, and we want this code + // to be compatible with earlier Android versions + try { + resourceStream = getClass().getClassLoader().getResourceAsStream(resource); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = 0; + while ((length = resourceStream.read(buffer)) != -1) { + baos.write(buffer, 0, length); + } + + String result = new String(baos.toByteArray(), "UTF-8"); + return result; + } catch (IOException e) { + throw new IllegalStateException(e); + } finally { + if (resourceStream != null) { + try { + resourceStream.close(); + } catch (IOException e) { + // Not much we can do here + } + } + } + } + + @SuppressWarnings("unchecked") // Lots of conversions with generic types + private void monkeyPatchApplication() { + // StubApplication is created by reflection in Application#handleBindApplication() -> + // LoadedApk#makeApplication(), and its return value is used to set the Application field in all + // sorts of Android internals. + // + // Fortunately, Application#onCreate() is called quite soon after, so what we do is monkey + // patch in the real Application instance in StubApplication#onCreate(). + // + // A few places directly use the created Application instance (as opposed to the fields it is + // eventually stored in). Fortunately, it's easy to forward those to the actual real + // Application class. + try { + // Find the ActivityThread instance for the current thread + Class activityThread = Class.forName("android.app.ActivityThread"); + Method m = activityThread.getMethod("currentActivityThread"); + m.setAccessible(true); + Object currentActivityThread = m.invoke(null); + + // Find the mInitialApplication field of the ActivityThread to the real application + Field mInitialApplication = activityThread.getDeclaredField("mInitialApplication"); + mInitialApplication.setAccessible(true); + Application initialApplication = (Application) mInitialApplication.get(currentActivityThread); + if (initialApplication == StubApplication.this) { + mInitialApplication.set(currentActivityThread, realApplication); + } + + // Replace all instance of the stub application in ActivityThread#mAllApplications with the + // real one + Field mAllApplications = activityThread.getDeclaredField("mAllApplications"); + mAllApplications.setAccessible(true); + List allApplications = (List) mAllApplications + .get(currentActivityThread); + for (int i = 0; i < allApplications.size(); i++) { + if (allApplications.get(i) == StubApplication.this) { + allApplications.set(i, realApplication); + } + } + + // Figure out how loaded APKs are stored. + + // API version 8 has PackageInfo, 10 has LoadedApk. 9, I don't know. + Class loadedApkClass; + try { + loadedApkClass = Class.forName("android.app.LoadedApk"); + } catch (ClassNotFoundException e) { + loadedApkClass = Class.forName("android.app.ActivityThread$PackageInfo"); + } + Field mApplication = loadedApkClass.getDeclaredField("mApplication"); + mApplication.setAccessible(true); + Field mResDir = loadedApkClass.getDeclaredField("mResDir"); + mResDir.setAccessible(true); + + // 10 doesn't have this field, 14 does. Fortunately, there are not many Honeycomb devices + // floating around. + Field mLoadedApk = null; + try { + mLoadedApk = Application.class.getDeclaredField("mLoadedApk"); + } catch (NoSuchFieldException e) { + // According to testing, it's okay to ignore this. + } + + // Enumerate all LoadedApk (or PackageInfo) fields in ActivityThread#mPackages and + // ActivityThread#mResourcePackages and do two things: + // - Replace the Application instance in its mApplication field with the real one + // - Replace mResDir to point to the external resource file instead of the .apk. This is + // used as the asset path for new Resources objects. + // - Set Application#mLoadedApk to the found LoadedApk instance + for (String fieldName : new String[] { "mPackages", "mResourcePackages" }) { + Field field = activityThread.getDeclaredField(fieldName); + field.setAccessible(true); + Object value = field.get(currentActivityThread); + + for (Map.Entry> entry : + ((Map>) value).entrySet()) { + Object loadedApk = entry.getValue().get(); + if (loadedApk == null) { + continue; + } + + if (mApplication.get(loadedApk) == StubApplication.this) { + mApplication.set(loadedApk, realApplication); + if (externalResourceFile != null) { + mResDir.set(loadedApk, externalResourceFile); + } + + if (mLoadedApk != null) { + mLoadedApk.set(realApplication, loadedApk); + } + } + } + } + } catch (IllegalAccessException e) { + throw new IllegalStateException(e); + } catch (NoSuchFieldException e) { + throw new IllegalStateException(e); + } catch (NoSuchMethodException e) { + throw new IllegalStateException(e); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(e); + } catch (InvocationTargetException e) { + throw new IllegalStateException(e); + } + } + + private void monkeyPatchExistingResources() { + if (externalResourceFile == null) { + return; + } + + try { + // Create a new AssetManager instance and point it to the resources installed under + // /sdcard + AssetManager newAssetManager = AssetManager.class.getConstructor().newInstance(); + Method mAddAssetPath = AssetManager.class.getDeclaredMethod("addAssetPath", String.class); + mAddAssetPath.setAccessible(true); + if (((int) (Integer) mAddAssetPath.invoke(newAssetManager, externalResourceFile)) == 0) { + throw new IllegalStateException("Could not create new AssetManager"); + } + + // Kitkat needs this method call, Lollipop doesn't. However, it doesn't seem to cause any harm + // in L, so we do it unconditionally. + Method mEnsureStringBlocks = AssetManager.class.getDeclaredMethod("ensureStringBlocks"); + mEnsureStringBlocks.setAccessible(true); + mEnsureStringBlocks.invoke(newAssetManager); + + // Find the singleton instance of ResourcesManager + Class clazz = Class.forName("android.app.ResourcesManager"); + Method mGetInstance = clazz.getDeclaredMethod("getInstance"); + mGetInstance.setAccessible(true); + Object resourcesManager = mGetInstance.invoke(null); + + Field mAssets = Resources.class.getDeclaredField("mAssets"); + mAssets.setAccessible(true); + + // Iterate over all known Resources objects + Field fMActiveResources = clazz.getDeclaredField("mActiveResources"); + fMActiveResources.setAccessible(true); + @SuppressWarnings("unchecked") + Map> arrayMap = + (Map>) fMActiveResources.get(resourcesManager); + for (WeakReference wr : arrayMap.values()) { + Resources resources = wr.get(); + // Set the AssetManager of the Resources instance to our brand new one + mAssets.set(resources, newAssetManager); + resources.updateConfiguration(resources.getConfiguration(), resources.getDisplayMetrics()); + } + } catch (IllegalAccessException e) { + throw new IllegalStateException(e); + } catch (NoSuchFieldException e) { + throw new IllegalStateException(e); + } catch (NoSuchMethodException e) { + throw new IllegalStateException(e); + } catch (ClassNotFoundException e) { + throw new IllegalStateException(e); + } catch (InvocationTargetException e) { + throw new IllegalStateException(e); + } catch (InstantiationException e) { + throw new IllegalStateException(e); + } + } + + private void instantiateRealApplication(File codeCacheDir, String appNativeLibraryDir, String dataDir) { + externalResourceFile = getExternalResourceFile(); + + String nativeLibDir = appNativeLibraryDir; + + List dexes = getDexList(packageName); + if (dexes == null) + return; // it is during initial Seppuku. + IncrementalClassLoader.inject( + StubApplication.class.getClassLoader(), + packageName, + codeCacheDir, + nativeLibDir, + dexes); + + try { + @SuppressWarnings("unchecked") + Class realClass = + (Class) Class.forName(realClassName); + Constructor ctor = realClass.getConstructor(); + realApplication = ctor.newInstance(); + real_application_not_instantiated = false; + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + // This copies native libraries from apk contents to avoid permission issue and it somehow loads, + // but causes runtime crash later. Right now it has to be disabled. + private String copyNativeLibs(String appNativeLibraryDir) throws IOException { + String nativeLibDir = mIncrementalDeploymentDir + ".__override__/lib"; + File dstdir = new File (nativeLibDir); + if (!dstdir.exists ()) + dstdir.mkdirs (); + File srcdir = new File (appNativeLibraryDir); + for (File src : srcdir.listFiles ()) { + File dst = new File (nativeLibDir + '/' + src.getName ()); + if (!dst.exists ()) { + copy (src, dst); + } else { + Log.i("StubApplication", "Skip copying native library, already exists: " + dst.getPath()); + } + } + return nativeLibDir; + } + + private static void copy(File src, File dst) throws IOException { + Log.v("StubApplication", "Copying " + src + " -> " + dst); + InputStream in = null; + OutputStream out = null; + try { + in = new FileInputStream(src); + out = new FileOutputStream(dst); + + // Transfer bytes from in to out + byte[] buf = new byte[1048576]; + int len; + while ((len = in.read(buf)) > 0) { + out.write(buf, 0, len); + } + } finally { + if (in != null) { + in.close(); + } + + if (out != null) { + out.close(); + } + } + } + + @Override + protected void attachBaseContext(Context context) { + + mIncrementalDeploymentDir = getIncrementalDeploymentDir(context) + '/'; + + instantiateRealApplication( + context.getCacheDir(), + context.getApplicationInfo().nativeLibraryDir, + context.getApplicationInfo().dataDir); + + // This is called from ActivityThread#handleBindApplication() -> LoadedApk#makeApplication(). + // Application#mApplication is changed right after this call, so we cannot do the monkey + // patching here. So just forward this method to the real Application instance. + super.attachBaseContext(context); + + if (real_application_not_instantiated) // it is in the bootstrap process with Seppuku. + return; + + try { + Method attachBaseContext = + ContextWrapper.class.getDeclaredMethod("attachBaseContext", Context.class); + attachBaseContext.setAccessible(true); + attachBaseContext.invoke(realApplication, context); + + } catch (Exception e) { + throw new IllegalStateException(e); + } + } + + @Override + public void onCreate() { + if (real_application_not_instantiated) // it is in the bootstrap process with Seppuku. + return; + monkeyPatchApplication(); + monkeyPatchExistingResources(); + super.onCreate(); + realApplication.onCreate(); + } +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Resources/machine.config b/src/Xamarin.Android.Build.Tasks/Resources/machine.config new file mode 100644 index 00000000000..76ccdb48914 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/machine.config @@ -0,0 +1,273 @@ + + + + + +

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+ +
+ + +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+
+
+
+
+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Resources/proguard_xamarin.cfg b/src/Xamarin.Android.Build.Tasks/Resources/proguard_xamarin.cfg new file mode 100644 index 00000000000..c2282dfa1f2 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Resources/proguard_xamarin.cfg @@ -0,0 +1,31 @@ +# This is Xamarin-specific (and enhanced) configuration. + +-dontobfuscate + +-keep class mono.MonoRuntimeProvider { *; (...); } +-keep class mono.MonoPackageManager { *; (...); } +-keep class mono.MonoPackageManager_Resources { *; (...); } +-keep class mono.android.** { *; (...); } +-keep class mono.java.** { *; (...); } +-keep class mono.javax.** { *; (...); } +-keep class opentk.platform.android.AndroidGameView { *; (...); } +-keep class opentk.GameViewBase { *; (...); } +-keep class opentk_1_0.platform.android.AndroidGameView { *; (...); } +-keep class opentk_1_0.GameViewBase { *; (...); } + +-keep class android.runtime.** { (***); } +-keep class assembly_mono_android.android.runtime.** { (***); } +# hash for android.runtime and assembly_mono_android.android.runtime. +-keep class md52ce486a14f4bcd95899665e9d932190b.** { *; (...); } +-keepclassmembers class md52ce486a14f4bcd95899665e9d932190b.** { *; (...); } + +# Android's template misses fluent setters... +-keepclassmembers class * extends android.view.View { + *** set*(***); +} + +# also misses those inflated custom layout stuff from xml... +-keepclassmembers class * extends android.view.View { + (android.content.Context,android.util.AttributeSet); + (android.content.Context,android.util.AttributeSet,int); +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs new file mode 100644 index 00000000000..8fee7a03ee5 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs @@ -0,0 +1,306 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using System.Collections.Generic; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class Aapt : ToolTask + { + private const string error_regex_string = @"(?.*)\s*:\s*(?\d*)\s*:\s*error:\s*(?.+)"; + private Regex error_regex = new Regex (error_regex_string, RegexOptions.Compiled); + + public ITaskItem[] AdditionalAndroidResourcePaths { get; set; } + + public string AndroidComponentResgenFlagFile { get; set; } + + public bool NonConstantId { get; set; } + + public string AssetDirectory { get; set; } + + [Required] + public string ManifestFile { get; set; } + + [Required] + public string ResourceDirectory { get; set; } + + public string ResourceOutputFile { get; set; } + + [Required] + public string JavaDesignerOutputDirectory { get; set; } + + [Required] + public string JavaPlatformJarPath { get; set; } + + public string UncompressedFileExtensions { get; set; } + public string PackageName { get; set; } + + [Required] + public string ApplicationName { get; set; } + + public string ExtraPackages { get; set; } + + public ITaskItem [] AdditionalResourceDirectories { get; set; } + + public ITaskItem [] LibraryProjectJars { get; set; } + + public string ExtraArgs { get; set; } + + protected override string ToolName { get { return OS.IsWindows ? "aapt.exe" : "aapt"; } } + + public string ApiLevel { get; set; } + + public bool AndroidUseLatestPlatformSdk { get; set; } + + public string SupportedAbis { get; set; } + + public bool CreatePackagePerAbi { get; set; } + + public string ImportsDirectory { get; set; } + public string OutputImportDirectory { get; set; } + public bool UseShortFileNames { get; set; } + + public string ResourceNameCaseMap { get; set; } + + public bool ExplicitCrunch { get; set; } + + string currentAbi; + string currentResourceOutputFile; + Dictionary resource_name_case_map = new Dictionary (); + + bool ManifestIsUpToDate (string manifestFile) + { + return !String.IsNullOrEmpty (AndroidComponentResgenFlagFile) && + File.Exists (AndroidComponentResgenFlagFile) && File.Exists (manifestFile) && + File.GetLastWriteTime (AndroidComponentResgenFlagFile) > File.GetLastWriteTime (manifestFile); + } + + bool ExecuteForAbi (string abi) + { + currentAbi = abi; + var ret = base.Execute (); + if (ret && !string.IsNullOrEmpty (currentResourceOutputFile)) { + var tmpfile = currentResourceOutputFile + ".bk"; + MonoAndroidHelper.CopyIfZipChanged (tmpfile, currentResourceOutputFile); + File.Delete (tmpfile); + } + return ret; + } + + public override bool Execute () + { + Log.LogDebugMessage ("Aapt Task"); + Log.LogDebugMessage (" AssetDirectory: {0}", AssetDirectory); + Log.LogDebugMessage (" ManifestFile: {0}", ManifestFile); + Log.LogDebugMessage (" ResourceDirectory: {0}", ResourceDirectory); + Log.LogDebugMessage (" JavaDesignerOutputDirectory: {0}", JavaDesignerOutputDirectory); + Log.LogDebugMessage (" PackageName: {0}", PackageName); + Log.LogDebugMessage (" UncompressedFileExtensions: {0}", UncompressedFileExtensions); + Log.LogDebugMessage (" ExtraPackages: {0}", ExtraPackages); + Log.LogDebugTaskItems (" AdditionalResourceDirectories: ", AdditionalResourceDirectories); + Log.LogDebugTaskItems (" AdditionalAndroidResourcePaths: ", AdditionalAndroidResourcePaths); + Log.LogDebugTaskItems (" LibraryProjectJars: ", LibraryProjectJars); + Log.LogDebugMessage (" ExtraArgs: {0}", ExtraArgs); + Log.LogDebugMessage (" CreatePackagePerAbi: {0}", CreatePackagePerAbi); + Log.LogDebugMessage (" ResourceNameCaseMap: {0}", ResourceNameCaseMap); + if (CreatePackagePerAbi) + Log.LogDebugMessage (" SupportedAbis: {0}", SupportedAbis); + + bool upToDate = ManifestIsUpToDate (ManifestFile); + + if (ResourceNameCaseMap != null) + foreach (var arr in ResourceNameCaseMap.Split (';').Select (l => l.Split ('|')).Where (a => a.Length == 2)) + resource_name_case_map [arr [1]] = arr [0]; // lowercase -> original + + if (AdditionalAndroidResourcePaths != null) + foreach (var dir in AdditionalAndroidResourcePaths) + if (!string.IsNullOrEmpty (dir.ItemSpec)) + upToDate = upToDate && ManifestIsUpToDate (string.Format ("{0}{1}{2}", dir, Path.DirectorySeparatorChar, "manifest", Path.DirectorySeparatorChar, "AndroidManifest.xml")); + + if (upToDate) { + Log.LogMessage (MessageImportance.Normal, " Additional Android Resources manifsets files are unchanged. Skipping."); + return true; + } + + ExecuteForAbi (null); + + if (CreatePackagePerAbi) { + var abis = SupportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + if (abis.Length > 1) + foreach (var abi in abis) + ExecuteForAbi (abi); + } + return !Log.HasLoggedErrors; + } + + protected override string GenerateCommandLineCommands () + { + // For creating Resource.Designer.cs: + // Running command: C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\aapt + // "package" + // "-M" "C:\Users\Jonathan\AppData\Local\Temp\ryob4gaw.way\AndroidManifest.xml" + // "-J" "C:\Users\Jonathan\AppData\Local\Temp\ryob4gaw.way" + // "-F" "C:\Users\Jonathan\AppData\Local\Temp\ryob4gaw.way\resources.apk" + // "-S" "c:\users\jonathan\documents\visual studio 2010\Projects\MonoAndroidApplication4\MonoAndroidApplication4\obj\Debug\res" + // "-I" "C:\Program Files (x86)\Android\android-sdk-windows\platforms\android-8\android.jar" + // "--max-res-version" "10" + + // For packaging: + // Running command: C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\aapt + // "package" + // "-f" + // "-m" + // "-M" "AndroidManifest.xml" + // "-J" "src" + // "--custom-package" "androidmsbuildtest.androidmsbuildtest" + // "-F" "bin\packaged_resources" + // "-S" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\res" + // "-I" "C:\Program Files (x86)\Android\android-sdk-windows\platforms\android-8\android.jar" + // "--extra-packages" "com.facebook.android:my.another.library" + + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitch ("package"); + + if (MonoAndroidHelper.LogInternalExceptions) + cmd.AppendSwitch ("-v"); + if (NonConstantId) + cmd.AppendSwitch ("--non-constant-id"); + cmd.AppendSwitch ("-f"); + cmd.AppendSwitch ("-m"); + string manifestFile; + string manifestDir = Path.Combine (Path.GetDirectoryName (ManifestFile), currentAbi != null ? currentAbi : "manifest"); + Directory.CreateDirectory (manifestDir); + manifestFile = Path.Combine (manifestDir, Path.GetFileName (ManifestFile)); + ManifestDocument manifest = new ManifestDocument (ManifestFile, this.Log); + if (currentAbi != null) + manifest.SetAbi (currentAbi); + manifest.ApplicationName = ApplicationName; + manifest.Save (manifestFile); + currentResourceOutputFile = currentAbi != null ? string.Format ("{0}-{1}", ResourceOutputFile, currentAbi) : ResourceOutputFile; + + cmd.AppendSwitchIfNotNull ("-M ", manifestFile); + Directory.CreateDirectory (JavaDesignerOutputDirectory); + cmd.AppendSwitchIfNotNull ("-J ", JavaDesignerOutputDirectory); + + if (PackageName != null) + cmd.AppendSwitchIfNotNull ("--custom-package ", PackageName.ToLowerInvariant ()); + + if (!string.IsNullOrEmpty (currentResourceOutputFile)) + cmd.AppendSwitchIfNotNull ("-F ", currentResourceOutputFile + ".bk"); + // The order of -S arguments is *important*, always make sure this one comes FIRST + cmd.AppendSwitchIfNotNull ("-S ", ResourceDirectory.TrimEnd ('\\')); + if (AdditionalResourceDirectories != null) + foreach (var resdir in AdditionalResourceDirectories) + cmd.AppendSwitchIfNotNull ("-S ", resdir.ItemSpec.TrimEnd ('\\')); + if (AdditionalAndroidResourcePaths != null) + foreach (var dir in AdditionalAndroidResourcePaths) + cmd.AppendSwitchIfNotNull ("-S ", Path.Combine (dir.ItemSpec.TrimEnd (System.IO.Path.DirectorySeparatorChar), "res")); + + if (LibraryProjectJars != null) + foreach (var jar in LibraryProjectJars) + cmd.AppendSwitchIfNotNull ("-j ", jar); + + cmd.AppendSwitchIfNotNull ("-I ", JavaPlatformJarPath); + + // Add asset directory if it exists + if (!string.IsNullOrWhiteSpace (AssetDirectory) && Directory.Exists (AssetDirectory)) + cmd.AppendSwitchIfNotNull ("-A ", AssetDirectory.TrimEnd ('\\')); + + if (!string.IsNullOrWhiteSpace (UncompressedFileExtensions)) + foreach (var ext in UncompressedFileExtensions.Split (new char[] { ';', ','}, StringSplitOptions.RemoveEmptyEntries)) + cmd.AppendSwitchIfNotNull ("-0 ", ext); + + if (!string.IsNullOrEmpty (ExtraPackages)) + cmd.AppendSwitchIfNotNull ("--extra-packages ", ExtraPackages); + + // TODO: handle resource names + if (ExplicitCrunch) + cmd.AppendSwitch ("--no-crunch"); + + cmd.AppendSwitch ("--auto-add-overlay"); + + var extraArgsExpanded = ExpandString (ExtraArgs); + if (extraArgsExpanded != ExtraArgs) + Log.LogDebugMessage (" ExtraArgs expanded: {0}", extraArgsExpanded); + + if (!string.IsNullOrWhiteSpace (extraArgsExpanded)) + cmd.AppendSwitch (extraArgsExpanded); + + if (!AndroidUseLatestPlatformSdk) + cmd.AppendSwitchIfNotNull ("--max-res-version ", ApiLevel); + + return cmd.ToString (); + } + + string ExpandString (string s) + { + if (s == null) + return null; + int start = 0; + int st = s.IndexOf ("${library.imports:", start, StringComparison.Ordinal); + if (st >= 0) { + int ed = s.IndexOf ('}', st); + if (ed < 0) + return s.Substring (0, st + 1) + ExpandString (s.Substring (st + 1)); + int ast = st + "${library.imports:".Length; + string aname = s.Substring (ast, ed - ast); + return s.Substring (0, st) + Path.Combine (OutputImportDirectory, UseShortFileNames ? MonoAndroidHelper.GetLibraryImportDirectoryNameForAssembly (aname) : aname, ImportsDirectory) + Path.DirectorySeparatorChar + ExpandString (s.Substring (ed + 1)); + } + else + return s; + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) + { + // Aapt errors looks like this: + // C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\res\layout\main.axml:7: error: No resource identifier found for attribute 'id2' in package 'android' (TaskId:22) + // Look for them and convert them to MSBuild compatible errors. + + var match = error_regex.Match (singleLine); + + if (match.Success) { + var file = match.Groups["file"].Value; + var line = int.Parse (match.Groups["line"].Value) + 1; + var error = match.Groups["error"].Value; + + // Try to map back to the original resource file, so when the user + // double clicks the error, it won't take them to the obj/Debug copy + if (file.StartsWith (ResourceDirectory, StringComparison.InvariantCultureIgnoreCase)) { + file = file.Substring (ResourceDirectory.Length); + file = resource_name_case_map.ContainsKey (file) ? resource_name_case_map [file] : file; + file = Path.Combine ("Resources", file); + } + + // Strip any "Error:" text from aapt's output + if (error.StartsWith ("error: ", StringComparison.InvariantCultureIgnoreCase)) + error = error.Substring ("error: ".Length); + + singleLine = string.Format ("{0}({1}): error APT0000: {2}", file, line, error); + messageImportance = MessageImportance.High; + } + + // Handle additional error that doesn't match the regex + if (singleLine.Trim ().StartsWith ("invalid resource directory name:")) { + Log.LogError ("", "", "", ToolName, -1, -1, -1, -1, "Invalid resource directory name: \"{0}\".", singleLine.Substring (singleLine.LastIndexOfAny (new char[] { '\\', '/' }) + 1)); + messageImportance = MessageImportance.High; + } + + base.LogEventsFromTextOutput (singleLine, messageImportance); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AdjustJavacVersionArguments.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AdjustJavacVersionArguments.cs new file mode 100644 index 00000000000..06ced7d6857 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AdjustJavacVersionArguments.cs @@ -0,0 +1,70 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.IO; +using System.Diagnostics; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class AdjustJavacVersionArguments : ToolTask + { + [Required] + public string ToolPath { get; set; } + + public bool EnableProguard { get; set; } + + public bool EnableMultiDex { get; set; } + + [Output] + public string TargetVersion { get; set; } + + [Output] + public string SourceVersion { get; set; } + + protected override string ToolName { + get { return OS.IsWindows ? "javac.exe" : "javac"; } + } + + public override bool Execute () + { + Log.LogDebugMessage ("ToolPath: {0}", ToolPath); + Log.LogDebugMessage ("ToolExe: {0}", ToolExe); + Log.LogDebugMessage ("EnableProguard: {0}", EnableProguard); + Log.LogDebugMessage ("EnableMultiDex: {0}", EnableMultiDex); + + // so far only proguard matters. + if (!EnableProguard && !EnableMultiDex) + return true; + + var psi = new ProcessStartInfo (Path.Combine (ToolPath, ToolExe ?? ToolName), "-version") { + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + }; + var proc = Process.Start (psi); + proc.WaitForExit (); + var line = proc.StandardError.ReadLine (); + if (!line.StartsWith ("javac ")) + // otherwise ignore. + return true; + + var version = line.Substring (6); + + if (version.StartsWith ("1.8")) { + TargetVersion = SourceVersion = "1.7"; + Log.LogDebugMessage ("Javac TargetVersion adjusted to: {0}", TargetVersion); + Log.LogDebugMessage ("Javac SourceVersion adjusted to: {0}", SourceVersion); + } + + return true; + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidCreateDebugKey.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidCreateDebugKey.cs new file mode 100644 index 00000000000..941937d9bcb --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidCreateDebugKey.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class AndroidCreateDebugKey : KeyTool + { + public int Validity { get; set; } + + public string KeyAlgorithm { get; set; } + + string dname = "CN=Android Debug,O=Android,C=US"; + + public AndroidCreateDebugKey () + { + KeyAlgorithm = "RSA"; + Validity = 30 * 365; // 30 years + } + + public override bool Execute () + { + Log.LogDebugMessage ("AndroidCreateDebugKey : {0}", Command); + Log.LogDebugMessage (" {0}",Validity); + Log.LogDebugMessage (" {0}",KeyAlgorithm); + return base.Execute (); + } + + protected override CommandLineBuilder CreateCommandLine () + { + var cmd = base.CreateCommandLine (); + cmd.AppendSwitchIfNotNull ("-dname ", dname); + cmd.AppendSwitchIfNotNull ("-keyalg ", KeyAlgorithm); + cmd.AppendSwitchIfNotNull ("-validity ", Validity.ToString()); + return cmd; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidSignPackage.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidSignPackage.cs new file mode 100644 index 00000000000..0ba322cc509 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidSignPackage.cs @@ -0,0 +1,82 @@ +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class AndroidSignPackage : AndroidToolTask + { + bool hasWarnings; + + [Required] + public string UnsignedApk { get; set; } + + [Required] + public string SignedApkDirectory { get; set; } + + [Required] + [Output] + public string KeyStore { get; set; } + + [Required] + public string KeyAlias { get; set; } + + [Required] + public string KeyPass { get; set; } + + [Required] + public string StorePass { get; set; } + + public string TimestampAuthorityUrl { get; set; } + + public string TimestampAuthorityCertificateAlias { get; set; } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitchIfNotNull ("-tsa ", TimestampAuthorityUrl); + cmd.AppendSwitchIfNotNull ("-tsacert ", TimestampAuthorityCertificateAlias); + cmd.AppendSwitchIfNotNull ("-keystore ", KeyStore); + cmd.AppendSwitchIfNotNull ("-storepass ", StorePass); + cmd.AppendSwitchIfNotNull ("-keypass ", KeyPass); + cmd.AppendSwitchIfNotNull ("-digestalg ", "SHA1"); + cmd.AppendSwitchIfNotNull ("-sigalg ", "md5withRSA"); + cmd.AppendSwitchIfNotNull ("-signedjar ", String.Format ("{0}{1}{2}-Signed-Unaligned.apk", SignedApkDirectory, Path.DirectorySeparatorChar, Path.GetFileNameWithoutExtension (UnsignedApk))); + + cmd.AppendFileNameIfNotNull (UnsignedApk); + cmd.AppendSwitch (KeyAlias); + + return cmd.ToString (); + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance) + { + singleLine = singleLine.Trim (); + if (singleLine.Length == 0) + return; + + if (singleLine.StartsWith ("Warning:")) { + hasWarnings = true; + return; + } + + if (hasWarnings) + Log.LogWarning (singleLine); + else + Log.LogMessage (singleLine, importance); + } + + protected override string ToolName + { + get { return IsWindows ? "jarsigner.exe" : "jarsigner"; } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidToolTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidToolTask.cs new file mode 100644 index 00000000000..bd03b8ac002 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidToolTask.cs @@ -0,0 +1,91 @@ +using System; +using System.IO; +using System.Text.RegularExpressions; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +namespace Xamarin.Android.Tasks +{ + public abstract class AndroidToolTask : ToolTask + { + protected static bool IsWindows = Path.DirectorySeparatorChar == '\\'; + + protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) + { + base.LogEventsFromTextOutput (singleLine, messageImportance); + + if (messageImportance != StandardErrorLoggingImportance) + return; + + Log.LogFromStandardError (singleLine); + } + + // Disabled because this regex does not match our errors: + // monodroid : error 1: System.InvalidOperationException: PackageName can only contain lowercase alphanumeric characters (regex: [a-z0-9.]). + + // Code from class/Microsoft.Build.Utilities/Microsoft.Build.Utilities/ToolTask.cs + //protected override void LogEventsFromTextOutput (string singleLine, MessageImportance importance) + //{ + // singleLine = singleLine.Trim (); + // if (singleLine.Length == 0) { + // Log.LogMessage (singleLine, importance); + // return; + // } + + // // When IncludeDebugInformation is true, prevents the debug symbols stats from braeking this. + // if (singleLine.StartsWith ("WROTE SYMFILE") || + // singleLine.StartsWith ("OffsetTable") || + // singleLine.StartsWith ("Compilation succeeded") || + // singleLine.StartsWith ("Compilation failed")) + // return; + + // Match match = ErrorRegex.Match (singleLine); + // if (!match.Success) { + // Log.LogMessage (importance, singleLine); + // return; + // } + + // string filename = match.Result ("${file}") ?? ""; + // string line = match.Result ("${line}"); + // int lineNumber = !string.IsNullOrEmpty (line) ? Int32.Parse (line) : 0; + + // string category = match.Result ("${level}"); + // string text = match.Result ("${message}"); + + // if (!Path.IsPathRooted (filename) && !String.IsNullOrEmpty (BaseDirectory)) + // filename = Path.Combine (BaseDirectory, filename); + + // if (String.Compare (category, "warning", StringComparison.OrdinalIgnoreCase) == 0) { + // Log.LogWarning (null, null, null, filename, lineNumber, 0, -1, + // -1, text, null); + // } else if (String.Compare (category, "error", StringComparison.OrdinalIgnoreCase) == 0) { + // Log.LogError (null, null, null, filename, lineNumber, 0, -1, + // -1, text, null); + // } else { + // Log.LogMessage (importance, singleLine); + // } + //} + + protected virtual Regex ErrorRegex { + get { return AndroidErrorRegex; } + } + + /* This gets pre-pended to any filenames that we get from error strings */ + protected string BaseDirectory { get; set; } + + static Regex androidErrorRegex; + static Regex AndroidErrorRegex { + get { + if (androidErrorRegex == null) + androidErrorRegex = new Regex (@"^(\s*(?[^:]+):(?\d*)?:\s+)*(?\w+)\s*:\s*(?.*)", RegexOptions.Compiled | RegexOptions.ExplicitCapture); + return androidErrorRegex; + } + } + + protected static string QuoteString (string value) + { + return string.Format ("\"{0}\"", value); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidUpdateResDir.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidUpdateResDir.cs new file mode 100644 index 00000000000..1621ff79dee --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidUpdateResDir.cs @@ -0,0 +1,197 @@ +// +// AndroidUpdateResDir.cs +// +// Author: +// Michael Hutchinson +// +// Copyright (c) 2010 Novell, Inc. (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.IO; +using System.Collections.Generic; +using System.Linq; + +namespace Xamarin.Android.Tasks +{ + public class AndroidComputeResPaths : Task + { + [Required] + public ITaskItem[] ResourceFiles { get; set; } + + [Required] + public string IntermediateDir { get; set; } + + public string Prefixes { get; set; } + + public bool LowercaseFilenames { get; set; } + + public string ProjectDir { get; set; } + + [Output] + public ITaskItem[] IntermediateFiles { get; set; } + + [Output] + public string ResourceNameCaseMap { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage (" IntermediateDir: {0}", IntermediateDir); + Log.LogDebugMessage (" Prefixes: {0}", Prefixes); + Log.LogDebugMessage (" ProjectDir: {0}", ProjectDir); + Log.LogDebugMessage (" LowercaseFilenames: {0}", LowercaseFilenames); + Log.LogDebugTaskItems (" ResourceFiles:", ResourceFiles); + + IntermediateFiles = new ITaskItem[ResourceFiles.Length]; + + string[] prefixes = Prefixes != null ? Prefixes.Split (';') : null; + if (prefixes != null) { + for (int i = 0; i < prefixes.Length; i++) { + string p = prefixes [i]; + char c = p [p.Length - 1]; + if (c != '\\' && c != '/') + prefixes [i] = p + Path.DirectorySeparatorChar; + } + } + + var nameCaseMap = new StringWriter (); + + for (int i = 0; i < ResourceFiles.Length; i++) { + var item = ResourceFiles [i]; + + //compute the target path + string rel; + var logicalName = item.GetMetadata ("LogicalName"); + if (item.GetMetadata ("IsWearApplicationResource") == "True") { + rel = item.ItemSpec.Substring (IntermediateDir.Length); + } else if (!string.IsNullOrEmpty (logicalName)) { + rel = logicalName; + } else { + rel = item.GetMetadata ("Link"); + if (string.IsNullOrEmpty (rel)) { + rel = item.GetMetadata ("Identity"); + if (!string.IsNullOrEmpty (ProjectDir)) { + var fullRelPath = Path.GetFullPath (rel); + var fullProjectPath = Path.GetFullPath (ProjectDir); + if (fullRelPath.StartsWith (fullProjectPath)) { + rel = fullRelPath.Replace (fullProjectPath, string.Empty); + } + } + } + if (prefixes != null) { + foreach (var p in prefixes) { + if (rel.StartsWith (p)) + rel = rel.Substring (p.Length); + } + } + } + + string baseFileName = LowercaseFilenames ? rel.ToLowerInvariant () : rel; + if (Path.GetExtension (baseFileName) == ".axml") + baseFileName = Path.ChangeExtension (baseFileName, ".xml"); + if (baseFileName != rel) + nameCaseMap.WriteLine ("{0}|{1}", rel, baseFileName); + string dest = Path.GetFullPath (Path.Combine (IntermediateDir, baseFileName)); + var newItem = new TaskItem (dest); + newItem.SetMetadata ("LogicalName", rel); + item.CopyMetadataTo (newItem); + IntermediateFiles [i] = newItem; + } + + ResourceNameCaseMap = nameCaseMap.ToString ().Replace (nameCaseMap.NewLine, ";"); + Log.LogDebugTaskItems (" IntermediateFiles:", IntermediateFiles); + Log.LogDebugTaskItems (" ResourceNameCaseMap:", ResourceNameCaseMap); + return true; + } + } + + public class RemoveUnknownFiles : Task + { + static bool IsWindows = Path.DirectorySeparatorChar == '\\'; + + [Required] + public ITaskItem[] Files { get; set; } + + [Required] + public string Directory { get; set; } + + public bool RemoveDirectories { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("RemoveUnknownFiles Task"); + Log.LogDebugTaskItems ("Files", Files); + + var absDir = Path.GetFullPath (Directory); + + HashSet knownFiles; + + // Do a case insensitive compare on windows, because the file + // system is case insensitive [Bug #645833] + if (IsWindows) + knownFiles = new HashSet (Files.Select (f => f.GetMetadata ("FullPath")), StringComparer.InvariantCultureIgnoreCase); + else + knownFiles = new HashSet (Files.Select (f => f.GetMetadata ("FullPath"))); + + var files = System.IO.Directory.GetFiles (absDir, "*", SearchOption.AllDirectories); + foreach (string f in files) + if (!knownFiles.Contains (f)) { + Log.LogDebugMessage ("Deleting File {0}", f); + MonoAndroidHelper.SetWriteable (f); + File.Delete (f); + } + + if (RemoveDirectories) { + var knownDirs = new HashSet (knownFiles.Select (d => Path.GetDirectoryName (d))); + var dirs = System.IO.Directory.GetDirectories (absDir, "*", SearchOption.AllDirectories); + + foreach (string d in dirs.OrderByDescending (s => s.Length)) + if (!knownDirs.Contains (d) && IsDirectoryEmpty (d)) { + Log.LogDebugMessage ("Deleting Directory {0}", d); + MonoAndroidHelper.SetDirectoryWriteable (d); + System.IO.Directory.Delete (d); + } + } + return true; + } + + // We are having issues with trees like this: + // - /Assets + // - /test + // - /test2 + // - myasset.txt + // /test is not in known directories, so we are trying to delete it, + // even though we need it because of its subdirectories + // [Bug #654535] + private bool IsDirectoryEmpty (string dir) + { + if (System.IO.Directory.GetFiles (dir).Length != 0) + return false; + + if (System.IO.Directory.GetDirectories (dir).Length != 0) + return false; + + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AndroidZipAlign.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidZipAlign.cs new file mode 100644 index 00000000000..099daa6b228 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AndroidZipAlign.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class AndroidZipAlign : AndroidToolTask + { + public AndroidZipAlign () + { + } + + [Required] + public ITaskItem Source { get; set; } + + [Required] + public ITaskItem DestinationDirectory { get; set; } + + int alignment = 4; + public int Alignment { + get {return alignment;} + set {alignment = value;} + } + + static readonly string strSignedUnaligned = "-Signed-Unaligned"; + protected override string GenerateCommandLineCommands () + { + string sourceFilename = Path.GetFileNameWithoutExtension (Source.ItemSpec); + if (sourceFilename.EndsWith (strSignedUnaligned)) + sourceFilename = sourceFilename.Remove (sourceFilename.Length - strSignedUnaligned.Length); + return string.Format ("{0} \"{1}\" \"{2}{3}{4}-Signed.apk\"", + Alignment, Source.ItemSpec, DestinationDirectory.ItemSpec, Path.DirectorySeparatorChar, sourceFilename); + } + + public override bool Execute () + { + Log.LogDebugMessage ("AndroidZipAlign Task"); + Log.LogDebugMessage (" Alignment: {0}", Alignment); + Log.LogDebugMessage (" Source: {0}", Source.ItemSpec); + Log.LogDebugMessage (" DestinationDirectory: {0}", DestinationDirectory.ItemSpec); + + return base.Execute (); + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + protected override string ToolName + { + get { return IsWindows ? "zipalign.exe" : "zipalign"; } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs new file mode 100644 index 00000000000..90579fb251d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Aot.cs @@ -0,0 +1,413 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.AndroidTools; + +using Java.Interop.Tools.Diagnostics; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public enum AotMode + { + Normal, + Hybrid, + Full + } + + public enum SequencePointsMode { + None, + Normal, + Offline, + } + + // can't be a single ToolTask, because it has to run mkbundle many times for each arch. + public class Aot : Task + { + [Required] + public string AndroidAotMode { get; set; } + + [Required] + public string AndroidNdkDirectory { get; set; } + + [Required] + public string AndroidApiLevel { get; set; } + + [Required] + public string SdkBinDirectory { get; set; } + + [Required] + public ITaskItem[] ResolvedAssemblies { get; set; } + + // Which ABIs to include native libs for + [Required] + public string SupportedAbis { get; set; } + + [Required] + public string AotOutputDirectory { get; set; } + + [Required] + public string IntermediateAssemblyDir { get; set; } + + public string LinkMode { get; set; } + + public bool EnableLLVM { get; set; } + + public string AndroidSequencePointsMode { get; set; } + + public ITaskItem[] AdditionalNativeLibraryReferences { get; set; } + + [Output] + public string[] NativeLibrariesReferences { get; set; } + + AotMode AotMode; + SequencePointsMode sequencePointsMode; + + public Aot () + { + } + + public override bool Execute () + { + try { + return DoExecute (); + } catch (Exception e) { + Log.LogCodedError ("XA3001", "{0}", e); + return false; + } + } + + public static bool GetAndroidAotMode(string androidAotMode, out AotMode aotMode) + { + aotMode = AotMode.Normal; + + switch ((androidAotMode ?? string.Empty).ToLowerInvariant().Trim()) + { + case "normal": + aotMode = AotMode.Normal; + return true; + case "hybrid": + aotMode = AotMode.Hybrid; + return true; + case "full": + aotMode = AotMode.Full; + return true; + } + + return false; + } + + public static bool TryGetSequencePointsMode (string value, out SequencePointsMode mode) + { + mode = SequencePointsMode.None; + switch ((value ?? string.Empty).ToLowerInvariant().Trim ()) { + case "none": + mode = SequencePointsMode.None; + return true; + case "normal": + mode = SequencePointsMode.Normal; + return true; + case "offline": + mode = SequencePointsMode.Offline; + return true; + } + return false; + } + + static string GetNdkToolchainLibraryDir(string binDir) + { + var baseDir = Path.GetFullPath(Path.Combine(binDir, "..")); + + var gccLibDir = Directory.EnumerateDirectories( + Path.Combine(baseDir, "lib", "gcc")).ToList(); + gccLibDir.Sort(); + + var libPath = gccLibDir.LastOrDefault(); + if (libPath == null) + throw new Exception("Could not find a valid NDK GCC toolchain library path"); + + gccLibDir = Directory.EnumerateDirectories(libPath).ToList(); + gccLibDir.Sort(); + + libPath = gccLibDir.LastOrDefault(); + if (libPath == null) + throw new Exception("Could not find a valid NDK GCC toolchain library path"); + + return libPath; + } + + static string QuoteFileName(string fileName) + { + var builder = new CommandLineBuilder(); + builder.AppendFileNameIfNotNull(fileName); + return builder.ToString(); + } + + static bool ValidateAotConfiguration (TaskLoggingHelper log, AndroidTargetArch arch, bool enableLLVM) + { + if (arch == AndroidTargetArch.Arm64 && enableLLVM) { + log.LogCodedError ("XA3004", "arm64-v8a architecture is not currently supported on LLVM AOT mode."); + return false; + } + + if (arch == AndroidTargetArch.X86_64) { + log.LogCodedError ("XA3004", "x86_64 architecture is not currently supported on AOT mode."); + return false; + } + + return true; + } + + static int GetNdkApiLevel(string androidNdkPath, string androidApiLevel, AndroidTargetArch arch) + { + int level = int.Parse(androidApiLevel); + // Some Android API levels do not exist on the NDK level. Workaround this my mapping them to the + // most appropriate API level that does exist. + if (level == 6 || level == 7) level = 5; + else if (level == 10) level = 9; + else if (level == 11) level = 12; + else if (level == 20) level = 19; + else if (level == 22) level = 21; + else if (level == 23) level = 21; + + // API levels below level 21 do not provide support for 64-bit architectures. + if (NdkUtil.IsNdk64BitArch(arch) && level < 21) { + level = 21; + } + + // We perform a downwards API level lookup search since we might not have hardcoded the correct API + // mapping above and we do not want to crash needlessly. + for (; level >= 5; level--) { + try { + NdkUtil.GetNdkPlatformLibPath (androidNdkPath, arch, level); + break; + } catch (InvalidOperationException ex) { + // Path not found, continue searching... + continue; + } + } + + return level; + } + + bool DoExecute () { + Log.LogDebugMessage ("Aot:", AndroidAotMode); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugMessage (" AndroidAotMode: {0}", AndroidAotMode); + Log.LogDebugMessage (" AndroidSequencePointsMode: {0}", AndroidSequencePointsMode); + Log.LogDebugMessage (" AndroidNdkDirectory: {0}", AndroidNdkDirectory); + Log.LogDebugMessage (" AotOutputDirectory: {0}", AotOutputDirectory); + Log.LogDebugMessage (" EnableLLVM: {0}", EnableLLVM); + Log.LogDebugMessage (" IntermediateAssemblyDir: {0}", IntermediateAssemblyDir); + Log.LogDebugMessage (" LinkMode: {0}", LinkMode); + Log.LogDebugMessage (" SdkBinDirectory: {0}", SdkBinDirectory); + Log.LogDebugMessage (" SupportedAbis: {0}", SupportedAbis); + Log.LogDebugTaskItems (" ResolvedAssemblies:", ResolvedAssemblies); + Log.LogDebugTaskItems (" AdditionalNativeLibraryReferences:", AdditionalNativeLibraryReferences); + + bool hasValidAotMode = GetAndroidAotMode (AndroidAotMode, out AotMode); + if (!hasValidAotMode) { + Log.LogCodedError ("XA3001", "Invalid AOT mode: {0}", AndroidAotMode); + return false; + } + + TryGetSequencePointsMode (AndroidSequencePointsMode, out sequencePointsMode); + + var nativeLibs = new List (); + + if (!Directory.Exists (AotOutputDirectory)) + Directory.CreateDirectory (AotOutputDirectory); + + // Check that we have a compatible NDK version for the targeted ABIs. + NdkUtil.NdkVersion ndkVersion; + bool hasNdkVersion = NdkUtil.GetNdkToolchainRelease (AndroidNdkDirectory, out ndkVersion); + + var abis = SupportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + foreach (var abi in abis) { + string aotCompiler = ""; + string outdir = ""; + string mtriple = ""; + AndroidTargetArch arch; + + switch (abi) { + case "armeabi": + aotCompiler = Path.Combine (SdkBinDirectory, "cross-arm"); + outdir = Path.Combine (AotOutputDirectory, "armeabi"); + mtriple = "armv5-linux-gnueabi"; + arch = AndroidTargetArch.Arm; + break; + + case "armeabi-v7a": + aotCompiler = Path.Combine (SdkBinDirectory, "cross-arm"); + outdir = Path.Combine (AotOutputDirectory, "armeabi-v7a"); + mtriple = "armv7-linux-gnueabi"; + arch = AndroidTargetArch.Arm; + break; + + case "arm64": + case "arm64-v8a": + case "aarch64": + aotCompiler = Path.Combine (SdkBinDirectory, "cross-arm64"); + outdir = Path.Combine (AotOutputDirectory, "arm64-v8a"); + mtriple = "aarch64-linux-android"; + arch = AndroidTargetArch.Arm64; + break; + + case "x86": + aotCompiler = Path.Combine (SdkBinDirectory, "cross-x86"); + outdir = Path.Combine (AotOutputDirectory, "x86"); + mtriple = "i686-linux-android"; + arch = AndroidTargetArch.X86; + break; + + case "x86_64": + aotCompiler = Path.Combine (SdkBinDirectory, "cross-x86_64"); + outdir = Path.Combine (AotOutputDirectory, "x86_64"); + mtriple = "x86_64-linux-android"; + arch = AndroidTargetArch.X86_64; + break; + + // case "mips": + default: + throw new Exception ("Unsupported Android target architecture ABI: " + abi); + } + + if (!NdkUtil.ValidateNdkPlatform (Log, AndroidNdkDirectory, arch, requireLibm:EnableLLVM)) { + return false; + } + + if (!ValidateAotConfiguration(Log, arch, EnableLLVM)) { + return false; + } + + outdir = Path.GetFullPath (outdir); + if (!Directory.Exists (outdir)) + Directory.CreateDirectory (outdir); + + var toolPrefix = NdkUtil.GetNdkToolPrefix (AndroidNdkDirectory, arch); + var toolchainPath = toolPrefix.Substring(0, toolPrefix.LastIndexOf(Path.DirectorySeparatorChar)); + var ldFlags = string.Empty; + if (EnableLLVM) { + int level = GetNdkApiLevel (AndroidNdkDirectory, AndroidApiLevel, arch); + + string androidLibPath = string.Empty; + try { + androidLibPath = NdkUtil.GetNdkPlatformLibPath(AndroidNdkDirectory, arch, level); + } catch (InvalidOperationException ex) { + Diagnostic.Error (5101, ex.Message); + } + var libs = new List() { + QuoteFileName (Path.Combine(GetNdkToolchainLibraryDir(toolchainPath), "libgcc.a")), + QuoteFileName (Path.Combine(androidLibPath, "libc.so")), + QuoteFileName (Path.Combine(androidLibPath, "libm.so")) + }; + ldFlags = string.Join(";", libs); + } + + foreach (var assembly in ResolvedAssemblies) { + string outputFile = Path.Combine(outdir, string.Format ("libaot-{0}.so", + Path.GetFileName (assembly.ItemSpec))); + + string seqpointsFile = Path.Combine(outdir, string.Format ("{0}.msym", + Path.GetFileName (assembly.ItemSpec))); + + string aotOptions = string.Format ( + "{0}--aot={8}{1}outfile={2},asmwriter,mtriple={3},tool-prefix={4},ld-flags={5},llvm-path={6},temp-path={7}", + EnableLLVM ? "--llvm " : string.Empty, + AotMode != AotMode.Normal ? string.Format("{0},", AotMode.ToString().ToLowerInvariant()) : string.Empty, + QuoteFileName (outputFile), + mtriple, + QuoteFileName (toolPrefix), + ldFlags, + QuoteFileName (SdkBinDirectory), + QuoteFileName (outdir), + sequencePointsMode == SequencePointsMode.Offline ? string.Format("gen-seq-points-file={0},", seqpointsFile) : string.Empty + ); + + // Due to a Monodroid MSBuild bug we can end up with paths to assemblies that are not in the intermediate + // assembly directory (typically obj/assemblies). This can lead to problems with the Mono loader not being + // able to find their dependency laters, since framework assemblies are stored in different directories. + // This can happen when linking is disabled (AndroidLinkMode=None). Workaround this problem by resolving + // the paths to the right assemblies manually. + var resolvedPath = Path.GetFullPath (assembly.ItemSpec); + var intermediateAssemblyPath = Path.Combine (IntermediateAssemblyDir, Path.GetFileName (assembly.ItemSpec)); + + if (LinkMode.ToLowerInvariant () == "none") { + if (!resolvedPath.Contains (IntermediateAssemblyDir) && File.Exists (intermediateAssemblyPath)) + resolvedPath = intermediateAssemblyPath; + } + + var assembliesPath = Path.GetFullPath (Path.GetDirectoryName (resolvedPath)); + var assemblyPath = QuoteFileName (Path.GetFullPath (resolvedPath)); + + if (!RunAotCompiler (assembliesPath, aotCompiler, aotOptions, assemblyPath)) { + Log.LogCodedError ("XA3001", "Could not AOT the assembly: {0}", assembly.ItemSpec); + return false; + } + nativeLibs.Add (outputFile); + } + } + + NativeLibrariesReferences = nativeLibs.ToArray (); + + Log.LogDebugTaskItems ("Aot Outputs:"); + Log.LogDebugTaskItems (" NativeLibrariesReferences: ", NativeLibrariesReferences); + + return true; + } + + bool RunAotCompiler (string assembliesPath, string aotCompiler, string aotOptions, string assembly) + { + var arguments = aotOptions + " " + assembly; + + Log.LogMessage (MessageImportance.High, "[AOT] " + assembly); + Log.LogMessage (MessageImportance.Low, "Mono arguments: " + arguments); + Log.LogMessage (MessageImportance.Low, "MONO_PATH=" + assembliesPath); + + var psi = new ProcessStartInfo () { + FileName = aotCompiler, + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + + // we do not want options to be provided out of band to the cross compilers + psi.EnvironmentVariables ["MONO_ENV_OPTIONS"] = String.Empty; + // the C code cannot parse all the license details, including the activation code that tell us which license level is allowed + // so we provide this out-of-band to the cross-compilers - this can be extended to communicate a few others bits as well + psi.EnvironmentVariables ["MONO_PATH"] = assembliesPath; + if (sequencePointsMode != SequencePointsMode.None) + psi.EnvironmentVariables ["MONO_DEBUG"] = "gen-compact-seq-points"; + + var proc = new Process (); + proc.OutputDataReceived += OnAotOutputData; + proc.ErrorDataReceived += OnAotErrorData; + proc.StartInfo = psi; + proc.Start (); + proc.BeginOutputReadLine (); + proc.BeginErrorReadLine (); + proc.WaitForExit (); + return proc.ExitCode == 0; + } + + void OnAotOutputData (object sender, DataReceivedEventArgs e) + { + if (e.Data != null) + Log.LogMessage ("[aot-compiler stdout] {0}", e.Data); + } + + void OnAotErrorData (object sender, DataReceivedEventArgs e) + { + if (e.Data != null) + Log.LogMessage ("[aot-compiler stderr] {0}", e.Data); + } + + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/AsyncTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/AsyncTask.cs new file mode 100644 index 00000000000..f045dbb4bfb --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/AsyncTask.cs @@ -0,0 +1,280 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Threading; +using System.Collections; + +namespace Xamarin.Android.Tasks +{ + public class AsyncTask : Task, ICancelableTask { + + CancellationTokenSource tcs = new CancellationTokenSource (); + Queue logMessageQueue =new Queue (); + Queue warningMessageQueue = new Queue (); + Queue errorMessageQueue = new Queue (); + ManualResetEvent logDataAvailable = new ManualResetEvent (false); + ManualResetEvent errorDataAvailable = new ManualResetEvent (false); + ManualResetEvent warningDataAvailable = new ManualResetEvent (false); + ManualResetEvent taskCancelled = new ManualResetEvent (false); + ManualResetEvent completed = new ManualResetEvent (false); + bool isRunning = true; + object _eventlock = new object (); + int UIThreadId = 0; + + private enum WaitHandleIndex + { + LogDataAvailable, + ErrorDataAvailable, + WarningDataAvailable, + TaskCancelled, + Completed, + } + + public CancellationToken Token { get { return tcs.Token; } } + + public bool YieldDuringExecution { get; set; } + + [Obsolete ("Do not use the Log.LogXXXX from within your Async task as it will Lock the Visual Studio UI. Use the this.LogXXXX methods instead.")] + private new TaskLoggingHelper Log + { + get { return base.Log; } + } + + public AsyncTask () + { + YieldDuringExecution = false; + UIThreadId = Thread.CurrentThread.ManagedThreadId; + } + + public void Cancel () + { + taskCancelled.Set (); + } + + public void Complete() + { + completed.Set (); + } + + public void LogDebugTaskItems (string message, params string[] items) + { + LogDebugMessage (message); + + if (items == null) + return; + + foreach (var item in items) + LogDebugMessage (" {0}", item); + } + + public void LogDebugTaskItems (string message, ITaskItem[] items) + { + LogDebugMessage (message); + + if (items == null) + return; + + foreach (var item in items) + LogDebugMessage (" {0}", item.ItemSpec); + } + + protected void LogMessage (string message, params object[] messageArgs) + { + LogMessage (string.Format (message, messageArgs)); + } + + protected void LogDebugMessage (string message, params object[] messageArgs) + { + LogMessage (string.Format (message, messageArgs), importance: MessageImportance.Low); + } + + protected void LogMessage (string message, MessageImportance importance = MessageImportance.Normal) + { + if (UIThreadId == Thread.CurrentThread.ManagedThreadId) + #pragma warning disable 618 + Log.LogMessage (importance, message); + #pragma warning restore 618 + + lock (logMessageQueue.SyncRoot) { + logMessageQueue.Enqueue (new BuildMessageEventArgs ( + message: message, + helpKeyword : null, + senderName: null, + importance: importance + )); + lock (_eventlock) { + if (isRunning) + logDataAvailable.Set (); + } + } + } + + protected void LogError (string message, params object[] messageArgs) + { + LogError (string.Format (message, messageArgs)); + } + + protected void LogCodedError (string code, string message, params object[] messageArgs) + { + LogError (code, string.Format (message, messageArgs)); + } + + protected void LogError (string code, string message) + { + if (UIThreadId == Thread.CurrentThread.ManagedThreadId) + #pragma warning disable 618 + Log.LogError ( + subcategory: null, + errorCode: code, + helpKeyword: null, + file: null, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: message + ); + #pragma warning restore 618 + + lock (errorMessageQueue.SyncRoot) { + errorMessageQueue.Enqueue (new BuildErrorEventArgs ( + subcategory: null, + code: code, + file: null, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: message, + helpKeyword: null, + senderName: null + )); + lock (_eventlock) { + if (isRunning) + errorDataAvailable.Set (); + } + } + } + + protected void LogWarning (string message, params object[] messageArgs) + { + LogWarning (string.Format (message, messageArgs)); + } + + protected void LogWarning (string message) + { + if (UIThreadId == Thread.CurrentThread.ManagedThreadId) + #pragma warning disable 618 + Log.LogWarning (message); + #pragma warning restore 618 + + lock (warningMessageQueue.SyncRoot) { + warningMessageQueue.Enqueue (new BuildWarningEventArgs ( + subcategory: null, + code: null, + file: null, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: message, + helpKeyword: null, + senderName: null + )); + lock (_eventlock) { + if (isRunning) + warningDataAvailable.Set (); + } + } + } + + public override bool Execute () + { + WaitForCompletion (); + #pragma warning disable 618 + return !Log.HasLoggedErrors; + #pragma warning restore 618 + } + + private void LogMessages () + { + lock (logMessageQueue.SyncRoot) { + while (logMessageQueue.Count > 0) { + var args = (BuildMessageEventArgs)logMessageQueue.Dequeue (); + #pragma warning disable 618 + Log.LogMessage (args.Importance, args.Message); + #pragma warning restore 618 + } + logDataAvailable.Reset (); + } + } + + private void LogErrors () + { + lock (errorMessageQueue.SyncRoot) { + while (errorMessageQueue.Count > 0) { + var args = (BuildErrorEventArgs)errorMessageQueue.Dequeue (); + #pragma warning disable 618 + Log.LogCodedError (args.Code, args.Message); + #pragma warning restore 618 + } + errorDataAvailable.Reset (); + } + } + + private void LogWarnings () + { + lock (warningMessageQueue.SyncRoot) { + while (warningMessageQueue.Count > 0) { + var args = (BuildWarningEventArgs)warningMessageQueue.Dequeue (); + #pragma warning disable 618 + Log.LogWarning (args.Message); + #pragma warning restore 618 + } + warningDataAvailable.Reset (); + } + } + + protected void WaitForCompletion () + { + WaitHandle[] handles = new WaitHandle[] { + logDataAvailable, + errorDataAvailable, + warningDataAvailable, + taskCancelled, + completed, + }; + if (YieldDuringExecution && BuildEngine is IBuildEngine3) + (BuildEngine as IBuildEngine3).Yield (); + try { + while (isRunning) { + var index = (WaitHandleIndex)System.Threading.WaitHandle.WaitAny (handles, TimeSpan.FromMilliseconds (10)); + switch (index) { + case WaitHandleIndex.LogDataAvailable: + LogMessages (); + break; + case WaitHandleIndex.ErrorDataAvailable: + LogErrors (); + break; + case WaitHandleIndex.WarningDataAvailable: + LogWarnings (); + break; + case WaitHandleIndex.TaskCancelled: + tcs.Cancel (); + isRunning = false; + break; + case WaitHandleIndex.Completed: + isRunning = false; + break; + } + } + + } + finally { + if (YieldDuringExecution && BuildEngine is IBuildEngine3) + (BuildEngine as IBuildEngine3).Reacquire (); + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs new file mode 100644 index 00000000000..14ccf3352f7 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/BuildApk.cs @@ -0,0 +1,549 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Reflection; +using System.Text; +using System.Text.RegularExpressions; + +using Ionic.Zlib; +using Ionic.Zip; + +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +using Java.Interop.Tools.Cecil; + +using ArchiveFileList = System.Collections.Generic.List>; +using Mono.Cecil; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class BuildApk : Task + { + public string AndroidNdkDirectory { get; set; } + + public string SdkBinDirectory { get; set; } + + [Required] + public string ApkInputPath { get; set; } + + [Required] + public string ApkOutputPath { get; set; } + + [Required] + public ITaskItem[] ResolvedUserAssemblies { get; set; } + + [Required] + public ITaskItem[] ResolvedFrameworkAssemblies { get; set; } + + public ITaskItem[] AdditionalNativeLibraryReferences { get; set; } + + public ITaskItem[] EmbeddedNativeLibraryAssemblies { get; set; } + + [Required] + public ITaskItem[] NativeLibraries { get; set; } + + public ITaskItem[] BundleNativeLibraries { get; set; } + + public ITaskItem[] Environments { get; set; } + + [Required] + public ITaskItem[] TypeMappings { get; set; } + + [Required] + public string [] DalvikClasses { get; set; } + + [Required] + public string SupportedAbis { get; set; } + + public bool CreatePackagePerAbi { get; set; } + + [Required] + public string UseSharedRuntime { get; set; } + + public bool EmbedAssemblies { get; set; } + + public bool BundleAssemblies { get; set; } + + public ITaskItem[] JavaSourceFiles { get; set; } + + public ITaskItem[] JavaLibraries { get; set; } + + public string[] DoNotPackageJavaLibraries { get; set; } + + public string Debug { get; set; } + + public string AndroidAotMode { get; set; } + + public string AndroidSequencePointsMode { get; set; } + + public bool EnableLLVM { get; set; } + + public string StubApplicationDataFile { get; set; } + public string AndroidGdbDebugServer { get; set; } + public string AndroidEmbedProfilers { get; set; } + public string HttpClientHandlerType { get; set; } + + [Output] + public ITaskItem[] OutputFiles { get; set; } + + [Output] + public string BuildId { get; set; } + + bool _Debug { + get { + return string.Equals (Debug, "true", StringComparison.OrdinalIgnoreCase); + } + } + + SequencePointsMode sequencePointsMode = SequencePointsMode.None; + + AndroidDebugServer debugServer = AndroidDebugServer.Gdb; + Guid buildId = Guid.NewGuid (); + + public ITaskItem[] LibraryProjectJars { get; set; } + + void ExecuteWithAbi (string supportedAbis, string apkInputPath, string apkOutputPath) + { + ArchiveFileList files = new ArchiveFileList (); + using (ZipFile apk = apkInputPath != null ? MonoAndroidHelper.ReadZipFile (apkInputPath) : new ZipFile ()) { + + apk.AddEntry ("NOTICE", + Assembly.GetExecutingAssembly ().GetManifestResourceStream ("NOTICE.txt")); + + // Add classes.dx + apk.AddFiles (DalvikClasses, string.Empty); + + if (EmbedAssemblies && !BundleAssemblies) + AddAssemblies (apk); + + AddEnvironment (apk); + AddRuntimeLibraries (apk, supportedAbis); + AddNativeLibraries (files, supportedAbis); + AddAdditionalNativeLibraries (files, supportedAbis); + AddNativeLibrariesFromAssemblies (apk, supportedAbis); + + foreach (ITaskItem typemap in TypeMappings) { + apk.AddFile (typemap.ItemSpec, "").CompressionLevel = CompressionLevel.None; + } + + foreach (var file in files) { + var item = Path.Combine (file.Item2, Path.GetFileName (file.Item1)) + .Replace (Path.DirectorySeparatorChar, '/'); + if (apk.ContainsEntry (item)) { + Log.LogWarning (null, "XA4301", null, file.Item1, 0, 0, 0, 0, "Apk already contains the item {0}; ignoring.", item); + continue; + } + apk.AddFile (file.Item1, file.Item2); + } + if (_Debug) + AddGdbservers (apk, files, supportedAbis, debugServer); + + var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where (f => f.ItemSpec.EndsWith (".jar")) : null; + if (jarFiles != null && JavaLibraries != null) + jarFiles = jarFiles.Concat (JavaLibraries); + else if (JavaLibraries != null) + jarFiles = JavaLibraries; + + var libraryProjectJars = MonoAndroidHelper.ExpandFiles (LibraryProjectJars) + .Where (jar => !MonoAndroidHelper.IsEmbeddedReferenceJar (jar)); + + var jarFilePaths = libraryProjectJars.Concat (jarFiles != null ? jarFiles.Select (j => j.ItemSpec) : Enumerable.Empty ()); + jarFilePaths = MonoAndroidHelper.DistinctFilesByContent (jarFilePaths); + + foreach (var jarFile in jarFilePaths) { + using (var jar = MonoAndroidHelper.ReadZipFile (jarFile)) { + foreach (var jarItem in jar.Where (ze => !ze.IsDirectory && !ze.FileName.StartsWith ("META-INF") && !ze.FileName.EndsWith (".class") && !ze.FileName.EndsWith (".java") && !ze.FileName.EndsWith ("MANIFEST.MF"))) { + byte[] data; + using (var d = new System.IO.MemoryStream ()) + using (var i = jarItem.OpenReader ()) { + i.CopyTo (d); + data = d.ToArray (); + } + if (apk.Entries.Any (e => e.FileName == jarItem.FileName)) + Log.LogMessage ("Warning: failed to add jar entry {0} from {1}: the same file already exists in the apk", jarItem.FileName, Path.GetFileName (jarFile)); + else + apk.AddEntry (jarItem.FileName, data); + } + } + } + if (StubApplicationDataFile != null && File.Exists (StubApplicationDataFile)) + AddZipEntry (apk, StubApplicationDataFile, string.Empty); + + apk.Save (apkOutputPath + "new"); + MonoAndroidHelper.CopyIfZipChanged (apkOutputPath + "new", apkOutputPath); + File.Delete (apkOutputPath + "new"); + } + + } + + public override bool Execute () + { + Log.LogDebugMessage ("BuildApk Task"); + Log.LogDebugMessage (" ApkInputPath: {0}", ApkInputPath); + Log.LogDebugMessage (" ApkOutputPath: {0}", ApkOutputPath); + Log.LogDebugMessage (" BundleAssemblies: {0}", BundleAssemblies); + Log.LogDebugTaskItems (" DalvikClasses:", DalvikClasses); + Log.LogDebugMessage (" SupportedAbis: {0}", SupportedAbis); + Log.LogDebugMessage (" UseSharedRuntime: {0}", UseSharedRuntime); + Log.LogDebugMessage (" Debug: {0}", Debug ?? "no"); + Log.LogDebugMessage (" EmbedAssemblies: {0}", EmbedAssemblies); + Log.LogDebugMessage (" AndroidAotMode: {0}", AndroidAotMode); + Log.LogDebugMessage (" AndroidSequencePointsMode: {0}", AndroidSequencePointsMode); + Log.LogDebugTaskItems (" Environments:", Environments); + Log.LogDebugTaskItems (" ResolvedUserAssemblies:", ResolvedUserAssemblies); + Log.LogDebugTaskItems (" ResolvedFrameworkAssemblies:", ResolvedFrameworkAssemblies); + Log.LogDebugTaskItems (" NativeLibraries:", NativeLibraries); + Log.LogDebugTaskItems (" AdditionalNativeLibraryReferences:", AdditionalNativeLibraryReferences); + Log.LogDebugTaskItems (" BundleNativeLibraries:", BundleNativeLibraries); + Log.LogDebugTaskItems (" JavaSourceFiles:", JavaSourceFiles); + Log.LogDebugTaskItems (" JavaLibraries:", JavaLibraries); + Log.LogDebugTaskItems (" LibraryProjectJars:", LibraryProjectJars); + Log.LogDebugTaskItems (" AdditionalNativeLibraryReferences:", AdditionalNativeLibraryReferences); + Log.LogDebugTaskItems (" HttpClientHandlerType:", HttpClientHandlerType); + + Aot.TryGetSequencePointsMode (AndroidSequencePointsMode, out sequencePointsMode); + + var androidDebugServer = GdbPaths.GetAndroidDebugServer (AndroidGdbDebugServer); + if (!androidDebugServer.HasValue) { + Log.LogError ("Unable to determine debug server variant: {0}", AndroidGdbDebugServer); + return false; + } + debugServer = androidDebugServer.Value; + + var outputFiles = new List (); + + ExecuteWithAbi (SupportedAbis, ApkInputPath, ApkOutputPath); + outputFiles.Add (ApkOutputPath); + if (CreatePackagePerAbi) { + var abis = SupportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + if (abis.Length > 1) + foreach (var abi in abis) { + var path = Path.GetDirectoryName (ApkOutputPath); + var apk = Path.GetFileNameWithoutExtension (ApkOutputPath); + ExecuteWithAbi (abi, String.Format ("{0}-{1}", ApkInputPath, abi), + Path.Combine (path, String.Format ("{0}-{1}.apk", apk, abi))); + outputFiles.Add (Path.Combine (path, String.Format ("{0}-{1}.apk", apk, abi))); + } + } + + BuildId = buildId.ToString (); + + Log.LogDebugMessage (" [Output] BuildId: {0}", BuildId); + + OutputFiles = outputFiles.Select (a => new TaskItem (a)).ToArray (); + + Log.LogDebugTaskItems (" [Output] OutputFiles :", OutputFiles); + + return !Log.HasLoggedErrors; + } + + void AddZipEntry (ZipFile apk, string file, string path) + { + apk.AddFile (file, path); + } + + private void AddAssemblies (ZipFile apk) + { + bool debug = _Debug; + bool use_shared_runtime = String.Equals (UseSharedRuntime, "true", StringComparison.OrdinalIgnoreCase); + + foreach (ITaskItem assembly in ResolvedUserAssemblies) { + // Add assembly + apk.AddFile (assembly.ItemSpec, GetTargetDirectory (assembly.ItemSpec)).CompressionLevel = CompressionLevel.None; + + // Try to add config if exists + var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config"); + if (File.Exists (config)) + apk.AddFile (config, "assemblies").CompressionLevel = CompressionLevel.None; + + // Try to add symbols if Debug + if (debug) { + var symbols = Path.ChangeExtension (assembly.ItemSpec, "dll.mdb"); + + if (File.Exists (symbols)) + apk.AddFile (symbols, "assemblies").CompressionLevel = CompressionLevel.None; + } + } + + if (use_shared_runtime) + return; + + // Add framework assemblies + foreach (ITaskItem assembly in ResolvedFrameworkAssemblies) { + apk.AddFile (assembly.ItemSpec, "assemblies").CompressionLevel = CompressionLevel.None; + var config = Path.ChangeExtension (assembly.ItemSpec, "dll.config"); + if (File.Exists (config)) + apk.AddFile (config).CompressionLevel = CompressionLevel.None; + // Try to add symbols if Debug + if (debug) { + var symbols = Path.ChangeExtension (assembly.ItemSpec, "dll.mdb"); + + if (File.Exists (symbols)) + apk.AddFile (symbols, "assemblies").CompressionLevel = CompressionLevel.None; + } + } + } + static string GetTargetDirectory (string path) + { + string culture, file; + if (SatelliteAssembly.TryGetSatelliteCultureAndFileName (path, out culture, out file)) { + return "assemblies/" + culture; + } + return "assemblies"; + } + + void AddEnvironment (ZipFile apk) + { + var environment = new StringWriter () { + NewLine = "\n", + }; + + if (EnableLLVM) { + environment.WriteLine ("mono.llvm=true"); + } + + AotMode aotMode; + if (AndroidAotMode != null && Aot.GetAndroidAotMode(AndroidAotMode, out aotMode)) { + environment.WriteLine ("mono.aot={0}", aotMode.ToString().ToLowerInvariant()); + } + + const string defaultLogLevel = "MONO_LOG_LEVEL=info"; + const string defaultMonoDebug = "MONO_DEBUG=gen-compact-seq-points"; + const string defaultHttpMessageHandler = "XA_HTTP_CLIENT_HANDLER_TYPE=Xamarin.Android.Net.AndroidClientHandler"; + string xamarinBuildId = string.Format ("XAMARIN_BUILD_ID={0}", buildId); + + if (Environments == null) { + if (_Debug) + environment.WriteLine (defaultLogLevel); + if (sequencePointsMode != SequencePointsMode.None) + environment.WriteLine (defaultMonoDebug); + environment.WriteLine (xamarinBuildId); + apk.AddEntry ("environment", environment.ToString(), + new UTF8Encoding (encoderShouldEmitUTF8Identifier:false)); + return; + } + + bool haveLogLevel = false; + bool haveMonoDebug = false; + bool havebuildId = false; + bool haveHttpMessageHandler = false; + + foreach (ITaskItem env in Environments) { + environment.WriteLine ("## Source File: {0}", env.ItemSpec); + foreach (string line in File.ReadLines (env.ItemSpec)) { + var lineToWrite = line; + if (lineToWrite.StartsWith ("MONO_LOG_LEVEL=", StringComparison.Ordinal)) + haveLogLevel = true; + if (lineToWrite.StartsWith ("XAMARIN_BUILD_ID=", StringComparison.Ordinal)) + havebuildId = true; + if (lineToWrite.StartsWith ("MONO_DEBUG=", StringComparison.Ordinal)) { + haveMonoDebug = true; + if (sequencePointsMode != SequencePointsMode.None && !lineToWrite.Contains ("gen-compact-seq-points")) + lineToWrite = line + ",gen-compact-seq-points"; + } + if (lineToWrite.StartsWith ("XA_HTTP_CLIENT_HANDLER_TYPE=", StringComparison.Ordinal)) + haveHttpMessageHandler = true; + environment.WriteLine (lineToWrite); + } + } + + if (_Debug && !haveLogLevel) { + environment.WriteLine (defaultLogLevel); + } + + if (sequencePointsMode != SequencePointsMode.None && !haveMonoDebug) { + environment.WriteLine (defaultMonoDebug); + } + + if (!havebuildId) + environment.WriteLine (xamarinBuildId); + + if (!haveHttpMessageHandler) + environment.WriteLine (HttpClientHandlerType == null ? defaultHttpMessageHandler : $"XA_HTTP_CLIENT_HANDLER_TYPE={HttpClientHandlerType.Trim ()}"); + + apk.AddEntry ("environment", environment.ToString (), + new UTF8Encoding (encoderShouldEmitUTF8Identifier:false)); + } + + class LibInfo + { + public string Path; + public string Abi; + } + + static readonly string[] ArmAbis = new[]{ + "arm64-v8a", + "armeabi-v7a", + "armeabi", + }; + + public static readonly string[] ValidProfilers = new[]{ + "log", + }; + + HashSet ParseProfilers (string value) + { + var results = new HashSet (); + var values = value.Split (',', ';'); + foreach (var v in values) { + if (string.Compare (v, "all", true) == 0) { + results.UnionWith (ValidProfilers); + break; + } + if (Array.BinarySearch (ValidProfilers, v, StringComparer.OrdinalIgnoreCase) < 0) + throw new InvalidOperationException ("Unsupported --profiler value: " + v + "."); + results.Add (v.ToLowerInvariant ()); + } + return results; + } + + void AddProfilers (ZipFile apk, string abi) + { + var root = Path.GetDirectoryName (typeof(BuildApk).Assembly.Location); + if (!string.IsNullOrEmpty (AndroidEmbedProfilers)) { + foreach (var profiler in ParseProfilers (AndroidEmbedProfilers)) { + var library = string.Format ("libmono-profiler-{1}.{0}.so", abi, profiler); + var path = Path.Combine (root, "lib", abi, library); + apk.AddEntry (string.Format ("lib/{0}/libmono-profiler.so", abi), File.OpenRead (path)); + } + } + } + + void AddRuntimeLibraries (ZipFile apk, string supportedAbis) + { + var root = Path.GetDirectoryName (typeof(BuildApk).Assembly.Location); + bool use_shared_runtime = String.Equals (UseSharedRuntime, "true", StringComparison.OrdinalIgnoreCase); + var abis = supportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + foreach (var abi in abis) { + string library = string.Format ("libmono-android.{0}.{1}.so", _Debug ? "debug" : "release", abi); + var path = Path.Combine (root, "lib", abi, library); + apk.AddEntry (string.Format ("lib/{0}/libmonodroid.so", abi), File.OpenRead (path)); + if (!use_shared_runtime) { + // include the sgen + library = string.Format ("libmonosgen-2.0.{0}.so", abi); + path = Path.Combine (root, "lib", abi, library); + apk.AddEntry (string.Format ("lib/{0}/libmonosgen-2.0.so", abi), File.OpenRead (path)); + } + AddProfilers (apk, abi); + } + } + + void AddNativeLibrariesFromAssemblies (ZipFile apk, string supportedAbis) + { + var abis = supportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + var res = new DirectoryAssemblyResolver (Console.WriteLine, loadDebugSymbols: false); + foreach (var ass in EmbeddedNativeLibraryAssemblies) + res.Load (ass.ItemSpec); + foreach (var assPath in EmbeddedNativeLibraryAssemblies) { + var ass = res.GetAssembly (assPath.ItemSpec); + foreach (var mod in ass.Modules) { + var ressozip = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidNativeLibraries__.zip") as EmbeddedResource; + if (ressozip == null) + continue; + var data = ressozip.GetResourceData (); + using (var ms = new MemoryStream (data)) { + using (var zip = ZipFile.Read (ms)) { + foreach (var e in zip.Entries.Where (x => abis.Any (a => x.FileName.Contains (a)))) { + if (e.IsDirectory) + continue; + var key = e.FileName.Replace ("native_library_imports", "lib"); + if (apk.ContainsEntry (key)) { + Log.LogCodedWarning ("4301", "Apk already contains the item {0}; ignoring.", key); + continue; + } + using (var s = new MemoryStream ()) { + e.Extract (s); + apk.AddEntry (key, s.ToArray ()); + } + } + } + } + } + } + } + + private void AddNativeLibraries (ArchiveFileList files, string supportedAbis) + { + var libs = NativeLibraries.Concat (BundleNativeLibraries ?? Enumerable.Empty ()) + .Select (v => new LibInfo { Path = v.ItemSpec, Abi = GetNativeLibraryAbi (v) }); + + AddNativeLibraries (files, supportedAbis, libs); + } + + string GetNativeLibraryAbi (ITaskItem lib) + { + // If Abi is explicitly specified, simply return it. + var lib_abi = MonoAndroidHelper.GetNativeLibraryAbi (lib); + + if (string.IsNullOrWhiteSpace (lib_abi)) { + Log.LogError ("Cannot determine abi of native library {0}.", lib); + return null; + } + + return lib_abi; + } + + void AddNativeLibraries (ArchiveFileList files, string supportedAbis, System.Collections.Generic.IEnumerable libs) + { + if (libs.Any (lib => lib.Abi == null)) + Log.LogCodedWarning ( + "4301", + "Could not determine abi of some native libraries, ignoring those: " + + string.Join (", ", libs.Where (lib => lib.Abi == null).Select (lib => lib.Path))); + libs = libs.Where (lib => lib.Abi != null); + + var abis = supportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + libs = libs.Where (lib => abis.Contains (lib.Abi)); + foreach (var arm in ArmAbis) + foreach (var info in libs.Where (lib => lib.Abi == arm)) + AddNativeLibrary (files, info.Path, info.Abi); + foreach (var info in libs.Where (lib => !ArmAbis.Contains (lib.Abi))) + AddNativeLibrary (files, info.Path, info.Abi); + } + + private void AddAdditionalNativeLibraries (ArchiveFileList files, string supportedAbis) + { + if (AdditionalNativeLibraryReferences == null || !AdditionalNativeLibraryReferences.Any ()) + return; + + var libs = AdditionalNativeLibraryReferences + .Select (l => new LibInfo { Path = l.ItemSpec, Abi = MonoAndroidHelper.GetNativeLibraryAbi (l) }); + + AddNativeLibraries (files, supportedAbis, libs); + } + + void AddNativeLibrary (ArchiveFileList files, string path, string abi) + { + Log.LogMessage (MessageImportance.Low, "\tAdding {0}", path); + files.Add (new Tuple (path, string.Format ("lib/{0}", abi))); + } + + private void AddGdbservers (ZipFile apk, ArchiveFileList files, string supportedAbis, AndroidDebugServer debugServer) + { + if (string.IsNullOrEmpty (AndroidNdkDirectory)) + return; + + foreach (var sabi in supportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)) { + var arch = GdbPaths.GetArchFromAbi (sabi); + var abi = GdbPaths.GetAbiFromArch (arch); + if (abi == null) + continue; + var debugServerFile = GdbPaths.GetDebugServerFileName (debugServer); + if (files.Any (f => string.Equals (Path.GetFileName (f.Item1), debugServerFile, StringComparison.Ordinal) && + string.Equals (f.Item2, "lib/" + sabi, StringComparison.Ordinal))) + continue; + var entryName = string.Format ("lib/{0}/{1}", sabi, debugServerFile); + var debugServerPath = GdbPaths.GetDebugServerPath (debugServer, arch, AndroidNdkDirectory, SdkBinDirectory); + if (!File.Exists (debugServerPath)) + continue; + Log.LogDebugMessage ("Adding {0} debug server '{1}' to the APK as '{2}'", sabi, debugServerPath, entryName); + apk.AddEntry (entryName, File.OpenRead (debugServerPath)); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CalculateAdditionalResourceCacheDirectories.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CalculateAdditionalResourceCacheDirectories.cs new file mode 100644 index 00000000000..63c0e34882e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CalculateAdditionalResourceCacheDirectories.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Security.Cryptography; +using System.Text.RegularExpressions; +using System.Text; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CalculateAdditionalResourceCacheDirectories : Task + { + [Required] + public string[] AdditionalAndroidResourcePaths { get; set; } + + [Required] + public string CacheDirectory { get; set; } + + [Output] + public ITaskItem[] AdditionalResourceCachePaths { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("CalculateAdditionalResourceCacheDirectories Task"); + Log.LogDebugTaskItems (" AdditionalAndroidResourcePaths:", AdditionalAndroidResourcePaths); + Log.LogDebugMessage (" CacheDirectory: {0}", CacheDirectory); + + if (!AdditionalAndroidResourcePaths.Any ()) + return true; + + var md5 = MD5.Create (); + Directory.CreateDirectory (CacheDirectory); + var directories = new List (); + + foreach (var path in AdditionalAndroidResourcePaths) { + var cacheSubDirectory = string.Concat (md5.ComputeHash ( + Encoding.UTF8.GetBytes (path)).Select (b => b.ToString ("X02")) + ); + var targetDir = Path.Combine (CacheDirectory, cacheSubDirectory); + directories.Add (new TaskItem (Path.GetFullPath (targetDir).TrimEnd (Path.DirectorySeparatorChar))); + } + AdditionalResourceCachePaths = directories.ToArray (); + + Log.LogDebugTaskItems (" [Output] AdditionalResourceCachePaths:", AdditionalResourceCachePaths); + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CheckDuplicateJavaLibraries.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CheckDuplicateJavaLibraries.cs new file mode 100644 index 00000000000..78e3f7da406 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CheckDuplicateJavaLibraries.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CheckDuplicateJavaLibraries : Task + { + public ITaskItem[] JavaSourceFiles { get; set; } + public ITaskItem[] JavaLibraries { get; set; } + public ITaskItem[] LibraryProjectJars { get; set; } + + public override bool Execute () + { + Log.LogDebugTaskItems (" JavaSourceFiles:", JavaSourceFiles); + Log.LogDebugTaskItems (" JavaLibraries:", JavaLibraries); + Log.LogDebugTaskItems (" LibraryProjectJars:", LibraryProjectJars); + + var jarFiles = (JavaSourceFiles != null) ? JavaSourceFiles.Where (f => f.ItemSpec.EndsWith (".jar")) : null; + if (jarFiles != null && JavaLibraries != null) + jarFiles = jarFiles.Concat (JavaLibraries); + else if (JavaLibraries != null) + jarFiles = JavaLibraries; + var jarFilePaths = (LibraryProjectJars ?? new ITaskItem [0]).Concat (jarFiles ?? new ITaskItem [0]).Select (j => j.ItemSpec); + + // Remove duplicate identical jars by name, size and content, and reject any jars that conflicts by name (i.e. different content). + var jars = MonoAndroidHelper.DistinctFilesByContent (jarFilePaths).ToArray (); + var dups = MonoAndroidHelper.GetDuplicateFileNames (jars, new string [] {"classes.jar"}); + if (dups.Any ()) { + Log.LogError ("You have Jar libraries, {0}, that have the identical name with inconsistent file contents. Please make sure to remove any conflicting libraries in EmbeddedJar, InputJar and AndroidJavaLibrary.", String.Join (", ", dups.ToArray ())); + return false; + } + + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CheckForRemovedItems.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CheckForRemovedItems.cs new file mode 100644 index 00000000000..00516394adc --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CheckForRemovedItems.cs @@ -0,0 +1,44 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CheckForRemovedItems : Task + { + [Required] + public ITaskItem[] Files { get; set; } + + [Required] + public string Directory { get; set; } + + [Output] + public ITaskItem RemovedFilesFlag { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("RemoveUnknownFiles Task"); + Log.LogDebugTaskItems ("Files", Files); + Log.LogDebugMessage (" Directory:", Directory); + + var absDir = Path.GetFullPath (Directory); + + HashSet knownFiles; + + knownFiles = new HashSet (Files.Select (f => f.GetMetadata ("FullPath")), StringComparer.InvariantCultureIgnoreCase); + + RemovedFilesFlag = null; + var files = System.IO.Directory.GetFiles (absDir, "*", SearchOption.AllDirectories); + foreach (string f in files) + if (!knownFiles.Contains (f)) { + RemovedFilesFlag = new TaskItem (Path.Combine (absDir, "removedfiles.flag")); + } + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CheckProjectItems.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CheckProjectItems.cs new file mode 100644 index 00000000000..5bedf6ed89c --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CheckProjectItems.cs @@ -0,0 +1,45 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.IO; + +namespace Xamarin.Android.Tasks +{ + public class CheckProjectItems : Task + { + public bool IsApplication { get; set; } + public ITaskItem [] EmbeddedNativeLibraries { get; set; } + public ITaskItem [] NativeLibraries { get; set; } + public ITaskItem [] JavaLibraries { get; set; } + public ITaskItem [] JavaSourceFiles { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("CheckProjectItems Task"); + Log.LogDebugTaskItems (" NativeLibraries:", NativeLibraries); + Log.LogDebugTaskItems (" JavaLibraries:", JavaLibraries); + Log.LogDebugTaskItems (" JavaSourceFiles:", JavaSourceFiles); + if (IsApplication && EmbeddedNativeLibraries != null && EmbeddedNativeLibraries.Length > 0) { + foreach (ITaskItem lib in EmbeddedNativeLibraries) { + Log.LogError ( + subcategory: string.Empty, + errorCode: "XA0100", + helpKeyword: string.Empty, + file: lib.ItemSpec, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: "EmbeddedNativeLibrary '{0}' is invalid in Android Application project. Please use AndroidNativeLibrary instead.", + messageArgs: new [] {lib.ItemSpec} + ); + } + return false; + } + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CheckTargetFrameworks.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CheckTargetFrameworks.cs new file mode 100644 index 00000000000..e09fa451d23 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CheckTargetFrameworks.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.IO; +using System.Linq; + +using Java.Interop.Tools.Cecil; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CheckTargetFrameworks : Task + { + + [Required] + public ITaskItem[] ResolvedAssemblies { get; set; } + + [Required] + public string TargetFrameworkVersion { get; set; } + + [Required] + public string ProjectFile { get; set; } + + Dictionary apiLevels = new Dictionary (); + DirectoryAssemblyResolver res; + + int ExtractApiLevel(ITaskItem ass) + { + Log.LogDebugMessage (ass.ItemSpec); + foreach (var ca in res.GetAssembly (ass.ItemSpec).CustomAttributes) { + switch (ca.AttributeType.FullName) { + case "System.Runtime.Versioning.TargetFrameworkAttribute": + foreach (var p in ca.ConstructorArguments) { + var value = p.Value.ToString (); + if (value.StartsWith ("MonoAndroid")) { + var values = value.Split ('='); + return AndroidVersion.TryOSVersionToApiLevel (values[1]); + } + } + break; + } + } + return 0; + } + + public override bool Execute () + { + Log.LogDebugMessage ("CheckTargetFrameworks Task"); + Log.LogDebugMessage (" TargetFrameworkVersion: {0}", TargetFrameworkVersion); + Log.LogDebugMessage (" ProjectFile: {0}", ProjectFile); + Log.LogDebugTaskItems (" ResolvedUserAssemblies: {0}", ResolvedAssemblies); + + res = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols: false); + foreach (var ass in ResolvedAssemblies) { + res.Load (Path.GetFullPath (ass.ItemSpec)); + var apiLevel = ExtractApiLevel (ass); + if (apiLevel > 0) { + Log.LogDebugMessage ("{0}={1}", Path.GetFileNameWithoutExtension (ass.ItemSpec), apiLevel); + apiLevels.Add (ass, apiLevel); + } + } + + var mainapiLevel = AndroidVersion.TryOSVersionToApiLevel (TargetFrameworkVersion); + foreach (var item in apiLevels.Where (x => mainapiLevel < x.Value)) { + var itemOSVersion = AndroidVersion.TryApiLevelToOSVersion (item.Value); + Log.LogWarning (null, "XA0105", null, ProjectFile, 0, 0, 0, 0, + "The $(TargetFrameworkVersion) for {0} (v{1}) is greater than the $(TargetFrameworkVersion) for your project ({2}). " + + "You need to increase the $(TargetFrameworkVersion) for your project.", Path.GetFileName (item.Key.ItemSpec), itemOSVersion, TargetFrameworkVersion); + } + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CilStrip.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CilStrip.cs new file mode 100644 index 00000000000..cbfa1a3e586 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CilStrip.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CilStrip : Task + { + [Required] + public string AndroidAotMode { get; set; } + + [Required] + public string ToolPath { get; set; } + + [Required] + public ITaskItem[] ResolvedAssemblies { get; set; } + + public CilStrip () + { + } + + public override bool Execute () + { + try { + return DoExecute (); + } catch (Exception e) { + Log.LogCodedError ("XA3001", "{0}", e); + return false; + } + } + + bool DoExecute () { + Log.LogDebugTaskItems (" Targets:", ResolvedAssemblies); + + AotMode aotMode; + bool hasValidAotMode = Aot.GetAndroidAotMode (AndroidAotMode, out aotMode); + if (!hasValidAotMode) { + Log.LogCodedError ("XA3001", "Invalid AOT mode: {0}", AndroidAotMode); + return false; + } + + // Create a directory to move the original non IL-stripped assemblies. + string assembliesDir = Path.GetDirectoryName (ResolvedAssemblies.First ().ItemSpec); + string nonstripDir = Path.Combine (assembliesDir, "non-stripped"); + + if (!Directory.Exists (nonstripDir)) + Directory.CreateDirectory (nonstripDir); + + foreach (var assembly in ResolvedAssemblies) { + string assemblyPath = Path.GetFullPath (assembly.ItemSpec); + string nonstripPath = Path.Combine (nonstripDir, Path.GetFileName (assemblyPath)); + + File.Move (assemblyPath, nonstripPath); + + if (!RunCilStrip (nonstripPath, assemblyPath)) { + Log.LogCodedError ("XA3001", "Could not strip IL of assembly: {0}", assembly.ItemSpec); + return false; + } + } + + return true; + } + + bool RunCilStrip (string assembly, string output) + { + Log.LogMessage (MessageImportance.High, "[cil-strip] " + assembly); + + var arguments = string.Format("{0} {1}", assembly, output); + + var psi = new ProcessStartInfo () { + FileName = Path.Combine (ToolPath, "cil-strip.exe"), + Arguments = arguments, + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + + var proc = new Process (); + proc.OutputDataReceived += OnOutputData; + proc.ErrorDataReceived += OnErrorData; + proc.StartInfo = psi; + proc.Start (); + + proc.BeginOutputReadLine (); + proc.BeginErrorReadLine (); + proc.WaitForExit (); + + return proc.ExitCode == 0; + } + + void OnOutputData (object sender, DataReceivedEventArgs e) + { + if (e.Data != null) + Log.LogMessage ("[cil-strip stdout] {0}", e.Data); + } + + void OnErrorData (object sender, DataReceivedEventArgs e) + { + if (e.Data != null) + Log.LogMessage ("[cil-strip stderr] {0}", e.Data); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ClassParse.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ClassParse.cs new file mode 100644 index 00000000000..0a0bba701ef --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ClassParse.cs @@ -0,0 +1,87 @@ +// Copyright (C) 2012 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Reflection; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.AndroidTools; +using System.Text; +using Bytecode = Xamarin.Android.Tools.Bytecode; +using System.Diagnostics; + +namespace Xamarin.Android.Tasks +{ + public class ClassParse : Task + { + [Required] + public string OutputFile { get; set; } + + [Required] + public ITaskItem[] SourceJars { get; set; } + + public ITaskItem[] JavaDocPaths { get; set; } + + public ITaskItem[] Java7DocPaths { get; set; } + + public ITaskItem[] Java8DocPaths { get; set; } + + public ITaskItem[] DroidDocPaths { get; set; } + + public IEnumerable DocsPaths { + get { + Func> f = l => l ?? Enumerable.Empty (); + return f (JavaDocPaths).Concat (f (Java7DocPaths)).Concat (f (Java8DocPaths)).Concat (f (DroidDocPaths)); + } + } + + public override bool Execute () + { + Log.LogDebugMessage ("ClassParse Task"); + Log.LogDebugMessage (" OutputFile: {0}", OutputFile); + Log.LogDebugMessage (" SourceJars: {0}", SourceJars); + Log.LogDebugMessage (" JavaDocsPaths: {0}", JavaDocPaths); + Log.LogDebugMessage (" Java7DocsPaths: {0}", Java7DocPaths); + Log.LogDebugMessage (" Java8DocsPaths: {0}", Java8DocPaths); + Log.LogDebugMessage (" DroidDocsPaths: {0}", DroidDocPaths); + + using (var output = new StreamWriter (OutputFile, append: false, + encoding: new UTF8Encoding (encoderShouldEmitUTF8Identifier: false))) { + Bytecode.Log.OnLog = LogEventHandler; + var classPath = new Bytecode.ClassPath () { + ApiSource = "class-parse", + DocumentationPaths = (DocsPaths ?? Enumerable.Empty ()).Select(x => x.ItemSpec) + }; + foreach (var jar in SourceJars) { + if (Bytecode.ClassPath.IsJarFile (jar.ItemSpec)) { + classPath.Load (jar.ItemSpec); + } + } + classPath.SaveXmlDescription (output); + } + return true; + } + + void LogEventHandler (TraceLevel type, int verbosity, string message, params object[] args) + { + switch (type) { + case TraceLevel.Error: + Log.LogError (message, args); + break; + case TraceLevel.Warning: + Log.LogWarning (message, args); + break; + case TraceLevel.Info: + Log.LogMessage ((MessageImportance)verbosity, message, args); + break; + default: + Log.LogDebugMessage (message, args); + break; + + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CollectLibraryAssets.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CollectLibraryAssets.cs new file mode 100644 index 00000000000..135e339ecfa --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CollectLibraryAssets.cs @@ -0,0 +1,23 @@ +using System; +using System.Linq; +using Microsoft.Build.Utilities; +using System.IO; + +namespace Xamarin.Android.Tasks +{ + public class CollectLibraryAssets : Task + { + public string AssetDirectory { get; set; } + public string [] AdditionalAssetDirectories { get; set; } + + public override bool Execute () + { + if (AdditionalAssetDirectories != null) + foreach (var dir in AdditionalAssetDirectories) + foreach (var file in Directory.GetFiles (dir, "*", SearchOption.AllDirectories)) + MonoAndroidHelper.CopyIfChanged (file, file.Replace (dir, AssetDirectory)); + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CompileToDalvik.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CompileToDalvik.cs new file mode 100644 index 00000000000..811d8c81070 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CompileToDalvik.cs @@ -0,0 +1,131 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; + +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CompileToDalvik : JavaToolTask + { + public ITaskItem[] AdditionalJavaLibraryReferences { get; set; } + + [Required] + public string ClassesOutputDirectory { get; set; } + + public string DxJarPath { get; set; } + + public string JavaToolPath { get; set; } + + [Required] + public ITaskItem[] JavaLibrariesToCompile { get; set; } + + public string OptionalObfuscatedJarFile { get; set; } + + public bool UseDx { get; set; } + + public bool MultiDexEnabled { get; set; } + + public string MultiDexMainDexListFile { get; set; } + + public string JavaOptions { get; set; } + + public string JavaMaximumHeapSize { get; set; } + + [Output] + public string [] DexOutputs { get; set; } + + protected override string ToolName { + get { + if (UseDx) + return OS.IsWindows ? "dx.bat" : "dx"; + return OS.IsWindows ? "java.exe" : "java"; + } + } + + public override bool Execute () + { + Log.LogDebugMessage ("CompileToDalvik"); + Log.LogDebugMessage (" JavaOptions: {0}", JavaOptions); + Log.LogDebugMessage (" JavaMaximumHeapSize: {0}", JavaMaximumHeapSize); + Log.LogDebugMessage (" ClassesOutputDirectory: {0}", ClassesOutputDirectory); + Log.LogDebugMessage (" JavaToolPath: {0}", JavaToolPath); + Log.LogDebugMessage (" DxJarPath: {0}", DxJarPath); + Log.LogDebugMessage (" ToolExe: {0}", ToolExe); + Log.LogDebugMessage (" ToolPath: {0}", ToolPath); + Log.LogDebugMessage (" UseDx: {0}", UseDx); + Log.LogDebugMessage (" MultiDexEnabled: {0}", MultiDexEnabled); + Log.LogDebugMessage (" MultiDexMainDexListFile: {0}", MultiDexMainDexListFile); + Log.LogDebugTaskItems (" JavaLibrariesToCompile:", JavaLibrariesToCompile); + + if (!Directory.Exists (ClassesOutputDirectory)) + Directory.CreateDirectory (ClassesOutputDirectory); + + bool ret = base.Execute (); + + DexOutputs = Directory.GetFiles (Path.GetDirectoryName (ClassesOutputDirectory), "*.dex", SearchOption.TopDirectoryOnly); + + Log.LogDebugTaskItems (" DexOutputs: ", DexOutputs); + + return ret; + } + + protected override string GenerateCommandLineCommands () + { + // Running command: C:\Program Files\Java\jdk1.6.0_25\bin\java.exe -jar + // C:\Program Files (x86)\Android\android-sdk\platform-tools\lib\dx.jar --dex + // --output=C:\Users\jeff\Documents\Visual Studio 2010\Projects\\...\android\bin\classes.dex + // C:\Users\jeff\Documents\Visual Studio 2010\Projects\\...\android\bin\classes + // C:\Users\jeff\Documents\Visual Studio 2010\Projects\\...\android\bin\mono.android.jar + + var cmd = new CommandLineBuilder (); + + if (!UseDx) { + // Add the JavaOptions if they are not null + // These could be any of the additional options + if (!string.IsNullOrEmpty (JavaOptions)) { + cmd.AppendSwitch (JavaOptions); + } + + // Add the specific -XmxN to override the default heap size for the JVM + // N can be in the form of Nm or NGB (e.g 100m or 1GB ) + cmd.AppendSwitchIfNotNull("-Xmx", JavaMaximumHeapSize); + + cmd.AppendSwitchIfNotNull ("-jar ", Path.Combine (DxJarPath)); + cmd.AppendSwitch ("--no-strict"); + } + + cmd.AppendSwitch ("--dex"); + + if (MultiDexEnabled) { + cmd.AppendSwitch ("--multi-dex"); + cmd.AppendSwitchIfNotNull ("--main-dex-list=", MultiDexMainDexListFile); + } + cmd.AppendSwitchIfNotNull ("--output=", Path.GetDirectoryName (ClassesOutputDirectory)); + + + // .jar files + if (File.Exists (OptionalObfuscatedJarFile)) + cmd.AppendFileNameIfNotNull (OptionalObfuscatedJarFile); + else { + cmd.AppendFileNameIfNotNull (ClassesOutputDirectory); + foreach (var jar in JavaLibrariesToCompile) + cmd.AppendFileNameIfNotNull (jar.ItemSpec); + } + + return cmd.ToString (); + } + + protected override string GenerateFullPathToTool () + { + if (UseDx) + return Path.Combine (ToolPath, ToolExe); + return Path.Combine (JavaToolPath, ToolName); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ConvertDebuggingFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ConvertDebuggingFiles.cs new file mode 100644 index 00000000000..449a784a55d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ConvertDebuggingFiles.cs @@ -0,0 +1,37 @@ +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Pdb2Mdb; + +namespace Xamarin.Android.Tasks +{ + public class ConvertDebuggingFiles : Task + { + // The .pdb files we need to convert + [Required] + public ITaskItem[] Files { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("ConvertDebuggingFiles Task"); + Log.LogDebugMessage (" InputFiles: {0}", Files); + + foreach (var file in Files) { + var pdb = file.ToString (); + + if (!File.Exists (pdb)) + continue; + + try { + MonoAndroidHelper.SetWriteable (pdb); + Converter.Convert (Path.ChangeExtension (pdb, ".dll")); + } catch (Exception ex) { + Log.LogWarningFromException (ex, true); + } + } + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ConvertResourcesCases.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ConvertResourcesCases.cs new file mode 100644 index 00000000000..4cb6d7744cd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ConvertResourcesCases.cs @@ -0,0 +1,81 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Monodroid; + +namespace Xamarin.Android.Tasks +{ + public class ConvertResourcesCases : Task + { + [Required] + public ITaskItem[] ResourceDirectories { get; set; } + + [Required] + public string AcwMapFile { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("ConvertResourcesCases Task"); + Log.LogDebugMessage (" ResourceDirectories: {0}", ResourceDirectories); + Log.LogDebugMessage (" AcwMapFile: {0}", AcwMapFile); + + var acw_map = MonoAndroidHelper.LoadAcwMapFile (AcwMapFile); + + // Look in the resource xml's for capitalized stuff and fix them + FixupResources (acw_map); + + return true; + } + + + void FixupResources (Dictionary acwMap) + { + foreach (var dir in ResourceDirectories) + FixupResources (dir, acwMap); + } + + void FixupResources (ITaskItem item, Dictionary acwMap) + { + var resdir = item.ItemSpec; + // Find all the xml and axml files + var xmls = new[] { resdir } + .Concat (Directory.EnumerateDirectories (resdir, "*", SearchOption.AllDirectories) + .Except (Directory.EnumerateDirectories (resdir, "color*", SearchOption.TopDirectoryOnly)) + .Except (Directory.EnumerateDirectories (resdir, "raw*", SearchOption.TopDirectoryOnly))) + .SelectMany (dir => Directory.EnumerateFiles (dir, "*.xml") + .Concat (Directory.EnumerateFiles (dir, "*.axml"))); + + // Fix up each file + foreach (string file in xmls) { + Log.LogMessage (MessageImportance.High, " Processing: {0}", file); + var srcmodifiedDate = File.GetLastWriteTimeUtc (file); + var tmpdest = file + ".tmp"; + MonoAndroidHelper.CopyIfChanged (file, tmpdest); + MonoAndroidHelper.SetWriteable (tmpdest); + try { + AndroidResource.UpdateXmlResource (tmpdest, acwMap, + ResourceDirectories.Where (s => s != item).Select(s => s.ItemSpec)); + + // We strip away an eventual UTF-8 BOM from the XML file. + // This is a requirement for the Android designer because the desktop Java renderer + // doesn't support those type of BOM (it really wants the document to start + // with " modifiedFiles = new List(); + + public override bool Execute () + { + Log.LogDebugMessage ("CopyAndConvertResources Task"); + Log.LogDebugTaskItems (" SourceFiles:", SourceFiles); + Log.LogDebugTaskItems (" DestinationFiles:", DestinationFiles); + Log.LogDebugMessage (" AcwMapFile: {0}", AcwMapFile); + + if (SourceFiles.Length != DestinationFiles.Length) + throw new ArgumentException ("source and destination count mismatch"); + + var acw_map = MonoAndroidHelper.LoadAcwMapFile (AcwMapFile); + + var xmlFilesToUpdate = new Dictionary (); + for (int i = 0; i < SourceFiles.Length; i++) { + var filename = SourceFiles [i].ItemSpec; + + if (File.Exists (filename)) { + var ext = Path.GetExtension (filename); + var destfilename = DestinationFiles [i].ItemSpec; + var srcmodifiedDate = File.GetLastWriteTimeUtc (filename); + var dstmodifiedDate = File.Exists (destfilename) ? File.GetLastAccessTimeUtc (destfilename) : DateTime.MinValue; + var isXml = ext == ".xml" || ext == ".axml"; + + Directory.CreateDirectory (Path.GetDirectoryName (destfilename)); + + if (isXml) { + xmlFilesToUpdate.Add (filename, DestinationFiles [i].ItemSpec); + continue; + } + if (dstmodifiedDate < srcmodifiedDate && MonoAndroidHelper.CopyIfChanged (filename, destfilename)) { + MonoAndroidHelper.SetWriteable (destfilename); + + // If the resource is not part of a raw-folder we strip away an eventual UTF-8 BOM + // This is a requirement for the Android designer because the desktop Java renderer + // doesn't support those type of BOM (it really wants the document to start + // with " i.ItemSpec == destfilename)) + modifiedFiles.Add (new TaskItem (destfilename)); + } + } finally { + File.Delete (tmpdest); + } + } + merger.Save (); + ModifiedFiles = modifiedFiles.ToArray (); + + Log.LogDebugTaskItems (" ModifiedFiles:", ModifiedFiles); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CopyConfigFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CopyConfigFiles.cs new file mode 100644 index 00000000000..24499a94343 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CopyConfigFiles.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.IO; + +namespace Xamarin.Android.Tasks +{ + public class CopyConfigFiles : Task + { + + public ITaskItem[] SourceFiles { get; set; } + + public ITaskItem[] DestinationFiles { get; set; } + + public override bool Execute () + { + Log.LogDebugTaskItems ("SourceFiles:", SourceFiles); + Log.LogDebugTaskItems ("DestinationFiles:", DestinationFiles); + + if (SourceFiles.Length != DestinationFiles.Length) + throw new ArgumentException ("source and destination count mismatch"); + + for (int i = 0; i < SourceFiles.Length; i++) { + var src = SourceFiles [i].ItemSpec; + var dst = DestinationFiles [i].ItemSpec; + var date = DateTime.Now; + if (File.Exists (src)) { + MonoAndroidHelper.CopyIfChanged (src, dst); + MonoAndroidHelper.SetWriteable (dst); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (dst, date, Log); + } + } + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CopyGeneratedJavaResourceClasses.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CopyGeneratedJavaResourceClasses.cs new file mode 100644 index 00000000000..edb178d9ae2 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CopyGeneratedJavaResourceClasses.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CopyGeneratedJavaResourceClasses : Task + { + [Required] + public string SourceTopDirectory { get; set; } + [Required] + public string DestinationTopDirectory { get; set; } + [Required] + public string PrimaryPackageName { get; set; } + + public string ExtraPackages { get; set; } + [Output] + public string PrimaryJavaResgenFile { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("SourceTopDirectory: {0}", SourceTopDirectory); + Log.LogDebugMessage ("DestinationTopDirectory: {0}", DestinationTopDirectory); + Log.LogDebugMessage ("PrimaryPackageName: {0}", PrimaryPackageName); + Log.LogDebugMessage ("ExtraPackages: {0}", ExtraPackages); + + var list = new List (); + foreach (var pkg in GetPackages ()) { + string subpath = Path.Combine (pkg.Split ('.')); + string src = Path.Combine (SourceTopDirectory, subpath, "R.java"); + string dst = Path.Combine (DestinationTopDirectory, subpath, "R.java"); + + if (!File.Exists (src)) + continue; + + var date = File.GetLastWriteTimeUtc (src); + if (MonoAndroidHelper.CopyIfChanged (src, dst)) { + MonoAndroidHelper.SetWriteable (dst); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (dst, date, Log); + } + list.Add (dst); + } + // so far we only need the package's R.java for GenerateResourceDesigner input. + PrimaryJavaResgenFile = list.FirstOrDefault (); + + Log.LogDebugMessage ("Output PrimaryJavaResgenFile: {0}", PrimaryJavaResgenFile); + + return true; + } + + IEnumerable GetPackages () + { + yield return PrimaryPackageName.ToLowerInvariant (); + if (!string.IsNullOrEmpty (ExtraPackages)) + foreach (var pkg in ExtraPackages.Split (':')) + yield return pkg; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CopyIfChanged.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CopyIfChanged.cs new file mode 100644 index 00000000000..53c72828532 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CopyIfChanged.cs @@ -0,0 +1,50 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using System.Text; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Security.Cryptography; + +namespace Xamarin.Android.Tasks +{ + // When we regenerate the Resource.Designer.cs, if we write it + // when it hasn't actually changed, the user will get a "Reload?" + // prompt in IDEs, so we only want to copy the file if there is + // an actual change. + public class CopyIfChanged : Task + { + [Required] + public ITaskItem[] SourceFiles { get; set; } + + [Required] + public ITaskItem[] DestinationFiles { get; set; } + + public bool KeepDestinationDates { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("CopyIfChanged Task"); + Log.LogDebugTaskItems (" SourceFiles: {0}", SourceFiles); + Log.LogDebugTaskItems (" DestinationFiles: {0}", DestinationFiles); + + if (SourceFiles.Length != DestinationFiles.Length) + throw new ArgumentException ("source and destination count mismatch"); + + for (int i = 0; i < SourceFiles.Length; i++) { + var src = SourceFiles [i].ItemSpec; + if (!File.Exists (src)) + continue; + var dest = DestinationFiles [i].ItemSpec; + var lastWriteTime = File.GetLastWriteTimeUtc (File.Exists (dest) ? dest : src); + MonoAndroidHelper.SetWriteable (dest); + if (!MonoAndroidHelper.CopyIfChanged (src, dest)) + continue; + if (KeepDestinationDates) + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (dest, lastWriteTime, Log); + } + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CopyMdbFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CopyMdbFiles.cs new file mode 100644 index 00000000000..482bab19df1 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CopyMdbFiles.cs @@ -0,0 +1,35 @@ +using System; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.IO; +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks { + + public class CopyMdbFiles : Task { + + public ITaskItem[] SourceFiles { get; set; } + + public ITaskItem[] DestinationFiles { get; set; } + + public override bool Execute() { + Log.LogDebugTaskItems ("SourceFiles:", SourceFiles); + Log.LogDebugTaskItems ("DestinationFiles:", DestinationFiles); + + if (SourceFiles.Length != DestinationFiles.Length) + throw new ArgumentException ("source and destination count mismatch"); + + for (int i = 0; i < SourceFiles.Length; i++) { + var src = SourceFiles[i].ItemSpec; + if (File.Exists (src)) { + var dst = DestinationFiles [i].ItemSpec; + var date = DateTime.Now; + MonoAndroidHelper.CopyIfChanged (src, dst); + MonoAndroidHelper.SetWriteable (dst); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (dst, date, Log); + } + } + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CopyResource.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CopyResource.cs new file mode 100644 index 00000000000..3ffe0457eff --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CopyResource.cs @@ -0,0 +1,81 @@ +// Author: Jonathan Pobst +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Reflection; + +using Java.Interop.Tools.JavaCallableWrappers; + +namespace Xamarin.Android.Tasks +{ + public class CopyResource : Task + { + [Required] + public string ResourceName { get; set; } + + [Required] + public string OutputPath { get; set; } + + static readonly Assembly assm = Assembly.GetExecutingAssembly (); + + public override bool Execute () + { + return Run (assm, ResourceName, OutputPath, Log); + } + + public bool Run (Assembly assm, string ResourceName, string OutputPath, TaskLoggingHelper Log) + { + // Ensure our output directory exists + if (!Directory.Exists (Path.GetDirectoryName (OutputPath))) + Directory.CreateDirectory (Path.GetDirectoryName (OutputPath)); + + // Copy out one of our embedded resources to a path + using (var from = GetManifestResourceStream (ResourceName)) { + + // If the resource already exists, only overwrite if it's changed + if (File.Exists (OutputPath)) { + var hash1 = MonoAndroidHelper.HashFile (OutputPath); + var hash2 = MonoAndroidHelper.HashStream (from); + + if (hash1 == hash2) { + Log.LogDebugMessage ("Resource {0} is unchanged. Skipping.", OutputPath); + return true; + } + } + + // Hash calculation read to the end, move back to beginning of file + from.Position = 0; + + // Write out the resource + using (var to = File.Create (OutputPath)) + Copy (from, to); + + Log.LogDebugMessage ("Wrote resource {0}.", OutputPath); + } + + return true; + } + + Stream GetManifestResourceStream (string name) + { + var r = assm.GetManifestResourceStream (name); + if (r != null) + return r; + r = typeof (JavaCallableWrapperGenerator).Assembly.GetManifestResourceStream (name); + return r; + } + + public static void Copy (Stream input, Stream output) + { + byte[] buffer = new byte [8192]; + int cnt; + + while ((cnt = input.Read (buffer, 0, buffer.Length)) > 0) + output.Write (buffer, 0, cnt); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateAdditionalLibraryResourceCache.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateAdditionalLibraryResourceCache.cs new file mode 100644 index 00000000000..8f240ec1e1d --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateAdditionalLibraryResourceCache.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Security.Cryptography; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +namespace Xamarin.Android.Tasks +{ + public class CreateAdditionalLibraryResourceCache : Task + { + + [Required] + public ITaskItem[] AdditionalAndroidResourcePaths { get; set; } + + [Required] + public ITaskItem[] AdditionalAndroidResourceCachePaths { get; set; } + + [Output] + public ITaskItem[] CopiedResources { get; set; } + + + public override bool Execute () + { + Log.LogDebugMessage ("CreateAdditionalLibraryResourceCache Task"); + Log.LogDebugTaskItems (" AdditionalAndroidResourcePaths:", AdditionalAndroidResourcePaths); + Log.LogDebugTaskItems (" AdditionalAndroidResourceCachePaths: ", AdditionalAndroidResourceCachePaths); + var copiedResources = new List (); + + for (int i = 0; i < AdditionalAndroidResourcePaths.Length; i++) { + var src = Path.GetFullPath (AdditionalAndroidResourcePaths [i].ItemSpec); + var dest = Path.GetFullPath (AdditionalAndroidResourceCachePaths [i].ItemSpec); + + foreach (string dirPath in Directory.EnumerateDirectories (src, "*", SearchOption.AllDirectories)) + Directory.CreateDirectory (dirPath.Replace (src, dest)); + + //Copy all the files & Replaces any files with the same name + foreach (string newPath in Directory.EnumerateFiles (src, "*", SearchOption.AllDirectories)) { + var destPath = newPath.Replace (src, dest); + var cachedDate = File.GetLastWriteTimeUtc (src); + MonoAndroidHelper.CopyIfChanged (newPath, destPath); + MonoAndroidHelper.SetWriteable (destPath); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (destPath, cachedDate, Log); + copiedResources.Add (new TaskItem (destPath)); + } + } + + CopiedResources = copiedResources.ToArray (); + + Log.LogDebugTaskItems (" [Output] CopiedResources:", CopiedResources); + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateAndroidResourceStamp.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateAndroidResourceStamp.cs new file mode 100644 index 00000000000..a84b1542c3a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateAndroidResourceStamp.cs @@ -0,0 +1,37 @@ +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CreateAndroidResourceStamp : Task + { + [Required] + public string AndroidResgenFile { get; set; } + [Required] + public string [] AndroidResourceDest { get; set; } + [Required] + public string MonoAndroidResDirIntermediate { get; set; } + [Required] + public string AndroidResgenFlagFile { get; set; } + + public override bool Execute () + { + if ((AndroidResourceDest == null || AndroidResourceDest.Length == 0) && File.Exists (AndroidResgenFile)) + File.Delete (AndroidResgenFile); + Directory.CreateDirectory (MonoAndroidResDirIntermediate); + // touch resgen file. + if (!File.Exists (AndroidResgenFile) && Directory.Exists (Path.GetDirectoryName (AndroidResgenFile))) { + File.AppendText (AndroidResgenFile).Close (); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (AndroidResgenFile, DateTime.UtcNow, Log); + } + // touch resgen flag file + File.AppendText (AndroidResgenFlagFile).Close (); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (AndroidResgenFlagFile, DateTime.UtcNow, Log); + + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateLibraryResourceArchive.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateLibraryResourceArchive.cs new file mode 100644 index 00000000000..f781c241edf --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateLibraryResourceArchive.cs @@ -0,0 +1,213 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Ionic.Zip; + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + public class CreateLibraryResourceArchive : Task + { + [Required] + public string OutputDirectory { get; set; } + + [Required] + public string OutputJarsDirectory { get; set; } + + [Required] + public string OutputAnnotationsDirectory { get; set; } + + [Required] + public ITaskItem[] LibraryProjectPropertiesFiles { get; set; } + + [Required] + public ITaskItem[] LibraryProjectZipFiles { get; set; } + + public CreateLibraryResourceArchive () + { + } + + public override bool Execute () + { + if (LibraryProjectPropertiesFiles.Length == 0 && LibraryProjectZipFiles.Length == 0) + return true; + + Log.LogDebugMessage ("CreateLibraryResourceArchive Task"); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" OutputJarsDirectory: {0}", OutputJarsDirectory); + Log.LogDebugMessage (" OutputAnnotationsDirectory: {0}", OutputAnnotationsDirectory); + Log.LogDebugMessage (" LibraryProjectProperties:"); + + foreach (var p in LibraryProjectPropertiesFiles) + Log.LogDebugMessage (" " + p.ItemSpec); + Log.LogDebugMessage (" LibraryProjectZip:"); + foreach (var z in LibraryProjectZipFiles) + Log.LogDebugMessage (" " + z.ItemSpec); + + var outDirInfo = new DirectoryInfo (OutputDirectory); + + // Copy files into _LibraryProjectImportsDirectoryName (library_project_imports) dir. + if (!outDirInfo.Exists) + outDirInfo.Create (); + + var projectsResolved = ResolveLibraryProjectReferences (LibraryProjectPropertiesFiles.Select (p => Path.GetFullPath (p.ItemSpec))); + var imports = projectsResolved.Concat (LibraryProjectZipFiles.Select (p => p.ItemSpec)); + + foreach (var p in imports) { + // note that imports could contain file name that neither of build items contains + // (it may be one of those resolved references in project.properties). + // Also non-zip files are now specified in full path. + if (!LibraryProjectZipFiles.Any (l => l.ItemSpec == p)) { + // project.properties + + var fileInfo = new FileInfo (p); + if (!fileInfo.Exists) + throw new InvalidOperationException (String.Format ("Library project properties file '{0}' does not exist.", p)); + var bindir = fileInfo.Directory.FullName; + + CopyLibraryContent (bindir, false); + } else { + // zip + string tmpname = Path.Combine (Path.GetTempPath (), "monodroid_import_" + Guid.NewGuid ().ToString ()); + try { + Directory.CreateDirectory (tmpname); + using (var lz = new ZipFile (p, new System.Text.UTF8Encoding (false))) { + Files.ExtractAll (lz, tmpname, ExtractExistingFileAction.DoNotOverwrite); + } + + if (!CopyLibraryContent (tmpname, p.EndsWith (".aar", StringComparison.OrdinalIgnoreCase))) + return false; + } finally { + Directory.Delete (tmpname, true); + } + } + } + + // Archive them in a zip. + var zip = new ZipFile (new System.Text.UTF8Encoding (false)); + zip.AddDirectory (OutputDirectory, outDirInfo.Name); + + string outpath = Path.Combine (outDirInfo.Parent.FullName, "__AndroidLibraryProjects__.zip"); + if (Files.ArchiveZip (outpath, f => zip.Save (f))) + Log.LogDebugMessage ("Saving contents to " + outpath); + + return true; + } + + bool CopyLibraryContent (string projdir, bool isAar) + { + if (Path.GetFullPath (OutputDirectory).StartsWith (Path.GetFullPath (projdir), StringComparison.InvariantCultureIgnoreCase)) { + Log.LogError ("The source directory is under the output directory. Skip it."); + return false; + } + foreach (var subdir in Directory.GetDirectories (projdir)) { + var dinfo = new DirectoryInfo (subdir); + switch (dinfo.Name.ToLowerInvariant ()) { + case "gen": + case "src": + continue; + } + CopyDirectory (dinfo, OutputDirectory, true); + CopyFiles (subdir); + } + if (isAar) { + CopyFiles (projdir); + // for .aar, copy top-level files (*.jar, AndroidManifest.xml etc.) into bin too. + var dstdir = Path.Combine (OutputDirectory, "bin"); + foreach (var file in Directory.GetFiles (projdir)) { + string dstpath = Path.Combine (dstdir, Path.GetFileName (file)); + if (!File.Exists (dstpath)) + MonoAndroidHelper.CopyIfChanged (file, dstpath); + } + } + return true; + } + + void CopyFiles (string srcdir) + { + foreach (var file in Directory.GetFiles (srcdir)) { + if (file.EndsWith (".jar", StringComparison.OrdinalIgnoreCase)) { + var dstpath = Path.Combine (OutputJarsDirectory, Path.GetFileName (file)); + if (!File.Exists (dstpath)) + MonoAndroidHelper.CopyIfChanged (file, dstpath); + } else if (file.EndsWith ("annotations.zip", StringComparison.OrdinalIgnoreCase)) { + var dstpath = Path.Combine (OutputAnnotationsDirectory, Path.GetFileName (file)); + if (!File.Exists (dstpath)) + MonoAndroidHelper.CopyIfChanged (file, dstpath); + } else { + var dstpath = Path.Combine (OutputDirectory, Path.GetFileName (file)); + if (!File.Exists (dstpath)) + MonoAndroidHelper.CopyIfChanged (file, dstpath); + } + } + } + + static void CopyDirectory (DirectoryInfo srcSubdirInfo, string dst, bool top) + { + string dstsub = Path.Combine (dst, srcSubdirInfo.Name); + if (!Directory.Exists (dstsub)) + Directory.CreateDirectory (dstsub); + foreach (var subsub in srcSubdirInfo.GetDirectories ()) { + // Skip "classes" dir. + if (top && subsub.Name.ToLowerInvariant () == "classes") + continue; + CopyDirectory (subsub, dstsub, false); + } + foreach (var file in srcSubdirInfo.GetFiles ()) + MonoAndroidHelper.CopyIfChanged (file.FullName, Path.Combine (dstsub, file.Name)); + } + + IEnumerable ResolveLibraryProjectReferences (IEnumerable inputs) + { + var l = new List (); + foreach (var input in inputs) + foreach (var s in ResolveLibraryProjectReferences (input, new List ())) + if (!l.Contains (s)) + l.Add (s); + return l; + } + + const string librefspec = "android.library.reference."; + + IEnumerable ResolveLibraryProjectReferences (string singleFile, List recurse) + { + if (recurse.Contains (singleFile)) + throw new InvalidOperationException (String.Format ("The library project '{0}' contains invalid recursive references.", singleFile)); + recurse.Add (singleFile); + var l = new List (); + l.Add (singleFile); + var ordered = new SortedList (); + foreach (var line in File.ReadAllLines (singleFile)) { + var s = line.Trim (); + if (s.StartsWith ("#")) + continue; + if (!s.StartsWith (librefspec)) + continue; + int eqpos = s.IndexOf ('='); + if (eqpos < 0) + throw new InvalidOperationException (String.Format ("Wrong project reference description in '{0}': '=' is missing", singleFile)); + string numspec = s.Substring (librefspec.Length, eqpos - librefspec.Length); + int num; + if (!int.TryParse (numspec, out num)) + throw new InvalidOperationException (String.Format ("Wrong project reference description in '{0}': wrong number format for reference priority", singleFile)); + string path = s.Substring (eqpos + 1); + string refpath = Path.GetFullPath (Path.Combine (Path.GetDirectoryName (singleFile), path, "project.properties")); + ordered.Add (num, refpath); + } + foreach (var refpath in ordered.Values) + foreach (var sub in ResolveLibraryProjectReferences (refpath, recurse)) + l.Add (sub); + recurse.Remove (singleFile); + return l; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateManagedLibraryResourceArchive.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateManagedLibraryResourceArchive.cs new file mode 100644 index 00000000000..0d7662234c5 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateManagedLibraryResourceArchive.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Ionic.Zip; + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + public class CreateManagedLibraryResourceArchive : Task + { + public bool IsApplication { get; set; } + + [Required] + public string OutputDirectory { get; set; } + + public ITaskItem[] AndroidAssets { get; set; } + + public string MonoAndroidAssetsPrefix { get; set; } + + public ITaskItem[] AndroidJavaSources { get; set; } + + public ITaskItem[] AndroidJavaLibraries { get; set; } + + public ITaskItem[] AndroidResourcesInThisExactProject { get; set; } + + [Required] + public string ResourceDirectory { get; set; } + + public CreateManagedLibraryResourceArchive () + { + } + + public override bool Execute () + { + if (IsApplication) + return true; + + Log.LogDebugMessage ("CreateManagedLibraryResourceArchive Task"); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" ResourceDirectory: {0}", ResourceDirectory); + Log.LogDebugTaskItems (" AndroidAssets:", AndroidAssets); + Log.LogDebugTaskItems (" AndroidJavaSources:", AndroidJavaSources); + Log.LogDebugTaskItems (" AndroidJavaLibraries:", AndroidJavaLibraries); + + var outDirInfo = new DirectoryInfo (OutputDirectory); + + // Copy files into _LibraryProjectImportsDirectoryName (library_project_imports) dir. + if (!outDirInfo.Exists) + outDirInfo.Create (); + foreach (var sub in new string [] {"assets", "res", "java", "bin"}) { + var subdirInfo = new DirectoryInfo (Path.Combine (outDirInfo.FullName, sub)); + if (!subdirInfo.Exists) + subdirInfo.Create (); + } + + var dir_sep = new char [] {Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar}; + if (AndroidAssets != null) { + var dstsub = Path.Combine (outDirInfo.FullName, "assets"); + if (!Directory.Exists (dstsub)) + Directory.CreateDirectory (dstsub); + foreach (var item in AndroidAssets) { + var path = item.GetMetadata ("Link"); + path = !string.IsNullOrWhiteSpace (path) ? path : item.ItemSpec; + var head = string.Join ("\\", path.Split (dir_sep).TakeWhile (s => !s.Equals (MonoAndroidAssetsPrefix, StringComparison.OrdinalIgnoreCase))); + path = head.Length == path.Length ? path : path.Substring ((head.Length == 0 ? 0 : head.Length + Path.DirectorySeparatorChar) + MonoAndroidAssetsPrefix.Length).TrimStart (dir_sep); + MonoAndroidHelper.CopyIfChanged (item.ItemSpec, Path.Combine (dstsub, path)); + } + } + // resources folders are converted to the structure that aapt accepts. + bool hasInvalidName = false; + foreach (var srcsub in Directory.GetDirectories (ResourceDirectory)) { + var dstsub = Path.Combine (outDirInfo.FullName, "res", Path.GetFileName (srcsub)); + if (!Directory.Exists (dstsub)) + Directory.CreateDirectory (dstsub); + foreach (var file in Directory.GetFiles (srcsub)) { + var filename = Path.GetFileName (file); + MonoAndroidHelper.CopyIfChanged (file, Path.Combine (dstsub, Path.GetFileName (file))); + } + } + if (hasInvalidName) + return false; + if (AndroidJavaSources != null) + foreach (var item in AndroidJavaSources) + MonoAndroidHelper.CopyIfChanged (item.ItemSpec, Path.Combine (outDirInfo.FullName, item.ItemSpec)); + if (AndroidJavaLibraries != null) + foreach (var item in AndroidJavaLibraries) + MonoAndroidHelper.CopyIfChanged (item.ItemSpec, Path.Combine (outDirInfo.FullName, item.ItemSpec)); + + var nameCaseMap = new StringWriter (); + + // add resource case mapping descriptor to the archive. + if (AndroidResourcesInThisExactProject != null && AndroidResourcesInThisExactProject.Any ()) { + Log.LogMessage ("writing __res_name_case_map.txt..."); + foreach (var res in AndroidResourcesInThisExactProject) + nameCaseMap.WriteLine ("{0};{1}", res.GetMetadata ("LogicalName").Replace ('\\', '/'), Path.Combine (Path.GetFileName (Path.GetDirectoryName (res.ItemSpec)), Path.GetFileName (res.ItemSpec)).Replace ('\\', '/')); + File.WriteAllText (Path.Combine (OutputDirectory, "__res_name_case_map.txt"), nameCaseMap.ToString ()); + } + + // Archive them in a zip. + var zip = new ZipFile (new System.Text.UTF8Encoding (false)); + zip.AddDirectory (OutputDirectory, outDirInfo.Name); + + string outpath = Path.Combine (outDirInfo.Parent.FullName, "__AndroidLibraryProjects__.zip"); + if (Files.ArchiveZip (outpath, f => zip.Save (f))) + Log.LogDebugMessage ("Saving contents to " + outpath); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateMsymManifest.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateMsymManifest.cs new file mode 100644 index 00000000000..b8b5f290bf9 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateMsymManifest.cs @@ -0,0 +1,33 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Xml.Linq; +using System.IO; + +namespace Xamarin.Android.Tasks +{ + public class CreateMsymManifest : Task + { + [Required] + public string BuildId { get; set; } + + [Required] + public string PackageName { get; set; } + + [Required] + public string OutputDirectory { get; set; } + + public override bool Execute () + { + XDocument doc = new XDocument ( + new XElement ("mono-debug", new XAttribute("version", "1"), + new XElement ("app-id", PackageName), + new XElement ("build-date", DateTime.UtcNow.ToString ("O")), + new XElement ("build-id", BuildId)) + ); + doc.Save (Path.Combine (OutputDirectory, "manifest.xml")); + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateMultiDexMainDexClassList.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateMultiDexMainDexClassList.cs new file mode 100644 index 00000000000..688b2cf9988 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateMultiDexMainDexClassList.cs @@ -0,0 +1,86 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; + +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using Xamarin.AndroidTools; +using System.Collections.Specialized; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CreateMultiDexMainDexClassList : ToolTask + { + [Required] + public string ClassesOutputDirectory { get; set; } + + [Required] + public ITaskItem[] JavaLibraries { get; set; } + + public string MultiDexMainDexListFile { get; set; } + public ITaskItem[] CustomMainDexListFiles { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("CreateMultiDexMainDexClassList"); + Log.LogDebugMessage (" ClassesOutputDirectory: {0}", ClassesOutputDirectory); + Log.LogDebugTaskItems (" JavaLibraries:", JavaLibraries); + Log.LogDebugMessage (" MultiDexMainDexListFile: {0}", MultiDexMainDexListFile); + Log.LogDebugTaskItems (" CustomMainDexListFiles:", CustomMainDexListFiles); + Log.LogDebugMessage (" ToolExe: {0}", ToolExe); + Log.LogDebugMessage (" ToolPath: {0}", ToolPath); + + if (CustomMainDexListFiles != null && CustomMainDexListFiles.Any ()) { + var content = string.Concat (CustomMainDexListFiles.Select (i => File.ReadAllText (i.ItemSpec))); + File.WriteAllText (MultiDexMainDexListFile, content); + return true; + } + + return base.Execute (); + } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitch ("--output"); + cmd.AppendFileNameIfNotNull (MultiDexMainDexListFile); + + var jars = JavaLibraries.Select (i => i.ItemSpec).Concat (new string [] { ClassesOutputDirectory }); + string files = string.Join (Path.PathSeparator.ToString (), jars.Select (s => '\'' + s + '\'')); + if (OS.IsWindows) + cmd.AppendSwitch ('"' + files + '"'); + else + cmd.AppendSwitch (files); + + return cmd.ToString (); + } + + protected override string ToolName { + get { + return OS.IsWindows ? "mainDexClasses.bat" : "mainDexClasses"; + } + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + // Windows seems to need special care. + protected override StringDictionary EnvironmentOverride { + get { + var sd = base.EnvironmentOverride ?? new StringDictionary (); + var opts = sd.ContainsKey ("JAVA_TOOL_OPTIONS") ? sd ["JAVA_TOOL_OPTIONS"] : null; + opts += " -Dfile.encoding=UTF8"; + sd ["JAVA_TOOL_OPTIONS"] = opts; + return sd; + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateNativeLibraryArchive.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateNativeLibraryArchive.cs new file mode 100644 index 00000000000..f130dd5046e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateNativeLibraryArchive.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Ionic.Zip; + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + public class CreateNativeLibraryArchive : Task + { + [Required] + public string OutputDirectory { get; set; } + + [Required] + public ITaskItem[] EmbeddedNativeLibraries { get; set; } + + public CreateNativeLibraryArchive () + { + } + + public override bool Execute () + { + Log.LogDebugMessage ("EmbeddedNativeLibraries Task"); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugTaskItems (" EmbeddedNativeLibraries:", EmbeddedNativeLibraries); + + var outDirInfo = new DirectoryInfo (OutputDirectory); + + // Copy files into _NativeLibraryImportsDirectoryName (native_library_imports) dir. + if (!outDirInfo.Exists) + outDirInfo.Create (); + foreach (var lib in EmbeddedNativeLibraries) { + // seealso bug #3477 to find out why we use this method. + var abi = MonoAndroidHelper.GetNativeLibraryAbi (lib); + if (abi == null) { + Log.LogWarning ( + subcategory: string.Empty, + warningCode: "XA4300", + helpKeyword: string.Empty, + file: lib.ItemSpec, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: "Native library '{0}' will not be bundled because it has an unsupported ABI.", + messageArgs: new []{ + lib.ItemSpec, + } + ); + continue; + } + if (!outDirInfo.GetDirectories (abi).Any ()) + outDirInfo.CreateSubdirectory (abi); + MonoAndroidHelper.CopyIfChanged (lib.ItemSpec, Path.Combine (OutputDirectory, abi, Path.GetFileName (lib.ItemSpec))); + } + + // Archive native libraries in a zip. + var zip = new ZipFile (new System.Text.UTF8Encoding (false)); + zip.AddDirectory (OutputDirectory, outDirInfo.Name); + string outpath = Path.Combine (outDirInfo.Parent.FullName, "__AndroidNativeLibraries__.zip"); + if (Files.ArchiveZip (outpath, f => zip.Save (f))) + Log.LogDebugMessage ("Saving contents to " + outpath); + + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateResgenManifest.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateResgenManifest.cs new file mode 100644 index 00000000000..b17ad97c5af --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateResgenManifest.cs @@ -0,0 +1,45 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using System.Text; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + public class CreateResgenManifest : Task + { + [Required] + public string ManifestOutputFile { get; set; } + + [Required] + public string PackageName { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("CreateResgenManifest Task"); + Log.LogDebugMessage (" ManifestOutputFile: {0}", ManifestOutputFile); + Log.LogDebugMessage (" PackageName: {0}", PackageName); + + // + + var doc = new XDocument ( + new XDeclaration ("1.0", "utf-8", null), + new XElement ("manifest", + new XAttribute (XNamespace.Xmlns + "android", "http://schemas.android.com/apk/res/android"), + new XAttribute ("package", PackageName))); + + Files.Archive (ManifestOutputFile, manifest => { + using (var o = new StreamWriter (manifest, false, Encoding.UTF8)) + doc.Save (o); + }); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/CreateTemporaryDirectory.cs b/src/Xamarin.Android.Build.Tasks/Tasks/CreateTemporaryDirectory.cs new file mode 100644 index 00000000000..dd5ddd05764 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/CreateTemporaryDirectory.cs @@ -0,0 +1,26 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class CreateTemporaryDirectory : Task + { + [Output] + public string TemporaryDirectory { get; set; } + + public override bool Execute () + { + TemporaryDirectory = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); + Directory.CreateDirectory (TemporaryDirectory); + + Log.LogDebugMessage ("CreateTemporaryDirectory Task"); + Log.LogDebugMessage (" OUTPUT: TemporaryDirectory: {0}", TemporaryDirectory); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Crunch.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Crunch.cs new file mode 100644 index 00000000000..1b57e946b26 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Crunch.cs @@ -0,0 +1,94 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class Crunch : ToolTask + { + [Required] + public ITaskItem[] SourceFiles { get; set; } + + protected override string ToolName { get { return OS.IsWindows ? "aapt.exe" : "aapt"; } } + + private string tempDirectory; + private string tempOutputDirectory; + + public override bool Execute () + { + Log.LogDebugMessage ("Crunch Task"); + Log.LogDebugTaskItems (" SourceFiles:", SourceFiles); + Log.LogDebugMessage (" ToolPath: {0}", ToolPath); + Log.LogDebugMessage (" ToolExe: {0}", ToolExe); + + // copy the changed files over to a temp location for processing + var imageFiles = SourceFiles.Where (x => string.Equals (Path.GetExtension (x.ItemSpec),".png", StringComparison.OrdinalIgnoreCase)); + + if (!imageFiles.Any ()) + return true; + + foreach (var imageGroup in imageFiles.GroupBy ( x => Path.GetDirectoryName (Path.GetFullPath (x.ItemSpec)))) { + + tempDirectory = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); + Directory.CreateDirectory (tempDirectory); + tempOutputDirectory = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); + Directory.CreateDirectory (tempOutputDirectory); + try { + Log.LogDebugMessage ("Crunch Processing : {0}", imageGroup.Key); + Log.LogDebugTaskItems (" Items :", imageGroup.ToArray ()); + foreach (var item in imageGroup) { + var dest = Path.GetFullPath (item.ItemSpec).Replace (imageGroup.Key, tempDirectory); + Directory.CreateDirectory (Path.GetDirectoryName (dest)); + MonoAndroidHelper.CopyIfChanged (item.ItemSpec, dest); + MonoAndroidHelper.SetWriteable (dest); + } + + // crunch them + if (!base.Execute ()) + return false; + + // copy them back + foreach (var item in imageGroup) { + var dest = Path.GetFullPath (item.ItemSpec).Replace (imageGroup.Key, tempOutputDirectory); + var srcmodifiedDate = File.GetLastWriteTimeUtc (item.ItemSpec); + if (!File.Exists (dest)) + continue; + MonoAndroidHelper.CopyIfChanged (dest, item.ItemSpec); + MonoAndroidHelper.SetWriteable (dest); + // reset the Dates so MSBuild/xbuild doesn't think they changed. + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (item.ItemSpec, srcmodifiedDate, Log); + } + } finally { + Directory.Delete (tempDirectory, recursive:true); + Directory.Delete (tempOutputDirectory, recursive:true); + } + } + + return !Log.HasLoggedErrors; + } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitch ("c"); + cmd.AppendSwitchIfNotNull ("-S " , tempDirectory); + cmd.AppendSwitchIfNotNull ("-C " , tempOutputDirectory); + + return cmd.ToString (); + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/DetermineJavaLibrariesToCompile.cs b/src/Xamarin.Android.Build.Tasks/Tasks/DetermineJavaLibrariesToCompile.cs new file mode 100644 index 00000000000..36b88cf99c7 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/DetermineJavaLibrariesToCompile.cs @@ -0,0 +1,98 @@ +using System; +using Microsoft.Build.Utilities; +using System.Collections.Generic; +using Microsoft.Build.Framework; +using System.Linq; +using System.IO; + +namespace Xamarin.Android.Tasks +{ + public class DetermineJavaLibrariesToCompile : Task + { + [Required] + public string MonoPlatformJarPath { get; set; } + + public bool EnableInstantRun { get; set; } + + public ITaskItem[] JavaSourceFiles { get; set; } + + public ITaskItem[] JavaLibraries { get; set; } + + public ITaskItem[] ExternalJavaLibraries { get; set; } + + public ITaskItem[] DoNotPackageJavaLibraries { get; set; } + + public ITaskItem[] LibraryProjectJars { get; set; } + + public ITaskItem[] AdditionalJavaLibraryReferences { get; set; } + + [Output] + public ITaskItem[] JavaLibrariesToCompile { get; set; } + + [Output] + public ITaskItem[] ReferenceJavaLibraries { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("DetermineJavaLibrariesToCompile"); + Log.LogDebugMessage (" EnableInstantRun: {0}", EnableInstantRun); + Log.LogDebugMessage (" MonoPlatformJarPath: {0}", MonoPlatformJarPath); + Log.LogDebugTaskItems (" JavaSourceFiles:", JavaSourceFiles); + Log.LogDebugTaskItems (" JavaLibraries:", JavaLibraries); + Log.LogDebugTaskItems (" ExternalJavaLibraries:", ExternalJavaLibraries); + Log.LogDebugTaskItems (" LibraryProjectJars:", LibraryProjectJars); + Log.LogDebugTaskItems (" AdditionalJavaLibraryReferences:", AdditionalJavaLibraryReferences); + Log.LogDebugTaskItems (" DoNotPackageJavaLibraries:", DoNotPackageJavaLibraries); + + var jars = new List (); + if (!EnableInstantRun) + jars.Add (new TaskItem (MonoPlatformJarPath)); + if (JavaSourceFiles != null) + foreach (var jar in JavaSourceFiles.Where (p => Path.GetExtension (p.ItemSpec) == ".jar")) + jars.Add (jar); + if (JavaLibraries != null) + foreach (var jarfile in JavaLibraries) + jars.Add (jarfile); + if (LibraryProjectJars != null) + foreach (var jar in LibraryProjectJars) + if (!MonoAndroidHelper.IsEmbeddedReferenceJar (jar.ItemSpec)) + jars.Add (jar); + if (AdditionalJavaLibraryReferences != null) + foreach (var jar in AdditionalJavaLibraryReferences.Distinct (TaskItemComparer.DefaultComparer)) + jars.Add (jar); + + var distinct = MonoAndroidHelper.DistinctFilesByContent (jars); + jars = jars.Where (j => distinct.Contains (j)).ToList (); + + JavaLibrariesToCompile = jars.Where (j => !IsExcluded (j.ItemSpec)).ToArray (); + ReferenceJavaLibraries = jars.Except (JavaLibrariesToCompile).ToArray (); + + Log.LogDebugTaskItems (" JavaLibrariesToCompile:", JavaLibrariesToCompile); + Log.LogDebugTaskItems (" ReferenceJavaLibraries:", ReferenceJavaLibraries); + + return true; + } + + bool IsExcluded (string jar) + { + if (DoNotPackageJavaLibraries == null) + return false; + return DoNotPackageJavaLibraries.Any (x => Path.GetFileName (jar).EndsWith (x.ItemSpec, StringComparison.OrdinalIgnoreCase)); + } + } + + class TaskItemComparer : IEqualityComparer { + public static readonly TaskItemComparer DefaultComparer = new TaskItemComparer (); + + public bool Equals (ITaskItem a, ITaskItem b) + { + return string.Compare (a.ItemSpec, b.ItemSpec, StringComparison.OrdinalIgnoreCase) == 0; + } + + public int GetHashCode (ITaskItem value) + { + return value.ItemSpec.GetHashCode (); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Dx.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Dx.cs new file mode 100644 index 00000000000..f4cce86e0fd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Dx.cs @@ -0,0 +1,90 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Linq; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Text; +using System.Collections.Generic; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + + public class Dx : JavaToolTask + { + [Required] + public string DxJarPath { get; set; } + + [Required] + public string OutputDexDirectory { get; set; } + + public string [] Jars { get; set; } + + [Output] + public string [] OutputDexFiles { get; set; } + + [Output] + public string OutputPlatformDexPath { get; set; } + + protected override string ToolName { + get { return OS.IsWindows ? "java.exe" : "java"; } + } + + public override bool Execute () + { + Log.LogDebugMessage ("Dx Task"); + Log.LogDebugMessage (" DxJarPath: ", DxJarPath); + Log.LogDebugMessage (" OutputJackDirectory: ", OutputDexDirectory); + Log.LogDebugTaskItems (" Jars: ", Jars); + + if (!Directory.Exists (OutputDexDirectory)) + Directory.CreateDirectory (OutputDexDirectory); + + bool retval = true; + var outputJackFiles = new List (); + var md5 = System.Security.Cryptography.MD5.Create (); + foreach (var jar in Jars) { + context_jar = jar; + context_dex = Path.Combine (OutputDexDirectory, BitConverter.ToString (md5.ComputeHash (Encoding.UTF8.GetBytes (jar))) + Path.GetFileNameWithoutExtension (context_jar) + ".dex"); + retval &= base.Execute (); + outputJackFiles.Add (context_dex); + } + OutputDexFiles = outputJackFiles.ToArray (); + + Log.LogDebugTaskItems (" OutputJackFiles: ", OutputDexFiles); + return retval; + } + + string context_jar; + string context_dex; + + protected override string GenerateCommandLineCommands () + { + // Running command: C:\Program Files (x86)\Java\jdk1.6.0_20\bin\java.exe + // "-jar" "C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\jill.jar" + // "--output-dex" "bin\classes" + // "-classpath" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\android\bin\mono.android.jar" + // "@C:\Users\Jonathan\AppData\Local\Temp\tmp79c4ac38.tmp" + + //var android_dir = MonoDroid.MonoDroidSdk.GetAndroidProfileDirectory (TargetFrameworkDirectory); + + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitchIfNotNull ("-jar ", DxJarPath); + + //cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8"); + + cmd.AppendSwitch ("--dex"); + cmd.AppendSwitch ("--verbose"); + + cmd.AppendSwitchIfNotNull ("--output=", context_dex); + cmd.AppendFileNameIfNotNull (context_jar); + + return cmd.ToString (); + } + } + +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs new file mode 100644 index 00000000000..88e7d806639 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateJavaStubs.cs @@ -0,0 +1,278 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using MonoDroid.Utils; +using Mono.Cecil; + + +using Java.Interop.Tools.Cecil; +using Java.Interop.Tools.Diagnostics; +using Java.Interop.Tools.JavaCallableWrappers; +using Java.Interop.Tools.TypeNameMappings; + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + using PackageNamingPolicyEnum = PackageNamingPolicy; + + public class GenerateJavaStubs : Task + { + [Required] + public ITaskItem[] ResolvedAssemblies { get; set; } + + [Required] + public ITaskItem[] ResolvedUserAssemblies { get; set; } + + [Required] + public string AcwMapFile { get; set; } + + public string ManifestTemplate { get; set; } + public string[] MergedManifestDocuments { get; set; } + + public bool Debug { get; set; } + public string ApplicationName { get; set; } + public string PackageName { get; set; } + + public string AndroidSdkDir { get; set; } + + public string AndroidSdkPlatform { get; set; } + public string OutputDirectory { get; set; } + public string MergedAndroidManifestOutput { get; set; } + + public bool EmbedAssemblies { get; set; } + public bool NeedsInternet { get; set; } + + public bool UseSharedRuntime { get; set; } + + [Required] + public string ResourceDirectory { get; set; } + + public string BundledWearApplicationName { get; set; } + + public string PackageNamingPolicy { get; set; } + + public string ApplicationJavaClass { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GenerateJavaStubs Task"); + Log.LogDebugMessage (" ManifestTemplate: {0}", ManifestTemplate); + Log.LogDebugMessage (" Debug: {0}", Debug); + Log.LogDebugMessage (" ApplicationName: {0}", ApplicationName); + Log.LogDebugMessage (" PackageName: {0}", PackageName); + Log.LogDebugMessage (" AndroidSdkDir: {0}", AndroidSdkDir); + Log.LogDebugMessage (" AndroidSdkPlatform: {0}", AndroidSdkPlatform); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" MergedAndroidManifestOutput: {0}", MergedAndroidManifestOutput); + Log.LogDebugMessage (" UseSharedRuntime: {0}", UseSharedRuntime); + Log.LogDebugTaskItems (" ResolvedAssemblies:", ResolvedAssemblies); + Log.LogDebugTaskItems (" ResolvedUserAssemblies:", ResolvedUserAssemblies); + Log.LogDebugMessage (" BundledWearApplicationName: {0}", BundledWearApplicationName); + Log.LogDebugTaskItems (" MergedManifestDocuments:", MergedManifestDocuments); + Log.LogDebugMessage (" PackageNamingPolicy: {0}", PackageNamingPolicy); + Log.LogDebugMessage (" ApplicationJavaClass: {0}", ApplicationJavaClass); + + try { + Run (); + } + catch (XamarinAndroidException e) { + Log.LogCodedError (string.Format ("XA{0:0000}", e.Code), e.MessageWithoutCode); + if (MonoAndroidHelper.LogInternalExceptions) + Log.LogMessage (e.ToString ()); + } + + return !Log.HasLoggedErrors; + } + + void Run () + { + PackageNamingPolicy pnp; + JniType.PackageNamingPolicy = Enum.TryParse (PackageNamingPolicy, out pnp) ? pnp : PackageNamingPolicyEnum.LowercaseHash; + var temp = Path.Combine (Path.GetTempPath (), Path.GetRandomFileName ()); + Directory.CreateDirectory (temp); + + // We're going to do 3 steps here instead of separate tasks so + // we can share the list of JLO TypeDefinitions between them + var res = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols:true); + + var selectedWhitelistAssemblies = new List (); + + // Put every assembly we'll need in the resolver + foreach (var ass in ResolvedAssemblies) { + res.Load (Path.GetFullPath (ass.ItemSpec)); + if (MonoAndroidHelper.FrameworkAttributeLookupTargets.Any (a => Path.GetFileName (ass.ItemSpec) == a)) + selectedWhitelistAssemblies.Add (Path.GetFullPath (ass.ItemSpec)); + } + + // However we only want to look for JLO types in user code + var assemblies = ResolvedUserAssemblies.Select (p => p.ItemSpec).ToList (); + var fxAdditions = MonoAndroidHelper.GetFrameworkAssembliesToTreatAsUserAssemblies (ResolvedAssemblies) + .Where (a => assemblies.All (x => Path.GetFileName (x) != Path.GetFileName (a))); + assemblies = assemblies.Concat (fxAdditions).ToList (); + + // Step 1 - Find all the JLO types + var all_java_types = JavaTypeScanner.GetJavaTypes (assemblies, res, Log.LogWarning); + + WriteTypeMappings (all_java_types); + + var java_types = all_java_types.Where (t => !JavaTypeScanner.ShouldSkipJavaCallableWrapperGeneration (t)); + + // Step 2 - Generate Java stub code + var keep_going = Generator.CreateJavaSources (Log, java_types, temp, UseSharedRuntime, int.Parse (AndroidSdkPlatform) <= 10, ResolvedAssemblies.Any (ass => Path.GetFileName (ass.ItemSpec) == "Mono.Android.Export.dll")); + + var temp_map_file = Path.Combine (temp, "acw-map.temp"); + + // We need to save a map of .NET type -> ACW type for resource file fixups + var managed = new Dictionary (); + var java = new Dictionary (); + var acw_map = new StreamWriter (temp_map_file); + + foreach (var type in java_types) { + string managedKey = type.FullName.Replace ('/', '.'); + string javaKey = JniType.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)) { + Log.LogWarning ( + "Duplicate managed type found! Mappings between managed types and Java types must be unique. " + + "First Type: '{0}'; Second Type: '{1}'.", + conflict.GetAssemblyQualifiedName (), + type.GetAssemblyQualifiedName ()); + Log.LogWarning ( + "References to the type '{0}' will refer to '{1}'.", + managedKey, conflict.GetAssemblyQualifiedName ()); + continue; + } + if (java.TryGetValue (javaKey, out conflict)) { + Log.LogError ( + "Duplicate Java type found! Mappings between managed types and Java types must be unique. " + + "First Type: '{0}'; Second Type: '{1}'", + conflict.GetAssemblyQualifiedName (), + type.GetAssemblyQualifiedName ()); + keep_going = false; + continue; + } + managed.Add (managedKey, type); + java.Add (javaKey, type); + acw_map.WriteLine ("{0};{1}", managedKey, javaKey); + acw_map.WriteLine ("{0};{1}", JniType.ToCompatJniName (type).Replace ('/', '.'), javaKey); + } + + acw_map.Close (); + + //The previous steps found an error, so we must abort and not generate any further output + //We must do so subsequent unchanged builds fail too. + if (!keep_going) { + File.Delete (temp_map_file); + return; + } + + MonoAndroidHelper.CopyIfChanged (temp_map_file, AcwMapFile); + + try { File.Delete (temp_map_file); } catch (Exception) { } + + // Only overwrite files if the contents actually changed + foreach (var file in Directory.GetFiles (temp, "*", SearchOption.AllDirectories)) { + var dest = Path.GetFullPath (Path.Combine (OutputDirectory, "src", file.Substring (temp.Length + 1))); + + MonoAndroidHelper.CopyIfChanged (file, dest); + } + + // Step 3 - Merge [Activity] and friends into AndroidManifest.xml + var manifest = new ManifestDocument (ManifestTemplate, this.Log); + + manifest.PackageName = PackageName; + manifest.ApplicationName = ApplicationName ?? PackageName; + manifest.Assemblies.AddRange (assemblies); + manifest.Resolver = res; + manifest.SdkDir = AndroidSdkDir; + manifest.SdkVersion = AndroidSdkPlatform; + manifest.Debug = Debug; + manifest.NeedsInternet = NeedsInternet; + + var additionalProviders = manifest.Merge (all_java_types, selectedWhitelistAssemblies, ApplicationJavaClass, EmbedAssemblies, BundledWearApplicationName, MergedManifestDocuments); + + var temp_manifest = Path.Combine (temp, "AndroidManifest.xml"); + var real_manifest = Path.GetFullPath (MergedAndroidManifestOutput); + + manifest.Save (temp_manifest); + + // Only write the new manifest if it actually changed + MonoAndroidHelper.CopyIfChanged (temp_manifest, real_manifest); + + // Create additional runtime provider java sources. + string providerTemplateFile = UseSharedRuntime ? "MonoRuntimeProvider.Shared.java" : "MonoRuntimeProvider.Bundled.java"; + string providerTemplate = new StreamReader (typeof (JavaCallableWrapperGenerator).Assembly.GetManifestResourceStream (providerTemplateFile)).ReadToEnd (); + + foreach (var provider in additionalProviders) { + var temp_provider = Path.Combine (temp, provider + ".java"); + File.WriteAllText (temp_provider, providerTemplate.Replace ("MonoRuntimeProvider", provider)); + var real_provider_dir = Path.GetFullPath (Path.Combine (OutputDirectory, "src", "mono")); + Directory.CreateDirectory (real_provider_dir); + var real_provider = Path.Combine (real_provider_dir, provider + ".java"); + MonoAndroidHelper.CopyIfChanged (temp_provider, real_provider); + } + + // Create additional application java sources. + + Action> save = (resource, filename, destDir, applyTemplate) => { + string temp_file = Path.Combine (temp, filename); + string template = applyTemplate (new StreamReader (typeof (GenerateJavaStubs).Assembly.GetManifestResourceStream (resource)).ReadToEnd ()); + File.WriteAllText (temp_file, template); + Directory.CreateDirectory (destDir); + var real_file = Path.Combine (destDir, filename); + MonoAndroidHelper.CopyIfChanged (temp_file, real_file); + }; + + StringWriter regCallsWriter = new StringWriter (); + regCallsWriter.WriteLine ("\t\t// Application and Instrumentation ACWs must be registered first."); + foreach (var type in java_types) { + if (JniType.IsApplication (type) || JniType.IsInstrumentation (type)) { + string javaKey = JniType.ToJniName (type).Replace ('/', '.'); + regCallsWriter.WriteLine ("\t\tmono.android.Runtime.register (\"{0}\", {1}.class, {1}.__md_methods);", + type.GetAssemblyQualifiedName (), javaKey); + } + } + regCallsWriter.Close (); + + var real_app_dir = Path.GetFullPath (Path.Combine (OutputDirectory, "src", "mono", "android", "app")); + string applicationTemplateFile = "ApplicationRegistration.java"; + save (applicationTemplateFile, applicationTemplateFile, real_app_dir, + template => template.Replace ("// REGISTER_APPLICATION_AND_INSTRUMENTATION_CLASSES_HERE", regCallsWriter.ToString ())); + + // Create NotifyTimeZoneChanges java sources. + string notifyTimeZoneChangesFile = "NotifyTimeZoneChanges.java"; + save (notifyTimeZoneChangesFile, notifyTimeZoneChangesFile, real_app_dir, template => template); + + // Delete our temp directory + try { Directory.Delete (temp, true); } catch (Exception) { } + } + + void WriteTypeMappings (List types) + { + var gen = UseSharedRuntime + ? new TypeNameMapGenerator (types, Log.LogDebugMessage) + : new TypeNameMapGenerator (ResolvedAssemblies.Select (p => p.ItemSpec), Log.LogDebugMessage); + UpdateWhenChanged (Path.Combine (OutputDirectory, "typemap.jm"), gen.WriteJavaToManaged); + UpdateWhenChanged (Path.Combine (OutputDirectory, "typemap.mj"), gen.WriteManagedToJava); + } + + void UpdateWhenChanged (string path, Action generator) + { + var np = path + ".new"; + using (var o = File.OpenWrite (np)) + generator (o); + Files.CopyIfChanged (np, path); + File.Delete (np); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateManagedAidlProxies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateManagedAidlProxies.cs new file mode 100644 index 00000000000..af0bb424a12 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateManagedAidlProxies.cs @@ -0,0 +1,116 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. +using System; +using System.CodeDom; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Mono.Cecil; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Monodroid; +using Xamarin.AndroidTools.Aidl; + +namespace Xamarin.Android.Tasks +{ + public class GenerateManagedAidlProxies : Task + { + [Required] + public ITaskItem[] References { get; set; } + + [Required] + public ITaskItem[] SourceAidlFiles { get; set; } + + [Required] + public string IntermediateOutputDirectory { get; set; } + + public string OutputNamespace { get; set; } + + public string ParcelableHandlingOption { get; set; } + + public GenerateManagedAidlProxies () + { + } + + public override bool Execute () + { + Log.LogDebugMessage ("Task GenerateManagedAidlProxies"); + Log.LogDebugTaskItems (" References:", References); + Log.LogDebugTaskItems (" SourceAidlFiles:", SourceAidlFiles); + Log.LogDebugMessage (" IntermediateOutputDirectory: {0}", IntermediateOutputDirectory); + Log.LogDebugMessage (" OutputNamespace: {0}", OutputNamespace); + Log.LogDebugMessage (" ParcelableHandlingOption: {0}", ParcelableHandlingOption); + + if (SourceAidlFiles.Length == 0) // nothing to do + return true; + + var opts = new ConverterOptions () { + Verbose = true, + ParcelableHandling = ParcelableHandling.Ignore, + }; + + if (!string.IsNullOrEmpty (ParcelableHandlingOption)) + opts.ParcelableHandling = ToParcelableHandling (ParcelableHandlingOption); + + if (!string.IsNullOrEmpty (OutputNamespace)) + opts.OutputNS = OutputNamespace; + + foreach (var file in References) + opts.AddReference (file.ItemSpec); + foreach (var file in SourceAidlFiles) + opts.AddFile (file.ItemSpec); + + var tool = new AidlCompiler (); + using (var fsw = TextWriter.Null/*File.AppendText (Path.Combine (IntermediateOutputDirectory, "AidlFilesWrittenAbsolute.txt"))*/) { + tool.FileWritten += (file, source) => Log.LogDebugMessage ("Written ... {0}", file); + string outPath = Path.Combine (IntermediateOutputDirectory, "aidl"); + var ret = tool.Run (opts, assfile => AssemblyDefinition.ReadAssembly (assfile), (dir, file) => { + var dst = Path.GetFullPath (Path.Combine (outPath, Path.ChangeExtension (file, ".cs"))); + if (!dst.StartsWith (outPath)) + dst = Path.Combine (outPath, Path.ChangeExtension (Path.GetFileName (file), ".cs")); + string dstdir = Path.GetDirectoryName (dst); + if (!Directory.Exists (dstdir)) + Directory.CreateDirectory (dstdir); + fsw.WriteLine (dst); + fsw.Flush (); + return dst; + }); + if (ret.LogMessages.Count > 0) { + foreach (var p in ret.LogMessages) + Log.LogError ("{0} {1}: {2}", Path.GetFullPath (p.Key), p.Value.Location, p.Value.ToString ()); + } + } + return true; + } + + static ParcelableHandling ToParcelableHandling (string option) + { + switch (option) { + case "ignore": return ParcelableHandling.Ignore; + case "error": return ParcelableHandling.Error; + case "stub": return ParcelableHandling.Stub; + } + throw new ArgumentException ("Invalid parcelable option: " + option); + } + } + + static class Extensions + { + internal static void AddFile (this ConverterOptions opts, string name) + { + if (File.Exists (name)) + opts.InputFiles.Add (name); + else + throw new InvalidOperationException (String.Format ("File {0} not exist", name)); + } + + internal static void AddReference (this ConverterOptions opts, string name) + { + if (File.Exists (name)) + opts.References.Add (name); + else + throw new InvalidOperationException (String.Format ("File {0} not exist", name)); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs new file mode 100644 index 00000000000..7455f075bc4 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GeneratePackageManagerJava.cs @@ -0,0 +1,113 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using System.Linq; +using System.Reflection; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +using Xamarin.Android.Tools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GeneratePackageManagerJava : Task + { + [Required] + public ITaskItem[] ResolvedAssemblies { get; set; } + + [Required] + public ITaskItem[] ResolvedUserAssemblies { get; set; } + + [Required] + public string OutputDirectory { get; set; } + + [Required] + public string UseSharedRuntime { get; set; } + + [Required] + public string MainAssembly { get; set; } + + [Required] + public string TargetFrameworkVersion { get; set; } + + [Required] + public string Manifest { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GeneratePackageManagerJava Task"); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" TargetFrameworkVersion: {0}", TargetFrameworkVersion); + Log.LogDebugMessage (" Manifest: {0}", Manifest); + Log.LogDebugMessage (" UseSharedRuntime: {0}", UseSharedRuntime); + Log.LogDebugMessage (" MainAssembly: {0}", MainAssembly); + Log.LogDebugTaskItems (" ResolvedAssemblies:", ResolvedAssemblies); + Log.LogDebugTaskItems (" ResolvedUserAssemblies:", ResolvedUserAssemblies); + + var shared_runtime = string.Compare (UseSharedRuntime, "true", true) == 0; + var doc = AndroidAppManifest.Load (Manifest); + int minApiVersion = doc.MinSdkVersion == null ? 4 : (int) doc.MinSdkVersion; + // We need to include any special assemblies in the Assemblies list + var assemblies = ResolvedUserAssemblies.Select (p => p.ItemSpec) + .Concat (MonoAndroidHelper.GetFrameworkAssembliesToTreatAsUserAssemblies (ResolvedAssemblies)) + .ToList (); + var mainFileName = Path.GetFileName (MainAssembly); + Func fileNameEq = (a,b) => a.Equals (b, StringComparison.OrdinalIgnoreCase); + assemblies = assemblies.Where (a => fileNameEq (a, mainFileName)).Concat (assemblies.Where (a => !fileNameEq (a, mainFileName))).ToList (); + + // Write first to a temporary file + var temp = Path.GetTempFileName (); + + using (var pkgmgr = File.CreateText (temp)) { + // Write the boilerplate from the MonoPackageManager.java resource + var packageManagerResource = minApiVersion < 9 ? "MonoPackageManager.api4.java" : "MonoPackageManager.java"; + using (var template = new StreamReader (Assembly.GetExecutingAssembly ().GetManifestResourceStream (packageManagerResource))) { + string line; + while ((line = template.ReadLine ()) != null) { + pkgmgr.WriteLine (line); + } + } + + // Write all the user assemblies + pkgmgr.WriteLine ("class MonoPackageManager_Resources {"); + pkgmgr.WriteLine ("\tpublic static final String[] Assemblies = new String[]{"); + + pkgmgr.WriteLine ("\t\t/* We need to ensure that \"{0}\" comes first in this list. */", mainFileName); + foreach (var assembly in assemblies) { + pkgmgr.WriteLine ("\t\t\"" + Path.GetFileName (assembly) + "\","); + } + + // Write the assembly dependencies + pkgmgr.WriteLine ("\t};"); + pkgmgr.WriteLine ("\tpublic static final String[] Dependencies = new String[]{"); + + //foreach (var assembly in assemblies.Except (args.Assemblies)) { + // if (args.SharedRuntime && !Toolbox.IsInSharedRuntime (assembly)) + // pkgmgr.WriteLine ("\t\t\"" + Path.GetFileName (assembly) + "\","); + //} + + pkgmgr.WriteLine ("\t};"); + + // Write the platform api apk we need + pkgmgr.WriteLine ("\tpublic static final String ApiPackageName = {0};", shared_runtime + ? string.Format ("\"Mono.Android.Platform.ApiLevel_{0}\"", + MonoDroidSdk.GetApiLevelForFrameworkVersion (TargetFrameworkVersion)) + : "null"); + pkgmgr.WriteLine ("}"); + } + + // Only copy to the real location if the contents actually changed + var dest = Path.GetFullPath (Path.Combine (OutputDirectory, "MonoPackageManager.java")); + + MonoAndroidHelper.CopyIfChanged (temp, dest); + + try { File.Delete (temp); } catch (Exception) { } + + try { File.Delete (temp); } catch (Exception) { } + + return !Log.HasLoggedErrors; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs new file mode 100644 index 00000000000..10cc43c8c79 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GenerateResourceDesigner.cs @@ -0,0 +1,275 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. +using System; +using System.CodeDom; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Monodroid; + +using Java.Interop.Tools.Cecil; + +namespace Xamarin.Android.Tasks +{ + public class GenerateResourceDesigner : Task + { + [Required] + public string NetResgenOutputFile { get; set; } + + public string JavaResgenInputFile { get; set; } + + public string Namespace { get; set; } + + [Required] + public string ProjectDir { get; set; } + + [Required] + public ITaskItem[] Resources { get; set; } + + [Required] + public string ResourceDirectory { get; set; } + + public ITaskItem[] AdditionalResourceDirectories { get; set; } + + [Required] + public bool IsApplication { get; set; } + + public ITaskItem[] References { get; set; } + + private Dictionary resource_fixup = new Dictionary (StringComparer.OrdinalIgnoreCase); + + public override bool Execute () + { + // In Xamarin Studio, if the project name isn't a valid C# identifier + // then $(RootNamespace) is not set, and the generated Activity is + // placed into the "Application" namespace. VS just munges the project + // name to be a valid C# identifier. + // Use "Application" as the default namespace name to work with XS. + Namespace = Namespace ?? "Application"; + + Log.LogDebugMessage ("GenerateResourceDesigner Task"); + Log.LogDebugMessage (" NetResgenOutputFile: {0}", NetResgenOutputFile); + Log.LogDebugMessage (" JavaResgenInputFile: {0}", JavaResgenInputFile); + Log.LogDebugMessage (" Namespace: {0}", Namespace); + Log.LogDebugMessage (" ResourceDirectory: {0}", ResourceDirectory); + Log.LogDebugTaskItemsAndLogical (" AdditionalResourceDirectories:", AdditionalResourceDirectories); + Log.LogDebugMessage (" IsApplication: {0}", IsApplication); + Log.LogDebugTaskItemsAndLogical (" Resources:", Resources); + Log.LogDebugTaskItemsAndLogical (" References:", References); + + if (!File.Exists (JavaResgenInputFile)) + return true; + + // ResourceDirectory may be a relative path, and + // we need to compare it to absolute paths + ResourceDirectory = Path.GetFullPath (ResourceDirectory); + + // Create our capitalization maps so we can support mixed case resources + foreach (var item in Resources) { + if (!item.ItemSpec.StartsWith (ResourceDirectory)) + continue; + + var name = item.ItemSpec.Substring (ResourceDirectory.Length); + var logical_name = item.GetMetadata ("LogicalName"); + + AddRename (name.Replace ('/', Path.DirectorySeparatorChar), logical_name.Replace ('/', Path.DirectorySeparatorChar)); + } + if (AdditionalResourceDirectories != null) { + foreach (var additionalDir in AdditionalResourceDirectories) { + var file = Path.Combine (ProjectDir, Path.GetDirectoryName (additionalDir.ItemSpec), "__res_name_case_map.txt"); + if (File.Exists (file)) { + foreach (var line in File.ReadAllLines (file).Where (l => !string.IsNullOrEmpty (l))) { + string [] tok = line.Split (';'); + AddRename (tok [1].Replace ('/', Path.DirectorySeparatorChar), tok [0].Replace ('/', Path.DirectorySeparatorChar)); + } + } + } + } + + // Parse out the resources from the R.java file + JavaResourceParser.Log = Log; + var resources = JavaResourceParser.Parse (JavaResgenInputFile, IsApplication, resource_fixup); + + var extension = Path.GetExtension (NetResgenOutputFile); + var language = string.Compare (extension, ".fs", StringComparison.OrdinalIgnoreCase) == 0 ? "F#" : CodeDomProvider.GetLanguageFromExtension (extension); + bool isVB = string.Equals (extension, ".vb", StringComparison.OrdinalIgnoreCase); + bool isFSharp = string.Equals (language, "F#", StringComparison.OrdinalIgnoreCase); + bool isCSharp = string.Equals (language, "C#", StringComparison.OrdinalIgnoreCase); + + // Let VB put this in the default namespace + if (isVB) + Namespace = string.Empty; + + // Create static resource overwrite methods for each Resource class in libraries. + var assNames = new List (); + if (IsApplication && References != null && References.Any ()) { + // FIXME: should this be unified to some better code with ResolveLibraryProjectImports? + var resolver = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols: false); + foreach (var assname in References) { + var suffix = assname.ItemSpec.EndsWith (".dll") ? String.Empty : ".dll"; + string hintPath = assname.GetMetadata ("HintPath").Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + string fileName = assname.ItemSpec + suffix; + resolver.Load (Path.GetFullPath (assname.ItemSpec)); + if (!String.IsNullOrEmpty (hintPath) && !File.Exists (hintPath)) // ignore invalid HintPath + hintPath = null; + string assPath = String.IsNullOrEmpty (hintPath) ? fileName : hintPath; + if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (Path.GetFileName (fileName))) + continue; + Log.LogDebugMessage ("Scan assembly {0} for resource generator", fileName); + assNames.Add (assPath); + } + var asses = assNames.Select (ass => resolver.GetAssembly (ass)); + new ResourceDesignerImportGenerator (Namespace, resources) + .CreateImportMethods (asses); + } + + AdjustConstructor (isFSharp, resources); + foreach (var member in resources.Members) + if (member is CodeTypeDeclaration) + AdjustConstructor (isFSharp, (CodeTypeDeclaration) member); + + // Write out our Resources.Designer.cs file + + WriteFile (NetResgenOutputFile, resources, language, isFSharp, isCSharp); + + return !Log.HasLoggedErrors; + } + + // Remove private constructor in F#. + // Add static constructor. (but ignored in F#) + void AdjustConstructor (bool isFSharp, CodeTypeDeclaration type) + { + if (isFSharp) { + foreach (CodeTypeMember tm in type.Members) { + if (tm is CodeConstructor) { + type.Members.Remove (tm); + break; + } + } + } + + var staticCtor = new CodeTypeConstructor () { Attributes = MemberAttributes.Static }; + staticCtor.Statements.Add ( + new CodeExpressionStatement ( + new CodeMethodInvokeExpression ( + new CodeTypeReferenceExpression ( + new CodeTypeReference ( + "Android.Runtime.ResourceIdManager", + CodeTypeReferenceOptions.GlobalReference)), + "UpdateIdValues"))); + type.Members.Add (staticCtor); + } + + private void WriteFile (string file, CodeTypeDeclaration resources, string language, bool isFSharp, bool isCSharp) + { + CodeDomProvider provider = + isFSharp ? new FSharp.Compiler.CodeDom.FSharpCodeProvider () : + CodeDomProvider.CreateProvider (language); + + string code = null; + using (var o = new StringWriter ()) { + var options = new CodeGeneratorOptions () { + BracingStyle = "C", + IndentString = "\t", + }; + + var ns = string.IsNullOrEmpty (Namespace) + ? new CodeNamespace () + : new CodeNamespace (Namespace); + + if (resources != null) + ns.Types.Add (resources); + + var unit = new CodeCompileUnit (); + unit.Namespaces.Add (ns); + + var resgenatt = new CodeAttributeDeclaration (new CodeTypeReference ("Android.Runtime.ResourceDesignerAttribute", CodeTypeReferenceOptions.GlobalReference)); + resgenatt.Arguments.Add (new CodeAttributeArgument (new CodePrimitiveExpression (Namespace.Length > 0 ? Namespace + ".Resource" : "Resource"))); + resgenatt.Arguments.Add (new CodeAttributeArgument ("IsApplication", new CodePrimitiveExpression (IsApplication))); + unit.AssemblyCustomAttributes.Add (resgenatt); + + // Add Pragma to disable warnings about no Xml documentation + if (isCSharp) + provider.GenerateCodeFromCompileUnit(new CodeSnippetCompileUnit("#pragma warning disable 1591"), o, options); + + provider.CreateGenerator (o).GenerateCodeFromCompileUnit (unit, o, options); + + // Add Pragma to re-enable warnings about no Xml documentation + if (isCSharp) + provider.GenerateCodeFromCompileUnit(new CodeSnippetCompileUnit("#pragma warning restore 1591"), o, options); + + code = o.ToString (); + + // post-processing for F# + if (isFSharp) { + code = code.Replace ("\r\n", "\n"); + while (true) { + int skipLen = " = class".Length; + int idx = code.IndexOf (" = class"); + if (idx < 0) + break; + int end = code.IndexOf (" end"); + string head = code.Substring (0, idx); + string mid = end < 0 ? code.Substring (idx) : code.Substring (idx + skipLen, end - idx - skipLen); + string last = end < 0 ? null : code.Substring (end + " end".Length); + code = head + @" () = + static do Android.Runtime.ResourceIdManager.UpdateIdValues()" + mid + "\n" + last; + } + } + } + + var temp_o = Path.Combine (Path.GetDirectoryName (file), "__" + Path.GetFileName (file) + ".new"); + using (TextWriter o = File.CreateText (temp_o)) + o.Write (code); + MonoAndroidHelper.CopyIfChanged (temp_o, file); + try { File.Delete (temp_o); } catch (Exception) { } + } + + private void AddRename (string android, string user) + { + var from = android; + var to = user; + + if (from.Contains ('.')) + from = from.Substring (0, from.LastIndexOf ('.')); + if (to.Contains ('.')) + to = to.Substring (0, to.LastIndexOf ('.')); + + from = NormalizeAlternative (from); + to = NormalizeAlternative (to); + + string curTo; + + if (resource_fixup.TryGetValue (from, out curTo)) { + if (string.Compare (to, curTo, StringComparison.OrdinalIgnoreCase) != 0) { + var ext = Path.GetExtension (android); + var dir = Path.GetDirectoryName (user); + + Log.LogWarning ("Resource target names differ; got '{0}', expected '{1}'.", + Path.Combine (dir, Path.GetFileName (to) + ext), + Path.Combine (dir, Path.GetFileName (curTo) + ext)); + } + + return; + } + + resource_fixup.Add (from, to); + } + + static string NormalizeAlternative (string value) + { + int s = value.IndexOfAny (new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); + + if (s < 0) + return value; + + int a = value.IndexOf ('-'); + + return + JavaResourceParser.GetNestedTypeName (value.Substring (0, (a < 0 || a >= s) ? s : a)).ToLowerInvariant () + + value.Substring (s); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs new file mode 100644 index 00000000000..1ca835afe27 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Generator.cs @@ -0,0 +1,152 @@ +// Copyright (C) 2012 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class BindingsGenerator : AndroidToolTask + { + [Required] + public string OutputDirectory { get; set; } + + [Required] + public string EnumDirectory { get; set; } + + [Required] + public string EnumMetadataDirectory { get; set; } + + [Required] + public string AndroidApiLevel { get; set; } + + [Required] + public string ApiXmlInput { get; set; } + + [Required] + public string AssemblyName { get; set; } + + public string CodegenTarget { get; set; } + + public bool NoStdlib { get; set; } + + public string TypeMappingReportFile { get; set; } + + public bool UseShortFileNames { get; set; } + + // apart from ReferencedManagedLibraries we need it to find mscorlib.dll. + [Required] + public string MonoAndroidFrameworkDirectories { get; set; } + + public ITaskItem[] TransformFiles { get; set; } + public ITaskItem[] ReferencedManagedLibraries { get; set; } + public ITaskItem[] AnnotationsZipFiles { get; set; } + + private List> transform_files = new List> (); + + public override bool Execute () + { + Log.LogDebugMessage ("BindingsGenerator Task"); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" EnumDirectory: {0}", EnumDirectory); + Log.LogDebugMessage (" EnumMetadataDirectory: {0}", EnumMetadataDirectory); + Log.LogDebugMessage (" ApiXmlInput: {0}", ApiXmlInput); + Log.LogDebugMessage (" AssemblyName: {0}", AssemblyName); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugMessage (" UseShortFileNames: {0}", UseShortFileNames); + Log.LogDebugTaskItems (" TransformFiles:", TransformFiles); + Log.LogDebugTaskItems (" ReferencedManagedLibraries:", ReferencedManagedLibraries); + Log.LogDebugTaskItems (" AnnotationsZipFiles:", AnnotationsZipFiles); + Log.LogDebugTaskItems (" TypeMappingReportFile:", TypeMappingReportFile); + + Directory.CreateDirectory (OutputDirectory); + + // We need to do this validation in Execute rather than GenerateCommandLineCommands + // because we can't terminate the build nicely in GenerateCommandLineCommands. + if (TransformFiles != null) + foreach (var fixup in TransformFiles) { + try { + var doc = XDocument.Load (fixup.ItemSpec); + + switch (doc.Root.Name.LocalName) { + case "metadata": + Log.LogDebugMessage ("Adding transform file {0} as metadata.", fixup.ItemSpec); + transform_files.Add (new Tuple (fixup.ItemSpec, "fixup")); + break; + case "enum-field-mappings": + Log.LogDebugMessage ("Adding transform file {0} as enumfields.", fixup.ItemSpec); + transform_files.Add (new Tuple (fixup.ItemSpec, "enumfields")); + break; + case "enum-method-mappings": + Log.LogDebugMessage ("Adding transform file {0} as enummethods.", fixup.ItemSpec); + transform_files.Add (new Tuple (fixup.ItemSpec, "enummethods")); + break; + default: + Log.LogError ("Unrecognized file", string.Empty, string.Empty, fixup.ItemSpec, 0, 0, 0, 0, "Unrecognized transform root element: {0}.", doc.Root.Name.LocalName); + return false; + } + } catch (Exception ex) { + Log.LogError ("Invalid Xml", string.Empty, string.Empty, fixup.ItemSpec, 0, 0, 0, 0, "Error parsing xml.\n{0}", ex); + return false; + } + } + + return base.Execute (); + } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + + cmd.AppendFileNameIfNotNull (ApiXmlInput); + + cmd.AppendSwitchIfNotNull ("--codegen-target=", CodegenTarget); + cmd.AppendSwitchIfNotNull ("--csdir=", OutputDirectory); + cmd.AppendSwitchIfNotNull ("--enumdir=", EnumDirectory); + cmd.AppendSwitchIfNotNull ("--enummetadata=", EnumMetadataDirectory); + cmd.AppendSwitchIfNotNull ("--assembly=", AssemblyName); + + if (!NoStdlib) { + string fxpath = MonoAndroidFrameworkDirectories.Split (';').First (p => new DirectoryInfo (p).GetFiles ("mscorlib.dll").Any ()); + cmd.AppendSwitchIfNotNull ("--ref=", Path.Combine (Path.GetFullPath (fxpath), "mscorlib.dll")); + } + + if (ReferencedManagedLibraries != null) + foreach (var lib in ReferencedManagedLibraries) + cmd.AppendSwitchIfNotNull ("--ref=", Path.GetFullPath (lib.ItemSpec)); + if (AnnotationsZipFiles != null) + foreach (var zip in AnnotationsZipFiles) + cmd.AppendSwitchIfNotNull ("--annotations=", Path.GetFullPath (zip.ItemSpec)); + + foreach (var tf in transform_files) + cmd.AppendSwitchIfNotNull (string.Format ("--{0}=", tf.Item2), tf.Item1); + + cmd.AppendSwitchIfNotNull ("--api-level=", AndroidApiLevel); + + cmd.AppendSwitchIfNotNull ("--type-map-report=", TypeMappingReportFile); + + cmd.AppendSwitch ("--global"); + cmd.AppendSwitch ("--public"); + + if (UseShortFileNames) + cmd.AppendSwitch ("--use-short-file-names"); + + return cmd.ToString (); + } + + protected override string ToolName { + get { return OS.IsWindows ? "generator.exe" : "generator"; } + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAddOnPlatformLibraries.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAddOnPlatformLibraries.cs new file mode 100644 index 00000000000..af5cc11caaa --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAddOnPlatformLibraries.cs @@ -0,0 +1,37 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Xml.Linq; +using System.Collections.Generic; +using System.Linq; + +namespace Xamarin.Android.Tasks +{ + public class GetAddOnPlatformLibraries : Task + { + [Required] + public string AndroidSdkDir { get; set; } + [Required] + public string AndroidSdkPlatform { get; set; } + [Required] + public string Manifest { get; set; } + + [Output] + public ITaskItem[] AddOnPlatformLibraries { get; set; } + + public override bool Execute () + { + var doc = XDocument.Load (Manifest); + var addons = new List (); + ManifestDocument.AddAddOns (doc.Element ("manifest").Element ("application"), AndroidSdkDir, AndroidSdkPlatform, addons); + + // If the manifest had any AddOn libraries, like maps, we'll need them later + if (addons.Any ()) { + AddOnPlatformLibraries = addons.Select (p => new TaskItem (p)).ToArray (); + Log.LogDebugTaskItems (" [OUTPUT] AddOnPlatformLibraries:", AddOnPlatformLibraries); + } + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs new file mode 100644 index 00000000000..88778c9907b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAdditionalResourcesFromAssemblies.cs @@ -0,0 +1,367 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Mono.Cecil; +using System.Xml; +using System.Xml.Linq; +using System.Security.Cryptography; +using System.Net; +using System.ComponentModel; +using Xamarin.Android.Tools; + +using Java.Interop.Tools.Cecil; + +namespace Xamarin.Android.Tasks { + + public class GetAdditionalResourcesFromAssemblies : AsyncTask + { + /// + /// Environment variable named XAMARIN_CACHEPATH that can be set + /// to override the default cache path. + /// + public const string CachePathEnvironmentVar = "%XAMARIN_CACHEPATH%"; + + /// + /// Base directory under the user's local app data folder that is used as the + /// default cache location. + /// + const string CacheBaseDir = "Xamarin"; + + /// + /// Assemblies whose resources are unzipped to a cache path rather than in-place. + /// + const string AssemblyNamePrefix = "Xamarin."; + + [Required] + public string AndroidSdkDirectory { get; set; } + + [Required] + public string AndroidNdkDirectory { get; set; } + + [Required] + public ITaskItem[] Assemblies { get; set; } + + [Required] + public string CacheFile { get; set;} + + string CachePath; + MD5 md5 = MD5.Create (); + + public GetAdditionalResourcesFromAssemblies () + { + } + + readonly Regex regex = new Regex(@"(\$(\w+))\b"); + + string SubstituteEnvVariables (string path) + { + MatchEvaluator replaceEnvVar = (Match m) => { + var e = m.Groups [2].Value; + switch (e) { + case "ANDROID_SDK_PATH": return AndroidSdkDirectory.TrimEnd (Path.DirectorySeparatorChar); + case "ANDROID_NDK_PATH": return AndroidNdkDirectory.TrimEnd (Path.DirectorySeparatorChar); + default: + var v = Environment.GetEnvironmentVariable (e); + if (!string.IsNullOrEmpty (v)) + return v; + return m.Groups [1].Value; + } + }; + + return regex.Replace (path, replaceEnvVar); + } + + internal static string ErrorMessage (CustomAttribute attr) + { + if (!attr.HasProperties) + return ""; + CustomAttributeNamedArgument arg = attr.Properties.FirstOrDefault (p => p.Name == "PackageName"); + if (arg.Name != null) { + string packageName = arg.Argument.Value as string; + if (packageName != null) + return string.Format ("Please install package: '{0}' available in SDK installer", packageName); + } + arg = attr.Properties.FirstOrDefault (p => p.Name == "InstallInstructions"); + if (arg.Name != null) { + string instInstructs = arg.Argument.Value as string; + if (instInstructs != null) + return "Installation instructions: " + instInstructs; + } + return null; + } + + void AddAttributeValue (ICollection items, CustomAttribute attr, string errorCode, string errorFmt, + bool isDirectory, string fullPath) + { + if (!attr.HasConstructorArguments || attr.ConstructorArguments.Count != 1) { + LogWarning ("Attribute {0} doesn't have expected one constructor agrument", attr.AttributeType.FullName); + return; + } + + CustomAttributeArgument arg = attr.ConstructorArguments.First (); + string path = arg.Value as string; + if (string.IsNullOrEmpty (path)) { + LogWarning ("Attribute {0} contructor argument is empty or not set to string", attr.AttributeType.FullName); + return; + } + path = SubstituteEnvVariables (path).TrimEnd (Path.DirectorySeparatorChar); + string baseDir = null; + CustomAttributeNamedArgument sourceUrl = attr.Properties.FirstOrDefault (p => p.Name == "SourceUrl"); + CustomAttributeNamedArgument embeddedArchive = attr.Properties.FirstOrDefault (p => p.Name == "EmbeddedArchive"); + CustomAttributeNamedArgument version = attr.Properties.FirstOrDefault (p => p.Name == "Version"); + CustomAttributeNamedArgument sha1sum = attr.Properties.FirstOrDefault (p => p.Name == "Sha1sum"); + var isXamarinAssembly = Path.GetFileName (fullPath).StartsWith (AssemblyNamePrefix, StringComparison.OrdinalIgnoreCase); + var assemblyDir = Path.Combine (CachePath, Path.GetFileNameWithoutExtension (fullPath)); + // upgrade the paths to not strip off the Xamarin. prefix as it might cause assembly + // collisions now that we cache everything here. + var oldAssemblyDir = Path.Combine (CachePath, Path.GetFileNameWithoutExtension (fullPath).Substring (isXamarinAssembly ? AssemblyNamePrefix.Length : 0)); + if (string.Compare (assemblyDir, oldAssemblyDir, StringComparison.OrdinalIgnoreCase) != 0 && Directory.Exists (oldAssemblyDir)) { + Directory.CreateDirectory (assemblyDir); + foreach (var oldDir in Directory.GetDirectories (oldAssemblyDir, "*", SearchOption.AllDirectories)) { + var newDir = oldDir.Replace (oldAssemblyDir, assemblyDir); + Directory.CreateDirectory (newDir); + } + foreach (string oldFile in Directory.GetFiles(oldAssemblyDir, "*.*", + SearchOption.AllDirectories)) { + var newFile = oldFile.Replace (oldAssemblyDir, assemblyDir); + Directory.CreateDirectory (Path.GetDirectoryName (newFile)); + File.Copy (oldFile, newFile, true); + } + Directory.Delete (oldAssemblyDir, recursive: true); + } + if (sourceUrl.Name != null) { + if (new Uri (sourceUrl.Argument.Value as string).IsFile) + assemblyDir = Path.GetDirectoryName (fullPath); + baseDir = MakeSureLibraryIsInPlace (assemblyDir, sourceUrl.Argument.Value as string, + version.Argument.Value as string, embeddedArchive.Argument.Value as string, sha1sum.Argument.Value as string); + } + if (!string.IsNullOrEmpty (baseDir) && !Path.IsPathRooted (path)) + path = Path.Combine (baseDir, path); + if ((isDirectory && Directory.Exists (path)) || + (!isDirectory && File.Exists (path))) { + items.Add (Path.GetFullPath (path).TrimEnd (Path.DirectorySeparatorChar)); + return; + } + LogCodedError (errorCode, errorFmt, ErrorMessage (attr), path); + } + + bool ExtractArchive (string url, string file, string contentDir) + { + if (!File.Exists (file)) { + // this file is supposed to exist! why doesn't it. + return false; + } + if (!Directory.Exists (contentDir)) { + try { + Directory.CreateDirectory (contentDir); + LogMessage ("Extracting {0} to {1}", file, contentDir); + using (var zip = MonoAndroidHelper.ReadZipFile (file)) { + int extracted = 0; + var o = Math.Max(1, (zip.Entries.Count / 10)); + zip.ExtractProgress += (object sender, Ionic.Zip.ExtractProgressEventArgs e) => { + if ((e.EntriesExtracted % o) != 0 || extracted == e.EntriesExtracted || e.EntriesExtracted == 0) + return; + LogMessage ("Extracted {0} of {1} files", e.EntriesExtracted, e.EntriesTotal); + extracted = e.EntriesExtracted; + }; + Files.ExtractAll (zip, contentDir); + } + } + catch (Exception e) { + LogCodedError ("XA5209", "Unzipping failed. Please download {0} and extract it to the {1} directory.", url, contentDir); + LogCodedError ("XA5209", "Reason: {0}", e.Message); + Log.LogMessage (MessageImportance.Low, e.ToString ()); + Directory.Delete (contentDir, true); + return false; + } + } + return true; + } + + bool IsValidDownload(string file, string sha1) + { + if (string.IsNullOrEmpty (sha1)) + return true; + var hash = Xamarin.Android.Tools.Files.HashFile (file).Replace ("-", String.Empty); + Log.LogDebugMessage ("File :{0}", file); + Log.LogDebugMessage ("SHA1 : {0}", hash); + Log.LogDebugMessage ("Expected SHA1 : {0}", sha1); + return string.Compare (hash, sha1, StringComparison.InvariantCultureIgnoreCase) == 0; + } + + string MakeSureLibraryIsInPlace (string destinationBase, string url, string version, string embeddedArchive, string sha1) + { + if (string.IsNullOrEmpty (url)) + return null; + + Log.LogDebugMessage ("Making sure we have {0} downloaded and extracted {1} from it...", url, embeddedArchive); + + string destinationDir = version == null ? destinationBase : Path.Combine (destinationBase, version); + bool createDestinationDirectory = !Directory.Exists (destinationDir); + if (createDestinationDirectory) + Directory.CreateDirectory (destinationDir); + + var hash = string.Concat (md5.ComputeHash (Encoding.UTF8.GetBytes (url)).Select (b => b.ToString ("X02"))); + var uri = new Uri (url); + + string zipDir = !uri.IsFile ? Path.Combine (CachePath, "zips") : destinationDir; + bool createZipDirectory = !Directory.Exists (zipDir); + if (createZipDirectory) + Directory.CreateDirectory (zipDir); + + string file = Path.Combine (zipDir, !uri.IsFile ? hash + ".zip" : Path.GetFileName (uri.AbsolutePath)); + if (!File.Exists (file) || !IsValidDownload (file, sha1)) { + int progress = -1; + DownloadProgressChangedEventHandler downloadHandler = (o, e) => { + if (e.ProgressPercentage % 10 != 0 || progress == e.ProgressPercentage) + return; + progress = e.ProgressPercentage; + LogMessage ("\t({0}/{1}b), total {2:F1}%", e.BytesReceived, + e.TotalBytesToReceive, e.ProgressPercentage); + }; + using (var client = new System.Net.WebClient ()) { + client.DownloadProgressChanged += downloadHandler; + LogMessage (" Downloading {0} into {1}", url, zipDir); + try { + client.DownloadFileTaskAsync (url, file).Wait (Token); + LogMessage (" Downloading Complete"); + } catch (Exception e) { + LogCodedError ("XA5208", "Download failed. Please download {0} and put it to the {1} directory.", url, destinationDir); + LogCodedError ("XA5208", "Reason: {0}", e.GetBaseException ().Message); + Log.LogMessage (MessageImportance.Low, e.ToString ()); + if (File.Exists (file)) + File.Delete (file); + } + client.DownloadProgressChanged -= downloadHandler; + } + } + else + LogDebugMessage (" reusing existing archive: {0}", file); + string contentDir = Path.Combine (destinationDir, "content"); + + int attempt = 0; + while (attempt < 3 && !Log.HasLoggedErrors) { + var success = ExtractArchive (url, file, contentDir); + if (!success && Log.HasLoggedErrors) { + break; + } + + if (!string.IsNullOrEmpty (embeddedArchive)) { + string embeddedDir = Path.Combine (destinationDir, "embedded"); + success = ExtractArchive (string.Format ("{0}:{1}", url, embeddedArchive), Path.Combine (contentDir, embeddedArchive), embeddedDir); + if (success) { + contentDir = embeddedDir; + break; + } + if (Log.HasLoggedErrors) + break; + if (!success) { + Log.LogWarning ("Expected File {0} does not exist. Trying to extract again.", Path.Combine (contentDir, embeddedArchive)); + if (Directory.Exists (contentDir)) + Directory.Delete (contentDir, recursive: true); + } + } else + break; + attempt++; + } + + if (string.IsNullOrEmpty (contentDir) || !Directory.Exists (contentDir)) { + if (createZipDirectory) + Directory.Delete (zipDir); + if (createDestinationDirectory) + Directory.Delete (destinationDir); + } + + return contentDir; + } + + public override bool Execute () + { + LogDebugMessage ("GetAdditionalResourcesFromAssemblies Task"); + LogDebugMessage (" AndroidSdkDirectory: {0}", AndroidSdkDirectory); + LogDebugMessage (" AndroidNdkDirectory: {0}", AndroidNdkDirectory); + LogDebugTaskItems (" Assemblies: ", Assemblies); + + if (Environment.GetEnvironmentVariable ("XA_DL_IGNORE_CERT_ERRROS") == "yesyesyes") { + ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true; + LogDebugMessage (" Disabling download certificate validation callback."); + } + var androidResources = new HashSet (); + var javaLibraries = new HashSet (); + var nativeLibraries = new HashSet (); + var assemblies = new HashSet (); + + if (Assemblies == null) + return true; + + System.Threading.Tasks.Task.Run (() => { + // The cache location can be overriden by the (to be documented) XAMARIN_CACHEPATH + CachePath = Environment.ExpandEnvironmentVariables (CachePathEnvironmentVar); + CachePath = CachePath != CachePathEnvironmentVar + ? CachePath + : Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), CacheBaseDir); + + var resolver = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols: false); + foreach (var assItem in Assemblies) { + string fullPath = Path.GetFullPath (assItem.ItemSpec); + if (assemblies.Contains (fullPath)) { + LogDebugMessage (" Skip assembly: {0}, it was already processed", fullPath); + continue; + } + assemblies.Add (fullPath); + resolver.Load (fullPath); + // Append source file name (without the Xamarin. prefix or extension) to the base folder + // This would help avoid potential collisions. + foreach (var ca in resolver.GetAssembly (assItem.ItemSpec).CustomAttributes) { + switch (ca.AttributeType.FullName) { + case "Android.IncludeAndroidResourcesFromAttribute": + AddAttributeValue (androidResources, ca, "XA5206", "{0}. Android resource directory {1} doesn't exist.", true, fullPath); + break; + case "Java.Interop.JavaLibraryReferenceAttribute": + AddAttributeValue (javaLibraries, ca, "XA5207", "{0}. Java library file {1} doesn't exist.", false, fullPath); + break; + case "Android.NativeLibraryReferenceAttribute": + AddAttributeValue (nativeLibraries, ca, "XA5210", "{0}. Native library file {1} doesn't exist.", false, fullPath); + break; + } + } + } + }).ContinueWith ((t) => { + if (t.Exception != null) + Log.LogErrorFromException (t.Exception.GetBaseException ()); + Complete (); + }); + + var result = base.Execute (); + + var AdditionalAndroidResourcePaths = androidResources.ToArray (); + var AdditionalJavaLibraryReferences = javaLibraries.ToArray (); + var AdditionalNativeLibraryReferences = nativeLibraries + .Where (x => MonoAndroidHelper.GetNativeLibraryAbi (x) != null) + .ToArray (); + + var document = new XDocument ( + new XDeclaration ("1.0", "UTF-8", null), + new XElement ("Paths", + new XElement ("AdditionalAndroidResourcePaths", + AdditionalAndroidResourcePaths.Select(e => new XElement ("AdditionalAndroidResourcePath", e))), + new XElement ("AdditionalJavaLibraryReferences", + AdditionalJavaLibraryReferences.Select(e => new XElement ("AdditionalJavaLibraryReference", e))), + new XElement ("AdditionalNativeLibraryReferences", + AdditionalNativeLibraryReferences.Select(e => new XElement ("AdditionalNativeLibraryReference", e))) + )); + document.Save (CacheFile); + + LogDebugTaskItems (" AdditionalAndroidResourcePaths: ", AdditionalAndroidResourcePaths); + LogDebugTaskItems (" AdditionalJavaLibraryReferences: ", AdditionalJavaLibraryReferences); + LogDebugTaskItems (" AdditionalNativeLibraryReferences: ", AdditionalNativeLibraryReferences); + + return result && !Log.HasLoggedErrors; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidDefineConstants.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidDefineConstants.cs new file mode 100644 index 00000000000..df1da09fcca --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidDefineConstants.cs @@ -0,0 +1,38 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetAndroidDefineConstants : Task + { + [Required] + public int AndroidApiLevel { get; set; } + + public string ProductVersion { get; set; } + + [Output] + public string AndroidDefineConstants { get; set; } + + public override bool Execute () + { + var sb = new StringBuilder (); + + if (!string.IsNullOrEmpty (ProductVersion)) { + sb.AppendFormat ("__XAMARIN_ANDROID_{0}__;", Regex.Replace (ProductVersion, "[^A-Za-z0-9]", "_")); + } + sb.Append ("__MOBILE__;__ANDROID__"); + + for (int i = 1; i <= AndroidApiLevel; ++i) + sb.Append (";__ANDROID_").Append (i).Append ("__"); + + AndroidDefineConstants = sb.ToString (); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidPackageName.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidPackageName.cs new file mode 100644 index 00000000000..27a1b2e17fa --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAndroidPackageName.cs @@ -0,0 +1,77 @@ +// +// GetAndroidPackageName.cs +// +// Author: +// Jonathan Pobst +// +// Copyright (c) 2010 Novell, Inc. (http://www.novell.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.IO; +using System.Xml; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.Android.Tasks; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetAndroidPackageName : Task + { + public string ManifestFile { get; set; } + + [Required] + public string AssemblyName { get; set; } + + [Output] + public string PackageName { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GetAndroidPackageName Task"); + Log.LogDebugMessage (" ManifestFile: {0}", ManifestFile); + Log.LogDebugMessage (" AssemblyName: {0}", AssemblyName); + + // If we don't have a manifest, default to using the assembly name + // If the assembly doesn't have a period in it, duplicate it so it does + PackageName = AndroidAppManifest.CanonicalizePackageName (AssemblyName); + + if (string.IsNullOrEmpty (ManifestFile) || !File.Exists (ManifestFile)) { + Log.LogMessage (" PackageName: {0}", PackageName); + return true; + } + + XmlDocument doc = new XmlDocument (); + + doc.Load (ManifestFile); + + if (!doc.DocumentElement.HasAttribute ("package")) { + Log.LogMessage (" PackageName: {0}", PackageName); + return true; + } + + PackageName = AndroidAppManifest.CanonicalizePackageName (doc.DocumentElement.GetAttribute ("package")); + + Log.LogMessage (" PackageName: {0}", PackageName); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetAppSettingsDirectory.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetAppSettingsDirectory.cs new file mode 100644 index 00000000000..f39365c12b2 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetAppSettingsDirectory.cs @@ -0,0 +1,27 @@ +// Author: Jonathan Pobst +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetAppSettingsDirectory : Task + { + [Output] + public string AppSettingsDirectory { get; set; } + + public override bool Execute () + { + // Windows: C:\Users\Jonathan\AppData\Local\Xamarin\Mono for Android + var appdata_local = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData); + + AppSettingsDirectory = Path.Combine (appdata_local, "Xamarin", "Mono for Android"); + + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetConvertedJavaLibraries.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetConvertedJavaLibraries.cs new file mode 100644 index 00000000000..cf1d4403dcc --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetConvertedJavaLibraries.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Security.Cryptography; +using System.Text; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetConvertedJavaLibraries : Task + { + [Required] + public string Extension { get; set; } + public string OutputJackDirectory { get; set; } + public string [] JarsToConvert { get; set; } + [Output] + public string [] ConvertedFilesToBeGenerated { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GetConvertedJavaLibraries Task"); + Log.LogDebugMessage (" Extension: {0}", Extension); + Log.LogDebugMessage (" OutputJackDirectory: {0}", OutputJackDirectory); + Log.LogDebugTaskItems (" JarsToConvert:", JarsToConvert); + + var md5 = MD5.Create (); + ConvertedFilesToBeGenerated = + (JarsToConvert ?? new string [0]).Select ( + j => Path.Combine (OutputJackDirectory, + BitConverter.ToString (md5.ComputeHash (Encoding.UTF8.GetBytes (j))) + Path.ChangeExtension (Path.GetFileName (j), Extension))) + .ToArray (); + Log.LogDebugTaskItems (" ConvertedFilesToBeGenerated:", ConvertedFilesToBeGenerated); + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetExtraPackages.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetExtraPackages.cs new file mode 100644 index 00000000000..a6a1f6247c8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetExtraPackages.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetExtraPackages : Task + { + [Required] + public string IntermediateOutputPath { get; set; } + + [Output] + public string ExtraPackages { get; set; } + + [Required] + public string LibraryProjectImportsDirectoryName { get; set; } + + public override bool Execute () + { + var extraPackages = new List (); + var libProjects = Path.Combine (IntermediateOutputPath, "__library_projects__"); + if (Directory.Exists (libProjects)) { + foreach (var assDir in Directory.GetDirectories (libProjects)) { + foreach (var importBaseDir in new string [] { LibraryProjectImportsDirectoryName, "library_project_imports", }) { + string importsDir = Path.Combine (assDir, importBaseDir); + string libpkg = GetPackageNameForLibrary (importsDir, Path.GetDirectoryName (assDir)); + if (libpkg != null) + extraPackages.Add (libpkg); + } + } + } + + ExtraPackages = String.Join (":", extraPackages.Distinct ().ToArray ()); + + Log.LogDebugMessage ("CreateTemporaryDirectory Task"); + Log.LogDebugMessage (" OUTPUT: ExtraPackages: {0}", ExtraPackages); + + return true; + } + + string GetPackageNameForLibrary (string path, string assemblyName) + { + // It looks for: + // 1) bin/AndroidManifest.xml + // 2) AndroidManifest.xml + // and then tries to get /manifest/@package value + // if not found, return null. + + string manifest = "AndroidManifest.xml"; + foreach (var file in new string [] {Path.Combine (path, "bin", manifest), Path.Combine (path, manifest)}) { + try { + if (File.Exists (file)) + return XDocument.Load (file).Element (XName.Get ("manifest")).Attribute (XName.Get ("package")).Value; + } catch (Exception ex) { + throw new InvalidOperationException ("Failed to read 'package' attribute in 'manifest' element in AndroidManifest.xml from LibraryProject resource in {0}" + assemblyName, ex); + } + } + return null; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetFilesThatExist.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetFilesThatExist.cs new file mode 100644 index 00000000000..285e310365a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetFilesThatExist.cs @@ -0,0 +1,33 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + // We have a list of files, we want to get the + // ones that actually exist on disk. + public class GetFilesThatExist : Task + { + [Required] + public ITaskItem[] Files { get; set; } + + [Output] + public ITaskItem[] FilesThatExist { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GetFilesThatExist Task"); + Log.LogDebugTaskItems (" Files", Files); + + FilesThatExist = Files.Where (p => File.Exists (p.ItemSpec)).ToArray (); + + Log.LogDebugTaskItems (" [Output] FilesThatExist", FilesThatExist); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetImportedLibraries.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetImportedLibraries.cs new file mode 100644 index 00000000000..759c440f89a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetImportedLibraries.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Mono.Cecil; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Ionic.Zip; + +namespace Xamarin.Android.Tasks +{ + public class GetImportedLibraries : Task + { + [Required] + public string TargetDirectory { get; set; } + + public string CacheFile { get; set;} + + [Output] + public ITaskItem [] Jars { get; set; } + + [Output] + public ITaskItem [] NativeLibraries { get; set; } + + [Output] + public ITaskItem [] ManifestDocuments { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GetImportedLibraries Task"); + Log.LogDebugMessage (" TargetDirectory: {0}", TargetDirectory); + if (!Directory.Exists (TargetDirectory)) { + Log.LogDebugMessage ("Target directory was not found"); + return true; + } + // there could be ./AndroidManifest.xml and bin/AndroidManifest.xml, which will be the same. So, ignore "bin" ones. + ManifestDocuments = Directory.GetFiles (TargetDirectory, "AndroidManifest.xml", SearchOption.AllDirectories).Where (f => !Path.GetDirectoryName (f).EndsWith ("bin")).Select (p => new TaskItem (p)).ToArray (); + NativeLibraries = Directory.GetFiles (TargetDirectory, "*.so", SearchOption.AllDirectories) + .Where (p => MonoAndroidHelper.GetNativeLibraryAbi (p) != null) + .Select (p => new TaskItem (p)).ToArray (); + Jars = Directory.GetFiles (TargetDirectory, "*.jar", SearchOption.AllDirectories).Select (p => new TaskItem (p)).ToArray (); + Log.LogDebugTaskItems (" NativeLibraries: ", NativeLibraries); + Log.LogDebugTaskItems (" Jars: ", Jars); + Log.LogDebugTaskItems (" ManifestDocuments: ", ManifestDocuments); + + if (!string.IsNullOrEmpty (CacheFile)) { + var document = new XDocument ( + new XDeclaration ("1.0", "UTF-8", null), + new XElement ("Paths", + new XElement ("ManifestDocuments", ManifestDocuments.Select(e => new XElement ("ManifestDocument", e.ItemSpec))), + new XElement ("NativeLibraries", NativeLibraries.Select(e => new XElement ("NativeLibrary", e.ItemSpec))), + new XElement ("Jars", Jars.Select(e => new XElement ("Jar", e.ItemSpec))) + )); + document.Save (CacheFile); + } + + return true; + } + } + + +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs new file mode 100644 index 00000000000..37a5287ea29 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetJavaPlatformJar.cs @@ -0,0 +1,130 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +using Xamarin.Android.Tools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetJavaPlatformJar : Task + { + private XNamespace androidNs = "http://schemas.android.com/apk/res/android"; + + [Required] + public string AndroidSdkDirectory { get; set; } + + [Required] + public string AndroidSdkPlatform { get; set; } + + public string AndroidManifest { get; set; } + + [Output] + public string JavaPlatformJarPath { get; set; } + + [Output] + public string TargetSdkVersion { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GetJavaPlatformJar Task"); + Log.LogDebugMessage (" AndroidSdkDirectory: {0}", AndroidSdkDirectory); + Log.LogDebugMessage (" AndroidSdkPlatform: {0}", AndroidSdkPlatform); + Log.LogDebugMessage (" AndroidManifest: {0}", AndroidManifest); + + var platform = AndroidSdkPlatform; + + XAttribute target_sdk = null; + + // Look for targetSdkVersion in the user's AndroidManifest.xml + if (!string.IsNullOrWhiteSpace (AndroidManifest)) { + if (!File.Exists (AndroidManifest)) { + Log.LogError ("Specified AndroidManifest.xml file does not exist: {0}.", AndroidManifest); + return false; + } + + try { + var doc = XDocument.Load (AndroidManifest); + var manifest = doc.Root; + + if (manifest != null) { + var uses_sdk = manifest.Element ("uses-sdk"); + + if (uses_sdk != null) { + target_sdk = uses_sdk.Attribute (androidNs + "targetSdkVersion"); + + if (target_sdk != null && !string.IsNullOrWhiteSpace (target_sdk.Value)) + platform = target_sdk.Value; + } + } + } catch (Exception ex) { + // If they're manifest is bad, let's error them out + Log.LogErrorFromException (ex, true); + return false; + } + } + + platform = GetTargetSdkVersion (platform, target_sdk); + JavaPlatformJarPath = Path.Combine (AndroidSdkDirectory, "platforms", "android-" + MonoAndroidHelper.GetPlatformApiLevelName (platform), "android.jar"); + + if (!File.Exists (JavaPlatformJarPath)) { + Log.LogError ("Could not find android.jar for API Level {0}. " + + "This means the Android SDK platform for API Level {0} is not installed. " + + "Either install it in the Android SDK Manager (Tools > Open Android SDK Manager...), " + + "or change your Xamarin.Android project to target an API version that is installed. " + + "({1} missing.)", + platform, JavaPlatformJarPath); + return false; + } + + TargetSdkVersion = platform; + + Log.LogDebugMessage (" [Output] JavaPlatformJarPath: {0}", JavaPlatformJarPath); + Log.LogDebugMessage (" [Output] TargetSdkVersion: {0}", TargetSdkVersion); + + return true; + } + + string GetTargetSdkVersion (string target, XAttribute target_sdk) + { + string targetFrameworkVersion = MonoAndroidHelper.GetPlatformApiLevel (AndroidSdkPlatform); + string targetSdkVersion = MonoAndroidHelper.GetPlatformApiLevel (target); + + int frameworkSdk, targetSdk; + if (int.TryParse (targetFrameworkVersion, out frameworkSdk) && + int.TryParse (targetSdkVersion, out targetSdk) && + targetSdk < frameworkSdk) { + int lineNumber = 0; + int columnNumber = 0; + var lineInfo = target_sdk as IXmlLineInfo; + if (lineInfo != null && lineInfo.HasLineInfo ()) { + lineNumber = lineInfo.LineNumber; + columnNumber = lineInfo.LinePosition; + } + Log.LogWarning ( + subcategory: string.Empty, + warningCode: "XA4211", + helpKeyword: string.Empty, + file: AndroidManifest, + lineNumber: lineNumber, + columnNumber: columnNumber, + endLineNumber: 0, + endColumnNumber: 0, + message: "AndroidManifest.xml //uses-sdk/@android:targetSdkVersion '{0}' is less than $(TargetFrameworkVersion) '{1}'. Using API-{2} for ACW compilation.", + messageArgs: new[]{ + targetSdkVersion, + MonoDroidSdk.GetFrameworkVersionForApiLevel (targetFrameworkVersion), + MonoAndroidHelper.GetPlatformApiLevelName (targetFrameworkVersion), + } + ); + return targetFrameworkVersion; + } + return target; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/GetMonoPlatformJar.cs b/src/Xamarin.Android.Build.Tasks/Tasks/GetMonoPlatformJar.cs new file mode 100644 index 00000000000..9686d0d4198 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/GetMonoPlatformJar.cs @@ -0,0 +1,43 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class GetMonoPlatformJar : Task + { + [Required] + public string TargetFrameworkDirectory { get; set; } + + [Output] + public string MonoPlatformJarPath { get; set; } + + [Output] + public string MonoPlatformDexPath { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("GetMonoPlatformJar Task"); + Log.LogDebugMessage (" TargetFrameworkDirectory: {0}", TargetFrameworkDirectory); + + var directories = TargetFrameworkDirectory.Split (new char[] { ';', ','} ,StringSplitOptions.RemoveEmptyEntries); + + foreach (var dir in directories) + if (File.Exists (Path.Combine (dir, "mono.android.jar"))) { + MonoPlatformJarPath = Path.Combine (dir, "mono.android.jar"); + MonoPlatformDexPath = Path.ChangeExtension (MonoPlatformJarPath, ".dex"); + Log.LogMessage (MessageImportance.Low, " MonoPlatformJarPath: {0}", MonoPlatformJarPath); + Log.LogMessage (MessageImportance.Low, " MonoPlatformDexPath: {0}", MonoPlatformDexPath); + + return true; + } + + Log.LogError ("Could not find mono.android.jar"); + + return false; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ImportJavaDoc.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ImportJavaDoc.cs new file mode 100644 index 00000000000..6ad2714334b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ImportJavaDoc.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class ImportJavaDoc : ToolTask + { + public string [] JavaDocs { get; set; } + + public string [] References { get; set; } + + public string [] Transforms { get; set; } + + [Required] + public string TargetAssembly { get; set; } + + [Required] + public string OutputDocDirectory { get; set; } + + protected override string ToolName { + get { return "javadoc-to-mdoc"; } + } + + protected override string GenerateFullPathToTool () + { + return MonoDroidSdk.JavaDocToMDocExe; + } + + protected override string GenerateCommandLineCommands () + { + if (!Directory.Exists (OutputDocDirectory)) + Directory.CreateDirectory (OutputDocDirectory); + + var cmd = new CommandLineBuilder (); + cmd.AppendSwitch (Path.GetFullPath (TargetAssembly)); + //foreach (var r in References) + // cmd.AppendSwitch (r); + cmd.AppendSwitch ("-v=2"); + cmd.AppendSwitchIfNotNull ("--out=", OutputDocDirectory); + foreach (var j in JavaDocs) + cmd.AppendSwitchIfNotNull ("--doc-dir=", Path.GetDirectoryName (j)); + foreach (var t in Transforms) { + if (t.EndsWith ("Metadata.xml", StringComparison.InvariantCultureIgnoreCase)) + cmd.AppendSwitchIfNotNull ("--metadata=", t); + else + cmd.AppendSwitchIfNotNull ("--enum-map=", t); + } + return cmd.ToString (); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Jack.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Jack.cs new file mode 100644 index 00000000000..81b7ee9b2b3 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Jack.cs @@ -0,0 +1,136 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Linq; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Text; +using System.Collections.Generic; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class Jack : JavaCompileToolTask + { + [Required] + public string JackJarPath { get; set; } + + [Required] + public string JavaPlatformJackPath { get; set; } + + [Required] + public string [] InputJackFiles { get; set; } + + [Required] + public string OutputDexDirectory { get; set; } + + public bool EnableProguard { get; set; } + + public bool EnableMultiDex { get; set; } + + // not to run it, but to get some config file path. + public string ProguardJarPath { get; set; } + + public string AcwMapFile { get; set; } + public string ProguardGeneratedApplicationConfiguration { get; set; } + + public string ProguardGeneratedReferenceConfiguration { get; set; } + + public string ProguardCommonXamarinConfiguration { get; set; } + + public string ProguardConfigurationFiles { get; set; } + + public ITaskItem[] CustomMainDexListFiles { get; set; } + + protected override string ToolName { + get { return OS.IsWindows ? "java.exe" : "java"; } + } + + public override void OnLogStarted () + { + Log.LogDebugMessage (" JackJarPath:", JackJarPath); + Log.LogDebugMessage (" JavaPlatformJackPath:", JavaPlatformJackPath); + Log.LogDebugTaskItems (" InputJackFiles:", InputJackFiles); + Log.LogDebugMessage (" OutputDexDirectory:", OutputDexDirectory); + Log.LogDebugMessage (" AcwMapFile: {0}", AcwMapFile); + Log.LogDebugMessage (" ProguardGeneratedApplicationConfiguration: {0}", ProguardGeneratedApplicationConfiguration); + Log.LogDebugTaskItems (" ProguardGeneratedReferenceConfiguration:", ProguardGeneratedReferenceConfiguration); + Log.LogDebugTaskItems (" ProguardCommonXamarinConfiguration:", ProguardCommonXamarinConfiguration); + Log.LogDebugTaskItems (" ProguardConfigurationFiles:", ProguardConfigurationFiles); + Log.LogDebugTaskItems (" CustomMainDexListFiles:", CustomMainDexListFiles); + } + + public override bool Execute () + { + if (!Directory.Exists (OutputDexDirectory)) + Directory.CreateDirectory (OutputDexDirectory); + return base.Execute (); + } + + protected override string GenerateCommandLineCommands () + { + if (CustomMainDexListFiles != null && CustomMainDexListFiles.Any ()) + // FIXME: add error code. + Log.LogWarning ("Jack compiler does not accept custom main dex list file. You would have to either disable Jack or remove MultiDexMainDexList items."); + + // Running command: C:\Program Files (x86)\Java\jdk1.6.0_20\bin\java.exe + // "-jar" "C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\jack.jar" + // "--output-dex" "bin" + // "-classpath" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\android\bin\mono.android.jar" + // "@C:\Users\Jonathan\AppData\Local\Temp\tmp79c4ac38.tmp" + + //var android_dir = MonoDroid.MonoDroidSdk.GetAndroidProfileDirectory (TargetFrameworkDirectory); + + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitchIfNotNull ("-jar ", JackJarPath); + + //cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8"); + + cmd.AppendSwitchIfNotNull ("--output-dex ", OutputDexDirectory); + + cmd.AppendSwitchIfNotNull ("-cp ", JavaPlatformJackPath); + foreach (var jack in InputJackFiles) + cmd.AppendSwitchIfNotNull ("--import ", jack); + //cmd.AppendSwitchIfNotNull ("-bootclasspath ", JavaPlatformJarPath); + //cmd.AppendSwitchIfNotNull ("-encoding ", "UTF-8"); + cmd.AppendFileNameIfNotNull (string.Format ("@{0}", TemporarySourceListFile)); + + if (EnableMultiDex) + cmd.AppendSwitchIfNotNull ("--multi-dex ", "NATIVE"); + // Proguard settings + if (EnableProguard) { + var acwLines = File.ReadAllLines (AcwMapFile); + using (var appcfg = File.CreateText (ProguardGeneratedApplicationConfiguration)) + for (int i = 0; i + 3 < acwLines.Length; i += 4) + try { + var java = acwLines [i + 3].Substring (acwLines [i + 3].IndexOf (';') + 1); + appcfg.WriteLine ("-keep class " + java + " { *; }"); + } catch { + // skip invalid lines + } + var configs = ProguardConfigurationFiles + .Replace ("{sdk.dir}", Path.GetDirectoryName (Path.GetDirectoryName (ProguardJarPath)) + Path.DirectorySeparatorChar) + .Replace ("{intermediate.common.xamarin}", ProguardCommonXamarinConfiguration) + .Replace ("{intermediate.references}", ProguardGeneratedReferenceConfiguration) + .Replace ("{intermediate.application}", ProguardGeneratedApplicationConfiguration) + .Replace ("{project}", string.Empty) // current directory anyways. + .Split (';') + .Select (s => s.Trim ()) + .Where (s => !string.IsNullOrWhiteSpace (s)); + + foreach (var file in configs) { + if (File.Exists (file)) + cmd.AppendSwitchIfNotNull ("--config-proguard ", file); + else + Log.LogWarning ("Proguard configuration file '{0}' was not found.", file); + } + } + + return cmd.ToString (); + } + } + +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/JarToXml.cs b/src/Xamarin.Android.Build.Tasks/Tasks/JarToXml.cs new file mode 100644 index 00000000000..495743d29a8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/JarToXml.cs @@ -0,0 +1,194 @@ +// Copyright (C) 2012 Xamarin, Inc. All rights reserved. + +using System; +using System.Linq; +using System.IO; +using System.Reflection; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class JarToXml : ToolTask + { + [Required] + public string AndroidSdkDirectory { get; set; } + + [Required] + public string MonoAndroidToolsDirectory { get; set; } + + [Required] + public string JavaSdkDirectory { get; set; } + + [Required] + public string AndroidApiLevel { get; set; } + + [Required] + public string OutputFile { get; set; } + + [Required] + public ITaskItem[] SourceJars { get; set; } + + public ITaskItem[] ReferenceJars { get; set; } + public string DroidDocPaths { get; set; } + public string JavaDocPaths { get; set; } + public string Java7DocPaths { get; set; } + public string Java8DocPaths { get; set; } + public ITaskItem[] JavaDocs { get; set; } + + public ITaskItem[] LibraryProjectJars { get; set; } + + public string JavaOptions { get; set; } + + public string JavaMaximumHeapSize { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("JarToXml Task"); + Log.LogDebugMessage (" JavaOptions: {0}", JavaOptions); + Log.LogDebugMessage (" JavaMaximumHeapSize: {0}", JavaMaximumHeapSize); + Log.LogDebugMessage (" AndroidSdkDirectory: {0}", AndroidSdkDirectory); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugMessage (" MonoAndroidToolsDirectory: {0}", MonoAndroidToolsDirectory); + Log.LogDebugMessage (" JavaSdkDirectory: {0}", JavaSdkDirectory); + Log.LogDebugMessage (" OutputFile: {0}", OutputFile); + Log.LogDebugMessage (" DroidDocPaths: {0}", DroidDocPaths); + Log.LogDebugMessage (" JavaDocPaths: {0}", JavaDocPaths); + Log.LogDebugMessage (" Java7DocPaths: {0}", Java7DocPaths); + Log.LogDebugMessage (" Java8DocPaths: {0}", Java8DocPaths); + Log.LogDebugTaskItems (" JavaDocs: {0}", JavaDocs); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugTaskItems (" LibraryProjectJars:", LibraryProjectJars); + Log.LogDebugTaskItems (" SourceJars:", SourceJars); + Log.LogDebugTaskItems (" ReferenceJars:", ReferenceJars); + + if (SourceJars == null || SourceJars.Count () == 0) { + Log.LogError ("At least one Java library is required for binding, this must be either 'EmbeddedJar', 'InputJar' (for jar), 'LibraryProjectZip' (for aar or zip) or 'LibraryProjectProperties' (project.properties) build action."); + return false; + } + + // Ensure that the user has the platform they are targeting installed + var jarpath = Path.Combine (AndroidSdkDirectory, "platforms", "android-" + MonoAndroidHelper.GetPlatformApiLevelName (AndroidApiLevel), "android.jar"); + + if (!File.Exists (jarpath)) { + Log.LogError ("Could not find android.jar for API Level {0}. This means the Android SDK platform for API Level {0} is not installed. Either install it in the Android SDK Manager, or change your Android Bindings project to target an API version that is installed. ({1} missing.)", AndroidApiLevel, jarpath); + return false; + } + + // Ensure that all requested jars exist + foreach (var jar in SourceJars) + if (!File.Exists (jar.ItemSpec)) + Log.LogError ("Specified source jar not found: {0}", jar.ItemSpec); + + if (ReferenceJars != null) + foreach (var jar in ReferenceJars) + if (!File.Exists (jar.ItemSpec)) + Log.LogError ("Specified reference jar not found: {0}", jar.ItemSpec); + + if (Log.HasLoggedErrors) + return false; + + // Ensure our output directory exists + Directory.CreateDirectory (Path.GetDirectoryName (OutputFile)); + + return base.Execute (); + } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + + // Add the JavaOptions if they are not null + // These could be any of the additional options + if (!string.IsNullOrEmpty (JavaOptions)) { + cmd.AppendSwitch (JavaOptions); + } + + // Add the specific -XmxN to override the default heap size for the JVM + // N can be in the form of Nm or NGB (e.g 100m or 1GB ) + cmd.AppendSwitchIfNotNull("-Xmx", JavaMaximumHeapSize); + + // See https://bugzilla.xamarin.com/show_bug.cgi?id=21096 + cmd.AppendSwitch ("-XX:-UseSplitVerifier"); + + // Arguments sent to java.exe + cmd.AppendSwitchIfNotNull ("-jar ", Path.Combine (MonoAndroidToolsDirectory, "jar2xml.jar")); + + foreach (var jar in SourceJars) + cmd.AppendSwitchIfNotNull ("--jar=", Path.GetFullPath (jar.ItemSpec)); + + var libraryProjectJars = MonoAndroidHelper.ExpandFiles (LibraryProjectJars); + + foreach (var jar in libraryProjectJars) { + if (MonoAndroidHelper.IsEmbeddedReferenceJar (jar)) + cmd.AppendSwitchIfNotNull ("--ref=", Path.GetFullPath (jar)); + else + cmd.AppendSwitchIfNotNull ("--jar=", Path.GetFullPath (jar)); + } + + // Arguments sent to jar2xml + var jarpath = Path.Combine (AndroidSdkDirectory, "platforms", "android-" + MonoAndroidHelper.GetPlatformApiLevelName (AndroidApiLevel), "android.jar"); + cmd.AppendSwitchIfNotNull ("--ref=", Path.GetFullPath (jarpath)); + + cmd.AppendSwitchIfNotNull ("--out=", Path.GetFullPath (OutputFile)); + + if (ReferenceJars != null) + foreach (var jar in ReferenceJars) + cmd.AppendSwitchIfNotNull ("--ref=", Path.GetFullPath (jar.ItemSpec)); + + if (DroidDocPaths != null) + foreach (var path in DroidDocPaths.Split (';')) + cmd.AppendSwitchIfNotNull ("--droiddocpath=", Path.GetFullPath (path)); + + if (JavaDocPaths != null) + foreach (var path in JavaDocPaths.Split (';')) + cmd.AppendSwitchIfNotNull ("--javadocpath=", Path.GetFullPath (path)); + + if (Java7DocPaths != null) + foreach (var path in Java7DocPaths.Split (';')) + cmd.AppendSwitchIfNotNull ("--java7docpath=", Path.GetFullPath (path)); + + if (Java8DocPaths != null) + foreach (var path in Java8DocPaths.Split (';')) + cmd.AppendSwitchIfNotNull ("--java8docpath=", Path.GetFullPath (path)); + + if (JavaDocs != null) + foreach (var doc in JavaDocs) { + var opt = GetJavadocOption (doc.ItemSpec); + if (opt != null) + cmd.AppendSwitchIfNotNull (opt, Path.GetFullPath (Path.GetDirectoryName (doc.ItemSpec))); + } + return cmd.ToString (); + } + + string GetJavadocOption (string file) + { + string rawHTML = File.ReadAllText (file); + if (rawHTML.Length < 500) + return null; + if (rawHTML.Substring (0, 500).IndexOf ("Generated by javadoc (build 1.6", StringComparison.Ordinal) > 0) + return "--javadocpath="; + if (rawHTML.Substring (0, 500).IndexOf ("Generated by javadoc (version 1.7", StringComparison.Ordinal) > 0) + return "--java7docpath="; + return "--droiddocpath="; + } + + protected override string ToolName { get { return "jar2xml"; } } + + public override string ToolExe { + get { return OS.IsWindows ? "java.exe" : "java"; } + set { base.ToolExe = value; } + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (JavaSdkDirectory, "bin", ToolExe); + } + + private string GetMsBuildDirectory () + { + return Path.GetDirectoryName (Assembly.GetExecutingAssembly ().Location); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/JavaCompileToolTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/JavaCompileToolTask.cs new file mode 100644 index 00000000000..8a51b9550fe --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/JavaCompileToolTask.cs @@ -0,0 +1,98 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Linq; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Text; +using System.Collections.Generic; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + + public abstract class JavaCompileToolTask : JavaToolTask + { + [Required] + public string StubSourceDirectory { get; set; } + + public ITaskItem[] JavaSourceFiles { get; set; } + + public ITaskItem[] Jars { get; set; } + + [Required] + public string TargetFrameworkDirectory { get; set; } + + protected override string ToolName { + get { return OS.IsWindows ? "javac.exe" : "javac"; } + } + + private bool IsRunningInsideVS { + get { + var vside = false; + return bool.TryParse(Environment.GetEnvironmentVariable("VSIDE"), out vside) && vside; + } + } + + internal string TemporarySourceListFile; + + public virtual void OnLogStarted () + { + } + + public override bool Execute () + { + Log.LogDebugMessage ("{0} Task", GetType ().Name); + Log.LogDebugMessage (" StubSourceDirectory: {0}", StubSourceDirectory); + Log.LogDebugMessage (" TargetFrameworkDirectory: {0}", TargetFrameworkDirectory); + Log.LogDebugTaskItems (" JavaSourceFiles:", JavaSourceFiles); + Log.LogDebugTaskItems (" Jars:", Jars); + OnLogStarted (); + + GenerateResponseFile (); + + var retval = base.Execute (); + + try { + File.Delete (TemporarySourceListFile); + } catch (Exception) { + // Ignore exception, a tiny temp file will get left on the user's system + } + + return retval; + } + + private void GenerateResponseFile () + { + TemporarySourceListFile = Path.GetTempFileName (); + + using (var sw = new StreamWriter (path:TemporarySourceListFile, append:false, + encoding:new UTF8Encoding (encoderShouldEmitUTF8Identifier:false))) { + // Include any user .java files + if (JavaSourceFiles != null) + foreach (var file in JavaSourceFiles.Where (p => Path.GetExtension (p.ItemSpec) == ".java")) + sw.WriteLine (string.Format ("\"{0}\"", file.ItemSpec.Replace (@"\", @"\\"))); + + foreach (var file in Directory.GetFiles (StubSourceDirectory, "*.java", SearchOption.AllDirectories)) { + // This makes sense. BAD sense. but sense. + // Problem: + // A perfectly sensible path like "E:\tmp\a.java" generates a + // javac error that "E: mp.java" can't be found. + // Cause: + // javac uses java.io.StreamTokenizer to parse @response files, and + // the docs for StreamTokenizer.quoteChar(int) [0] say: + // The usual escape sequences such as "\n" and "\t" are recognized + // and converted to single characters as the string is parsed. + // i.e. '\' is an escape character! + // Solution: + // Since '\' is an escape character, we need to escape it. + // [0] http://download.oracle.com/javase/1.4.2/docs/api/java/io/StreamTokenizer.html#quoteChar(int) + sw.WriteLine (string.Format ("\"{0}\"", + file.Replace (@"\", @"\\").Normalize (NormalizationForm.FormC))); + } + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/JavaToolTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/JavaToolTask.cs new file mode 100644 index 00000000000..ccae7b07ed5 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/JavaToolTask.cs @@ -0,0 +1,178 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.IO; +using System.Text; +using System.Text.RegularExpressions; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + + +namespace Xamarin.Android.Tasks +{ + public abstract class JavaToolTask : ToolTask + { + /* + Example Javac output for errors. Regex Matches on the first line, we then need to + process the second line to get the column number so the IDE can correctly + mark where the error is. + + TestMe.java:1: error: class, interface, or enum expected + public classo TestMe { } + ^ + TestMe2.java:1: error: ';' expected + public class TestMe2 {public vod Test ()} + ^ + 2 errors + */ + const string CodeErrorRegExString = @"(?.+\.java):(?\d+):(?.+)"; + /* + + Sample OutOfMemoryError raised by java. RegEx matches the java.lang.* line of the error + and splits it into an exception and an error + + UNEXPECTED TOP-LEVEL ERROR: + java.lang.OutOfMemoryError: GC overhead limit exceeded + at java.util.Arrays.copyOf(Arrays.java:2219) + at java.util.ArrayList.grow(ArrayList.java:242) + at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:216) + at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:208) + at java.util.ArrayList.add(ArrayList.java:440) + at com.android.dx.ssa.Dominators$DfsWalker.visitBlock(Dominators.java:263) + at com.android.dx.ssa.SsaMethod.forEachBlockDepthFirst(SsaMethod.java:783) + at com.android.dx.ssa.Dominators.run(Dominators.java:185) + at com.android.dx.ssa.Dominators.make(Dominators.java:90) + at com.android.dx.ssa.DomFront.run(DomFront.java:86) + at com.android.dx.ssa.SsaConverter.placePhiFunctions(SsaConverter.java:297) + at com.android.dx.ssa.SsaConverter.convertToSsaMethod(SsaConverter.java:51) + at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:98) + at com.android.dx.ssa.Optimizer.optimize(Optimizer.java:72) + at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:297) + at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:137) + at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:93) + at com.android.dx.command.dexer.Main.processClass(Main.java:729) + at com.android.dx.command.dexer.Main.processFileBytes(Main.java:673) + at com.android.dx.command.dexer.Main.access$300(Main.java:83) + at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:602) + at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284) + at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166) + at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144) + at com.android.dx.command.dexer.Main.processOne(Main.java:632) + at com.android.dx.command.dexer.Main.processAllFiles(Main.java:505) + at com.android.dx.command.dexer.Main.runMultiDex(Main.java:334) + at com.android.dx.command.dexer.Main.run(Main.java:244) + at com.android.dx.command.dexer.Main.main(Main.java:215) + at com.android.dx.command.Main.main(Main.java:106) + */ + const string ExceptionRegExString = @"(?java.lang.+):(?.+)"; + Regex codeErrorRegEx = new Regex (CodeErrorRegExString, RegexOptions.Compiled); + Regex exceptionRegEx = new Regex (ExceptionRegExString, RegexOptions.Compiled); + bool foundError = false; + List errorLines = new List (); + StringBuilder errorText = new StringBuilder (); + string file; + int line, column; + + protected override string ToolName { + get { return OS.IsWindows ? "java.exe" : "java"; } + } + + protected override bool HandleTaskExecutionErrors () + { + if (foundError) { + errorText.Clear (); + foreach (var line in errorLines) { + if (!ProcessOutput (line)) + break; + } + if (foundError && errorText.Length > 0) { + Log.LogError (ToolName, null, null, file, line - 1, column + 1, 0, 0, errorText.ToString ()); + } + return !Log.HasLoggedErrors; + } + return base.HandleTaskExecutionErrors (); + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + void LogFromException (string exception, string error) { + switch (exception) { + case "java.lang.OutOfMemoryError": + Log.LogCodedError ("XA5213", + "java.lang.OutOfMemoryError. Consider increasing the value of $(JavaMaximumHeapSize). Java ran out of memory while executing '{0} {1}'", + ToolName, GenerateCommandLineCommands ()); + break; + default: + Log.LogError ("{0} : {1}", exception, error); + break; + } + } + + bool ProcessOutput (string singleLine) + { + var match = codeErrorRegEx.Match (singleLine); + var exceptionMatch = exceptionRegEx.Match (singleLine); + + if (match.Success) { + if (!string.IsNullOrEmpty (file)) { + Log.LogError (ToolName, null, null, file, line - 1, column + 1, 0, 0, errorText.ToString ()); + errorText.Clear (); + } + file = match.Groups ["file"].Value; + line = int.Parse (match.Groups ["line"].Value) + 1; + var error = match.Groups ["error"].Value; + column = 0; + + errorText.AppendLine (error); + return true; + } else if (exceptionMatch.Success) { + var error = exceptionMatch.Groups ["error"].Value; + var exception = exceptionMatch.Groups ["exception"].Value; + line = 1; + file = ""; + column = 0; + LogFromException (exception, error); + return false; + } else if (foundError) { + if (singleLine.Trim () == "^") { + column = singleLine.IndexOf ("^"); + return true; + } + + if (singleLine.StartsWith ("Note:") || singleLine.Trim ().EndsWith ("errors")) { + // See if we have one last error to print out + Log.LogError (null, null, null, file, line - 1, column + 1, 0, 0, errorText.ToString ()); + errorText.Clear (); + foundError = false; + return true; + } + errorText.AppendLine (singleLine); + } + return true; + } + + protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) + { + var match = codeErrorRegEx.Match (singleLine); + var exceptionMatch = exceptionRegEx.Match (singleLine); + + if (match.Success || exceptionMatch.Success) { + Log.LogMessage (MessageImportance.High, singleLine); + foundError = true; + errorLines.Add (singleLine); + return; + } else if (foundError) { + Log.LogMessage (MessageImportance.High, singleLine); + errorLines.Add (singleLine); + return; + } + base.LogEventsFromTextOutput (singleLine, messageImportance); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs new file mode 100644 index 00000000000..08c52df6fde --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs @@ -0,0 +1,65 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Linq; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Text; +using System.Collections.Generic; + +namespace Xamarin.Android.Tasks +{ + public class Javac : JavaCompileToolTask + { + [Required] + public string ClassesOutputDirectory { get; set; } + + public string JavaPlatformJarPath { get; set; } + + public string JavacTargetVersion { get; set; } + public string JavacSourceVersion { get; set; } + + public override void OnLogStarted () + { + Log.LogDebugMessage (" ClassesOutputDirectory: {0}", ClassesOutputDirectory); + Log.LogDebugMessage (" JavacTargetVersion: {0}", JavacTargetVersion); + Log.LogDebugMessage (" JavacSourceVersion: {0}", JavacSourceVersion); + } + + public override bool Execute () + { + if (!Directory.Exists (ClassesOutputDirectory)) + Directory.CreateDirectory (ClassesOutputDirectory); + return base.Execute (); + } + + protected override string GenerateCommandLineCommands () + { + // Running command: C:\Program Files (x86)\Java\jdk1.6.0_20\bin\javac.exe + // "-J-Dfile.encoding=UTF8" + // "-d" "bin\classes" + // "-classpath" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\android\bin\mono.android.jar" + // "-bootclasspath" "C:\Program Files (x86)\Android\android-sdk-windows\platforms\android-8\android.jar" + // "-encoding" "UTF-8" + // "@C:\Users\Jonathan\AppData\Local\Temp\tmp79c4ac38.tmp" + + //var android_dir = MonoDroid.MonoDroidSdk.GetAndroidProfileDirectory (TargetFrameworkDirectory); + + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8"); + + cmd.AppendSwitchIfNotNull ("-d ", ClassesOutputDirectory); + + cmd.AppendSwitchIfNotNull ("-classpath ", Jars == null || !Jars.Any () ? null : string.Join (Path.PathSeparator.ToString (), Jars.Select (i => i.ItemSpec))); + cmd.AppendSwitchIfNotNull ("-bootclasspath ", JavaPlatformJarPath); + cmd.AppendSwitchIfNotNull ("-encoding ", "UTF-8"); + cmd.AppendFileNameIfNotNull (string.Format ("@{0}", TemporarySourceListFile)); + cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion); + cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion); + + return cmd.ToString (); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Jill.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Jill.cs new file mode 100644 index 00000000000..99d16a7def8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Jill.cs @@ -0,0 +1,88 @@ +// Copyright (C) 2011 Xamarin, Inc. All rights reserved. + +using System; +using System.Linq; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Text; +using System.Collections.Generic; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + + public class Jill : JavaToolTask + { + [Required] + public string JillJarPath { get; set; } + + [Required] + public string OutputJackDirectory { get; set; } + + public string [] Jars { get; set; } + + [Output] + public string [] OutputJackFiles { get; set; } + + [Output] + public string OutputPlatformJackPath { get; set; } + + protected override string ToolName { + get { return OS.IsWindows ? "java.exe" : "java"; } + } + + public override bool Execute () + { + Log.LogDebugMessage ("Jill Task"); + Log.LogDebugMessage (" JillJarPath: ", JillJarPath); + Log.LogDebugMessage (" OutputJackDirectory: ", OutputJackDirectory); + Log.LogDebugTaskItems (" Jars: ", Jars); + + if (!Directory.Exists (OutputJackDirectory)) + Directory.CreateDirectory (OutputJackDirectory); + + bool retval = true; + var outputJackFiles = new List (); + var md5 = System.Security.Cryptography.MD5.Create (); + foreach (var jar in Jars) { + context_jar = jar; + context_jack = Path.Combine (OutputJackDirectory, BitConverter.ToString (md5.ComputeHash (Encoding.UTF8.GetBytes (jar))) + Path.GetFileNameWithoutExtension (context_jar) + ".jack"); + retval &= base.Execute (); + outputJackFiles.Add (context_jack); + } + OutputJackFiles = outputJackFiles.ToArray (); + + Log.LogDebugTaskItems (" OutputJackFiles: ", OutputJackFiles); + return retval; + } + + string context_jar; + string context_jack; + + protected override string GenerateCommandLineCommands () + { + // Running command: C:\Program Files (x86)\Java\jdk1.6.0_20\bin\java.exe + // "-jar" "C:\Program Files (x86)\Android\android-sdk-windows\platform-tools\jill.jar" + // "--output-dex" "bin\classes" + // "-classpath" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\android\bin\mono.android.jar" + // "@C:\Users\Jonathan\AppData\Local\Temp\tmp79c4ac38.tmp" + + //var android_dir = MonoDroid.MonoDroidSdk.GetAndroidProfileDirectory (TargetFrameworkDirectory); + + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitchIfNotNull ("-jar ", JillJarPath); + + //cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8"); + + cmd.AppendSwitchIfNotNull ("--output ", context_jack); + cmd.AppendFileNameIfNotNull (context_jar); + cmd.AppendSwitch ("--verbose"); + + return cmd.ToString (); + } + } + +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/KeyTool.cs b/src/Xamarin.Android.Build.Tasks/Tasks/KeyTool.cs new file mode 100644 index 00000000000..4d2c8551e4f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/KeyTool.cs @@ -0,0 +1,72 @@ +using System; +using System.IO; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class KeyTool : AndroidToolTask + { + [Required] + public string KeyStore { get; set; } + + [Required] + public string KeyAlias { get; set; } + + [Required] + public string KeyPass { get; set; } + + [Required] + public string StorePass { get; set; } + + [Required] + public string Command { get; set; } + + public bool Verbose { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("KeyTool : {0}", Command); + Log.LogDebugMessage (" {0}",KeyStore); + // Ensure the path where are going to write the key exists + var store_dir = Path.GetDirectoryName (KeyStore); + + if (!Directory.Exists (store_dir)) + Directory.CreateDirectory (store_dir); + + return base.Execute (); + } + + protected virtual CommandLineBuilder CreateCommandLine() + { + var cmd = new CommandLineBuilder (); + + cmd.AppendSwitch (Command); + if (Verbose) { + cmd.AppendSwitch ("-V"); + } + cmd.AppendSwitchIfNotNull ("-alias ", KeyAlias); + cmd.AppendSwitchIfNotNull ("-storepass ", StorePass); + cmd.AppendSwitchIfNotNull ("-keypass ", KeyPass); + cmd.AppendSwitchIfNotNull ("-keystore ", KeyStore); + return cmd; + } + + protected override string GenerateCommandLineCommands () + { + var cmd = CreateCommandLine (); + return cmd.ToString (); + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + protected override string ToolName + { + get { return IsWindows ? "keytool.exe" : "keytool"; } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssemblies.cs new file mode 100644 index 00000000000..e64a17a6521 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LinkAssemblies.cs @@ -0,0 +1,165 @@ +// Copyright (C) 2011, Xamarin Inc. +// Copyright (C) 2010, Novell Inc. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Mono.Cecil; +using System.IO; +using MonoDroid.Tuner; +using Mono.Linker; + +using Java.Interop.Tools.Cecil; +using Java.Interop.Tools.Diagnostics; + +namespace Xamarin.Android.Tasks +{ + public class LinkAssemblies : Task + { + [Required] + public string UseSharedRuntime { get; set; } + + [Required] + public string MainAssembly { get; set; } + + [Required] + public string OutputDirectory { get; set; } + + [Required] + public ITaskItem[] ResolvedAssemblies { get; set; } + + [Required] + public ITaskItem[] LinkDescriptions { get; set; } + + public string I18nAssemblies { get; set; } + public string LinkMode { get; set; } + public string LinkSkip { get; set; } + + public bool EnableProguard { get; set; } + public string ProguardConfiguration { get; set; } + public bool DumpDependencies { get; set; } + + public string OptionalDestinationDirectory { get; set; } + public string LinkOnlyNewerThan { get; set; } + + IEnumerable GetRetainAssemblies (DirectoryAssemblyResolver res) + { + List retainList = null; + foreach (var ass in ResolvedAssemblies) { + var filename = Path.GetFileName (ass.ItemSpec); + if (!MonoAndroidHelper.IsForceRetainedAssembly (filename)) + continue; + if (retainList == null) + retainList = new List (); + retainList.Add (res.GetAssembly (ass.ItemSpec)); + } + return retainList; + } + + public override bool Execute () + { + Log.LogDebugMessage ("LinkAssemblies Task"); + Log.LogDebugMessage (" UseSharedRuntime: {0}", UseSharedRuntime); + Log.LogDebugMessage (" MainAssembly: {0}", MainAssembly); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" OptionalDestinationDirectory: {0}", OptionalDestinationDirectory); + Log.LogDebugMessage (" I18nAssemblies: {0}", I18nAssemblies); + Log.LogDebugMessage (" LinkMode: {0}", LinkMode); + Log.LogDebugMessage (" LinkSkip: {0}", LinkSkip); + Log.LogDebugTaskItems (" LinkDescriptions:", LinkDescriptions); + Log.LogDebugTaskItems (" ResolvedAssemblies:", ResolvedAssemblies); + Log.LogDebugMessage (" EnableProguard: {0}", EnableProguard); + Log.LogDebugMessage (" ProguardConfiguration: {0}", ProguardConfiguration); + Log.LogDebugMessage (" DumpDependencies: {0}", DumpDependencies); + Log.LogDebugMessage (" LinkOnlyNewerThan: {0}", LinkOnlyNewerThan); + + var res = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols: false); + + // Put every assembly we'll need in the resolver + foreach (var ass in ResolvedAssemblies) { + res.Load (Path.GetFullPath (ass.ItemSpec)); + } + + var resolver = new AssemblyResolver (res.ToResolverCache ()); + + // Set up for linking + var options = new LinkerOptions (); + options.MainAssembly = res.GetAssembly (MainAssembly); + options.OutputDirectory = Path.GetFullPath (OutputDirectory); + options.LinkSdkOnly = string.Compare (LinkMode, "SdkOnly", true) == 0; + options.LinkNone = string.Compare (LinkMode, "None", true) == 0; + options.Resolver = resolver; + options.LinkDescriptions = LinkDescriptions.Select (item => Path.GetFullPath (item.ItemSpec)).ToArray (); + options.I18nAssemblies = Linker.ParseI18nAssemblies (I18nAssemblies); + if (!options.LinkSdkOnly) + options.RetainAssemblies = GetRetainAssemblies (res); + options.DumpDependencies = DumpDependencies; + + var skiplist = new List (); + + if (string.Compare (UseSharedRuntime, "true", true) == 0) + skiplist.AddRange (Profile.SharedRuntimeAssemblies.Where (a => a.EndsWith (".dll")).Select (a => Path.GetFileNameWithoutExtension (a))); + if (!string.IsNullOrWhiteSpace (LinkOnlyNewerThan) && File.Exists (LinkOnlyNewerThan)) { + var newerThan = File.GetLastWriteTime (LinkOnlyNewerThan); + var skipOldOnes = ResolvedAssemblies.Where (a => File.GetLastWriteTime (a.ItemSpec) < newerThan); + foreach (var old in skipOldOnes) + Log.LogMessage (MessageImportance.Low, " Skip linking unchanged file: " + old.ItemSpec); + skiplist = skipOldOnes.Select (a => Path.GetFileNameWithoutExtension (a.ItemSpec)).Concat (skiplist).ToList (); + } + + // Add LinkSkip options + if (!string.IsNullOrWhiteSpace (LinkSkip)) + foreach (var ass in LinkSkip.Split (',', ';')) + skiplist.Add (ass); + + options.SkippedAssemblies = skiplist; + + if (EnableProguard) + options.ProguardConfiguration = ProguardConfiguration; + + // Link! + try { + LinkContext link_context; + Linker.Process (options, out link_context); + + var copydst = OptionalDestinationDirectory ?? OutputDirectory; + + foreach (var ass in ResolvedAssemblies) { + var copysrc = ass.ItemSpec; + var filename = Path.GetFileName (ass.ItemSpec); + + if (options.LinkNone) { + if (skiplist.Any (s => Path.GetFileNameWithoutExtension (filename) == s)) { + // For skipped assemblies, skip if there is existing file in the destination. + // We cannot just copy the linker output from *current* run output, because + // it always renew the assemblies, in *different* binary values, whereas + // the dll in the OptionalDestinationDirectory must retain old and unchanged. + if (File.Exists (Path.Combine (copydst, filename))) + continue; + copysrc = ass.ItemSpec; + } else { + // Prefer fixup assemblies if exists, otherwise just copy the original. + copysrc = Path.Combine (OutputDirectory, filename); + copysrc = File.Exists (copysrc) ? copysrc : ass.ItemSpec; + } + } + else if (!MonoAndroidHelper.IsForceRetainedAssembly (filename)) + continue; + + MonoAndroidHelper.CopyIfChanged (copysrc, Path.Combine (copydst, filename)); + try { + MonoAndroidHelper.CopyIfChanged (ass.ItemSpec + ".mdb", Path.Combine (copydst, filename + ".mdb")); + } catch (Exception) { // skip it, mdb sometimes fails to read and it's optional + } + } + } catch (ResolutionException ex) { + Diagnostic.Error (2006, ex, "Reference to metadata item '{0}' (defined in '{1}') from '{1}' could not be resolved.", ex.Member, ex.Member.Module.Assembly, ex.Scope); + } + + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs new file mode 100644 index 00000000000..b4778495687 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Lint.cs @@ -0,0 +1,294 @@ +using System; +using Microsoft.Build.Framework; +using Xamarin.AndroidTools; +using Microsoft.Build.Utilities; +using System.Text.RegularExpressions; +using System.IO; +using System.Xml.Linq; +using System.Linq; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class Lint : ToolTask + { + // we need to check for lint based errors and warnings + // Sample Manifest Warnings note the ^ and ~ differences.... + // + //AndroidManifest.xml:19: Warning: tag appears after tag [ManifestOrder] + // + // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + //AndroidManifest.xml:4: Warning: Should explicitly set android:icon, there is no default [MissingApplicationIcon] + // + // ^ + //android/AndroidManifest.xml:4: Error: Class referenced in the manifest, mono.android.app.Application, was not found in the project or the libraries [MissingRegistered] + // + // ^ + //android/src/sdldroidtest/SDLSurface.java:43: Error: Call requires API level 21 (current min is 10): new android.view.SurfaceView [NewApi] + //super (p0, p1, p2, p3); + //~~~~~ + // + const string CodeErrorRegExString = @"(?.+\.*):(?\d+):(?.+)"; + // We also need to capture warning and errors which DONOT include file information + // + //Warning: The resource R.layout.main appears to be unused [UnusedResources] + //Warning: The resource R.string.app_name appears to be unused [UnusedResources] + //Warning: The resource R.string.hello appears to be unused [UnusedResources] + const string NoFileWarningOrErrorRegExString = @"(?Warning|Error):(?.+)"; + Regex codeErrorRegEx = new Regex (CodeErrorRegExString, RegexOptions.Compiled); + Regex noFileWarningOrErrorRegEx = new Regex(NoFileWarningOrErrorRegExString, RegexOptions.Compiled); + bool matched = false; + string file; + int line; + int column; + string text; + string type; + + [Required] + public string TargetDirectory { get; set; } + + [Required] + public string IntermediateOutputPath { get; set; } + + /// + /// Location of an xml config files used to + /// determine whether issues are enabled or disabled + /// for lint. Normally named lint.xml however it can + /// be any file using the AndroidLintConfig build action + /// + /// Sample config file + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// The location of the config file. + public ITaskItem[] ConfigFiles { get; set; } + + /// + /// Enable the specific list of issues. This checks all + /// the default issues plus the specifically enabled + /// issues. The list should be a comma-separated list of + /// issue id's or categories. + /// + /// The list of issues to enable. + public string EnabledIssues { get; set; } + + /// + /// Disable the list of categories or specific issue + /// id's. The list should be a comma-separated list of + /// issue id's or categories + /// + /// The issues to disable. + public string DisabledIssues { get; set; } + + /// + /// Only check the specific list of issues. This will + /// disable everything and re-enable the given list of + /// issues. The list should be a comma-separated list of + /// issue id's or categories. + /// + /// The list of issues to check. + public string CheckIssues { get; set; } + + /// + /// Add the given folders (or paths) as a resource + /// directories for the project. + /// + /// An array of ITaskItems containing the resource directories. + public ITaskItem[] ResourceDirectories {get; set;} + + /// + /// Add the given folders (or paths) as a source directories + /// for the project. + /// + /// An array of ITaskItems containing the source directories. + public ITaskItem[] SourceDirectories {get; set;} + + /// + /// Add the given folder (or path) as a class directories + /// for the project. + /// + /// An array of ITaskItems containing class directories. + public ITaskItem[] ClassDirectories {get; set;} + + /// + /// Add the given folders (or jar files, or paths) as + /// class directories for the project. + /// + /// An array of ITaskItems containing the class path jars. + public ITaskItem[] ClassPathJars {get; set;} + + /// + /// Add the given folders (or paths) as a + /// class libraries for the project. + /// + /// An array of ITaskItems containing the list of directories + public ITaskItem[] LibraryDirectories {get; set;} + + /// + /// Add the given jar files as a + /// class library for the project. + /// + /// An array of ITaskITems containing the list of .jar files + public ITaskItem[] LibraryJars {get; set;} + + protected override string ToolName { + get { return OS.IsWindows ? "lint.bat" : "lint"; } + } + + public Lint () + { + ResourceDirectories = new ITaskItem[0]; + SourceDirectories = new ITaskItem[0]; + ClassDirectories = new ITaskItem[0]; + ClassPathJars = new ITaskItem[0]; + LibraryDirectories = new ITaskItem[0]; + LibraryJars = new ITaskItem[0]; + } + + public override bool Execute () + { + // We need to hard code this test in because Lint will issue an Error + // if android:debuggable appears in the manifest. We actually need that + // in debug mode. It seems the android tools do some voodoo/magic to + // decide if its needed or not. + if (string.IsNullOrEmpty (DisabledIssues) || !DisabledIssues.Contains ("HardcodedDebugMode")) + DisabledIssues = "HardcodedDebugMode" + (!string.IsNullOrEmpty(DisabledIssues) ? ","+DisabledIssues : ""); + + // We need to hard code this test as disabled in because Lint will issue a warning + // for all the resources not used. Now because we don't have any Java code + // that means for EVERYTHING! Which will be a HUGE amount of warnings for a large project + if (string.IsNullOrEmpty (DisabledIssues) || !DisabledIssues.Contains ("UnusedResources")) + DisabledIssues = "UnusedResources" + (!string.IsNullOrEmpty(DisabledIssues) ? ","+DisabledIssues : ""); + + Log.LogDebugMessage ("Lint Task"); + Log.LogDebugMessage (" TargetDirectory: {0}", TargetDirectory); + Log.LogDebugMessage (" EnabledChecks: {0}", EnabledIssues); + Log.LogDebugMessage (" DisabledChecks: {0}", DisabledIssues); + Log.LogDebugMessage (" CheckIssues: {0}", CheckIssues); + Log.LogDebugTaskItems (" ConfigFiles:", ConfigFiles); + Log.LogDebugTaskItems (" ResourceDirectories:", ResourceDirectories); + Log.LogDebugTaskItems (" SourceDirectories:", SourceDirectories); + Log.LogDebugTaskItems (" ClassDirectories:", ClassDirectories); + Log.LogDebugTaskItems (" LibraryDirectories:", LibraryDirectories); + Log.LogDebugTaskItems (" LibraryJars:", LibraryJars); + + base.Execute (); + + return !Log.HasLoggedErrors; + } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + cmd.AppendSwitch ("--quiet"); + if (ConfigFiles != null && ConfigFiles.Any ()) { + var config = MergeConfigFiles (); + var configPath = Path.Combine (IntermediateOutputPath, "lint.xml"); + config.Save (configPath); + cmd.AppendSwitchIfNotNull ("--config ", configPath); + } + cmd.AppendSwitchIfNotNull ("--enable ", EnabledIssues); + cmd.AppendSwitchIfNotNull ("--disable ", DisabledIssues); + cmd.AppendSwitchIfNotNull ("--check ", CheckIssues); + foreach (var item in ResourceDirectories) { + cmd.AppendSwitchIfNotNull ("--resources ", item.ItemSpec); + } + foreach (var item in SourceDirectories) { + cmd.AppendSwitchIfNotNull ("--sources ", item.ItemSpec); + } + foreach (var item in ClassDirectories) { + cmd.AppendSwitchIfNotNull ("--classpath ", item.ItemSpec); + } + foreach (var item in ClassPathJars) { + cmd.AppendSwitchIfNotNull ("--classpath ", item.ItemSpec); + } + foreach (var item in LibraryDirectories) { + cmd.AppendSwitchIfNotNull ("--libraries ", item.ItemSpec); + } + foreach (var item in LibraryJars) { + cmd.AppendSwitchIfNotNull ("--libraries ", item.ItemSpec); + } + cmd.AppendFileNameIfNotNull (TargetDirectory); + return cmd.ToString(); + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + protected override void LogEventsFromTextOutput (string singleLine, MessageImportance messageImportance) + { + var match = codeErrorRegEx.Match (singleLine); + if (!match.Success) + match = noFileWarningOrErrorRegEx.Match (singleLine); + if (match.Success) { + if (matched) { + // we are already in a warning/error ... + // dont loose it + GenerateErrorOrWarning (); + } + matched = true; + file = match.Groups ["file"].Value; + if (!string.IsNullOrEmpty (file)) + file = Path.Combine (TargetDirectory, file); + line = 0; + int.TryParse (match.Groups ["line"].Value, out line); + text = match.Groups ["text"].Value.Trim (); + type = match.Groups ["type"].Value; + if (string.IsNullOrEmpty (type)) + type = text.Contains ("Error") ? "Error" : "Warning"; + column = 0; + } + if (matched) { + if (singleLine.Trim () == "^") { + column = singleLine.IndexOf ("^"); + GenerateErrorOrWarning (); + } + if (singleLine.Trim ().Contains ("~")) { + column = singleLine.IndexOf ("~"); + GenerateErrorOrWarning (); + } + } else + base.LogEventsFromTextOutput (singleLine, messageImportance); + } + + void GenerateErrorOrWarning () + { + matched = false; + if (type == "Warning") { + if (!string.IsNullOrEmpty (file)) + Log.LogWarning ("", "XA0102", "", file, line, column, 0, 0, text.Replace ("Warning:", "")); + else + Log.LogWarning (text.Replace ("Warning:", "")); + } else { + if (!string.IsNullOrEmpty (file)) + Log.LogError ("", "XA0103", "", file, line, column, 0, 0, text.Replace ("Error:", "")); + else + Log.LogError (text.Replace ("Error:", "")); + } + } + + XDocument MergeConfigFiles() + { + var config = new XDocument (); + var lintRoot = new XElement ("lint"); + config.Add (lintRoot); + foreach (var configFile in ConfigFiles) { + var doc = XDocument.Load (configFile.ItemSpec); + var issues = doc.Element ("lint").Elements ("issue"); + lintRoot.Add (issues); + } + return config; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LogErrorsForFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LogErrorsForFiles.cs new file mode 100644 index 00000000000..a03d8c1f17e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LogErrorsForFiles.cs @@ -0,0 +1,32 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +namespace Xamarin.Android.Tasks +{ + public class LogErrorsForFiles : Task + { + [Required] + public ITaskItem[] Files { get; set; } + + [Required] + public string Code { get; set; } + + [Required] + public string Text { get; set; } + + public string SubCategory { get; set; } + + public string HelpKeyword { get; set; } + + public override bool Execute () + { + foreach (var item in Files) { + Log.LogError (SubCategory, Code, HelpKeyword, item.ItemSpec + , 0, 0, 0, 0, Text); + } + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/LogWarningsForFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/LogWarningsForFiles.cs new file mode 100644 index 00000000000..8a3c6fed667 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/LogWarningsForFiles.cs @@ -0,0 +1,32 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +namespace Xamarin.Android.Tasks +{ + public class LogWarningsForFiles : Task + { + [Required] + public ITaskItem[] Files { get; set; } + + [Required] + public string Code { get; set; } + + [Required] + public string Text { get; set; } + + public string SubCategory { get; set; } + + public string HelpKeyword { get; set; } + + public override bool Execute () + { + foreach (var item in Files) { + Log.LogWarning (SubCategory, Code, HelpKeyword, item.ItemSpec + , 0, 0, 0, 0, Text); + } + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/MDoc.cs b/src/Xamarin.Android.Build.Tasks/Tasks/MDoc.cs new file mode 100644 index 00000000000..8aded3d94af --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/MDoc.cs @@ -0,0 +1,65 @@ +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class MDoc : ToolTask + { + public string [] References { get; set; } + + [Required] + public string TargetAssembly { get; set; } + + [Required] + public string OutputDocDirectory { get; set; } + + public bool RunExport { get; set; } + + protected override string ToolName { + get { return OS.IsWindows ? "mdoc.exe" : "mdoc"; } + } + + protected override string GenerateFullPathToTool () + { + return Path.Combine (ToolPath, ToolExe); + } + + protected override string GenerateCommandLineCommands () + { + Log.LogDebugMessage ("MDoc"); + Log.LogDebugMessage (" RunExport: {0}", RunExport); + Log.LogDebugMessage (" TargetAssembly: {0}", TargetAssembly); + Log.LogDebugMessage (" References"); + if (References != null) + foreach (var reference in References) + Log.LogDebugMessage (" {0}", reference); + Log.LogDebugMessage (" OutputDocDirectory: {0}", OutputDocDirectory); + if (RunExport) { + var cmd = new CommandLineBuilder (); + cmd.AppendSwitch ("--debug"); + cmd.AppendSwitch ("export-msxdoc"); + cmd.AppendSwitchIfNotNull ("-o", Path.ChangeExtension (TargetAssembly, ".xml")); + cmd.AppendSwitch (OutputDocDirectory); + return cmd.ToString (); + } else { + var refPaths = References.Select (Path.GetDirectoryName).Distinct (); + var cmd = new CommandLineBuilder (); + cmd.AppendSwitch ("--debug"); + cmd.AppendSwitch ("update"); + cmd.AppendSwitch ("--delete"); + cmd.AppendSwitchIfNotNull ("-L", Path.GetDirectoryName (TargetAssembly)); + foreach (var rp in refPaths) + cmd.AppendSwitchIfNotNull ("-L", rp); + cmd.AppendSwitchIfNotNull ("-o", OutputDocDirectory); + cmd.AppendSwitch (Path.GetFullPath (TargetAssembly)); + return cmd.ToString (); + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs b/src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs new file mode 100644 index 00000000000..40bb2646754 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/MakeBundleNativeCodeExternal.cs @@ -0,0 +1,247 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text.RegularExpressions; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +using Java.Interop.Tools.Diagnostics; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + // can't be a single ToolTask, because it has to run mkbundle many times for each arch. + public class MakeBundleNativeCodeExternal : Task + { + [Required] + public string AndroidNdkDirectory { get; set; } + + [Required] + public ITaskItem[] Assemblies { get; set; } + + // Which ABIs to include native libs for + public string SupportedAbis { get; set; } + + [Required] + public string TempOutputPath { get; set; } + + public string IncludePath { get; set; } + + [Required] + public string ToolPath { get; set; } + + public bool AutoDeps { get; set; } + public bool EmbedDebugSymbols { get; set; } + public bool KeepTemp { get; set; } + + [Output] + public ITaskItem [] OutputNativeLibraries { get; set; } + + public MakeBundleNativeCodeExternal () + { + } + + public override bool Execute () + { + Log.LogDebugMessage ("Assemblies: {0}", Assemblies.Length); + Log.LogDebugMessage ("SupportedAbis: {0}", SupportedAbis); + Log.LogDebugMessage ("AutoDeps: {0}", AutoDeps); + try { + if (String.IsNullOrEmpty (AndroidNdkDirectory)) { + Log.LogCodedError ("XA5101", "Could not locate Android NDK. Please make sure to configure path to NDK in SDK Locations or set via /p:AndroidNdkDirectory in the MSBuild/xbuild argument."); + return false; + } + return DoExecute (); + } catch (XamarinAndroidException e) { + Log.LogCodedError (string.Format ("XA{0:0000}", e.Code), e.MessageWithoutCode); + if (MonoAndroidHelper.LogInternalExceptions) + Log.LogMessage (e.ToString ()); + } catch (Exception ex) { + Log.LogMessage (MessageImportance.High, "Error: {0}", ex); + } + return !Log.HasLoggedErrors; + } + + bool DoExecute () + { + var abis = SupportedAbis.Split (new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries); + var results = new List (); + string bundlepath = Path.Combine (TempOutputPath, "bundles"); + if (!Directory.Exists (bundlepath)) + Directory.CreateDirectory (bundlepath); + else + Directory.Delete (bundlepath, true); + foreach (var abi in abis) { + AndroidTargetArch arch = AndroidTargetArch.Other; + switch (abi) { + case "arm64": + case "arm64-v8a": + case "aarch64": + arch = AndroidTargetArch.Arm64; + break; + case "armeabi": + case "armeabi-v7a": + arch = AndroidTargetArch.Arm; + break; + case "x86": + arch = AndroidTargetArch.X86; + break; + case "x86_64": + arch = AndroidTargetArch.X86_64; + break; + case "mips": + arch = AndroidTargetArch.Mips; + break; + } + + if (!NdkUtil.ValidateNdkPlatform (Log, AndroidNdkDirectory, arch, requireLibm:true)) { + return false; + } + + // FIXME: it is kind of hacky, we should have something like NdkUtil.GetMinimumApiLevelFor(arch). + int level = NdkUtil.IsNdk64BitArch (arch) ? 21 : arch == AndroidTargetArch.Arm ? 4 : 9; + var outpath = Path.Combine (bundlepath, abi); + if (!Directory.Exists (outpath)) + Directory.CreateDirectory (outpath); + + var clb = new CommandLineBuilder (); + clb.AppendSwitch ("--dos2unix=false"); + clb.AppendSwitch ("--nomain"); + clb.AppendSwitch ("--style"); + clb.AppendSwitch ("linux"); + clb.AppendSwitch ("-c"); + clb.AppendSwitch ("-o"); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "temp.c")); + clb.AppendSwitch ("-oo"); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "assemblies.o")); + if (AutoDeps) + clb.AppendSwitch ("--autodeps"); + if (KeepTemp) + clb.AppendSwitch ("--keeptemp"); + clb.AppendSwitch ("-z"); // Compress + clb.AppendFileNamesIfNotNull (Assemblies, " "); + var psi = new ProcessStartInfo () { + FileName = MkbundlePath, + Arguments = clb.ToString (), + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + var gccNoQuotes = NdkUtil.GetNdkTool (AndroidNdkDirectory, arch, "gcc"); + var gcc = '"' + gccNoQuotes + '"'; + var gas = '"' + NdkUtil.GetNdkTool (AndroidNdkDirectory, arch, "as") + '"'; + psi.EnvironmentVariables ["CC"] = gcc; + psi.EnvironmentVariables ["AS"] = gas; + Log.LogDebugMessage ("CC=" + gcc); + Log.LogDebugMessage ("AS=" + gas); + //psi.EnvironmentVariables ["PKG_CONFIG_PATH"] = Path.Combine (Path.GetDirectoryName (MonoDroidSdk.MandroidTool), "lib", abi); + Log.LogDebugMessage ("[mkbundle] " + psi.FileName + " " + clb); + var proc = new Process (); + proc.OutputDataReceived += OnMkbundleOutputData; + proc.ErrorDataReceived += OnMkbundleErrorData; + proc.StartInfo = psi; + proc.Start (); + proc.BeginOutputReadLine (); + proc.BeginErrorReadLine (); + proc.WaitForExit (); + if (proc.ExitCode != 0) { + Log.LogCodedError ("XA5102", "Conversion from assembly to native code failed. Exit code {0}", proc.ExitCode); + return false; + } + + // make some changes in the mkbundle output so that it does not require libmonodroid.so + var mkbundleOutput = File.ReadAllText (Path.Combine (outpath, "temp.c")); + mkbundleOutput = mkbundleOutput.Replace ("void mono_mkbundle_init ()", "void mono_mkbundle_init (void (register_bundled_assemblies_func)(const MonoBundledAssembly **), void (register_config_for_assembly_func)(const char *, const char *))") + .Replace ("mono_register_config_for_assembly (\"", "register_config_for_assembly_func (\"") + .Replace ("install_dll_config_files (void)", "install_dll_config_files (void (register_config_for_assembly_func)(const char *, const char *))") + .Replace ("install_dll_config_files ()", "install_dll_config_files (register_config_for_assembly_func)") + .Replace ("mono_register_bundled_assemblies(", "register_bundled_assemblies_func("); + File.WriteAllText (Path.Combine (outpath, "temp.c"), mkbundleOutput); + + // then compile temp.c into temp.o and ... + + clb = new CommandLineBuilder (); + clb.AppendSwitch ("-c"); + clb.AppendSwitch ("-o"); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "temp.o")); + if (!string.IsNullOrWhiteSpace (IncludePath)) { + clb.AppendSwitch ("-I"); + clb.AppendFileNameIfNotNull (IncludePath); + } + clb.AppendSwitch ("-I"); + clb.AppendFileNameIfNotNull (NdkUtil.GetNdkPlatformIncludePath (AndroidNdkDirectory, arch, level)); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "temp.c")); + Log.LogDebugMessage ("[CC] " + gcc + " " + clb); + if (MonoAndroidHelper.RunProcess (gccNoQuotes, clb.ToString (), OnCcOutputData, OnCcErrorData) != 0) { + Log.LogCodedError ("XA5103", "NDK C compiler resulted in an error. Exit code {0}", proc.ExitCode); + return false; + } + + // ... link temp.o and assemblies.o into app.so + + clb = new CommandLineBuilder (); + clb.AppendSwitch ("--shared"); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "temp.o")); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "assemblies.o")); + clb.AppendSwitch ("-o"); + clb.AppendFileNameIfNotNull (Path.Combine (outpath, "libmonodroid_bundle_app.so")); + clb.AppendSwitch ("-L"); + clb.AppendFileNameIfNotNull (NdkUtil.GetNdkPlatformLibPath (AndroidNdkDirectory, arch, level)); + clb.AppendSwitch ("-lc"); + clb.AppendSwitch ("-lm"); + clb.AppendSwitch ("-ldl"); + clb.AppendSwitch ("-llog"); + clb.AppendSwitch ("-lz"); // Compress + string ld = NdkUtil.GetNdkTool (AndroidNdkDirectory, arch, "ld"); + Log.LogMessage (MessageImportance.Normal, "[LD] " + ld + " " + clb); + if (MonoAndroidHelper.RunProcess (ld, clb.ToString (), OnLdOutputData, OnLdErrorData) != 0) { + Log.LogCodedError ("XA5201", "NDK Linker resulted in an error. Exit code {0}", proc.ExitCode); + return false; + } + results.Add (new TaskItem (Path.Combine (outpath, "libmonodroid_bundle_app.so"))); + } + OutputNativeLibraries = results.ToArray (); + return true; + } + + void OnCcOutputData (object sender, DataReceivedEventArgs e) + { + Log.LogDebugMessage ("[cc stdout] {0}", e.Data ?? ""); + } + + void OnCcErrorData (object sender, DataReceivedEventArgs e) + { + Log.LogMessage ("[cc stderr] {0}", e.Data ?? ""); + } + + void OnLdOutputData (object sender, DataReceivedEventArgs e) + { + Log.LogDebugMessage ("[ld stdout] {0}", e.Data ?? ""); + } + + void OnLdErrorData (object sender, DataReceivedEventArgs e) + { + Log.LogMessage ("[ld stderr] {0}", e.Data ?? ""); + } + + void OnMkbundleOutputData (object sender, DataReceivedEventArgs e) + { + Log.LogDebugMessage ("[mkbundle stdout] {0}", e.Data ?? ""); + } + + void OnMkbundleErrorData (object sender, DataReceivedEventArgs e) + { + Log.LogMessage ("[mkbundle stderr] {0}", e.Data ?? ""); + } + + string MkbundlePath { + get { + return Path.Combine (ToolPath, "mkbundle.exe"); + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/MergeResources.cs b/src/Xamarin.Android.Build.Tasks/Tasks/MergeResources.cs new file mode 100644 index 00000000000..147ec61eddd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/MergeResources.cs @@ -0,0 +1,89 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using System.Text.RegularExpressions; + +namespace Xamarin.Android.Tasks +{ + public class MergeResources : Task + { + [Required] + public ITaskItem[] SourceFiles { get; set; } + + [Required] + public ITaskItem[] DestinationFiles { get; set; } + + [Required] + public string AcwMapFile { get; set; } + + [Required] + public string CacheFile { get; set; } + + ResourceMerger merger = null; + + public override bool Execute () + { + // ok copy all the files from Cache into dest path + // then copy over the App Resources + // emit warnings if we find duplicates. + Log.LogDebugMessage ("MergeResources Task"); + Log.LogDebugTaskItems (" SourceFiles: ", SourceFiles); + Log.LogDebugTaskItems (" DestinationFiles: ", DestinationFiles); + + List changedFiles = new List (); + + merger = new ResourceMerger () { + CacheFile = CacheFile, + Log = Log, + }; + merger.Load (); + + for (int i = 0; i < SourceFiles.Length; i++) { + var src = SourceFiles [i].ItemSpec; + var destfilename = DestinationFiles [i].ItemSpec; + if (File.GetLastWriteTimeUtc (src) > File.GetLastWriteTimeUtc (destfilename)) { + changedFiles.Add (i); + } + } + + for (int idx = 0; idx < changedFiles.Count; idx++) { + var i = changedFiles[idx]; + var src = SourceFiles [i].ItemSpec; + var destfilename = Path.GetFullPath (DestinationFiles [i].ItemSpec); + CopyResource (src, destfilename); + merger.RemoveResourcesForFile (destfilename); + } + + merger.Save (); + + return !Log.HasLoggedErrors; + } + + void CopyResource(string src, string destPath) + { + var cachedDate = File.GetLastWriteTimeUtc (src); + var path = Path.GetDirectoryName (src).Trim (new char[] { Path.DirectorySeparatorChar }); + if (File.Exists (destPath)) { + if (merger.NeedsMerge (path)) { + merger.MergeValues (src, destPath); + } else { + MonoAndroidHelper.CopyIfChanged (src, destPath); + } + } else { + if (merger.NeedsMerge (path)) + merger.MergeValues (src, destPath); + else + MonoAndroidHelper.CopyIfChanged (src, destPath); + } + MonoAndroidHelper.SetWriteable (destPath); + MonoAndroidHelper.SetLastAccessAndWriteTimeUtc (destPath, cachedDate, Log); + } + } + + +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/NdkUtils.cs b/src/Xamarin.Android.Build.Tasks/Tasks/NdkUtils.cs new file mode 100644 index 00000000000..31958ecb42e --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/NdkUtils.cs @@ -0,0 +1,201 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text.RegularExpressions; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +using Java.Interop.Tools.Diagnostics; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public static class NdkUtil { + + public static bool ValidateNdkPlatform (TaskLoggingHelper log, string ndkPath, AndroidTargetArch arch, bool requireLibm) + { + // Check that we have a compatible NDK version for the targeted ABIs. + NdkUtil.NdkVersion ndkVersion; + bool hasNdkVersion = NdkUtil.GetNdkToolchainRelease (ndkPath, out ndkVersion); + + if (NdkUtil.IsNdk64BitArch(arch) && hasNdkVersion && ndkVersion.Version < 10) { + log.LogMessage (MessageImportance.High, + "The detected Android NDK version is incompatible with the targeted 64-bit architecture, " + + "please upgrade to NDK r10 or newer."); + } + + // NDK r10d is buggy and cannot link x86_64 ABI shared libraries because they are 32-bits. + // See https://code.google.com/p/android/issues/detail?id=161421 + if (requireLibm && ndkVersion.Version == 10 && ndkVersion.Revision == "d" && arch == AndroidTargetArch.X86_64) { + log.LogCodedError ("XA3004", "Android NDK r10d is buggy and provides an incompatible x86_64 libm.so. " + + "See https://code.google.com/p/android/issues/detail?id=161422."); + return false; + } + + return true; + } + + public static string GetNdkToolPrefix (string androidNdkPath, AndroidTargetArch arch) + { + var path = GetNdkTool (androidNdkPath, arch, "as"); + if (path != null) + path = path.Substring (0, path.LastIndexOf ("-") + 1); + return path; + } + + public static List GetNdkToolchainPath(string androidNdkPath, AndroidTargetArch arch) + { + var toolchains = GetNdkToolchainDirectories (Path.Combine (androidNdkPath, "toolchains"), arch); + if (!toolchains.Any ()) + Diagnostic.Error (5101, + "Toolchain directory for target {0} was not found.", + arch); + // Sort the toolchains paths in reverse so that we prefer the latest versions. + Array.Sort(toolchains); + Array.Reverse(toolchains); + + return new List(toolchains); + } + + public static string GetNdkTool (string androidNdkPath, AndroidTargetArch arch, string tool) + { + var toolchains = GetNdkToolchainPath(androidNdkPath, arch); + string extension = OS.IsWindows ? ".exe" : string.Empty; + List toolPaths = null; + foreach (var platbase in toolchains) { + string path = Path.Combine (platbase, "prebuilt", AndroidSdk.AndroidNdkHostPlatform, "bin", GetNdkToolchainPrefix (arch) + tool + extension); + if (File.Exists (path)) + return path; + if (toolPaths == null) + toolPaths = new List(); + toolPaths.Add (path); + } + Diagnostic.Error (5101, + "C compiler for target {0} was not found. Tried paths: \"{1}\"", + arch, string.Join ("; ", toolPaths)); + return null; + } + + public static string GetNdkPlatformIncludePath (string androidNdkPath, AndroidTargetArch arch, int level) + { + string path = Path.Combine (androidNdkPath, "platforms", "android-" + level, "arch-" + GetPlatformArch (arch), "usr", "include"); + if (!Directory.Exists (path)) + throw new InvalidOperationException (String.Format ("Platform header files for target {0} and API Level {1} was not found. Expected path is \"{2}\"", arch, level, path)); + return path; + } + + public static string GetNdkPlatformLibPath (string androidNdkPath, AndroidTargetArch arch, int level) + { + string path = Path.Combine (androidNdkPath, "platforms", "android-" + level, "arch-" + GetPlatformArch (arch), "usr", "lib"); + if (!Directory.Exists (path)) + throw new InvalidOperationException (String.Format ("Platform library directory for target {0} and API Level {1} was not found. Expected path is \"{2}\"", arch, level, path)); + return path; + } + + static string GetPlatformArch (AndroidTargetArch arch) + { + switch (arch) { + case AndroidTargetArch.Arm: + return "arm"; + case AndroidTargetArch.Arm64: + return "arm64"; + case AndroidTargetArch.Mips: + return "mips"; + case AndroidTargetArch.X86: + return "x86"; + case AndroidTargetArch.X86_64: + return "x86_64"; + } + return null; + } + + static string[] GetNdkToolchainDirectories (string toolchainsPath, AndroidTargetArch arch) + { + if (!Directory.Exists (toolchainsPath)) + Diagnostic.Error (5101, + "Missing Android NDK toolchains directory '{0}'. Please install the Android NDK.", + toolchainsPath); + switch (arch) { + case AndroidTargetArch.Arm: + return Directory.GetDirectories (toolchainsPath, "arm-linux-androideabi-*"); + case AndroidTargetArch.Arm64: + return Directory.GetDirectories (toolchainsPath, "aarch64-linux-android-*"); + case AndroidTargetArch.X86: + return Directory.GetDirectories (toolchainsPath, "x86-*"); + case AndroidTargetArch.X86_64: + return Directory.GetDirectories (toolchainsPath, "x86_64-*"); + case AndroidTargetArch.Mips: + return Directory.GetDirectories (toolchainsPath, "mipsel-linux-android-*"); + default: // match any directory that contains the arch name. + return Directory.GetDirectories (toolchainsPath, "*" + arch + "*"); + } + } + + static string GetNdkToolchainPrefix (AndroidTargetArch arch) + { + switch (arch) { + case AndroidTargetArch.Arm: + return "arm-linux-androideabi-"; + case AndroidTargetArch.Arm64: + return "aarch64-linux-android-"; + case AndroidTargetArch.X86: + return "i686-linux-android-"; + case AndroidTargetArch.X86_64: + return "x86_64-linux-android-"; + case AndroidTargetArch.Mips: + return "mipsel-linux-android-"; + default: + // return empty. Since this method returns the "prefix", the resulting + // tool path just becomes the tool name i.e. "gcc" becomes "gcc". + // This should work for any custom arbitrary platform. + return String.Empty; + } + } + + static bool GetNdkToolchainRelease (string androidNdkPath, out string version) + { + var releaseVersionPath = Path.Combine (androidNdkPath, "RELEASE.txt"); + if (!File.Exists (releaseVersionPath)) + { + version = string.Empty; + return false; + } + + version = File.ReadAllText (releaseVersionPath).Trim(); + return true; + } + + public struct NdkVersion + { + public int Version; + public string Revision; + } + + public static bool GetNdkToolchainRelease (string androidNdkPath, out NdkVersion ndkVersion) + { + ndkVersion = new NdkVersion (); + + string version; + if (!GetNdkToolchainRelease(androidNdkPath, out version)) + return false; + + var match = Regex.Match(version, @"r(\d+)\s*(.*)\s+.*"); + if( !match.Success) + return false; + + ndkVersion.Version = int.Parse (match.Groups[1].Value.Trim()); + ndkVersion.Revision = match.Groups[2].Value.Trim().ToLowerInvariant(); + + return true; + } + + public static bool IsNdk64BitArch (AndroidTargetArch arch) + { + return arch == AndroidTargetArch.Arm64 || arch == AndroidTargetArch.X86_64; + } + } +} \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ParseAndroidWearProjectAndManifest.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ParseAndroidWearProjectAndManifest.cs new file mode 100644 index 00000000000..9075319a2ce --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ParseAndroidWearProjectAndManifest.cs @@ -0,0 +1,50 @@ +using System; +using System.IO; +using System.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Xml.Linq; + +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class ParseAndroidWearProjectAndManifest : Task + { + static readonly XNamespace msbuildNS = XNamespace.Get ("http://schemas.microsoft.com/developer/msbuild/2003"); + + public ITaskItem [] ProjectFiles { get; set; } + [Output] + public string ApplicationManifestFile { get; set; } + [Output] + public string ApplicationPackageName { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("ParseAndroidWearProjectAndManifest task"); + Log.LogDebugTaskItems (" ProjectFiles:", ProjectFiles); + if (ProjectFiles.Length != 1) + Log.LogError ("More than one Android Wear project is specified as the paired project. It can be at most one."); + + var wearProj = ProjectFiles.First (); + var manifestXml = XDocument.Load (wearProj.ItemSpec) + .Root.Elements (msbuildNS + "PropertyGroup").Elements (msbuildNS + "AndroidManifest").Select (e => e.Value).FirstOrDefault (); + if (string.IsNullOrEmpty (manifestXml)) + Log.LogError ("Target Wear application's project '{0}' does not specify required 'AndroidManifest' project property.", wearProj); + manifestXml = Path.Combine (Path.GetDirectoryName (wearProj.ItemSpec), manifestXml.Replace ('\\', Path.DirectorySeparatorChar)); + + ApplicationManifestFile = manifestXml; + + Log.LogDebugMessage (" [Output] ApplicationManifestFile: " + ApplicationManifestFile); + + ApplicationPackageName = AndroidAppManifest.CanonicalizePackageName (XDocument.Load (manifestXml).Root.Attributes ("package").Select (a => a.Value).FirstOrDefault ()); + if (string.IsNullOrEmpty (ApplicationPackageName)) + Log.LogError ("Target Wear application's AndroidManifest.xml does not specify required 'package' attribute."); + + Log.LogDebugMessage (" [Output] ApplicationPackageName: " + ApplicationPackageName); + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/PrepareWearApplicationFiles.cs b/src/Xamarin.Android.Build.Tasks/Tasks/PrepareWearApplicationFiles.cs new file mode 100644 index 00000000000..8779cd396ae --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/PrepareWearApplicationFiles.cs @@ -0,0 +1,69 @@ +using System; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.IO; +using System.Xml.Linq; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class PrepareWearApplicationFiles : Task + { + static readonly XNamespace androidNs = XNamespace.Get ("http://schemas.android.com/apk/res/android"); + + [Required] + public string PackageName { get; set; } + public string WearAndroidManifestFile { get; set; } + public string IntermediateOutputPath { get; set; } + public string WearApplicationApkPath { get; set; } + [Output] + public ITaskItem WearableApplicationDescriptionFile { get; set; } + [Output] + public ITaskItem BundledWearApplicationApkResourceFile { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("PrepareWearApplicationFiles task"); + Log.LogDebugTaskItems (" WearAndroidManifestFile:", WearAndroidManifestFile); + Log.LogDebugTaskItems (" IntermediateOutputPath:", IntermediateOutputPath); + Log.LogDebugTaskItems (" WearApplicationApkPath:", WearApplicationApkPath); + + string rawapk = "wearable_app.apk"; + string intermediateApkPath = Path.Combine (IntermediateOutputPath, "res", "raw", rawapk); + string intermediateXmlFile = Path.Combine (IntermediateOutputPath, "res", "xml", "wearable_app_desc.xml"); + + var doc = XDocument.Load (WearAndroidManifestFile); + var wearPackageName = AndroidAppManifest.CanonicalizePackageName (doc.Root.Attribute ("package").Value); + + if (PackageName != wearPackageName) + Log.LogCodedError ("XA5211", "Embedded wear app package name differs from handheld app package name ({0} != {1}).", wearPackageName, PackageName); + + if (!File.Exists (WearApplicationApkPath)) { + Log.LogWarning ("This application won't contain the paired Wear package because the Wear application package .apk is not created yet. If you are using MSBuild or XBuild, you have to invoke \"SignAndroidPackage\" target."); + return true; + } + + var xml = string.Format (@" + {1} + {2} + {3} + +", wearPackageName, doc.Root.Attribute (androidNs + "versionCode").Value, doc.Root.Attribute (androidNs + "versionName").Value, Path.GetFileNameWithoutExtension (rawapk)); + + MonoAndroidHelper.CopyIfChanged (WearApplicationApkPath, intermediateApkPath); + + Directory.CreateDirectory (Path.GetDirectoryName (intermediateXmlFile)); + if (!File.Exists (intermediateXmlFile) || !XDocument.DeepEquals (XDocument.Load (intermediateXmlFile), XDocument.Parse (xml))) { + File.WriteAllText (intermediateXmlFile, xml); + Log.LogDebugMessage (" Created additional resource as {0}", intermediateXmlFile); + } + WearableApplicationDescriptionFile = new TaskItem (intermediateXmlFile); + WearableApplicationDescriptionFile.SetMetadata ("IsWearApplicationResource", "True"); + BundledWearApplicationApkResourceFile = new TaskItem (intermediateApkPath); + BundledWearApplicationApkResourceFile.SetMetadata ("IsWearApplicationResource", "True"); + + return true; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs new file mode 100644 index 00000000000..4f0e1b9e58c --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Proguard.cs @@ -0,0 +1,222 @@ +// Copyright (C) 2012 Xamarin, Inc. All rights reserved. + +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Xamarin.AndroidTools; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + // See AOSP/sdk/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/build/BuildHelper.java + public class Proguard : ToolTask + { + public string ProguardJarPath { get; set; } + + public string JavaToolPath { get; set; } + + [Required] + public string JavaPlatformJarPath { get; set; } + + [Required] + public string ClassesOutputDirectory { get; set; } + + [Required] + public string AcwMapFile { get; set; } + + [Required] + public string ProguardJarInput { get; set; } + + [Required] + public string ProguardJarOutput { get; set; } + + [Required] + public string ProguardGeneratedReferenceConfiguration { get; set; } + + [Required] + public string ProguardGeneratedApplicationConfiguration { get; set; } + + [Required] + public string ProguardCommonXamarinConfiguration { get; set; } + + public string ProguardConfigurationFiles { get; set; } + + public ITaskItem[] JavaLibrariesToEmbed { get; set; } + + public ITaskItem[] ExternalJavaLibraries { get; set; } + + public ITaskItem[] DoNotPackageJavaLibraries { get; set; } + + public bool UseProguard { get; set; } + + public string JavaOptions { get; set; } + + public string JavaMaximumHeapSize { get; set; } + + public bool EnableLogging { get; set; } + public string DumpOutput { get; set; } + public string PrintSeedsOutput { get; set; } + public string PrintUsageOutput { get; set; } + public string PrintMappingOutput { get; set; } + + protected override string ToolName { + get { + if (UseProguard) + return OS.IsWindows ? "proguard.bat" : "proguard"; + return OS.IsWindows ? "java.exe" : "java"; + } + } + + public override bool Execute () + { + Log.LogDebugMessage ("Proguard"); + Log.LogDebugMessage (" JavaPlatformJarPath: {0}", JavaPlatformJarPath); + Log.LogDebugMessage (" ClassesOutputDirectory: {0}", ClassesOutputDirectory); + Log.LogDebugMessage (" AcwMapFile: {0}", AcwMapFile); + Log.LogDebugMessage (" ProguardGeneratedApplicationConfiguration: {0}", ProguardGeneratedApplicationConfiguration); + Log.LogDebugMessage (" ProguardJarInput: {0}", ProguardJarInput); + Log.LogDebugMessage (" ProguardJarOutput: {0}", ProguardJarOutput); + Log.LogDebugTaskItems (" ProguardGeneratedReferenceConfiguration:", ProguardGeneratedReferenceConfiguration); + Log.LogDebugTaskItems (" ProguardGeneratedApplicationConfiguration:", ProguardGeneratedApplicationConfiguration); + Log.LogDebugTaskItems (" ProguardCommonXamarinConfiguration:", ProguardCommonXamarinConfiguration); + Log.LogDebugTaskItems (" ProguardConfigurationFiles:", ProguardConfigurationFiles); + Log.LogDebugTaskItems (" ExternalJavaLibraries:", ExternalJavaLibraries); + Log.LogDebugTaskItems (" DoNotPackageJavaLibraries:", DoNotPackageJavaLibraries); + Log.LogDebugMessage (" UseProguard: {0}", UseProguard); + Log.LogDebugMessage (" EnableLogging: {0}", EnableLogging); + Log.LogDebugMessage (" DumpOutput: {0}", DumpOutput); + Log.LogDebugMessage (" PrintSeedsOutput: {0}", PrintSeedsOutput); + Log.LogDebugMessage (" PrintMappingOutput: {0}", PrintMappingOutput); + + return base.Execute (); + } + + protected override string GenerateCommandLineCommands () + { + var cmd = new CommandLineBuilder (); + + if (!UseProguard) { + // Add the JavaOptions if they are not null + // These could be any of the additional options + if (!string.IsNullOrEmpty (JavaOptions)) { + cmd.AppendSwitch (JavaOptions); + } + + // Add the specific -XmxN to override the default heap size for the JVM + // N can be in the form of Nm or NGB (e.g 100m or 1GB ) + cmd.AppendSwitchIfNotNull ("-Xmx", JavaMaximumHeapSize); + + cmd.AppendSwitchIfNotNull ("-jar ", Path.Combine (ProguardJarPath)); + } + + if (!ClassesOutputDirectory.EndsWith (Path.DirectorySeparatorChar.ToString ())) + ClassesOutputDirectory += Path.DirectorySeparatorChar; + var classesFullPath = Path.GetFullPath (ClassesOutputDirectory); + + if (File.Exists (ProguardJarInput)) + File.Delete (ProguardJarInput); + var zip = new Ionic.Zip.ZipFile (ProguardJarInput, new System.Text.UTF8Encoding (false)); + foreach (var file in Directory.GetFiles (classesFullPath, "*", SearchOption.AllDirectories)) + zip.AddFile (file, Path.GetDirectoryName (file.Substring (classesFullPath.Length))); + zip.Save (); + zip.Dispose (); + + var acwLines = File.ReadAllLines (AcwMapFile); + using (var appcfg = File.CreateText (ProguardGeneratedApplicationConfiguration)) + for (int i = 0; i + 3 < acwLines.Length; i += 4) + try { + var java = acwLines [i + 3].Substring (acwLines [i + 3].IndexOf (';') + 1); + appcfg.WriteLine ("-keep class " + java + " { *; }"); + } catch { + // skip invalid lines + } + + var injars = new List (); + var libjars = new List (); + injars.Add (ProguardJarInput); + if (JavaLibrariesToEmbed != null) + foreach (var jarfile in JavaLibrariesToEmbed) + injars.Add (jarfile.ItemSpec); + + using (var xamcfg = File.Create (ProguardCommonXamarinConfiguration)) + GetType ().Assembly.GetManifestResourceStream ("proguard_xamarin.cfg").CopyTo (xamcfg); + + var configs = ProguardConfigurationFiles + .Replace ("{sdk.dir}", Path.GetDirectoryName (Path.GetDirectoryName (ProguardHome)) + Path.DirectorySeparatorChar) + .Replace ("{intermediate.common.xamarin}", ProguardCommonXamarinConfiguration) + .Replace ("{intermediate.references}", ProguardGeneratedReferenceConfiguration) + .Replace ("{intermediate.application}", ProguardGeneratedApplicationConfiguration) + .Replace ("{project}", string.Empty) // current directory anyways. + .Split (';') + .Select (s => s.Trim ()) + .Where (s => !string.IsNullOrWhiteSpace (s)); + + foreach (var file in configs) { + if (File.Exists (file)) + cmd.AppendSwitchIfNotNull ("-include ", file); + else + Log.LogWarning ("Proguard configuration file '{0}' was not found.", file); + } + + libjars.Add (JavaPlatformJarPath); + if (ExternalJavaLibraries != null) + foreach (var jarfile in ExternalJavaLibraries.Select (p => p.ItemSpec)) + libjars.Add (jarfile); + + cmd.AppendSwitch ("\"-injars"); + cmd.AppendSwitch (string.Join (Path.PathSeparator.ToString (), injars.Distinct ().Select (s => '\'' + s + '\''))+"\""); + + cmd.AppendSwitch ("\"-libraryjars"); + cmd.AppendSwitch (string.Join (Path.PathSeparator.ToString (), libjars.Distinct ().Select (s => '\'' + s + '\''))+"\""); + + cmd.AppendSwitch ("-outjars"); + cmd.AppendSwitch ('"' + ProguardJarOutput + '"'); + + if (EnableLogging) { + cmd.AppendSwitchIfNotNull ("-dump ", DumpOutput); + cmd.AppendSwitchIfNotNull ("-printseeds ", PrintSeedsOutput); + cmd.AppendSwitchIfNotNull ("-printusage ", PrintUsageOutput); + cmd.AppendSwitchIfNotNull ("-printmapping ", PrintMappingOutput); + } + + return cmd.ToString (); + } + + protected override string GenerateFullPathToTool () + { + if (UseProguard) + return Path.Combine (ToolPath, ToolExe); + return Path.Combine (JavaToolPath, ToolName); + } + + // Windows seems to need special care. + protected override StringDictionary EnvironmentOverride { + get { + var sd = base.EnvironmentOverride ?? new StringDictionary (); + if (OS.IsWindows) { + if (!sd.ContainsKey ("PROGUARD_HOME")) + sd.Add ("PROGUARD_HOME", ProguardHome); + } + var opts = sd.ContainsKey ("JAVA_TOOL_OPTIONS") ? sd ["JAVA_TOOL_OPTIONS"] : null; + opts += " -Dfile.encoding=UTF8"; + sd ["JAVA_TOOL_OPTIONS"] = opts; + return sd; + } + } + + string ProguardHome { + get { + return Path.GetDirectoryName (Path.GetDirectoryName (UseProguard ? ToolPath : ProguardJarPath)); + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ReadAdditionalResourcesFromAssemblyCache.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ReadAdditionalResourcesFromAssemblyCache.cs new file mode 100644 index 00000000000..d97a8166a95 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ReadAdditionalResourcesFromAssemblyCache.cs @@ -0,0 +1,65 @@ +using System; +using System.IO; +using System.Linq; +using System.Xml.XPath; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class ReadAdditionalResourcesFromAssemblyCache : Task { + + [Required] + public string CacheFile { get; set;} + + [Output] + public string[] AdditionalAndroidResourcePaths { get; set; } + + [Output] + public string[] AdditionalJavaLibraryReferences { get; set; } + + [Output] + public string[] AdditionalNativeLibraryReferences { get; set; } + + [Output] + public bool IsResourceCacheValid { get; set; } + + public ReadAdditionalResourcesFromAssemblyCache () + { + AdditionalAndroidResourcePaths = new string [0]; + AdditionalJavaLibraryReferences = new string [0]; + AdditionalNativeLibraryReferences = new string [0]; + } + + public override bool Execute () + { + Log.LogDebugMessage ("Task ReadAdditionalResourcesFromAssemblyCache"); + Log.LogDebugMessage (" CacheFile: {0}", CacheFile); + if (!File.Exists (CacheFile)) { + Log.LogWarning ("{0} does not exist. No Additional Resources found", CacheFile); + return !Log.HasLoggedErrors; + } + var doc = XDocument.Load (CacheFile); + AdditionalAndroidResourcePaths = doc.GetPaths ("AdditionalAndroidResourcePaths", + "AdditionalAndroidResourcePath"); + AdditionalJavaLibraryReferences = doc.GetPaths ("AdditionalJavaLibraryReferences", + "AdditionalJavaLibraryReference"); + AdditionalNativeLibraryReferences = doc.GetPaths ("AdditionalNativeLibraryReferences", + "AdditionalNativeLibraryReference"); + + Log.LogDebugTaskItems (" AdditionalAndroidResourcePaths: ", AdditionalAndroidResourcePaths); + Log.LogDebugTaskItems (" AdditionalJavaLibraryReferences: ", AdditionalJavaLibraryReferences); + Log.LogDebugTaskItems (" AdditionalNativeLibraryReferences: ", AdditionalNativeLibraryReferences); + + IsResourceCacheValid = AdditionalAndroidResourcePaths.All (x => Directory.Exists (x)) && + AdditionalJavaLibraryReferences.All (x => File.Exists (x)) && + AdditionalNativeLibraryReferences.All (x => File.Exists (x)); + + Log.LogDebugMessage (" IsValid: {0}", IsResourceCacheValid); + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ReadImportedLibrariesCache.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ReadImportedLibrariesCache.cs new file mode 100644 index 00000000000..a404bea1416 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ReadImportedLibrariesCache.cs @@ -0,0 +1,70 @@ +// +// ReadImportedLibrariesCache.cs +// +// Author: +// Dean Ellis +// +// Copyright (c) 2013 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class ReadImportedLibrariesCache : Task + { + [Required] + public string CacheFile { get; set;} + + [Output] + public ITaskItem [] Jars { get; set; } + + [Output] + public ITaskItem [] NativeLibraries { get; set; } + + [Output] + public ITaskItem [] ManifestDocuments { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("Task ReadImportedLibrariesCache"); + Log.LogDebugMessage (" CacheFile: {0}", CacheFile); + if (!File.Exists (CacheFile)) { + Log.LogWarning ("{0} does not exist. No Imported Libraries found", CacheFile); + return !Log.HasLoggedErrors; + } + var doc = XDocument.Load (CacheFile); + Jars = doc.GetPathsAsTaskItems ("Jars", "Jar"); + NativeLibraries = doc.GetPathsAsTaskItems ("NativeLibraries", "NativeLibrary"); + ManifestDocuments = doc.GetPathsAsTaskItems ("ManifestDocuments", "ManifestDocument"); + + Log.LogDebugTaskItems (" NativeLibraries: ", NativeLibraries); + Log.LogDebugTaskItems (" Jars: ", Jars); + Log.LogDebugTaskItems (" ManifestDocuments: ", ManifestDocuments); + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ReadLibraryProjectImportsCache.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ReadLibraryProjectImportsCache.cs new file mode 100644 index 00000000000..2460c0e726a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ReadLibraryProjectImportsCache.cs @@ -0,0 +1,81 @@ +// +// ReadLibraryProjectImportsCache.cs +// +// Author: +// Dean Ellis +// +// Copyright (c) 2015 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +namespace Xamarin.Android.Tasks +{ + public class ReadLibraryProjectImportsCache : Task + { + [Required] + public string CacheFile { get; set;} + + [Output] + public ITaskItem [] Jars { get; set; } + + [Output] + public ITaskItem [] ResolvedAssetDirectories { get; set; } + + [Output] + public ITaskItem [] ResolvedResourceDirectories { get; set; } + + [Output] + public ITaskItem [] ResolvedEnvironmentFiles { get; set; } + + [Output] + public ITaskItem [] ResolvedResourceDirectoryStamps { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("Task ReadLibraryProjectImportsCache"); + Log.LogDebugMessage (" CacheFile: {0}", CacheFile); + if (!File.Exists (CacheFile)) { + Log.LogWarning ("{0} does not exist. No Project Library Imports found", CacheFile); + return !Log.HasLoggedErrors; + } + var doc = XDocument.Load (CacheFile); + Jars = doc.GetPathsAsTaskItems ("Jars", "Jar"); + ResolvedAssetDirectories = doc.GetPathsAsTaskItems ("ResolvedAssetDirectories", "ResolvedAssetDirectory"); + ResolvedResourceDirectories = doc.GetPathsAsTaskItems ("ResolvedResourceDirectories", "ResolvedResourceDirectory"); + ResolvedEnvironmentFiles = doc.GetPathsAsTaskItems ("ResolvedEnvironmentFiles", "ResolvedEnvironmentFile"); + ResolvedResourceDirectoryStamps = doc.GetPathsAsTaskItems ("ResolvedResourceDirectoryStamps" + , "ResolvedResourceDirectoryStamp"); + + Log.LogDebugTaskItems (" Jars: ", Jars); + Log.LogDebugTaskItems (" ResolvedAssetDirectories: ", ResolvedAssetDirectories); + Log.LogDebugTaskItems (" ResolvedResourceDirectories: ", ResolvedResourceDirectories); + Log.LogDebugTaskItems (" ResolvedEnvironmentFiles: ", ResolvedEnvironmentFiles); + Log.LogDebugTaskItems (" ResolvedResourceDirectoryStamps: ", ResolvedResourceDirectoryStamps); + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ReadResolvedSdksCache.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ReadResolvedSdksCache.cs new file mode 100644 index 00000000000..bfaf968396b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ReadResolvedSdksCache.cs @@ -0,0 +1,141 @@ +// +// ReadResolveSdksCache.cs +// +// Author: +// Dean Ellis +// +// Copyright (c) 2013 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using Xamarin.AndroidTools; + +namespace Xamarin.Android.Tasks +{ + public class ReadResolvedSdksCache : Task + { + [Required] + public string CacheFile { get; set;} + + [Output] + public string[] ReferenceAssemblyPaths { get; set; } + + [Output] + public string AndroidApiLevel { get; set; } + + [Output] + public string AndroidApiLevelName { get; set; } + + [Output] + public string SupportedApiLevel { get; set; } + + [Output] + public string TargetFrameworkVersion { get; set; } + + [Output] + public string MonoAndroidToolsPath { get; set; } + + [Output] + public string MonoAndroidBinPath { get; set; } + + [Output] + public string MonoAndroidIncludePath { get; set; } + + [Output] + public string AndroidNdkPath { get; set; } + + [Output] + public string AndroidSdkPath { get; set; } + + [Output] + public string JavaSdkPath { get; set; } + + [Output] + public string AndroidSdkBuildToolsPath { get; set; } + + [Output] + public string AndroidSdkBuildToolsBinPath { get; set; } + + [Output] + public string ZipAlignPath { get; set; } + + [Output] + public string AndroidSequencePointsMode { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("Task ReadResolvedSdksCache"); + Log.LogDebugMessage (" CacheFile: {0}", CacheFile); + if (!File.Exists (CacheFile)) { + Log.LogWarning ("{0} does not exist. No Resolved Sdks found", CacheFile); + return !Log.HasLoggedErrors; + } + var doc = XDocument.Load (CacheFile); + //Sdk/AndroidApiLevel + var sdk = doc.Element ("Sdk"); + AndroidApiLevel = sdk.Element ("AndroidApiLevel").Value; + AndroidApiLevelName = sdk.Element ("AndroidApiLevelName").Value; + SupportedApiLevel = sdk.Element ("SupportedApiLevel").Value; + TargetFrameworkVersion = sdk.Element ("TargetFrameworkVersion").Value; + MonoAndroidToolsPath = sdk.Element ("MonoAndroidToolsPath").Value; + MonoAndroidBinPath = sdk.Element ("MonoAndroidBinPath").Value; + MonoAndroidIncludePath = sdk.Element ("MonoAndroidIncludePath").Value; + AndroidNdkPath = sdk.Element ("AndroidNdkPath").Value; + AndroidSdkPath = sdk.Element ("AndroidSdkPath").Value; + JavaSdkPath = sdk.Element ("JavaSdkPath").Value; + AndroidSdkBuildToolsPath = sdk.Element ("AndroidSdkBuildToolsPath").Value; + AndroidSdkBuildToolsBinPath = sdk.Element ("AndroidSdkBuildToolsBinPath").Value; + ZipAlignPath = sdk.Element ("ZipAlignPath").Value; + ReferenceAssemblyPaths = sdk.Elements ("ReferenceAssemblyPaths") + .Elements ("ReferenceAssemblyPath") + .Select (e => e.Value) + .ToArray (); + AndroidSequencePointsMode = sdk.Element ("AndroidSequencePointsMode")?.Value ?? "None"; + + Log.LogDebugMessage ("ResolveSdksTask Outputs:"); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugMessage (" AndroidApiLevelName: {0}", AndroidApiLevelName); + Log.LogDebugMessage (" AndroidNdkPath: {0}", AndroidNdkPath); + Log.LogDebugMessage (" AndroidSdkBuildToolsPath: {0}", AndroidSdkBuildToolsPath); + Log.LogDebugMessage (" AndroidSdkBuildToolsBinPath: {0}", AndroidSdkBuildToolsBinPath); + Log.LogDebugMessage (" AndroidSdkPath: {0}", AndroidSdkPath); + Log.LogDebugMessage (" JavaSdkPath: {0}", JavaSdkPath); + Log.LogDebugMessage (" MonoAndroidBinPath: {0}", MonoAndroidBinPath); + Log.LogDebugMessage (" MonoAndroidToolsPath: {0}", MonoAndroidToolsPath); + Log.LogDebugMessage (" MonoAndroidIncludePath: {0}", MonoAndroidIncludePath); + Log.LogDebugMessage (" TargetFrameworkVersion: {0}", TargetFrameworkVersion); + Log.LogDebugMessage (" ZipAlignPath: {0}", ZipAlignPath); + Log.LogDebugMessage (" SupportedApiLevel: {0}", SupportedApiLevel); + Log.LogDebugMessage (" AndroidSequencePointsMode: {0}", AndroidSequencePointsMode); + + MonoAndroidHelper.TargetFrameworkDirectories = ReferenceAssemblyPaths; + + MonoAndroidHelper.RefreshAndroidSdk (AndroidSdkPath, AndroidNdkPath, JavaSdkPath); + MonoAndroidHelper.RefreshMonoDroidSdk (MonoAndroidToolsPath, MonoAndroidBinPath, ReferenceAssemblyPaths); + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/RemoveDirFixed.cs b/src/Xamarin.Android.Build.Tasks/Tasks/RemoveDirFixed.cs new file mode 100644 index 00000000000..15131f9e499 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/RemoveDirFixed.cs @@ -0,0 +1,111 @@ +// +// RemoveDir.cs: Removes a directory. +// +// Author: +// Marek Sieradzki (marek.sieradzki@gmail.com) +// +// (C) 2005 Marek Sieradzki +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Security; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +// The RemoveDir task that ships with Mono 2.10 doesn't work for +// recursive directories, so we ship this one until Mono 2.12 is +// the norm. +namespace Xamarin.Android.Tasks { + public class RemoveDirFixed : Task { + + ITaskItem[] directories; + ITaskItem[] removedDirectories; + + public RemoveDirFixed () + { + } + + public override bool Execute () + { + if (directories.Length == 0) + return true; + + List temporaryRemovedDirectories = new List (); + + foreach (ITaskItem directory in directories) { + try { + MonoAndroidHelper.SetDirectoryWriteable (directory.GetMetadata ("FullPath")); + Directory.Delete (directory.GetMetadata ("FullPath"), true); + temporaryRemovedDirectories.Add (directory); + } + catch (DirectoryNotFoundException ex) { + Log.LogErrorFromException (ex); + } + catch (PathTooLongException ex) { + Log.LogErrorFromException (ex); + } + catch (ArgumentNullException ex) { + Log.LogErrorFromException (ex); + } + catch (ArgumentException ex) { + Log.LogErrorFromException (ex); + } + catch (IOException ex) { + Log.LogErrorFromException (ex); + } + catch (SecurityException ex) { + Log.LogErrorFromException (ex); + } + catch (Exception ex) { + Log.LogErrorFromException (ex); + } + } + + removedDirectories = temporaryRemovedDirectories.ToArray (); + + Log.LogDebugTaskItems (" RemovedDirectories: ", RemovedDirectories); + + return true; + } + + [Required] + public ITaskItem[] Directories { + get { + return directories; + } + set { + directories = value; + } + } + + [Output] + public ITaskItem[] RemovedDirectories { + get { + return removedDirectories; + } + set { + removedDirectories = value; + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs b/src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs new file mode 100644 index 00000000000..7fa20d4148f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/RemoveRegisterAttribute.cs @@ -0,0 +1,68 @@ +// Copyright (C) 2011, Xamarin Inc. +// Copyright (C) 2010, Novell Inc. + +using System; +using System.IO; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Mono.Cecil; + +namespace Xamarin.Android.Tasks +{ + public class RemoveRegisterAttribute : Task + { + const string RegisterAttribute = "Android.Runtime.RegisterAttribute"; + + [Required] + public ITaskItem[] ShrunkFrameworkAssemblies { get; set; } + + public override bool Execute () + { + Log.LogDebugMessage ("RemoveRegisterAttribute Task"); + Log.LogDebugTaskItems (" ShrunkFrameworkAssemblies:", ShrunkFrameworkAssemblies); + + // Find Mono.Android.dll + var mono_android = ShrunkFrameworkAssemblies.First (f => Path.GetFileNameWithoutExtension (f.ItemSpec) == "Mono.Android").ItemSpec; + var assembly = AssemblyDefinition.ReadAssembly (mono_android); + + // Strip out [Register] attributes + foreach (TypeDefinition type in assembly.MainModule.Types) + ProcessType (type); + + assembly.Write (mono_android); + + return true; + } + + private static void ProcessType (TypeDefinition type) + { + if (type.HasFields) + foreach (FieldDefinition field in type.Fields) + ProcessAttributeProvider (field); + + if (type.HasMethods) + foreach (MethodDefinition method in type.Methods) + ProcessAttributeProvider (method); + } + + private static void ProcessAttributeProvider (Mono.Cecil.ICustomAttributeProvider provider) + { + if (!provider.HasCustomAttributes) + return; + + for (int i = 0; i < provider.CustomAttributes.Count; i++) { + if (!IsRegisterAttribute (provider.CustomAttributes [i])) + continue; + + provider.CustomAttributes.RemoveAt (i--); + } + } + + private static bool IsRegisterAttribute (CustomAttribute attribute) + { + return attribute.Constructor.DeclaringType.FullName == RegisterAttribute; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs new file mode 100644 index 00000000000..9b4aae75a94 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveAssemblies.cs @@ -0,0 +1,184 @@ +// Copyright (C) 2011, Xamarin Inc. +// Copyright (C) 2010, Novell Inc. + +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +using Mono.Cecil; +using MonoDroid.Tuner; +using System.IO; + +using Java.Interop.Tools.Cecil; + +namespace Xamarin.Android.Tasks +{ + public class ResolveAssemblies : Task + { + // The user's assemblies to package + [Required] + public ITaskItem[] Assemblies { get; set; } + + [Required] + public string ReferenceAssembliesDirectory { get; set; } + + public string I18nAssemblies { get; set; } + public string LinkMode { get; set; } + + // The user's assemblies, and all referenced assemblies + [Output] + public ITaskItem[] ResolvedAssemblies { get; set; } + + [Output] + public ITaskItem[] ResolvedUserAssemblies { get; set; } + + [Output] + public ITaskItem[] ResolvedFrameworkAssemblies { get; set; } + + [Output] + public ITaskItem[] ResolvedSymbols { get; set; } + + [Output] + public string[] ResolvedDoNotPackageAttributes { get; set; } + + DirectoryAssemblyResolver resolver; + + public override bool Execute () + { + Log.LogDebugMessage ("ResolveAssemblies Task"); + Log.LogDebugMessage (" ReferenceAssembliesDirectory: {0}", ReferenceAssembliesDirectory); + Log.LogDebugMessage (" I18nAssemblies: {0}", I18nAssemblies); + Log.LogDebugMessage (" LinkMode: {0}", LinkMode); + Log.LogDebugTaskItems (" Assemblies:", Assemblies); + + resolver = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols: false); + foreach (var dir in ReferenceAssembliesDirectory.Split (new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)) + resolver.SearchDirectories.Add (dir); + + var assemblies = new HashSet (); + + var topAssemblyReferences = new List (); + + try { + foreach (var ass in Assemblies) { + var ass_path = Path.GetDirectoryName (ass.ItemSpec); + + if (!resolver.SearchDirectories.Contains (ass_path)) + resolver.SearchDirectories.Add (ass_path); + + // Add each user assembly and all referenced assemblies (recursive) + var assembly = resolver.Load (ass.ItemSpec); + if (assembly == null) + throw new InvalidOperationException ("Failed to load assembly " + ass.ItemSpec); + topAssemblyReferences.Add (assembly); + assemblies.Add (assembly.MainModule.FullyQualifiedName); + } + } catch (Exception ex) { + Log.LogError ("Exception while loading assemblies: {0}", ex); + return false; + } + try { + foreach (var assembly in topAssemblyReferences) + AddAssemblyReferences (assemblies, assembly, true); + } catch (Exception ex) { + Log.LogError ("Exception while loading assemblies: {0}", ex); + return false; + } + + // Add I18N assemblies if needed + AddI18nAssemblies (assemblies); + + ResolvedAssemblies = assemblies.Select (a => new TaskItem (a)).ToArray (); + ResolvedSymbols = assemblies.Select (a => a + ".mdb").Where (a => File.Exists (a)).Select (a => new TaskItem (a)).ToArray (); + ResolvedFrameworkAssemblies = ResolvedAssemblies.Where (p => MonoAndroidHelper.IsFrameworkAssembly (p.ItemSpec, true)).ToArray (); + ResolvedUserAssemblies = ResolvedAssemblies.Where (p => !MonoAndroidHelper.IsFrameworkAssembly (p.ItemSpec, true)).ToArray (); + ResolvedDoNotPackageAttributes = do_not_package_atts.ToArray (); + + Log.LogDebugTaskItems (" [Output] ResolvedAssemblies:", ResolvedAssemblies); + Log.LogDebugTaskItems (" [Output] ResolvedUserAssemblies:", ResolvedUserAssemblies); + Log.LogDebugTaskItems (" [Output] ResolvedFrameworkAssemblies:", ResolvedFrameworkAssemblies); + Log.LogDebugTaskItems (" [Output] ResolvedDoNotPackageAttributes:", ResolvedDoNotPackageAttributes); + + return true; + } + + readonly List do_not_package_atts = new List (); + int indent = 2; + + private void AddAssemblyReferences (ICollection assemblies, AssemblyDefinition assembly, bool topLevel) + { + var fqname = assembly.MainModule.FullyQualifiedName; + + // Don't repeat assemblies we've already done + if (!topLevel && assemblies.Contains (fqname)) + return; + + foreach (var att in assembly.CustomAttributes.Where (a => a.AttributeType.FullName == "Java.Interop.DoNotPackageAttribute")) { + string file = (string) att.ConstructorArguments.First ().Value; + if (string.IsNullOrWhiteSpace (file)) + Log.LogError ("In referenced assembly {0}, Java.Interop.DoNotPackageAttribute requires non-null file name.", assembly.FullName); + do_not_package_atts.Add (Path.GetFileName (file)); + } + + Log.LogMessage (MessageImportance.Low, "{0}Adding assembly reference for {1}, recursively...", new string (' ', indent), assembly.Name); + indent += 2; + // Add this assembly + if (!topLevel && assemblies.All (a => new AssemblyNameDefinition (a, null).Name != assembly.Name.Name)) + assemblies.Add (fqname); + + // Recurse into each referenced assembly + foreach (AssemblyNameReference reference in assembly.MainModule.AssemblyReferences) { + var reference_assembly = resolver.Resolve (reference); + AddAssemblyReferences (assemblies, reference_assembly, false); + } + indent -= 2; + } + + static LinkModes ParseLinkMode (string linkmode) + { + if (string.IsNullOrWhiteSpace (linkmode)) + return LinkModes.SdkOnly; + + LinkModes mode = LinkModes.SdkOnly; + + Enum.TryParse (linkmode.Trim (), true, out mode); + + return mode; + } + + private void AddI18nAssemblies (ICollection assemblies) + { + var i18n = Linker.ParseI18nAssemblies (I18nAssemblies); + var link = ParseLinkMode (LinkMode); + + // Check if we should add any I18N assemblies + if (i18n == Mono.Linker.I18nAssemblies.None) + return; + + assemblies.Add (ResolveI18nAssembly ("I18N")); + + if (i18n.HasFlag (Mono.Linker.I18nAssemblies.CJK)) + assemblies.Add (ResolveI18nAssembly ("I18N.CJK")); + + if (i18n.HasFlag (Mono.Linker.I18nAssemblies.MidEast)) + assemblies.Add (ResolveI18nAssembly ("I18N.MidEast")); + + if (i18n.HasFlag (Mono.Linker.I18nAssemblies.Other)) + assemblies.Add (ResolveI18nAssembly ("I18N.Other")); + + if (i18n.HasFlag (Mono.Linker.I18nAssemblies.Rare)) + assemblies.Add (ResolveI18nAssembly ("I18N.Rare")); + + if (i18n.HasFlag (Mono.Linker.I18nAssemblies.West)) + assemblies.Add (ResolveI18nAssembly ("I18N.West")); + } + + private string ResolveI18nAssembly (string name) + { + var assembly = resolver.Resolve (AssemblyNameReference.Parse (name)); + return assembly.MainModule.FullyQualifiedName; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs new file mode 100644 index 00000000000..f29ca15268f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveLibraryProjectImports.cs @@ -0,0 +1,268 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Mono.Cecil; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Ionic.Zip; + +using Java.Interop.Tools.Cecil; + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + public class ResolveLibraryProjectImports : Task + { + [Required] + public string ImportsDirectory { get; set; } + + [Required] + public string OutputDirectory { get; set; } + + [Required] + public string OutputImportDirectory { get; set; } + + [Required] + public ITaskItem[] Assemblies { get; set; } + + [Required] + public bool UseShortFileNames { get; set; } + + public string CacheFile { get; set;} + + [Output] + public string [] Jars { get; set; } + + [Output] + public string [] ResolvedAssetDirectories { get; set; } + + [Output] + public ITaskItem [] ResolvedResourceDirectories { get; set; } + + [Output] + public string [] ResolvedEnvironmentFiles { get; set; } + + [Output] + public ITaskItem [] ResolvedResourceDirectoryStamps { get; set; } + + string imports_dir = "library_project_imports"; + + public ResolveLibraryProjectImports () + { + } + + // Extracts library project contents under e.g. obj/Debug/[__library_projects__/*.jar | res/*/*] + public override bool Execute () + { + Log.LogDebugMessage ("ResolveLibraryProjectImports Task"); + Log.LogDebugMessage (" ImportsDirectory: {0}", ImportsDirectory); + Log.LogDebugMessage (" OutputDirectory: {0}", OutputDirectory); + Log.LogDebugMessage (" OutputImportDirectory: {0}", OutputImportDirectory); + Log.LogDebugMessage (" UseShortFileNames: {0}", UseShortFileNames); + Log.LogDebugTaskItems (" Assemblies: ", Assemblies); + + var jars = new List (); + var resolvedResourceDirectories = new List (); + var resolvedAssetDirectories = new List (); + var resolvedEnvironmentFiles = new List (); + Extract (jars, resolvedResourceDirectories, resolvedAssetDirectories, resolvedEnvironmentFiles); + + Jars = jars.ToArray (); + ResolvedResourceDirectories = resolvedResourceDirectories + .Select (s => new TaskItem (Path.GetFullPath (s))) + .ToArray (); + ResolvedAssetDirectories = resolvedAssetDirectories.ToArray (); + ResolvedEnvironmentFiles = resolvedEnvironmentFiles.ToArray (); + + ResolvedResourceDirectoryStamps = ResolvedResourceDirectories + .Select (s => new TaskItem (Path.GetFullPath (Path.Combine (s.ItemSpec, "../..")) + ".stamp")) + .ToArray (); + + foreach (var directory in ResolvedResourceDirectories) { + MonoAndroidHelper.SetDirectoryWriteable (directory.ItemSpec); + } + + foreach (var directory in ResolvedAssetDirectories) { + MonoAndroidHelper.SetDirectoryWriteable (directory); + } + + if (!string.IsNullOrEmpty (CacheFile)) { + var document = new XDocument ( + new XDeclaration ("1.0", "UTF-8", null), + new XElement ("Paths", + new XElement ("Jars", string.Join (";", Jars)), + new XElement ("ResolvedResourceDirectories", + ResolvedResourceDirectories.Select(e => new XElement ("ResolvedResourceDirectory", e))), + new XElement ("ResolvedAssetDirectories", + ResolvedAssetDirectories.Select(e => new XElement ("ResolvedAssetDirectory", e))), + new XElement ("ResolvedEnvironmentFiles", + ResolvedEnvironmentFiles.Select(e => new XElement ("ResolvedEnvironmentFile", e))), + new XElement ("ResolvedResourceDirectoryStamps", + ResolvedResourceDirectoryStamps.Select(e => new XElement ("ResolvedResourceDirectoryStamp", e))) + )); + document.Save (CacheFile); + } + + Log.LogDebugTaskItems (" Jars: ", Jars.Select (s => new TaskItem (s)).ToArray ()); + Log.LogDebugTaskItems (" ResolvedResourceDirectories: ", ResolvedResourceDirectories.Select (s => new TaskItem (s)).ToArray ()); + Log.LogDebugTaskItems (" ResolvedAssetDirectories: ", ResolvedAssetDirectories.Select (s => new TaskItem (s)).ToArray ()); + Log.LogDebugTaskItems (" ResolvedEnvironmentFiles: ", ResolvedEnvironmentFiles.Select (s => new TaskItem (s)).ToArray ()); + Log.LogDebugTaskItems (" ResolvedResourceDirectoryStamps: ", ResolvedResourceDirectoryStamps); + + return !Log.HasLoggedErrors; + } + + static string GetTargetAssembly (ITaskItem assname) + { + var suffix = assname.ItemSpec.EndsWith (".dll") ? String.Empty : ".dll"; + string hintPath = assname.GetMetadata ("HintPath").Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + string fileName = assname.ItemSpec + suffix; + if (!String.IsNullOrEmpty (hintPath) && !File.Exists (hintPath)) + hintPath = null; + string assPath = String.IsNullOrEmpty (hintPath) ? fileName : hintPath; + if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (Path.GetFileName (fileName))) + return null; + return Path.GetFullPath (assPath); + } + + // Extracts library project contents under e.g. obj/Debug/[__library_projects__/*.jar | res/*/*] + void Extract ( + ICollection jars, + ICollection resolvedResourceDirectories, + ICollection resolvedAssetDirectories, + ICollection resolvedEnvironments) + { + var outdir = new DirectoryInfo (OutputImportDirectory); + if (!outdir.Exists) + outdir.Create (); + + var res = new DirectoryAssemblyResolver (Log.LogWarning, loadDebugSymbols: false); + foreach (var ass in Assemblies) + res.Load (ass.ItemSpec); + + // FIXME: reorder references by import priority (not sure how to do that yet) + foreach (var assPath in Assemblies + .Select (a => GetTargetAssembly (a)) + .Where (a => a != null) + .Distinct ()) { + foreach (var imp in new string [] {imports_dir, "library_project_imports"}.Distinct ()) { + string assemblyIdentName = Path.GetFileNameWithoutExtension (assPath); + if (UseShortFileNames) { + assemblyIdentName = Xamarin.Android.Tasks.MonoAndroidHelper.GetLibraryImportDirectoryNameForAssembly (assemblyIdentName); + } + string outDirForDll = Path.Combine (OutputImportDirectory, assemblyIdentName); + string importsDir = Path.Combine (outDirForDll, imp); +#if SEPARATE_CRUNCH + // FIXME: review these binResDir thing and enable this. Eclipse does this. + // Enabling these blindly causes build failure on ActionBarSherlock. + //string binResDir = Path.Combine (importsDir, "bin", "res"); + //string binAssDir = Path.Combine (importsDir, "bin", "assets"); +#endif + string resDir = Path.Combine (importsDir, "res"); + string assDir = Path.Combine (importsDir, "assets"); + + // Skip already-extracted resources. + var stamp = new FileInfo (Path.Combine (outdir.FullName, assemblyIdentName + ".stamp")); + if (stamp.Exists && stamp.LastWriteTime > new FileInfo (assPath).LastWriteTime) { + Log.LogDebugMessage ("Skipped resource lookup for {0}: extracted files are up to date", assPath); +#if SEPARATE_CRUNCH + // FIXME: review these binResDir/binAssDir thing and enable this. Eclipse does this. + // Enabling these blindly causes build failure on ActionBarSherlock. + if (Directory.Exists (binResDir)) + resolvedResourceDirectories.Add (binResDir); + if (Directory.Exists (binAssDir)) + resolvedAssetDirectories.Add (binAssDir); +#endif + if (Directory.Exists (resDir)) + resolvedResourceDirectories.Add (resDir); + if (Directory.Exists (assDir)) + resolvedAssetDirectories.Add (assDir); + continue; + } + + if (Directory.Exists (outDirForDll)) + Directory.Delete (outDirForDll, true); + + Directory.CreateDirectory (importsDir); + + var ass = res.GetAssembly (assPath); + + foreach (var mod in ass.Modules) { + // android environment files + foreach (var envtxt in mod.Resources + .Where (r => r.Name.StartsWith ("__AndroidEnvironment__", StringComparison.OrdinalIgnoreCase)) + .Where (r => r is EmbeddedResource) + .Cast ()) { + if (!Directory.Exists (outDirForDll)) + Directory.CreateDirectory (outDirForDll); + var finfo = new FileInfo (Path.Combine (outDirForDll, envtxt.Name)); + using (var fs = finfo.Create ()) { + var data = envtxt.GetResourceData (); + fs.Write (data, 0, data.Length); + } + resolvedEnvironments.Add (finfo.FullName); + } + + // embedded jars (EmbeddedJar, EmbeddedReferenceJar) + var resjars = mod.Resources + .Where (r => r.Name.EndsWith (".jar", StringComparison.InvariantCultureIgnoreCase)) + .Select (r => (EmbeddedResource) r); + foreach (var resjar in resjars) { + var data = resjar.GetResourceData (); + using (var outfs = File.Create (Path.Combine (importsDir, resjar.Name))) + outfs.Write (data, 0, data.Length); + } + + // embedded AndroidResourceLibrary archive + var reszip = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidLibraryProjects__.zip") as EmbeddedResource; + if (reszip != null) { + if (!Directory.Exists (outDirForDll)) + Directory.CreateDirectory (outDirForDll); + var finfo = new FileInfo (Path.Combine (outDirForDll, reszip.Name)); + using (var fs = finfo.Create ()) { + var data = reszip.GetResourceData (); + fs.Write (data, 0, data.Length); + } + + // temporarily extracted directory will look like: + // __library_projects__/[dllname]/[library_project_imports | jlibs]/bin + using (var zip = new ZipFile (finfo.FullName)) { + Files.ExtractAll (zip, outDirForDll); + } + + // We used to *copy* the resources to overwrite other resources, + // which resulted in missing resource issue. + // Here we replaced copy with use of '-S' option and made it to work. +#if SEPARATE_CRUNCH + // FIXME: review these binResDir/binAssDir thing and enable this. Eclipse does this. + // Enabling these blindly causes build failure on ActionBarSherlock. + if (Directory.Exists (binResDir)) + resolvedResourceDirectories.Add (binResDir); + if (Directory.Exists (binAssDir)) + resolvedAssetDirectories.Add (binAssDir); +#endif + if (Directory.Exists (resDir)) + resolvedResourceDirectories.Add (resDir); + if (Directory.Exists (assDir)) + resolvedAssetDirectories.Add (assDir); + + finfo.Delete (); + } + } + + stamp.Create ().Close (); + } + } + + foreach (var f in outdir.GetFiles ("*.jar") + .Select (fi => fi.FullName)) + jars.Add (f); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs new file mode 100644 index 00000000000..cc51b0f76c9 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/ResolveSdksTask.cs @@ -0,0 +1,387 @@ +// +// ResolveSdksTask.cs +// +// Author: +// Michael Hutchinson +// Jonathan Pryor +// +// Copyright (c) 2010 Novell, Inc. +// Copyright (c) 2013 Xamarin Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using Xamarin.Android.Tools; +using System.Xml.Linq; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public class ResolveSdks : Task + { + [Output] + public string AndroidApiLevel { get; set; } + + [Output] + public string AndroidApiLevelName { get; set; } + + [Output] + public string SupportedApiLevel { get; set; } + + public string AndroidSdkBuildToolsVersion { get; set; } + + public string BuildingInsideVisualStudio { get; set; } + + public string ProjectFilePath { get; set; } + public bool UseLatestAndroidPlatformSdk { get; set; } + public bool AotAssemblies { get; set; } + + public string[] ReferenceAssemblyPaths { get; set; } + + public string CacheFile { get; set;} + + public string SequencePointsMode { get; set;} + + [Output] + public string TargetFrameworkVersion { get; set; } + + [Output] + public string MonoAndroidToolsPath { get; set; } + + [Output] + public string MonoAndroidBinPath { get; set; } + + [Output] + public string MonoAndroidIncludePath { get; set; } + + [Output] + public string AndroidNdkPath { get; set; } + + [Output] + public string AndroidSdkPath { get; set; } + + [Output] + public string JavaSdkPath { get; set; } + + [Output] + public string AndroidSdkBuildToolsPath { get; set; } + + [Output] + public string AndroidSdkBuildToolsBinPath { get; set; } + + [Output] + public string ZipAlignPath { get; set; } + + [Output] + public string AndroidSequencePointsMode { get; set; } + + static bool IsWindows = Path.DirectorySeparatorChar == '\\'; + static readonly string ZipAlign = IsWindows ? "zipalign.exe" : "zipalign"; + static readonly string Aapt = IsWindows ? "aapt.exe" : "aapt"; + static readonly string Android = IsWindows ? "android.bat" : "android"; + + + public override bool Execute () + { + Log.LogDebugMessage ("ResolveSdksTask:"); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugMessage (" AndroidSdkBuildToolsVersion: {0}", AndroidSdkBuildToolsVersion); + Log.LogDebugTaskItems (" ReferenceAssemblyPaths: ", ReferenceAssemblyPaths); + Log.LogDebugMessage (" TargetFrameworkVersion: {0}", TargetFrameworkVersion); + Log.LogDebugMessage (" UseLatestAndroidPlatformSdk: {0}", UseLatestAndroidPlatformSdk); + Log.LogDebugMessage (" SequencePointsMode: {0}", SequencePointsMode); + + MonoAndroidHelper.RefreshAndroidSdk (AndroidSdkPath, AndroidNdkPath, JavaSdkPath); + MonoAndroidHelper.RefreshMonoDroidSdk (MonoAndroidToolsPath, MonoAndroidBinPath, ReferenceAssemblyPaths); + + // OS X: $prefix/lib/mandroid + // Windows: %ProgramFiles(x86)%\MSBuild\Xamarin\Android + this.MonoAndroidToolsPath = MonoDroidSdk.RuntimePath; + + // OS X: $prefix/bin + // Windows: %ProgramFiles(x86)%\MSBuild\Xamarin\Android + this.MonoAndroidBinPath = MonoDroidSdk.BinPath; + + if (this.MonoAndroidBinPath == null) { + Log.LogCodedError ("XA0020", "Could not find mandroid!"); + return false; + } + + string include; + if (MonoAndroidToolsPath != null && + Directory.Exists (include = Path.Combine (MonoAndroidToolsPath, "include"))) + MonoAndroidIncludePath = include; + + this.AndroidNdkPath = AndroidSdk.AndroidNdkPath; + this.AndroidSdkPath = AndroidSdk.AndroidSdkPath; + this.JavaSdkPath = AndroidSdk.JavaSdkPath; + + if (string.IsNullOrEmpty (AndroidSdkPath)) { + Log.LogCodedError ("XA5205", "The Android SDK Directory could not be found. Please set via /p:AndroidSdkDirectory."); + return false; + } + + string toolsZipAlignPath = Path.Combine (AndroidSdkPath, "tools", ZipAlign); + bool findZipAlign = (string.IsNullOrEmpty (ZipAlignPath) || !Directory.Exists (ZipAlignPath)) && !File.Exists (toolsZipAlignPath); + + foreach (var dir in AndroidSdk.GetBuildToolsPaths (AndroidSdkBuildToolsVersion)) { + Log.LogDebugMessage ("Trying build-tools path: {0}", dir); + if (dir == null || !Directory.Exists (dir)) + continue; + + var toolsPaths = new string[] { + Path.Combine (dir), + Path.Combine (dir, "bin"), + }; + + string aapt = toolsPaths.FirstOrDefault (x => File.Exists (Path.Combine (x, Aapt))); + if (string.IsNullOrEmpty (aapt)) { + Log.LogDebugMessage ("Could not find `{0}`; tried: {1}", Aapt, + string.Join (";", toolsPaths.Select (x => Path.Combine (x, Aapt)))); + continue; + } + AndroidSdkBuildToolsPath = Path.GetFullPath (dir); + AndroidSdkBuildToolsBinPath = Path.GetFullPath (aapt); + + string zipalign = toolsPaths.FirstOrDefault (x => File.Exists (Path.Combine (x, ZipAlign))); + if (findZipAlign && string.IsNullOrEmpty (zipalign)) { + Log.LogDebugMessage ("Could not find `{0}`; tried: {1}", ZipAlign, + string.Join (";", toolsPaths.Select (x => Path.Combine (x, ZipAlign)))); + continue; + } + else + break; + } + + if (string.IsNullOrEmpty (AndroidSdkBuildToolsPath)) { + Log.LogCodedError ("XA5205", + string.Format ( + "Cannot find `{0}`. Please install the Android SDK Build-tools package with the `{1}{2}tools{2}{3}` program.", + Aapt, AndroidSdkPath, Path.DirectorySeparatorChar, Android)); + return false; + } + + if (string.IsNullOrEmpty (ZipAlignPath) || !Directory.Exists (ZipAlignPath)) { + ZipAlignPath = new[]{ + Path.Combine (AndroidSdkBuildToolsPath), + Path.Combine (AndroidSdkBuildToolsBinPath), + Path.Combine (AndroidSdkPath, "tools"), + } + .Where (p => File.Exists (Path.Combine (p, ZipAlign))) + .FirstOrDefault (); + } + if (string.IsNullOrEmpty (ZipAlignPath)) { + Log.LogCodedError ("XA5205", + string.Format ( + "Cannot find `{0}`. Please install the Android SDK Build-tools package with the `{1}{2}tools{2}{3}` program.", + ZipAlign, AndroidSdkPath, Path.DirectorySeparatorChar, Android)); + return false; + } + + if (!ValidateApiLevels ()) + return false; + + string frameworksPath = Path.GetDirectoryName (MonoDroidSdk.FrameworkPath); + if (!Directory.Exists (Path.Combine (frameworksPath, TargetFrameworkVersion))) { + Log.LogError ( + subcategory: string.Empty, + errorCode: "XA0001", + helpKeyword: string.Empty, + file: ProjectFilePath, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: "Unsupported or invalid $(TargetFrameworkVersion) value of '{0}'. Please update your Project Options.", + messageArgs: new[]{ + TargetFrameworkVersion, + } + ); + return false; + } + + SequencePointsMode mode; + if (!Aot.TryGetSequencePointsMode (SequencePointsMode ?? "None", out mode)) + Log.LogCodedError ("XA0104", "Invalid Sequence Point mode: {0}", SequencePointsMode); + AndroidSequencePointsMode = mode.ToString (); + + + AndroidApiLevelName = MonoAndroidHelper.GetPlatformApiLevelName (AndroidApiLevel); + + Log.LogDebugMessage ("ResolveSdksTask Outputs:"); + Log.LogDebugMessage (" AndroidApiLevel: {0}", AndroidApiLevel); + Log.LogDebugMessage (" AndroidApiLevelName: {0}", AndroidApiLevelName); + Log.LogDebugMessage (" AndroidNdkPath: {0}", AndroidNdkPath); + Log.LogDebugMessage (" AndroidSdkBuildToolsPath: {0}", AndroidSdkBuildToolsPath); + Log.LogDebugMessage (" AndroidSdkBuildToolsBinPath: {0}", AndroidSdkBuildToolsBinPath); + Log.LogDebugMessage (" AndroidSdkPath: {0}", AndroidSdkPath); + Log.LogDebugMessage (" JavaSdkPath: {0}", JavaSdkPath); + Log.LogDebugMessage (" MonoAndroidBinPath: {0}", MonoAndroidBinPath); + Log.LogDebugMessage (" MonoAndroidToolsPath: {0}", MonoAndroidToolsPath); + Log.LogDebugMessage (" MonoAndroidIncludePath: {0}", MonoAndroidIncludePath); + Log.LogDebugMessage (" TargetFrameworkVersion: {0}", TargetFrameworkVersion); + Log.LogDebugMessage (" ZipAlignPath: {0}", ZipAlignPath); + Log.LogDebugMessage (" SupportedApiLevel: {0}", SupportedApiLevel); + Log.LogDebugMessage (" AndroidSequencePointMode: {0}", AndroidSequencePointsMode); + + if (!string.IsNullOrEmpty (CacheFile)) { + Directory.CreateDirectory (Path.GetDirectoryName (CacheFile)); + + var document = new XDocument ( + new XDeclaration ("1.0", "UTF-8", null), + new XElement ("Sdk", + new XElement ("AndroidApiLevel", AndroidApiLevel), + new XElement ("AndroidApiLevelName", AndroidApiLevelName), + new XElement ("AndroidNdkPath", AndroidNdkPath), + new XElement ("AndroidSdkBuildToolsPath", AndroidSdkBuildToolsPath), + new XElement ("AndroidSdkBuildToolsBinPath", AndroidSdkBuildToolsBinPath), + new XElement ("AndroidSdkPath", AndroidSdkPath), + new XElement ("JavaSdkPath", JavaSdkPath), + new XElement ("MonoAndroidBinPath", MonoAndroidBinPath), + new XElement ("MonoAndroidToolsPath", MonoAndroidToolsPath), + new XElement ("ReferenceAssemblyPaths", + (ReferenceAssemblyPaths ?? new string [0]) + .Select(e => new XElement ("ReferenceAssemblyPath", e))), + new XElement ("TargetFrameworkVersion", TargetFrameworkVersion), + new XElement ("ZipAlignPath", ZipAlignPath), + new XElement ("MonoAndroidIncludePath", MonoAndroidIncludePath), + new XElement ("SupportedApiLevel", SupportedApiLevel), + new XElement ("AndroidSequencePointsMode", AndroidSequencePointsMode.ToString ()) + )); + document.Save (CacheFile); + } + + //note: this task does not error out if it doesn't find all things. that's the job of the targets + return !Log.HasLoggedErrors; + } + + bool ValidateApiLevels () + { + // Priority: + // $(UseLatestAndroidPlatformSdk) > $(AndroidApiLevel) > $(TargetFrameworkVersion) + // + // If $(TargetFrameworkVersion) isn't set, and $(AndroidApiLevel) isn't + // set, act as if $(UseLatestAndroidPlatformSdk) is True + // + // If $(UseLatestAndroidPlatformSdk) is true, we do as it says: use the + // latest installed version. + // + // Otherwise, if $(AndroidApiLevel) is set, use it and set $(TargetFrameworkVersion). + // Rationale: monodroid/samples/xbuild.make uses $(AndroidApiLevel) + // to build for a specific API level. + // Otherwise, if $(TargetFrameworkVersion) is set, use it and set $(AndroidApiLevel). + + UseLatestAndroidPlatformSdk = UseLatestAndroidPlatformSdk || + (string.IsNullOrWhiteSpace (AndroidApiLevel) && string.IsNullOrWhiteSpace (TargetFrameworkVersion)); + + if (UseLatestAndroidPlatformSdk) { + AndroidApiLevel = GetMaxInstalledApiLevel ().ToString (); + SupportedApiLevel = GetMaxSupportedApiLevel (AndroidApiLevel); + TargetFrameworkVersion = GetTargetFrameworkVersionFromApiLevel (); + return TargetFrameworkVersion != null; + } + + if (!string.IsNullOrWhiteSpace (AndroidApiLevel)) { + AndroidApiLevel = AndroidApiLevel.Trim (); + SupportedApiLevel = GetMaxSupportedApiLevel (AndroidApiLevel); + TargetFrameworkVersion = GetTargetFrameworkVersionFromApiLevel (); + return TargetFrameworkVersion != null; + } + + if (!string.IsNullOrWhiteSpace (TargetFrameworkVersion)) { + TargetFrameworkVersion = TargetFrameworkVersion.Trim (); + string apiLevel = MonoDroidSdk.GetApiLevelForFrameworkVersion (TargetFrameworkVersion); + if (apiLevel == null) { + Log.LogCodedError ("XA0000", + "Could not determine API level for $(TargetFrameworkVersion) of '{0}'.", + TargetFrameworkVersion); + return false; + } + AndroidApiLevel = apiLevel; + SupportedApiLevel = apiLevel; + return true; + } + Log.LogCodedError ("XA0000", "Could not determine $(AndroidApiLevel) or $(TargetFrameworkVersion); SHOULD NOT BE REACHED."); + return false; + } + + int GetMaxInstalledApiLevel () + { + string platformsDir = Path.Combine (AndroidSdkPath, "platforms"); + var apiLevels = Directory.EnumerateDirectories (platformsDir) + .Select (platformDir => Path.GetFileName (platformDir)) + .Where (dir => dir.StartsWith ("android-", StringComparison.OrdinalIgnoreCase)) + .Select (dir => dir.Substring ("android-".Length)) + .Select (apiName => MonoAndroidHelper.GetPlatformApiLevel (apiName)); + int maxApiLevel = int.MinValue; + foreach (var level in apiLevels) { + int v; + if (!int.TryParse (level, NumberStyles.Integer, CultureInfo.InvariantCulture, out v)) + continue; + maxApiLevel = Math.Max (maxApiLevel, v); + } + if (maxApiLevel < 0) + Log.LogCodedError ("XA5300", + "No Android platforms installed at '{0}'. Please install an SDK Platform with the `{1}{2}tools{2}{3}` program.", + platformsDir, Path.DirectorySeparatorChar, Android); + return maxApiLevel; + } + + string GetMaxSupportedApiLevel (string apiLevel) + { + int level = 0; + if (!int.TryParse (apiLevel, NumberStyles.Integer, CultureInfo.InvariantCulture, out level)) + return apiLevel; + if (ReferenceAssemblyPaths == null) + return apiLevel; + foreach (string versionedDir in ReferenceAssemblyPaths) { + string parent = Path.GetDirectoryName (versionedDir.TrimEnd (Path.DirectorySeparatorChar)); + for ( int l = level ; l > 0; l--) { + string tfv = MonoDroidSdk.GetFrameworkVersionForApiLevel (l.ToString ()); + if (tfv == null) + continue; + string dir = Path.Combine (parent, tfv); + if (Directory.Exists (dir)) + return l.ToString (); + } + } + return apiLevel; + } + + string GetTargetFrameworkVersionFromApiLevel () + { + string targetFramework = MonoDroidSdk.GetFrameworkVersionForApiLevel (SupportedApiLevel) ?? + MonoDroidSdk.GetFrameworkVersionForApiLevel (AndroidApiLevel); + if (targetFramework != null) + return targetFramework; + Log.LogCodedError ("XA0000", + "Could not determine $(TargetFrameworkVersion) for API level '{0}.'", + AndroidApiLevel); + return null; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/StripEmbeddedLibraries.cs b/src/Xamarin.Android.Build.Tasks/Tasks/StripEmbeddedLibraries.cs new file mode 100644 index 00000000000..fe9bd8e360a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Tasks/StripEmbeddedLibraries.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Xml; +using System.Xml.Linq; +using Mono.Cecil; +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; +using System.Text.RegularExpressions; +using Ionic.Zip; + +using Java.Interop.Tools.Cecil; + +namespace Xamarin.Android.Tasks +{ + public class StripEmbeddedLibraries : Task + { + [Required] + public ITaskItem[] Assemblies { get; set; } + + public StripEmbeddedLibraries () + { + } + + public override bool Execute () + { + Log.LogDebugMessage ("StripEmbeddedLibraries Task"); + Log.LogDebugTaskItems (" Assemblies: ", Assemblies); + + var res = new DirectoryAssemblyResolver (Log.LogWarning, true); + foreach (var ass in Assemblies) + res.Load (Path.GetFullPath (ass.ItemSpec)); + + foreach (var assname in Assemblies) { + var suffix = assname.ItemSpec.EndsWith (".dll") ? String.Empty : ".dll"; + string hintPath = assname.GetMetadata ("HintPath").Replace (Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); + string fileName = assname.ItemSpec + suffix; + if (!String.IsNullOrEmpty (hintPath) && !File.Exists (hintPath)) // ignore invalid HintPath + hintPath = null; + string assPath = String.IsNullOrEmpty (hintPath) ? fileName : hintPath; + if (MonoAndroidHelper.IsFrameworkAssembly (fileName) && !MonoAndroidHelper.FrameworkEmbeddedJarLookupTargets.Contains (Path.GetFileName (fileName))) + continue; + + var ass = res.GetAssembly (assPath); + bool ass_modified = false; + foreach (var mod in ass.Modules) { + // embedded jars + var resjars = mod.Resources.Where (r => r.Name.EndsWith (".jar", StringComparison.InvariantCultureIgnoreCase)).Select (r => (EmbeddedResource) r); + foreach (var resjar in resjars.ToArray ()) { + Log.LogDebugMessage (" Stripped {0}", resjar.Name); + mod.Resources.Remove (resjar); + ass_modified = true; + } + // embedded AndroidNativeLibrary archive + var nativezip = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidNativeLibraries__.zip") as EmbeddedResource; + if (nativezip != null) { + Log.LogDebugMessage (" Stripped {0}", nativezip.Name); + mod.Resources.Remove (nativezip); + ass_modified = true; + } + // embedded AndroidResourceLibrary archive + var reszip = mod.Resources.FirstOrDefault (r => r.Name == "__AndroidLibraryProjects__.zip") as EmbeddedResource; + if (reszip != null) { + Log.LogDebugMessage (" Stripped {0}", reszip.Name); + mod.Resources.Remove (reszip); + ass_modified = true; + } + } + if (ass_modified) { + Log.LogDebugMessage (" The stripped library is saved as {0}", assPath); + + // Output assembly needs to regenerate symbol file even if no IL/metadata was touched + // because Cecil still rewrites all assembly types in Cecil order (type A, nested types of A, type B, etc) + // and not in the original order causing symbols if original order doesn't match Cecil order + var wp = new WriterParameters () { + WriteSymbols = ass.MainModule.HasSymbols + }; + + ass.Write (assPath, wp); + } + } + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/AndroidAddOnManifest.cs b/src/Xamarin.Android.Build.Tasks/Utilities/AndroidAddOnManifest.cs new file mode 100644 index 00000000000..11dabc32fcd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/AndroidAddOnManifest.cs @@ -0,0 +1,87 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace Monodroid +{ + class AndroidAddOn { + internal AndroidAddOn (string apiLevel, string name, string jarPath) + { + ApiLevel = apiLevel; + Name = name; + JarPath = jarPath; + } + + public string ApiLevel {get; private set;} + public string Name {get; private set;} + public string JarPath {get; private set;} + } + + class AndroidAddOnManifest + { + public AndroidAddOnManifest (string addOnDirectory) + { + string manifestFile = Path.Combine (addOnDirectory, "manifest.ini"); + if (!File.Exists (manifestFile)) { + Console.Error.WriteLine ("monodroid.exe : uses-library warning 1 : Could not find manifest.ini for add-on {0}.", addOnDirectory); + return; + } + using (var manifest = File.OpenText (manifestFile)) { + List libs = new List (); + string line; + while ((line = manifest.ReadLine ()) != null) { + if (string.IsNullOrEmpty (line) || line.StartsWith ("#")) + continue; + string[] keyValue = line.Split (new char[]{'='}, 2); + if (keyValue.Length == 1) + continue; + string key; + switch ((key = keyValue [0].Trim ())) { + case "api": + ApiLevel = keyValue [1].Trim (); + break; + case "libraries": + libs.AddRange (keyValue [1].Split (';').Select (lib => lib.Trim ())); + break; + default: + // library name? + if (libs.Contains (key)) { + // example: com.google.android.maps=maps.jar;API for Google Maps + // keyValue[1] before ';' is filename + string filename = keyValue [1].Split (new []{';'}, 2) [0]; + string libPath = Path.Combine (Path.Combine (addOnDirectory, "libs"), filename); + if (File.Exists (libPath)) + libraries.Add (new AndroidAddOn (ApiLevel, key, libPath)); + else { + Console.Error.WriteLine ("monodroid.exe : manifest.ini warning 1: Could not find source for library '{0}'; tried file '{1}'.", + key, libPath); + } + } + break; + } + } + } + } + + public string ApiLevel {get; private set;} + + List libraries = new List(); + public IEnumerable Libraries { + get { + return libraries; + } + } + + public static IEnumerable GetAddOnManifests (string androidSdkPath) + { + string addOnsDir = Path.Combine (androidSdkPath, "add-ons"); + if (!Directory.Exists (addOnsDir)) + yield break; + foreach (var addon in Directory.GetDirectories (addOnsDir)) { + yield return new AndroidAddOnManifest (addon); + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/InvalidActivityNameException.cs b/src/Xamarin.Android.Build.Tasks/Utilities/InvalidActivityNameException.cs new file mode 100644 index 00000000000..f23bf6764c7 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/InvalidActivityNameException.cs @@ -0,0 +1,11 @@ +using System; + +namespace Xamarin.Android.Tasks +{ + class InvalidActivityNameException : Exception + { + public InvalidActivityNameException (string message) : base (message) + { + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/JavaResourceParser.cs b/src/Xamarin.Android.Build.Tasks/Utilities/JavaResourceParser.cs new file mode 100644 index 00000000000..eb45373fb22 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/JavaResourceParser.cs @@ -0,0 +1,159 @@ +using System; +using System.CodeDom; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text.RegularExpressions; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + class JavaResourceParser + { + public static TaskLoggingHelper Log { get; set; } + + public static CodeTypeDeclaration Parse (string file, bool isApp, Dictionary resourceMap) + { + if (!File.Exists (file)) + throw new InvalidOperationException ("Specified Java resource file was not found: " + file); + + CodeTypeDeclaration resources = null; + + using (var reader = File.OpenText (file)) { + string line; + + while ((line = reader.ReadLine ()) != null) { + var info = Parser.Select (p => new { Match = p.Key.Match (line), Handler = p.Value }).FirstOrDefault (x => x.Match.Success); + + if (info == null) + continue; + + resources = info.Handler (info.Match, isApp, resources, resourceMap); + } + } + + return resources; + } + + static KeyValuePair, CodeTypeDeclaration>> Parse (string regex, Func, CodeTypeDeclaration> f) + { + return new KeyValuePair, CodeTypeDeclaration>> (new Regex (regex), f); + } + + // public finall class R { + // public static fnal class string|anim|styleable|etc. { + // public static final int foobar = 0xZZ; + // public static final int [] foobar = { + // 0xXX, 0xYY, 0xZZ + // } + // } + // } + static List, CodeTypeDeclaration>>> Parser = new List, CodeTypeDeclaration>>> () { + Parse ("^public final class R {", + (m, app, _, map) => { + var decl = new CodeTypeDeclaration ("Resource") { + IsPartial = true, + }; + var asm = Assembly.GetExecutingAssembly().GetName(); + var codeAttrDecl = + new CodeAttributeDeclaration("System.CodeDom.Compiler.GeneratedCodeAttribute", + new CodeAttributeArgument( + new CodePrimitiveExpression(asm.Name)), + new CodeAttributeArgument( + new CodePrimitiveExpression(asm.Version.ToString())) + ); + decl.CustomAttributes.Add(codeAttrDecl); + return decl; + + }), + Parse ("^ public static final class ([^ ]+) {$", + (m, app, g, map) => { + var t = new CodeTypeDeclaration (GetNestedTypeName (m.Groups [1].Value)) { + IsPartial = true, + TypeAttributes = TypeAttributes.Public, + }; + t.Members.Add (new CodeConstructor () { + Attributes = MemberAttributes.Private, + }); + g.Members.Add (t); + return g; + }), + Parse (@"^ public static final int ([^ =]+)\s*=\s*([^;]+);$", + (m, app, g, map) => { + var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name; + var f = new CodeMemberField (typeof (int), GetResourceName (name, m.Groups[1].Value, map)) { + Attributes = app ? MemberAttributes.Const | MemberAttributes.Public : MemberAttributes.Static | MemberAttributes.Public, + InitExpression = new CodePrimitiveExpression (ToInt32 (m.Groups [2].Value, m.Groups [2].Value.IndexOf ("0x") == 0 ? 16 : 10)), + Comments = { + new CodeCommentStatement ("aapt resource value: " + m.Groups [2].Value), + }, + }; + ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Members.Add (f); + return g; + }), + Parse (@"^ public static final int\[\] ([^ =]+) = {", + (m, app, g, map) => { + var name = ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Name; + var f = new CodeMemberField (typeof (int[]), GetResourceName (name, m.Groups[1].Value, map)) { + // pity I can't make the member readonly... + Attributes = MemberAttributes.Public | MemberAttributes.Static, + }; + ((CodeTypeDeclaration) g.Members [g.Members.Count-1]).Members.Add (f); + return g; + }), + Parse (@"^ (0x[xa-fA-F0-9, ]+)$", + (m, app, g, map) => { + var t = (CodeTypeDeclaration) g.Members [g.Members.Count-1]; + var f = (CodeMemberField) t.Members [t.Members.Count-1]; + string[] values = m.Groups [1].Value.Split (new[]{','}, StringSplitOptions.RemoveEmptyEntries); + CodeArrayCreateExpression c = (CodeArrayCreateExpression) f.InitExpression; + if (c == null) { + f.InitExpression = c = new CodeArrayCreateExpression (typeof (int[])); + } + foreach (string value in values) + c.Initializers.Add (new CodePrimitiveExpression (ToInt32 (value.Trim (), 16))); + return g; + }), + }; + + internal static int ToInt32 (string value, int @base) + { + try { + return Convert.ToInt32 (value, @base); + } catch (Exception e) { + throw new NotSupportedException ( + string.Format ("Could not convert value '{0}' (base '{1}') into an Int32.", + value, @base), + e); + } + } + + internal static string GetNestedTypeName (string name) + { + switch (name) { + case "anim": return "Animation"; + case "attr": return "Attribute"; + case "bool": return "Boolean"; + case "dimen": return "Dimension"; + default: return char.ToUpperInvariant (name[0]) + name.Substring (1); + } + } + + static string GetResourceName (string type, string name, Dictionary map) + { + string mappedValue; + string key = string.Format ("{0}{1}{2}", type, Path.DirectorySeparatorChar, name).ToLowerInvariant (); + + if (map.TryGetValue (key, out mappedValue)) { + Log.LogMessage (" - Remapping resource: {0}.{1} -> {2}", type, name, mappedValue); + return mappedValue.Substring (mappedValue.LastIndexOf (Path.DirectorySeparatorChar) + 1); + } + + Log.LogMessage (" - Not remapping resource: {0}.{1}", type, name); + + return name; + } + + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MSBuildExtensions.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MSBuildExtensions.cs new file mode 100644 index 00000000000..07227636f58 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MSBuildExtensions.cs @@ -0,0 +1,141 @@ +using System; +using System.Diagnostics; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; + +using Microsoft.Build.Utilities; +using Microsoft.Build.Framework; + +namespace Xamarin.Android.Tasks +{ + public static class MSBuildExtensions + { + private static bool IsRunningInsideVS { + get { + var vside = false; + return bool.TryParse(Environment.GetEnvironmentVariable("VSIDE"), out vside) && vside; + } + } + + public static void LogDebugMessage (this TaskLoggingHelper log, string message, params object[] messageArgs) + { + log.LogMessage (MessageImportance.Low, message, messageArgs); + } + + public static void LogTaskItems (this TaskLoggingHelper log, string message, ITaskItem[] items) + { + log.LogMessage (message); + + if (items == null) + return; + + foreach (var item in items) + log.LogMessage (" {0}", item.ItemSpec); + } + + public static void LogTaskItems (this TaskLoggingHelper log, string message, params string[] items) + { + log.LogMessage (message); + + if (items == null) + return; + + foreach (var item in items) + log.LogMessage (" {0}", item); + } + + public static void LogDebugTaskItems (this TaskLoggingHelper log, string message, ITaskItem[] items) + { + log.LogMessage (MessageImportance.Low, message); + + if (items == null) + return; + + foreach (var item in items) + log.LogMessage (MessageImportance.Low, " {0}", item.ItemSpec); + } + + public static void LogDebugTaskItems (this TaskLoggingHelper log, string message, params string[] items) + { + log.LogMessage (MessageImportance.Low, message); + + if (items == null) + return; + + foreach (var item in items) + log.LogMessage (MessageImportance.Low, " {0}", item); + } + + // looking for: mandroid: warning XA9000: message... + static readonly Regex Message = new Regex ( + @"^(?[^: ]+)\s*:\s*(?warning|error) (?[^:]+): (?.*)"); + + public static void LogFromStandardError (this TaskLoggingHelper log, string message) + { + if (string.IsNullOrEmpty (message)) + return; + + var m = Message.Match (message); + if (!m.Success) + return; + + string subcategory = m.Groups ["source"].Value; + string type = m.Groups ["type"].Value; + string code = m.Groups ["code"].Value; + string msg = m.Groups ["message"].Value; + + if (type == "warning") + log.LogWarning (subcategory, code, string.Empty, string.Empty, 0, 0, 0, 0, "{0}", msg); + else + log.LogError (subcategory, code, string.Empty, string.Empty, 0, 0, 0, 0, "{0}", msg); + } + + public static void LogDebugTaskItemsAndLogical (this TaskLoggingHelper log, string message, ITaskItem[] items) + { + log.LogMessage (MessageImportance.Low, message); + + if (items == null) + return; + + foreach (var item in items) { + log.LogMessage (MessageImportance.Low, " {0}", item.ItemSpec); + log.LogMessage (MessageImportance.Low, " [{0}]", item.GetMetadata ("LogicalName")); + } + } + + public static void LogCodedError (this TaskLoggingHelper log, string code, string message, params object[] messageArgs) + { + log.LogError (string.Empty, code, string.Empty, string.Empty, 0, 0, 0, 0, message, messageArgs); + } + + public static void LogCodedWarning (this TaskLoggingHelper log, string code, string message, params object [] messageArgs) + { + log.LogWarning ( + subcategory: string.Empty, + warningCode: code, + helpKeyword: string.Empty, + file: string.Empty, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: message, + messageArgs: messageArgs); + } + + public static IEnumerable Concat (params ITaskItem[][] values) + { + if (values == null) + yield break; + foreach (ITaskItem[] v in values) { + if (v == null) + continue; + foreach (ITaskItem t in v) + yield return t; + } + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs new file mode 100644 index 00000000000..475a339b04f --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocument.cs @@ -0,0 +1,835 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Reflection; +using System.Text.RegularExpressions; +using System.Xml.Linq; +using System.Xml.XPath; +using Microsoft.Build.Utilities; + +using Android.App; +using Android.Content; + +using Mono.Cecil; + +using MonoDroid.Utils; +using Monodroid; + +using Java.Interop.Tools.Cecil; +using Java.Interop.Tools.TypeNameMappings; + +using System.Xml; +using System.Text; +using Xamarin.Android.Build.Utilities; + +namespace Xamarin.Android.Tasks { + + internal class ManifestDocument + { + public static XNamespace AndroidXmlNamespace = "http://schemas.android.com/apk/res/android"; + + static XNamespace androidNs = AndroidXmlNamespace; + + XDocument doc; + + XName attName; + + XElement app; + + // (element, android:name attribute value) which must ALL be present for + // the to be considered a launcher + static readonly Dictionary LauncherIntentElements = new Dictionary { + { "action", "android.intent.action.MAIN" }, + { "category", "android.intent.category.LAUNCHER" }, + }; + + public string PackageName { get; set; } + public List Addons { get; private set; } + public string ApplicationName { get; set; } + public List Assemblies { get; set; } + public DirectoryAssemblyResolver Resolver { get; set; } + public string SdkDir { get; set; } + public string SdkVersion { get; set; } + public bool Debug { get; set; } + public bool NeedsInternet { get; set; } + public string VersionCode { + get { + XAttribute attr = doc.Root.Attribute (androidNs + "versionCode"); + if (attr != null) { + string code = attr.Value; + if (!string.IsNullOrEmpty (code)) + return code; + } + return "1"; + } + set { + doc.Root.SetAttributeValue (androidNs + "versionCode", value); + } + } + TaskLoggingHelper log; + + public ManifestDocument (string templateFilename, TaskLoggingHelper log) : base () + { + this.log = log; + Addons = new List (); + Assemblies = new List (); + + attName = androidNs + "name"; + + if (!string.IsNullOrEmpty (templateFilename)) { + doc = XDocument.Load (templateFilename, LoadOptions.SetLineInfo); + AndroidResource.UpdateXmlResource (doc.Root); + } else { + doc = new XDocument (new XElement ("manifest")); + } + } + + string SdkVersionName { + get { return MonoAndroidHelper.GetPlatformApiLevelName (SdkVersion); } + } + + string ToFullyQualifiedName (string typeName) + { + if (typeName.StartsWith (".")) + return PackageName + typeName; + if (typeName.Contains (".")) + return typeName; + return PackageName + "." + typeName; + } + + XElement GetActivityWithName (XElement app, string name) + { + name = ToFullyQualifiedName (name); + return app.Elements ("activity").FirstOrDefault (e => ToFullyQualifiedName ((string) e.Attribute (androidNs + "name")) == name); + } + + void ReorderElements (XElement app) + { + var elements = app.ElementsAfterSelf ("uses-permission"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + elements = app.ElementsAfterSelf ("permission"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + elements = app.ElementsAfterSelf ("permissionGroup"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + elements = app.ElementsAfterSelf ("permissionTree"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + elements = app.ElementsAfterSelf ("uses-feature"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + elements = app.ElementsAfterSelf ("uses-library"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + elements = app.ElementsAfterSelf ("supports-gl-texture"); + foreach (var e in elements) { + e.Remove (); + app.AddBeforeSelf (e); + } + } + + void ReorderActivityAliases (XElement app) + { + var aliases = app.Elements ("activity-alias").ToList (); + + foreach (XElement alias in aliases) { + XAttribute attr = alias.Attribute (androidNs + "targetActivity"); + if (attr == null) + continue; + XElement activity = GetActivityWithName (app, attr.Value); + if (activity != null) { + alias.Remove (); + activity.AddAfterSelf (alias); + } else { + log.LogWarning ("unable to find target activity for activity alias: " + attr.Value); + } + } + } + + public IList Merge (List subclasses, List selectedWhitelistAssemblies, string applicationClass, bool embed, string bundledWearApplicationName, IEnumerable mergedManifestDocuments) + { + string applicationName = ApplicationName; + + var manifest = doc.Root; + + if (manifest == null || manifest.Name != "manifest") + throw new Exception ("Root element must be 'manifest'"); + + var manifest_package = (string) manifest.Attribute ("package"); + + if (!string.IsNullOrWhiteSpace (manifest_package)) + PackageName = manifest_package; + + manifest.SetAttributeValue (XNamespace.Xmlns + "android", "http://schemas.android.com/apk/res/android"); + if (manifest.Attribute (androidNs + "versionCode") == null) + manifest.SetAttributeValue (androidNs + "versionCode", "1"); + if (manifest.Attribute (androidNs + "versionName") == null) + manifest.SetAttributeValue (androidNs + "versionName", "1.0"); + + app = CreateApplicationElement (manifest, applicationClass, subclasses, selectedWhitelistAssemblies); + + if (app.Attribute (androidNs + "label") == null && applicationName != null) + app.SetAttributeValue (androidNs + "label", applicationName); + + var existingTypes = new HashSet ( + app.Descendants ().Select (a => (string) a.Attribute (attName)).Where (v => v != null)); + + if (!string.IsNullOrEmpty (bundledWearApplicationName)) { + if (!app.Elements ("meta-data").Any (e => e.Attributes (androidNs + "name").Any (a => a.Value == bundledWearApplicationName))) + app.Add (new XElement ("meta-data", new XAttribute (androidNs + "name", "com.google.android.wearable.beta.app"), new XAttribute (androidNs + "resource", "@xml/wearable_app_desc"))); + } + + // If no is specified, add it with both minSdkVersion and + // targetSdkVersion set to TargetFrameworkVersion + if (!manifest.Elements ("uses-sdk").Any ()) { + manifest.AddFirst ( + new XElement ("uses-sdk", + new XAttribute (androidNs + "minSdkVersion", SdkVersionName), + new XAttribute (androidNs + "targetSdkVersion", SdkVersionName))); + } + + // If no minSdkVersion is specified, set it to TargetFrameworkVersion + var uses = manifest.Element ("uses-sdk"); + + if (uses.Attribute (androidNs + "minSdkVersion") == null) { + int minSdkVersion; + if (!int.TryParse (SdkVersionName, out minSdkVersion)) + minSdkVersion = 11; + minSdkVersion = Math.Min (minSdkVersion, 11); + uses.SetAttributeValue (androidNs + "minSdkVersion", minSdkVersion.ToString ()); + } + + string targetSdkVersion; + var tsv = uses.Attribute (androidNs + "targetSdkVersion"); + if (tsv != null) + targetSdkVersion = tsv.Value; + else { + targetSdkVersion = SdkVersionName; + uses.AddBeforeSelf (new XComment ("suppress UsesMinSdkAttributes")); + } + + int targetSdkVersionValue; + if (!int.TryParse (MonoAndroidHelper.GetPlatformApiLevel (targetSdkVersion), out targetSdkVersionValue)) + throw new InvalidOperationException (string.Format ("The targetSdkVersion ({0}) is not a valid API level", targetSdkVersion)); + + foreach (var t in subclasses) { + if (t.IsAbstract) + continue; + + if (PackageName == null) + PackageName = t.Namespace; + + var name = JniType.ToJniName (t).Replace ('/', '.'); + var compatName = JniType.ToCompatJniName (t).Replace ('/', '.'); + if (((string) app.Attribute (attName)) == compatName) { + app.SetAttributeValue (attName, name); + } + + Func generator = GetGenerator (t); + if (generator == null) + continue; + + try { + // activity not present: create a launcher for it IFF it has attribute + if (!existingTypes.Contains (name) && !existingTypes.Contains (compatName)) { + XElement fromCode = generator (t, name, targetSdkVersionValue); + if (fromCode == null) + continue; + + if (!t.Methods.Where (m => m.IsConstructor).Cast ().Any (c => !c.HasParameters && c.IsPublic)) + throw new InvalidOperationException (string.Format ("The type '{0}' needs to have a public default constructor.", + t.FullName)); + app.Add (fromCode); + } + foreach (var d in app.Descendants ().Where (a => ((string) a.Attribute (attName)) == compatName)) { + d.SetAttributeValue (attName, name); + } + } catch (InvalidActivityNameException ex) { + log.LogErrorFromException (ex); + } + } + + var icon = app.Attribute (androidNs + "icon"); + if (icon == null) { + var activity = app.Element ("activity"); + if (activity != null) { + var activityIcon = activity.Attribute (androidNs + "icon"); + if (activityIcon != null) + app.Add (new XAttribute (androidNs + "icon", activityIcon.Value)); + } + } + + PackageName = AndroidAppManifest.CanonicalizePackageName (PackageName); + + if (!PackageName.Contains ('.')) + throw new InvalidOperationException ("/manifest/@package attribute MUST contain a period ('.')."); + + manifest.SetAttributeValue ("package", PackageName); + + var providerNames = AddMonoRuntimeProviders (app); + + if (Debug) { + app.Add (new XComment ("suppress ExportedReceiver")); + app.Add (new XElement ("receiver", + new XAttribute (androidNs + "name", "mono.android.Seppuku"), + new XElement ("intent-filter", + new XElement ("action", + new XAttribute (androidNs + "name", "mono.android.intent.action.SEPPUKU")), + new XElement ("category", + new XAttribute (androidNs + "name", "mono.android.intent.category.SEPPUKU." + PackageName))))); + if (app.Attribute (androidNs + "debuggable") == null) + app.Add (new XAttribute (androidNs + "debuggable", "true")); + } + if (Debug || NeedsInternet) + AddInternetPermissionForDebugger (); + + if (!embed) + AddFastDeployPermissions (); + + AddAddOns (app, SdkDir, SdkVersionName, Addons); + + // If the manifest has android:installLocation, but we are targeting + // API 7 or lower, remove it for the user and show a warning + if (manifest.Attribute (androidNs + "installLocation") != null) { + if (targetSdkVersionValue < 8) { + manifest.Attribute (androidNs + "installLocation").Remove (); + Console.Error.WriteLine ("monodroid: warning 1 : installLocation cannot be specified for Android versions less than 2.2. Attribute installLocation ignored."); + } + } + + AddInstrumentations (manifest, subclasses, targetSdkVersionValue); + AddPermissions (app, selectedWhitelistAssemblies); + AddPermissionGroups (app, selectedWhitelistAssemblies); + AddPermissionTrees (app, selectedWhitelistAssemblies); + AddUsesPermissions (app, selectedWhitelistAssemblies); + AddUsesFeatures (app, selectedWhitelistAssemblies); + AddSupportsGLTextures (app, selectedWhitelistAssemblies); + + ReorderActivityAliases (app); + ReorderElements (app); + + if (mergedManifestDocuments != null) { + foreach (var mergedManifest in mergedManifestDocuments) { + try { + MergeLibraryManifest (mergedManifest); + } catch (Exception ex) { + log.LogWarningFromException (ex); + } + } + } + + return providerNames; + } + + // FIXME: our manifest merger is hacky. + // To support complete manifest merger, we will have to implement fairly complicated one, described at + // http://tools.android.com/tech-docs/new-build-system/user-guide/manifest-merger + void MergeLibraryManifest (string mergedManifest) + { + var nsResolver = new XmlNamespaceManager (new NameTable ()); + nsResolver.AddNamespace ("android", androidNs.NamespaceName); + var xdoc = XDocument.Load (mergedManifest); + foreach (var top in xdoc.XPathSelectElements ("/manifest/*")) { + var name = top.Attribute (AndroidXmlNamespace.GetName ("name")); + var existing = (name != null) ? + doc.XPathSelectElement (string.Format ("/manifest/{0}[@android:name='{1}']", top.Name.LocalName, XmlConvert.VerifyNCName (name.Value)), nsResolver) : + doc.XPathSelectElement (string.Format ("/manifest/{0}", top.Name.LocalName)); + if (existing != null) + // if there is existing node with the same android:name, then append contents to existing node. + existing.Add (top.Nodes ()); + else + // otherwise, just add to the doc. + doc.Root.Add (top); + } + } + + Func GetGenerator (TypeDefinition type) + { + if (type.IsSubclassOf ("Android.App.Activity")) + return ActivityFromTypeDefinition; + if (type.IsSubclassOf ("Android.App.Service")) + return (t, name, v) => ToElement (t, name, ServiceAttribute.FromTypeDefinition, x => x.ToElement (PackageName)); + if (type.IsSubclassOf ("Android.Content.BroadcastReceiver")) + return (t, name, v) => ToElement (t, name, BroadcastReceiverAttribute.FromTypeDefinition, x => x.ToElement (PackageName)); + if (type.IsSubclassOf ("Android.Content.ContentProvider")) + return (t, name, v) => ToProviderElement (t, name); + return null; + } + + XElement CreateApplicationElement (XElement manifest, string applicationClass, List subclasses, List selectedWhitelistAssemblies) + { + var application = manifest.Descendants ("application").FirstOrDefault (); + + List assemblyAttr = + Assemblies.Select (path => ApplicationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))) + .Where (attr => attr != null) + .ToList (); + List metadata = + Assemblies.SelectMany (path => MetaDataAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))) + .Where (attr => attr != null) + .ToList (); + var usesLibraryAttr = + Assemblies.Concat (selectedWhitelistAssemblies).SelectMany (path => UsesLibraryAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))) + .Where (attr => attr != null); + if (assemblyAttr.Count > 1) + throw new InvalidOperationException ("There can be only one [assembly:Application] attribute defined."); + + List typeAttr = new List (); + List typeUsesLibraryAttr = new List (); + foreach (var t in subclasses) { + ApplicationAttribute aa = ApplicationAttribute.FromCustomAttributeProvider (t); + if (aa == null) + continue; + + if (!t.IsSubclassOf ("Android.App.Application")) + throw new InvalidOperationException (string.Format ("Found [Application] on type {0}. [Application] can only be used on subclasses of Application.", t.FullName)); + + typeAttr.Add (aa); + metadata.AddRange (MetaDataAttribute.FromCustomAttributeProvider (t)); + + typeUsesLibraryAttr.AddRange (UsesLibraryAttribute.FromCustomAttributeProvider (t)); + } + + if (typeAttr.Count > 1) + throw new InvalidOperationException ("There can be only one type with an [Application] attribute; found: " + + string.Join (", ", typeAttr.Select (aa => aa.Name).ToArray ())); + + if (assemblyAttr.Count > 0 && typeAttr.Count > 0) + throw new InvalidOperationException ("Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute."); + + ApplicationAttribute appAttr = assemblyAttr.SingleOrDefault () ?? typeAttr.SingleOrDefault (); + var ull1 = usesLibraryAttr ?? new UsesLibraryAttribute [0]; + var ull2 = typeUsesLibraryAttr.AsEnumerable () ?? new UsesLibraryAttribute [0]; + var usesLibraryAttrs = ull1.Concat (ull2); + bool needManifestAdd = true; + + if (appAttr != null) { + var newapp = appAttr.ToElement (Resolver, PackageName); + if (application == null) + application = newapp; + else { + needManifestAdd = false; + foreach (var n in newapp.Attributes ()) + application.SetAttributeValue (n.Name, n.Value); + foreach (var n in newapp.Nodes ()) + application.Add (n); + } + } + else if (application == null) + application = new XElement ("application"); + else + needManifestAdd = false; + application.Add (metadata.Select (md => md.ToElement (PackageName))); + + if (needManifestAdd) + manifest.Add (application); + + AddUsesLibraries (application, usesLibraryAttrs); + + if (applicationClass != null && application.Attribute (androidNs + "name") == null) + application.Add (new XAttribute (androidNs + "name", applicationClass)); + + if (application.Attribute (androidNs + "allowBackup") == null) + application.Add (new XAttribute (androidNs + "allowBackup", "true")); + + return application; + } + + IList AddMonoRuntimeProviders (XElement app) + { + app.Add (CreateMonoRuntimeProvider ("mono.MonoRuntimeProvider", null)); + + var providerNames = new List (); + + var processAttrName = androidNs.GetName ("process"); + var procs = new List (); + foreach (XElement el in app.Elements ()) { + var proc = el.Attribute (processAttrName); + if (proc == null) + continue; + if (procs.Contains (proc.Value)) + continue; + procs.Add (proc.Value); + if (el.Name.NamespaceName != String.Empty) + continue; + switch (el.Name.LocalName) { + case "provider": + var autho = el.Attribute (androidNs.GetName ("authorities")); + if (autho != null && autho.Value.EndsWith (".__mono_init__")) + continue; + goto case "activity"; + case "activity": + case "receiver": + case "service": + string providerName = "MonoRuntimeProvider_" + procs.Count; + providerNames.Add (providerName); + app.Add (CreateMonoRuntimeProvider ("mono." + providerName, proc.Value)); + break; + } + } + + return providerNames; + } + + XElement CreateMonoRuntimeProvider (string name, string processName) + { + return new XElement ("provider", + new XAttribute (androidNs + "name", name), + new XAttribute (androidNs + "exported", "false"), + new XAttribute (androidNs + "initOrder", int.MaxValue.ToString ()), + processName == null ? null : new XAttribute (androidNs + "process", processName), + new XAttribute (androidNs + "authorities", PackageName + "." + name + ".__mono_init__")); + } + + bool IsMainLauncher (XElement intentFilter) + { + return LauncherIntentElements.All (entry => + intentFilter.Elements (entry.Key).Any (e => ((string) e.Attribute (attName) == entry.Value))); + } + + XElement ActivityFromTypeDefinition (TypeDefinition type, string name, int targetSdkVersion) + { + if (name.StartsWith ("_")) + throw new InvalidActivityNameException (string.Format ("Activity name '{0}' is invalid, because activity namespaces may not begin with an underscore.", type.FullName)); + + return ToElement (type, name, + ActivityAttribute.FromTypeDefinition, + aa => aa.ToElement (Resolver, PackageName, targetSdkVersion), + (aa, element) => {if (aa.MainLauncher) AddLauncherIntentElements (element);}); + } + + XElement InstrumentationFromTypeDefinition (TypeDefinition type, string name, int targetSdkVersion) + { + return ToElement (type, name, + t => InstrumentationAttribute.FromCustomAttributeProvider (t).FirstOrDefault (), + ia => { + if (ia.TargetPackage == null) + ia.SetTargetPackage (PackageName); + return ia.ToElement (PackageName); + }); + } + + XElement ToElement (TypeDefinition type, string name, Func parser, Func toElement) + where TAttribute : class + { + return ToElement (type, name, parser, toElement, null); + } + + XElement ToElement (TypeDefinition type, string name, Func parser, Func toElement, Action update) + where TAttribute : class + { + TAttribute attr = parser (type); + if (attr == null) + return null; + + IEnumerable metadata = MetaDataAttribute.FromCustomAttributeProvider (type); + IEnumerable intents = IntentFilterAttribute.FromTypeDefinition (type); + + XElement element = toElement (attr); + if (element.Attribute (attName) == null) + element.Add (new XAttribute (attName, name)); + element.Add (metadata.Select (md => md.ToElement (PackageName))); + element.Add (intents.Select (intent => intent.ToElement (PackageName))); + if (update != null) + update (attr, element); + return element; + } + + XElement ToProviderElement (TypeDefinition type, string name) + { + var attr = ContentProviderAttribute.FromTypeDefinition (type); + if (attr == null) + return null; + + IEnumerable metadata = MetaDataAttribute.FromCustomAttributeProvider (type); + IEnumerable grants = GrantUriPermissionAttribute.FromTypeDefinition (type); + + XElement element = attr.ToElement (PackageName); + if (element.Attribute (attName) == null) + element.Add (new XAttribute (attName, name)); + element.Add (metadata.Select (md => md.ToElement (PackageName))); + element.Add (grants.Select (intent => intent.ToElement (PackageName))); + return element; + } + + void AddLauncherIntentElements (XElement activity) + { + if (activity.Elements ("intent-filter").Any (f => IsMainLauncher (f))) + return; + + var filter = new XElement ("intent-filter"); + activity.AddFirst (filter); + foreach (KeyValuePair e in LauncherIntentElements) { + if (!filter.Elements (e.Key).Any (x => ((string) x.Attribute (attName)) == e.Value)) + filter.Add (new XElement (e.Key, new XAttribute (attName, e.Value))); + } + } + + internal static void AddAddOns (XElement app, string sdkDir, string sdkVersionName, IList addonList) + { + List manifests = AndroidAddOnManifest.GetAddOnManifests (sdkDir).ToList (); + foreach (string library in app.Elements ("uses-library") + .Select (ul => { + var n = (string) ul.Attribute (androidNs + "name"); + return n != null ? n.Trim () : n; + }) + .Where (ul => !string.IsNullOrEmpty (ul))) { + AndroidAddOn addOn = GetAddOn (manifests, sdkVersionName, library); + // uses-library could be used to specify such library that does not exist in + // application or even on the host (even if "required" is true). The target + // may contain that library. "android.test.runner" is such an example. + if (addOn != null) + addonList.Add (addOn.JarPath); + } + } + + static AndroidAddOn GetAddOn (List manifests, string sdkVersionName, string libraryName) + { + // try exact match + AndroidAddOn addon = manifests.SelectMany (manifest => manifest.Libraries) + .Where (ao => ao.Name == libraryName && ao.ApiLevel == sdkVersionName) + .FirstOrDefault (); + if (addon != null) + return addon; + + // Try to grab an addon with level <= sdkVersion + // Requires that sdkVersion & AndroidAddOn.ApiLevel be convertible to System.Int32. + // + // So far preview L does not come up with google add-on, so it's OK to leave it unsupported. + int targetLevel; + if (!int.TryParse (sdkVersionName, out targetLevel)) { + return null; + } + int curLevel = int.MinValue; + foreach (AndroidAddOn attempt in manifests.SelectMany (manifest => manifest.Libraries) + .Where (ao => ao.Name == libraryName)) { + int level; + if (!int.TryParse (attempt.ApiLevel, out level)) + continue; + if (curLevel > level) + continue; + if (level > targetLevel) + continue; + curLevel = level; + addon = attempt; + } + return addon; + } + + public void AddInternetPermissionForDebugger () + { + const string permInternet ="android.permission.INTERNET"; + if (!doc.Root.Descendants ("uses-permission").Any (x => (string)x.Attribute (attName) == permInternet)) + app.AddBeforeSelf (new XElement ("uses-permission", new XAttribute (attName, permInternet))); + } + + public void AddFastDeployPermissions () + { + const string permReadExternalStorage ="android.permission.READ_EXTERNAL_STORAGE"; + if (!doc.Root.Descendants ("uses-permission").Any (x => (string)x.Attribute (attName) == permReadExternalStorage)) + app.AddBeforeSelf (new XElement ("uses-permission", new XAttribute (attName, permReadExternalStorage))); + } + + void AddPermissions (XElement application, List selectedWhitelistAssemblies) + { + // Look in user assemblies + whitelist (like Maps) + var check_assemblies = Assemblies.Union (selectedWhitelistAssemblies); + + var assemblyAttrs = + check_assemblies.SelectMany (path => PermissionAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + // Add unique permissions to the manifest + foreach (var pa in assemblyAttrs.Distinct (new PermissionAttribute.PermissionAttributeComparer ())) + if (!application.Parent.Descendants ("permission").Any (x => (string)x.Attribute (attName) == pa.Name)) + application.AddBeforeSelf (pa.ToElement (PackageName)); + } + + void AddPermissionGroups (XElement application, List selectedWhitelistAssemblies) + { + // Look in user assemblies + whitelist (like Maps) + var check_assemblies = Assemblies.Union (selectedWhitelistAssemblies); + + var assemblyAttrs = + check_assemblies.SelectMany (path => PermissionGroupAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + + // Add unique permissionGroups to the manifest + foreach (var pga in assemblyAttrs.Distinct (new PermissionGroupAttribute.PermissionGroupAttributeComparer ())) + if (!application.Parent.Descendants ("permissionGroup").Any (x => (string)x.Attribute (attName) == pga.Name)) + application.AddBeforeSelf (pga.ToElement (PackageName)); + } + + void AddPermissionTrees (XElement application, List selectedWhitelistAssemblies) + { + // Look in user assemblies + whitelist (like Maps) + var check_assemblies = Assemblies.Union (selectedWhitelistAssemblies); + + var assemblyAttrs = + check_assemblies.SelectMany (path => PermissionTreeAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + + // Add unique permissionGroups to the manifest + foreach (var pta in assemblyAttrs.Distinct (new PermissionTreeAttribute.PermissionTreeAttributeComparer ())) + if (!application.Parent.Descendants ("permissionTree").Any (x => (string)x.Attribute (attName) == pta.Name)) + application.AddBeforeSelf (pta.ToElement (PackageName)); + } + + void AddUsesPermissions (XElement application, List selectedWhitelistAssemblies) + { + // Look in user assemblies + whitelist (like Maps) + var check_assemblies = Assemblies.Union (selectedWhitelistAssemblies); + + var assemblyAttrs = + check_assemblies.SelectMany (path => UsesPermissionAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + + // Add unique permissions to the manifest + foreach (var upa in assemblyAttrs.Distinct (new UsesPermissionAttribute.UsesPermissionComparer ())) + if (!application.Parent.Descendants ("uses-permission").Any (x => (string)x.Attribute (attName) == upa.Name)) + application.AddBeforeSelf (upa.ToElement (PackageName)); + } + + void AddUsesLibraries (XElement application, IEnumerable libraries) + { + // Add unique libraries to the manifest + foreach (var ula in libraries) + if (!application.Descendants ("uses-library").Any (x => (string)x.Attribute (attName) == ula.Name)) + application.Add (ula.ToElement (PackageName)); + } + + void AddUsesFeatures (XElement application, List selectedWhitelistAssemblies) + { + // Look in user assemblies + whitelist (like Maps) + var check_assemblies = Assemblies.Union (selectedWhitelistAssemblies); + + var assemblyAttrs = + check_assemblies.SelectMany (path => UsesFeatureAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + + // Add unique features by Name or glESVersion to the manifest + foreach (var feature in assemblyAttrs) { + if (!string.IsNullOrEmpty(feature.Name) && feature.GLESVersion == 0) { + if (!application.Parent.Descendants ("uses-feature").Any (x => (string)x.Attribute (attName) == feature.Name)) { + application.AddBeforeSelf (feature.ToElement (PackageName)); + } + } + if (feature.GLESVersion != 0){ + if (!application.Parent.Descendants ("uses-feature").Any (x => (string)x.Attribute (androidNs+"glEsVersion") == feature.GLESVesionAsString())) { + application.AddBeforeSelf (feature.ToElement (PackageName)); + } + } + + } + } + + void AddSupportsGLTextures (XElement application, List selectedWhitelistAssemblies) + { + // Look in user assemblies + whitelist (like Maps) + var check_assemblies = Assemblies.Union (selectedWhitelistAssemblies); + + var assemblyAttrs = + check_assemblies.SelectMany (path => SupportsGLTextureAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + + // Add unique items by Name to the manifest + foreach (var feature in assemblyAttrs) { + if (!application.Parent.Descendants ("supports-gl-texture").Any (x => (string)x.Attribute (attName) == feature.Name)) { + application.AddBeforeSelf (feature.ToElement (PackageName)); + } + } + } + + void AddInstrumentations (XElement manifest, IList subclasses, int targetSdkVersion) + { + var assemblyAttrs = + Assemblies.SelectMany (path => InstrumentationAttribute.FromCustomAttributeProvider (Resolver.GetAssembly (path))); + + // Add instrumentation to the manifest + foreach (var ia in assemblyAttrs) { + if (ia.TargetPackage == null) + ia.SetTargetPackage (PackageName); + if (!manifest.Descendants ("instrumentation").Any (x => (string) x.Attribute (attName) == ia.Name)) + manifest.Add (ia.ToElement (PackageName)); + } + + foreach (var type in subclasses) + if (type.IsSubclassOf ("Android.App.Instrumentation")) { + var xe = InstrumentationFromTypeDefinition (type, JniType.ToJniName (type).Replace ('/', '.'), targetSdkVersion); + if (xe != null) + manifest.Add (xe); + } + } + + public void Save (string filename) + { + using (var file = new StreamWriter (filename, false, new UTF8Encoding (false))) + Save (file); + } + + public void Save (System.IO.TextWriter stream) + { + var ms = new MemoryStream (); + doc.Save (ms); + ms.Flush (); + ms.Position = 0; + var s = new StreamReader (ms).ReadToEnd (); + if (ApplicationName != null) + s = s.Replace ("${applicationId}", ApplicationName); + stream.Write (s); + } + + public string GetLaunchableActivityName () + { + var application = doc.Root.Descendants ("application").FirstOrDefault (); + var aName = androidNs + "name"; + foreach (var activity in application.Elements ("activity")) { + var filter = activity.Element ("intent-filter"); + if (filter != null) { + foreach (var category in filter.Elements ("category")) + if (category != null && (string)category.Attribute (aName) == "android.intent.category.LAUNCHER") + return (string) activity.Attribute (aName); + } + } + return null; + } + + static int GetAbiCode (string abi) + { + switch (abi) { + case "armeabi": + return 1; + case "armeabi-v7a": + return 2; + case "x86": + return 3; + case "arm64-v8a": + return 4; + case "x86_64": + return 5; + default: + throw new ArgumentOutOfRangeException ("abi", "unsupported ABI"); + } + } + + public void SetAbi (string abi) + { + int code = 1; + if (!string.IsNullOrEmpty (VersionCode)) { + code = Convert.ToInt32 (VersionCode); + if (code > 0xffff || code < 0) + throw new ArgumentOutOfRangeException ("VersionCode", "VersionCode is outside 0, 65535 interval"); + } + code |= GetAbiCode (abi) << 16; + VersionCode = code.ToString (); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocumentElement.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocumentElement.cs new file mode 100644 index 00000000000..71b5aa12d10 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/ManifestDocumentElement.cs @@ -0,0 +1,337 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Xml.Linq; + +using Mono.Cecil; + +using Monodroid; +using MonoDroid.Tuner; +using MonoDroid.Utils; + +using Java.Interop.Tools.Cecil; +using Java.Interop.Tools.TypeNameMappings; + +using Android.App; +using Android.Content.PM; +using Android.Views; + +using Xamarin.Android.Tasks; + +namespace Xamarin.Android.Manifest { + + class ManifestDocumentElement { + + public static string ToString (TypeDefinition typeDef) + { + return JniType.ToJniName (typeDef).Replace ('/', '.'); + } + + public static TypeDefinition ResolveType (string type, ICustomAttributeProvider provider, IAssemblyResolver resolver) + { + if (provider == null) + throw new ArgumentException ("Type resolution support requires an AssemblyDefinition or TypeDefinition.", "provider"); + if (resolver == null) + throw new ArgumentException ("Type resolution support requires a IAssemblyResolver.", "resolver"); + + // `type` is either a "bare" type "Foo.Bar", or an + // assembly-qualified type "Foo.Bar, AssemblyName [Version=...]?". + // + // Bare types are looked up via `provider`; assembly-qualified types are + // looked up via `resolver` + + int c = type.IndexOf (','); + string typeName = c < 0 ? type : type.Substring (0, c); + string assmName = c < 0 ? null : type.Substring (c+1); + + AssemblyDefinition assembly = assmName == null ? null : resolver.Resolve (assmName); + if (assembly == null) { + assembly = provider as AssemblyDefinition; + if (assembly == null) { + TypeDefinition decl = (TypeDefinition) provider; + assembly = decl.Module.Assembly; + } + } + var ret = assembly.Modules.Cast () + .Select (md => md.Types.FirstOrDefault (t => t.FullName == typeName)) + .FirstOrDefault (td => td != null); + if (ret == null) + throw new ArgumentException ("Type not found: " + type, "type"); + + return ret; + } + } + + class ManifestDocumentElement : ManifestDocumentElement, IEnumerable { + + public ManifestDocumentElement (string element) + { + Element = element; + } + + public readonly string Element; + + class MappingInfo { + public string AttributeName; + public Func Getter; + public Action Setter; + public Type MemberType; + public Func AttributeValue; + public Func AttributeValue2; + } + + readonly IDictionary Mappings = new Dictionary (); + + public void Add (string member, string attributeName, Func getter, Action setter, Type memberType = null) + { + Mappings.Add (member, new MappingInfo { + AttributeName = attributeName, + Getter = getter, + Setter = setter, + MemberType = memberType, + }); + } + + public void Add (string member, string attributeName, Action setter, Func attributeValue) + { + Mappings.Add (member, new MappingInfo { + AttributeName = attributeName, + Setter = setter, + AttributeValue = attributeValue, + }); + } + + public void Add (string member, string attributeName, Action setter, Func attributeValue) + { + Mappings.Add (member, new MappingInfo { + AttributeName = attributeName, + Setter = setter, + AttributeValue2 = attributeValue, + }); + } + + public ICollection Load (T value, CustomAttribute attribute) + { + if (attribute == null) + return null; + + var specified = new HashSet (); + + foreach (var e in attribute.Properties) { + specified.Add (e.Name); + var s = Mappings [e.Name].Setter; + if (s != null) + s (value, e.Argument.GetSettableValue ()); + } + + return specified; + } + + public XElement ToElement (T value, ICollection specified, string packageName, + ICustomAttributeProvider provider = null, IAssemblyResolver resolver = null, int targetSdkVersion = 0) + { + var r = new XElement (Element, + specified.OrderBy (e => e) + .Select (e => ToAttribute (e, value, packageName, provider, resolver, targetSdkVersion)) + .Where (a => a != null)); + AndroidResource.UpdateXmlResource (r); + return r; + } + + XAttribute ToAttribute (string name, T value, string packageName, + ICustomAttributeProvider provider, IAssemblyResolver resolver, int targetSdkVersion = 0) + { + if (!Mappings.ContainsKey (name)) + throw new ArgumentException ("Invalid attribute name: " + name); + var m = Mappings [name]; + if (m.AttributeName == null) + return null; + + string v = ToAttributeValue (name, value, provider, resolver,targetSdkVersion); + if (v == null) + return null; + v = v.Replace ("@PACKAGE_NAME@", packageName); + return new XAttribute (ManifestDocument.AndroidXmlNamespace + m.AttributeName, v); + } + + string ToAttributeValue (string name, T value, ICustomAttributeProvider provider, IAssemblyResolver resolver, int targetSdkVersion = 0) + { + var m = Mappings [name]; + if (m.AttributeValue != null) + return m.AttributeValue (value); + if (m.AttributeValue2 != null) + return m.AttributeValue2 (value, provider, resolver); + + if (m.Getter == null) + return null; + + var v = m.Getter (value); + if (v == null) + return null; + + var t = m.MemberType ?? v.GetType (); + var c = ValueConverters [t]; + return c (v, provider, resolver, targetSdkVersion); + } + + static readonly Dictionary> ValueConverters = new Dictionary> () { + { typeof (bool), (value, p, r, v) => ToString ((bool) value) }, + { typeof (int), (value, p, r, v) => value.ToString () }, + { typeof (string), (value, p, r, v) => value.ToString () }, + { typeof (ConfigChanges), (value, p, r, v) => ToString ((ConfigChanges) value) }, + { typeof (LaunchMode), (value, p, r, v) => ToString ((LaunchMode) value) }, + { typeof (Protection), (value, p, r, v) => ToString ((Protection) value) }, + { typeof (ScreenOrientation), (value, p, r, v) => ToString ((ScreenOrientation) value, v) }, + { typeof (SoftInput), (value, p, r, v) => ToString ((SoftInput) value) }, + { typeof (UiOptions), (value, p, r, v) => ToString ((UiOptions) value) }, + { typeof (Type), (value, p, r, v) => ToString (value.ToString (), p, r) }, + }; + + static string ToString (bool value) + { + return value ? "true" : "false"; + } + + static string ToString (ConfigChanges value) + { + var values = new List (); + if ((value & ConfigChanges.Density) == ConfigChanges.Density) + values.Add ("density"); + if ((value & ConfigChanges.FontScale) == ConfigChanges.FontScale) + values.Add ("fontScale"); + if ((value & ConfigChanges.Keyboard) == ConfigChanges.Keyboard) + values.Add ("keyboard"); + if ((value & ConfigChanges.KeyboardHidden) == ConfigChanges.KeyboardHidden) + values.Add ("keyboardHidden"); + if ((value & ConfigChanges.LayoutDirection) == ConfigChanges.LayoutDirection) + values.Add ("layoutDirection"); + if ((value & ConfigChanges.Locale) == ConfigChanges.Locale) + values.Add ("locale"); + if ((value & ConfigChanges.Mcc) == ConfigChanges.Mcc) + values.Add ("mcc"); + if ((value & ConfigChanges.Mnc) == ConfigChanges.Mnc) + values.Add ("mnc"); + if ((value & ConfigChanges.Navigation) == ConfigChanges.Navigation) + values.Add ("navigation"); + if ((value & ConfigChanges.Orientation) == ConfigChanges.Orientation) + values.Add ("orientation"); + if ((value & ConfigChanges.SmallestScreenSize) == ConfigChanges.ScreenSize) + values.Add ("smallestScreenSize"); + if ((value & ConfigChanges.ScreenLayout) == ConfigChanges.ScreenLayout) + values.Add ("screenLayout"); + if ((value & ConfigChanges.ScreenSize) == ConfigChanges.ScreenSize) + values.Add ("screenSize"); + if ((value & ConfigChanges.Touchscreen) == ConfigChanges.Touchscreen) + values.Add ("touchscreen"); + if ((value & ConfigChanges.UiMode) == ConfigChanges.UiMode) + values.Add ("uiMode"); + + return string.Join ("|", values.ToArray ()); + } + + static string ToString (LaunchMode value) + { + switch (value) { + case LaunchMode.Multiple: return "standard"; + case LaunchMode.SingleInstance: return "singleInstance"; + case LaunchMode.SingleTask: return "singleTask"; + case LaunchMode.SingleTop: return "singleTop"; + default: + throw new ArgumentException ("Unsupported LaunchMode value '" + value + "'.", "LaunchMode"); + } + } + + static string ToString (Protection value) + { + value = value & Protection.MaskBase; + switch (value) { + case Protection.Dangerous: return "dangerous"; + case Protection.Normal: return "normal"; + case Protection.Signature: return "signature"; + case Protection.SignatureOrSystem: return "signatureOrSystem"; + default: + throw new ArgumentException ("Unsupported Protection value '" + value + "'.", "LaunchMode"); + } + } + + static string ToString (ScreenOrientation value, int targetSdkVersion = 0) + { + switch (value) { + case ScreenOrientation.Behind: return "behind"; + case ScreenOrientation.FullSensor: return "fullSensor"; + case ScreenOrientation.FullUser: return "fullUser"; + case ScreenOrientation.Landscape: return "landscape"; + case ScreenOrientation.Locked: return "locked"; + case ScreenOrientation.Nosensor: return "nosensor"; + case ScreenOrientation.Portrait: return "portrait"; + case ScreenOrientation.ReverseLandscape: return "reverseLandscape"; + case ScreenOrientation.ReversePortrait: return "reversePortrait"; + case ScreenOrientation.Sensor: return "sensor"; + case ScreenOrientation.SensorLandscape: return "sensorLandscape"; + case ScreenOrientation.SensorPortrait: return targetSdkVersion < 16 ? "sensorPortait" : "sensorPortrait"; // https://bugzilla.xamarin.com/show_bug.cgi?id=12935 !! + case ScreenOrientation.Unspecified: return "unspecified"; + case ScreenOrientation.User: return "user"; + case ScreenOrientation.UserLandscape: return "userLandscape"; + case ScreenOrientation.UserPortrait: return "userPortrait"; + default: + throw new ArgumentException ("Unsupported ScreenOrientation value '" + value + "'.", "ScreenOrientation"); + } + } + + static string ToString (SoftInput value) + { + string stateValue; + switch (value & SoftInput.MaskState) { + case SoftInput.StateAlwaysHidden: stateValue = "stateAlwaysHidden"; break; + case SoftInput.StateAlwaysVisible: stateValue = "stateAlwaysVisible"; break; + case SoftInput.StateHidden: stateValue = "stateHidden"; break; + case SoftInput.StateUnchanged: stateValue = "stateUnchanged"; break; + case SoftInput.StateUnspecified: stateValue = "stateUnspecified"; break; + case SoftInput.StateVisible: stateValue = "stateVisible"; break; + default: + throw new ArgumentException ("Unsupported WindowSoftInputMode state value: " + + (value & SoftInput.MaskAdjust) + ".", "value"); + } + + string adjustValue; + switch (value & SoftInput.MaskAdjust) { + case SoftInput.AdjustNothing: adjustValue = "adjustNothing"; break; + case SoftInput.AdjustUnspecified: adjustValue = "adjustUnspecified"; break; + case SoftInput.AdjustResize: adjustValue = "adjustResize"; break; + case SoftInput.AdjustPan: adjustValue = "adjustPan"; break; + default: + throw new ArgumentException ("Unsupported WindowSoftInputMode adjust value: " + + (value & SoftInput.MaskAdjust) + ".", "value"); + } + + return stateValue + "|" + adjustValue; + } + + static string ToString (UiOptions value) + { + switch (value) { + case UiOptions.None: return "none"; + case UiOptions.SplitActionBarWhenNarrow: return "splitActionBarWhenNarrow"; + default: + throw new ArgumentException ("Unsupported UiOptions value '" + value + "'.", "LaunchMode"); + } + } + + static string ToString (string value, ICustomAttributeProvider provider, IAssemblyResolver resolver) + { + var typeDef = ResolveType (value, provider, resolver); + return ToString (typeDef); + } + + IEnumerator IEnumerable.GetEnumerator () + { + return Mappings.Keys.GetEnumerator (); + } + + IEnumerator IEnumerable.GetEnumerator () + { + return Mappings.Keys.GetEnumerator (); + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs new file mode 100644 index 00000000000..eeebfdf51cd --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/MonoAndroidHelper.cs @@ -0,0 +1,441 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using System.IO; +using System.Security.Cryptography; +using Ionic.Zip; +using Mono.Security.Cryptography; +using Xamarin.Android.Build.Utilities; + + +#if MSBUILD +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; +#endif + +using Xamarin.Android.Tools; + +namespace Xamarin.Android.Tasks +{ + public class MonoAndroidHelper + { + // Set in ResolveSdks.Execute(); + // Requires that ResolveSdks.Execute() run before anything else + public static string[] TargetFrameworkDirectories; + readonly static byte[] Utf8Preamble = System.Text.Encoding.UTF8.GetPreamble (); + + public static int RunProcess (string name, string args, DataReceivedEventHandler onOutput, DataReceivedEventHandler onError) + { + var psi = new ProcessStartInfo (name, args) { + UseShellExecute = false, + RedirectStandardOutput = true, + RedirectStandardError = true, + }; + Process p = new Process (); + p.StartInfo = psi; + + p.OutputDataReceived += onOutput; + p.ErrorDataReceived += onError; + p.Start (); + p.BeginErrorReadLine (); + p.BeginOutputReadLine (); + p.WaitForExit (); + try { + return p.ExitCode; + } finally { + p.Close (); + } + } + +#if MSBUILD + public static void RefreshAndroidSdk (string sdkPath, string ndkPath, string javaPath) + { + AndroidSdk.Refresh (sdkPath, ndkPath, javaPath); + } + + public static void RefreshMonoDroidSdk (string toolsPath, string binPath, string[] referenceAssemblyPaths) + { + MonoDroidSdk.Refresh (toolsPath, binPath, + (from refPath in referenceAssemblyPaths + where !string.IsNullOrEmpty (refPath) + let path = refPath.TrimEnd (Path.DirectorySeparatorChar) + where File.Exists (Path.Combine (path, "mscorlib.dll")) + select path) + .FirstOrDefault ()); + } +#endif // MSBUILD + + class SizeAndContentFileComparer : IEqualityComparer +#if MSBUILD + , IEqualityComparer +#endif // MSBUILD + { + public static readonly SizeAndContentFileComparer DefaultComparer = new SizeAndContentFileComparer (); + + public bool Equals (FileInfo x, FileInfo y) + { + if (x.Exists != y.Exists || x.Length != y.Length) + return false; + using (var f1 = File.OpenRead (x.FullName)) { + using (var f2 = File.OpenRead (y.FullName)) { + var b1 = new byte [0x1000]; + var b2 = new byte [0x1000]; + int total = 0; + while (total < x.Length) { + int size = f1.Read (b1, 0, b1.Length); + total += size; + f2.Read (b2, 0, b2.Length); + if (!b1.Take (size).SequenceEqual (b2.Take (size))) + return false; + } + } + } + return true; + } + + public int GetHashCode (FileInfo obj) + { + return (int) obj.Length; + } + +#if MSBUILD + public bool Equals (ITaskItem x, ITaskItem y) + { + return Equals (new FileInfo (x.ItemSpec), new FileInfo (y.ItemSpec)); + } + + public int GetHashCode (ITaskItem obj) + { + return GetHashCode (new FileInfo (obj.ItemSpec)); + } +#endif // MSBUILD + } + + internal static bool LogInternalExceptions { + get { + return string.Equals ( + "icanhaz", + Environment.GetEnvironmentVariable ("__XA_LOG_ERRORS__"), + StringComparison.OrdinalIgnoreCase); + } + } + +#if MSBUILD + public static IEnumerable ExpandFiles (ITaskItem[] libraryProjectJars) + { + libraryProjectJars = libraryProjectJars ?? new ITaskItem [0]; + return (from path in libraryProjectJars + let dir = Path.GetDirectoryName (path.ItemSpec) + let pattern = Path.GetFileName (path.ItemSpec) + where Directory.Exists (dir) + select Directory.GetFiles (dir, pattern)) + .SelectMany (paths => paths); + } + + public static IEnumerable DistinctFilesByContent (IEnumerable filePaths) + { + return filePaths.Distinct (MonoAndroidHelper.SizeAndContentFileComparer.DefaultComparer); + } +#endif + + public static IEnumerable DistinctFilesByContent (IEnumerable filePaths) + { + return filePaths.Select (p => new FileInfo (p)).ToArray ().Distinct (new MonoAndroidHelper.SizeAndContentFileComparer ()).Select (f => f.FullName).ToArray (); + } + + public static IEnumerable GetDuplicateFileNames (IEnumerable fullPaths, string [] excluded) + { + var files = fullPaths.Select (full => Path.GetFileName (full)).Where (f => excluded == null || !excluded.Contains (f, StringComparer.OrdinalIgnoreCase)).ToArray (); + for (int i = 0; i < files.Length; i++) + for (int j = i + 1; j < files.Length; j++) + if (String.Compare (files [i], files [j], StringComparison.OrdinalIgnoreCase) == 0) + yield return files [i]; + } + + public static bool IsEmbeddedReferenceJar (string jar) + { + return jar.StartsWith ("__reference__"); + } + + public static void LogWarning (object log, string msg, params object [] args) + { +#if MSBUILD + var helper = log as TaskLoggingHelper; + if (helper != null) { + helper.LogWarning (msg, args); + return; + } + var action = log as Action; + if (action != null) { + action (string.Format (msg, args)); + return; + } +#else + Console.Error.WriteLine (msg, args); +#endif + } + +#if MSBUILD + + static readonly string[] ValidAbis = new[]{ + "arm64-v8a", + "armeabi", + "armeabi-v7a", + "x86", + "x86_64", + }; + + public static string GetNativeLibraryAbi (string lib) + { + var dirs = lib.ToLowerInvariant ().Split ('/', '\\'); + + return ValidAbis.Where (p => dirs.Contains (p)).FirstOrDefault (); + } + + public static string GetNativeLibraryAbi (ITaskItem lib) + { + // If Abi is explicitly specified, simply return it. + var lib_abi = lib.GetMetadata ("Abi"); + + if (!string.IsNullOrWhiteSpace (lib_abi)) + return lib_abi; + + // Try to figure out what type of abi this is from the path + // First, try nominal "Link" path. + var link = lib.GetMetadata ("Link"); + if (!string.IsNullOrWhiteSpace (link)) { + var linkdirs = link.ToLowerInvariant ().Split ('/', '\\'); + lib_abi = ValidAbis.Where (p => linkdirs.Contains (p)).FirstOrDefault (); + } + + if (!string.IsNullOrWhiteSpace (lib_abi)) + return lib_abi; + + // If not resolved, use ItemSpec + return GetNativeLibraryAbi (lib.ItemSpec); + } +#endif + + public static bool IsFrameworkAssembly (string assembly) + { + return IsFrameworkAssembly (assembly, false); + } + + public static bool IsFrameworkAssembly (string assembly, bool checkSdkPath) + { + var ass = Path.GetFileName (assembly); + + if (Profile.SharedRuntimeAssemblies.Contains (ass, StringComparer.InvariantCultureIgnoreCase)) { +#if MSBUILD + bool treatAsUser = Array.BinarySearch (FrameworkAssembliesToTreatAsUserAssemblies, ass, StringComparer.OrdinalIgnoreCase) >= 0; + // Framework assemblies don't come from outside the SDK Path; + // user assemblies do + if (checkSdkPath && treatAsUser && TargetFrameworkDirectories != null) { + return TargetFrameworkDirectories + // TargetFrameworkDirectories will contain a "versioned" directory, + // e.g. $prefix/lib/xbuild-frameworks/MonoAndroid/v1.0. + // Trim off the version. + .Select (p => Path.GetDirectoryName (p.TrimEnd (Path.DirectorySeparatorChar))) + .Any (p => assembly.StartsWith (p)); + } +#endif + return true; + } + + return false; + } + + public static bool IsForceRetainedAssembly (string assembly) + { + switch (assembly) { + case "Mono.Android.Export.dll": // this is totally referenced by reflection. + return true; + } + return false; + } + +#if MSBUILD + public static void SetLastAccessAndWriteTimeUtc (string source, DateTime dateUtc, TaskLoggingHelper Log) + { + try { + File.SetLastWriteTimeUtc (source, dateUtc); + File.SetLastAccessTimeUtc (source, dateUtc); + } catch (Exception ex) { + Log.LogWarning ("There was a problem setting the Last Access/Write time on file {0}", source); + Log.LogWarningFromException (ex); + } + } +#endif // MSBUILD + + public static void SetWriteable (string source) + { + if (!File.Exists (source)) + return; + + var fileInfo = new FileInfo (source); + if (fileInfo.IsReadOnly) + fileInfo.IsReadOnly = false; + } + + public static void SetDirectoryWriteable (string directory) + { + if (!Directory.Exists (directory)) + return; + + var dirInfo = new DirectoryInfo (directory); + dirInfo.Attributes &= ~FileAttributes.ReadOnly; + + foreach (var dir in Directory.EnumerateDirectories (directory, "*", SearchOption.AllDirectories)) { + dirInfo = new DirectoryInfo (dir); + dirInfo.Attributes &= ~FileAttributes.ReadOnly; + } + + foreach (var file in Directory.EnumerateFiles (directory, "*", SearchOption.AllDirectories)) { + SetWriteable (Path.GetFullPath (file)); + } + } + + public static bool CopyIfChanged (string source, string destination) + { + return Files.CopyIfChanged (source, destination); + } + + public static bool CopyIfZipChanged (Stream source, string destination) + { + return Files.CopyIfZipChanged (source, destination); + } + + public static bool CopyIfZipChanged (string source, string destination) + { + return Files.CopyIfZipChanged (source, destination); + } + + public static ZipFile ReadZipFile (string filename) + { + return Files.ReadZipFile (filename); + } + + public static string HashFile (string filename) + { + return Files.HashFile (filename); + } + + public static string HashFile (string filename, HashAlgorithm hashAlg) + { + return Files.HashFile (filename, hashAlg); + } + + public static string HashStream (Stream stream) + { + return Files.HashStream (stream); + } + + /// + /// Open a file given its path and remove the 3 bytes UTF-8 BOM if there is one + /// + public static void CleanBOM (string filePath) + { + if (string.IsNullOrEmpty (filePath) || !File.Exists (filePath)) + return; + + string outputFilePath = null; + using (var input = File.OpenRead (filePath)) { + // Check if the file actually has a BOM + for (int i = 0; i < Utf8Preamble.Length; i++) { + var next = input.ReadByte (); + if (next == -1) + return; + if (Utf8Preamble [i] != (byte)next) + return; + } + + outputFilePath = Path.GetTempFileName (); + using (var tempOutput = File.OpenWrite (outputFilePath)) + input.CopyTo (tempOutput); + } + + CopyIfChanged (outputFilePath, filePath); + try { + File.Delete (outputFilePath); + } catch { + } + } + + public static bool IsRawResourcePath (string projectPath) + { + // Extract resource type folder name + var dir = Path.GetDirectoryName (projectPath); + var name = Path.GetFileName (dir); + + return string.Equals (name, "raw", StringComparison.OrdinalIgnoreCase) + || name.StartsWith ("raw-", StringComparison.OrdinalIgnoreCase); + } + +#if MSBUILD + internal static IEnumerable GetFrameworkAssembliesToTreatAsUserAssemblies (ITaskItem[] resolvedAssemblies) + { + return resolvedAssemblies + .Where (f => Array.BinarySearch (FrameworkAssembliesToTreatAsUserAssemblies, Path.GetFileName (f.ItemSpec), StringComparer.OrdinalIgnoreCase) >= 0) + .Select(p => p.ItemSpec); + } +#endif + + internal static readonly string [] FrameworkAttributeLookupTargets = {"Mono.Android.GoogleMaps.dll"}; + internal static readonly string [] FrameworkEmbeddedJarLookupTargets = { + "Mono.Android.Support.v13.dll", + "Mono.Android.Support.v4.dll", + "Xamarin.Android.NUnitLite.dll", // AndroidResources + }; + // MUST BE SORTED CASE-INSENSITIVE + internal static readonly string[] FrameworkAssembliesToTreatAsUserAssemblies = { + "Mono.Android.GoogleMaps.dll", + "Mono.Android.Support.v13.dll", + "Mono.Android.Support.v4.dll", + "Xamarin.Android.NUnitLite.dll", + }; + + // It used to replace "21" with "L" when it was preview, or "23" with "MNC" (ditto). + // We may have to use this in the future too. + public static string GetPlatformApiLevelName (string platformApiLevel) + { + switch (platformApiLevel.Trim ()) { + case "24": + return "N"; + default: + return platformApiLevel; + } + } + + // It used to replace "21" with "L" when it was preview, or "23" with "MNC" (ditto). + // We may have to use this in the future too. + public static string GetPlatformApiLevel (string platformApiLevelName) + { + switch (platformApiLevelName.Trim ()) { + case "N": + return "24"; + default: + return platformApiLevelName; + } + } + + public static string GetLibraryImportDirectoryNameForAssembly (string assemblyIdentName) + { + return string.Concat (new MD2Managed ().ComputeHash (Encoding.UTF8.GetBytes (assemblyIdentName)).Take (5).Select (b => b.ToString ("X02"))); + } + + public static Dictionary LoadAcwMapFile (string acwPath) + { + var acw_map = new Dictionary (); + if (!File.Exists (acwPath)) + return acw_map; + foreach (var s in File.ReadLines (acwPath)) { + var items = s.Split (';'); + if (!acw_map.ContainsKey (items [0])) + acw_map.Add (items [0], items [1]); + } + return acw_map; + } + } +} diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs b/src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs new file mode 100644 index 00000000000..c823253115b --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/Profile.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; + +namespace Xamarin.Android.Tasks +{ + public partial class Profile + { + public static readonly HashSet Sdk = new HashSet { + "mscorlib", + "System", + "System.Core", + "System.Data", + "System.EnterpriseServices", + "System.Net.Http", + "System.Runtime.Serialization", + "System.ServiceModel", + "System.ServiceModel.Web", + "System.Transactions", + "System.Web.Services", + "System.Xml", + "System.Xml.Linq", + "System.Json", + "System.Numerics", + "Microsoft.CSharp", + "Mono.CSharp", + "Mono.CompilerServices.SymbolWriter", + "Mono.Security", + "Mono.Data.Tds", + "Mono.Data.Sqlite", + }; + + // KEEP THIS SORTED ALPHABETICALLY, CASE-INSENSITIVE + public static readonly string[] ValidAbis = new[]{ + "arm64-v8a", + "armeabi", + "armeabi-v7a", + "x86", + "x86_64", + }; + + public static readonly string[] ValidProfilers = new[]{ + "log", + }; + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ResourceDesignerImportGenerator.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ResourceDesignerImportGenerator.cs new file mode 100644 index 00000000000..59e86e34aab --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/ResourceDesignerImportGenerator.cs @@ -0,0 +1,61 @@ +using System; +using System.CodeDom; +using System.Collections.Generic; +using System.Linq; +using Mono.Cecil; + +namespace Xamarin.Android.Tasks +{ + public class ResourceDesignerImportGenerator + { + CodeTypeDeclaration primary; + string primary_name; + + public ResourceDesignerImportGenerator (string ns, CodeTypeDeclaration applicationResourceDesigner) + { + this.primary = applicationResourceDesigner; + primary_name = ns + (ns.Length > 0 ? "." : "") + primary.Name; + } + + public void CreateImportMethods (IEnumerable libraries) + { + var method = new CodeMemberMethod () { Name = "UpdateIdValues", Attributes = MemberAttributes.Public | MemberAttributes.Static }; + primary.Members.Add (method); + foreach (var ass in libraries) { + var att = ass.CustomAttributes.FirstOrDefault ( + ca => ca.AttributeType.FullName == "Android.Runtime.ResourceDesignerAttribute"); + if (att == null) + continue; + if ((bool) att.Properties.First (p => p.Name == "IsApplication").Argument.Value == true) + continue; // application resource IDs are constants, cannot merge. + var td = ass.Modules.SelectMany (m => m.Types).FirstOrDefault ( + t => t.FullName == (string)att.ConstructorArguments.First ().Value); + + // F# has no nested types, so we need special care. + if (td.NestedTypes.Any ()) + CreateImportFor (true, td.NestedTypes, method); + else + CreateImportFor (false, ass.Modules.SelectMany (m => m.Types).Where (t => !td.Equals (t) && t.FullName.StartsWith (td.FullName, StringComparison.Ordinal)), method); + } + } + + void CreateImportFor (bool isNestedSrc, IEnumerable types, CodeMemberMethod method) + { + foreach (var type in types) { + // If the library was written in F#, those resource ID classes are not nested but rather combined with '_'. + var srcClassRef = new CodeTypeReferenceExpression ( + new CodeTypeReference (primary_name + (isNestedSrc ? '.' : '_') + type.Name, CodeTypeReferenceOptions.GlobalReference)); + // destination language may not support nested types, but they should take care of such types by themselves. + var dstClassRef = new CodeTypeReferenceExpression ( + new CodeTypeReference (type.FullName.Replace ('/', '.'), CodeTypeReferenceOptions.GlobalReference)); + foreach (var field in type.Fields) { + var dstField = new CodeFieldReferenceExpression (dstClassRef, field.Name); + var srcField = new CodeFieldReferenceExpression (srcClassRef, field.Name); + // This simply assigns field regardless of whether it is int or int[]. + method.Statements.Add (new CodeAssignStatement (dstField, srcField)); + } + } + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/ResourceMerger.cs b/src/Xamarin.Android.Build.Tasks/Utilities/ResourceMerger.cs new file mode 100644 index 00000000000..f25096b71d3 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/ResourceMerger.cs @@ -0,0 +1,296 @@ +using System; +using System.IO; +using System.Linq; +using System.Xml.Linq; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Microsoft.Build.Utilities; +using System.Runtime.Serialization; +using System.Xml.Serialization; + +namespace Xamarin.Android.Tasks +{ + [Serializable] + internal struct ResourceItem { + public string Source; + public string Destination; + public string Name; + public string ElementName; + public string Value; + } + + internal class ResourceMerger { + + Dictionary> sources = new Dictionary>(); + Dictionary resources = new Dictionary (); + Regex localizationRegEx = new Regex ("([vV]alues[-])"); + Regex regEx = new Regex ("([vV]alues)[-A-Za-z0-9_]*"); + const string keyFormat = "{0} (\"{1}\")"; + const SaveOptions saveOptions = SaveOptions.DisableFormatting; + + public ResourceMerger () + { + } + + internal void Load () + { + if (File.Exists (CacheFile)) { + var document = XDocument.Load (CacheFile); + foreach (var k in document.Element ("Keys").Elements ("Key")) { + var name = k.Attribute ("name").Value; + sources.Add (name, new List ()); + foreach ( var i in k.Element ("Values").Elements ("Value")) { + sources [name].Add (new ResourceItem () { + ElementName = i.Element ("ElementName").Value, + Name = i.Element ("Name").Value, + Destination = i.Element ("Destination").Value, + Source = i.Element ("Source").Value, + Value = i.Element ("Value").Value + }); + } + + } + } + } + + internal void Save () + { + var document = new XDocument ( + new XDeclaration ("1.0", "UTF-8", null), + new XElement ("Keys", sources.Select ( + x => new XElement ("Key", + new XAttribute ("name", x.Key), + new XElement ("Values", + x.Value.Select (v => + new XElement ("Value", + new XElement ("ElementName", v.ElementName), + new XElement ("Name", v.Name), + new XElement ("Source", v.Source), + new XElement ("Destination", v.Destination), + new XElement ("Value", new XCData (v.Value)) + ) + ) + ) + )) + ) + ); + document.Save (CacheFile, options: saveOptions); + } + + internal void RegisterResource(string src, string dest, XElement m) + { + var key = CalculateKey (m); + if (!sources.ContainsKey (key)) { + sources [key] = new List (); + } + var item = new ResourceItem () { + Source = src, + Destination = dest, + Value = m.Value, + Name = m.Attribute("name").Value, + ElementName = m.Name.ToString (), + }; + if (!resources.ContainsKey (key)) + resources.Add (key, item); + if (!sources [key].Any (x => x.Source == src)) { + // ignore localized resources e.g values-da as we will always get duplicates + if (!localizationRegEx.Match (src).Success) { + Log.LogDebugMessage ("Adding {0} from {1} {2} {3}", key, item.Source, item.Destination, item.Value); + sources [key].Add (item); + } + } + } + + internal void RemoveResourcesForFile(string filename) + { + var removed = sources.Keys.Where (x => !resources.ContainsKey (x) && sources[x].Any (d => d.Destination == filename )).ToArray(); + if (removed.Any ()) { + var doc = XDocument.Load (filename); + foreach (var r in removed) { + var r1 = doc.Root.Elements ().FirstOrDefault (x => CalculateKey (x) == r); + if (r1 != null) + r1.Remove (); + // clean up the "cache" + sources [r].RemoveAll (x => x.Destination == filename); + if (sources [r].Count == 0) + sources.Remove (r); + } + doc.Save (filename, SaveOptions.DisableFormatting); + } + } + + internal string CalculateKey (XElement x) + { + return !HasNameAttribute (x) ? x.Name.ToString() : string.Format (keyFormat, x.Name, x.Attribute ("name").Value); + } + + internal bool ElementsMatch (XElement src, XElement dst) + { + return src.Name == dst.Name && + HasNameAttribute (src) && + HasNameAttribute (dst) && + src.Attribute ("name").Value == dst.Attribute ("name").Value; + } + + internal bool HasNameAttribute (XElement src) + { + return src.Attribute ("name") != null; + } + + internal void MergeValues (string src, string dest) + { + Log.LogDebugMessage ("Attempting to merge {0} and {1}" ,src, dest); + + XDocument source = null, destination = null; + try { + source = XDocument.Load (src); + } catch (Exception ex) { + Log.LogErrorFromException (ex, showStackTrace: true, showDetail: true, file: src); + return; + } + try { + destination = File.Exists (dest) ? XDocument.Load (dest) : new XDocument (); + } catch (Exception ex) { + Log.LogErrorFromException (ex, showStackTrace: true, showDetail: true, file: src); + return; + } + + if (source.Root != null) { + var duplicates = source.Root.Elements () + .Where (x => source.Root.Elements ().Any (d => x != d && ElementsMatch(x, d))); + foreach (var d in duplicates) { + Log.LogError ( + subcategory: null, + errorCode: "XA5216", + helpKeyword: null, + file: src, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: "Resource entry {0} is already defined in {1}", + messageArgs: new[] { + d.Attribute ("name").Value, + src, + }); + } + + foreach (var m in source.Root.Elements ()) { + if (sources == null) + continue; + if (!HasNameAttribute(m)) + continue; + RegisterResource (src, dest, m); + } + } + + var nameElements = (destination.Root == null ? new XElement[0] : destination.Root.Elements ()) + .Where (x => HasNameAttribute(x)) + .Select (x => x); + + var toMerge = (source.Root == null ? new XElement[0] : source.Root.Elements ()) + .Where (x => !nameElements.Any (d => ElementsMatch (d, x) && d.Value == x.Value)); + + if (!toMerge.Any ()) { + Log.LogDebugMessage ("Skipped merging, nothing to merge"); + if (!File.Exists (dest)) { + Directory.CreateDirectory (Path.GetDirectoryName (dest)); + source.Save (dest, saveOptions); + } + return; + } + Log.LogDebugMessage ("Merging values document {0} into {1}", src, dest); + + var existing = from x in toMerge + let key = CalculateKey (x) + where this [key].Any (d => d.Value != x.Value || + (d.Value == x.Value && Path.GetFullPath (d.Source) != Path.GetFullPath (src))) + select x; + + if (!localizationRegEx.Match (src).Success && existing.Any ()) { + foreach (var e in existing) { + var key = CalculateKey (e); + var others = string.Join (" ", this[key].Select (x => x.Source)); + Log.LogWarning ( + subcategory: null, + warningCode: "XA5215", + helpKeyword: null, + file: src, + lineNumber: 0, + columnNumber: 0, + endLineNumber: 0, + endColumnNumber: 0, + message: "Duplicate Resource found for {0}. Duplicates are in {1}", + messageArgs: new[] { + e.Attribute ("name").Value, + others, + }); + + // load the other file. replace the value + var existingFile = this[key].First (); + Log.LogDebugMessage ("Loading {0}", existingFile.Destination); + var doc = existingFile.Destination != dest ? XDocument.Load (existingFile.Destination) : destination; + var exist = doc.Root == null ? null : doc.Root.Elements ().FirstOrDefault (x => ElementsMatch (x, e)); + if (exist != null && !string.IsNullOrEmpty (e.Value)) { + Log.LogDebugMessage ("Replacing {0} with {1} in {2}", exist.Value, e.Value, existingFile.Destination); + exist.Value = e.Value; + } + if (existingFile.Destination != dest) { + Log.LogDebugMessage ("Saving {0}", existingFile.Destination); + doc.Save (existingFile.Destination, saveOptions); + } + // remove this element from this document + e.Remove (); + } + } + + foreach (var e in toMerge) { + Log.LogDebugMessage ("Need to Merge {0} {1}", e.Name, e.Value); + var d = destination.Root == null ? null : destination.Root.Elements ().FirstOrDefault (x => ElementsMatch (x, e)); + if (d != null) { + d.Value = e.Value; + Log.LogDebugMessage ("Merged {0} {1}", e.Name, e.Value); + } + } + + if (destination.Root != null) { + toMerge = toMerge.Where (x => !existing.Any (d => ElementsMatch(d, x))); + destination.Root.Add (toMerge); + destination.Save (dest, saveOptions); + } else { + Directory.CreateDirectory (Path.GetDirectoryName (dest)); + source.Save (dest, saveOptions); + } + } + + internal TaskLoggingHelper Log { + get; + set; + } + + internal string CacheFile { + get; + set; + } + + internal IEnumerable Keys { + get { + return sources.Keys; + } + } + + internal List this[string key] { + get { + if (sources.ContainsKey (key)) + return sources [key]; + return new List ();; + } + } + + public bool NeedsMerge (string path) + { + return regEx.Match (path).Success; + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs b/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs new file mode 100644 index 00000000000..d05efb5ab68 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Utilities/XDocumentExtensions.cs @@ -0,0 +1,27 @@ +using System; +using System.Linq; +using System.Xml.Linq; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tasks +{ + public static class XDocumentExtensions + { + public static ITaskItem[] GetPathsAsTaskItems (this XDocument doc, params string[] paths) + { + return doc.GetPaths (paths) + .Select(x => new TaskItem(x)) + .ToArray (); + } + + public static string[] GetPaths (this XDocument doc, params string[] paths) + { + var e = doc.Elements ("Paths"); + foreach (var p in paths) + e = e.Elements (p); + return e.Select (p => p.Value).ToArray (); + } + } +} + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Analysis.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Analysis.targets new file mode 100644 index 00000000000..f6191f1afb0 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Analysis.targets @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + + + + + + + + + Android + + The minSdkVersion can not be less than 10 (2.3) when targeting arm64-v8a + + + Update the minSdkVersion in your app + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Bindings.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Bindings.targets new file mode 100644 index 00000000000..f9680d7a4f8 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Bindings.targets @@ -0,0 +1,567 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + MonoAndroid + v5.0 + v2.3 + False + jar2xml + + + + + + + + + + + + + + + + + + + True + _SetLatestTargetFrameworkVersion + + + + + + + $(IntermediateOutputPath)generated\ + $(IntermediateOutputPath)api.xml + <_GeneratorStampFile>$(IntermediateOutputPath)generator.stamp + None + + <_LibraryProjectImportsDirectoryName>library_project_imports + <_NativeLibraryImportsDirectoryName>native_library_imports + <_AndroidResourcePathsCache>$(IntermediateOutputPath)resourcepaths.cache + <_AndroidSdksCache>$(IntermediateOutputPath)sdks.cache + <_AndroidLibraryImportsCache>$(IntermediateOutputPath)libraryimports.cache + <_AndroidLibraryProjectImportsCache>$(IntermediateOutputPath)libraryprojectimports.cache + + + + True + False + + + Off + + + + + AddLibraryJarsToBind; + $(BuildDependsOn); + BuildDocumentation; + + + + $(ResolveReferencesDependsOn); + AddBindingsToCompile; + AddEmbeddedJarsAsResources; + AddEmbeddedReferenceJarsAsResources; + AddLibraryProjectsToCompile; + _AddNativeLibraryArchiveToCompile + + + + $(CleanDependsOn); + CleanBindingsOutput; + CleanLibraryProjectIntermediaries; + CleanNativeLibraryIntermediaries; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _ResolveMonoAndroidSdks + ;_GetAdditionalResourcesFromAssemblies + ;_ExtractLibraryProjectImports + ;_GetLibraryImports + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj new file mode 100644 index 00000000000..a14e1116838 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.csproj @@ -0,0 +1,638 @@ + + + + + + + Debug + AnyCPU + {3F1F2F50-AF1A-4A5A-BEDB-193372F068D7} + Library + Properties + Xamarin.Android.Tasks + Xamarin.Android.Build.Tasks + 512 + v4.5 + + + True + full + False + ..\..\bin\$(Configuration)\lib\xbuild\Xamarin\Android + TRACE;DEBUG;HAVE_CECIL;MSBUILD;JCW_ONLY_TYPE_NAMES + TRACE;DEBUG;HAVE_CECIL;MSBUILD + prompt + 4 + + + + True + ..\..\bin\$(Configuration)\lib\xbuild\Xamarin\Android + TRACE;HAVE_CECIL;MSBUILD;JCW_ONLY_TYPE_NAMES + prompt + 4 + + + $(DefineConstants);XA_NO_PREVIEW_L_SUPPORT + + + + + + + + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.dll + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Mdb.dll + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Pdb.dll + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net45\Mono.Cecil.Rocks.dll + + + ..\..\packages\Irony.0.9.1\lib\net40\Irony.dll + + + ..\..\packages\Unofficial.Ionic.Zip.1.9.1.8\lib\Ionic.Zip.dll + False + + + ..\..\packages\FSharp.Compiler.CodeDom.1.0.0.1\lib\net40\FSharp.Compiler.CodeDom.dll + False + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Linker\Mono.Tuner\AdjustVisibility.cs + + + Linker\Mono.Tuner\CecilRocks.cs + + + Linker\Mono.Tuner\CheckVisibility.cs + + + + + + PreserveNewest + Designer + + + PreserveNewest + + + + + + + + + + + + + + + + + + + Designer + PreserveNewest + + + Designer + PreserveNewest + + + PreserveNewest + + + PreserveNewest + + + + + + + + + + + + + + + PreserveNewest + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + pdb2mdb\BitAccess.cs + + + pdb2mdb\BitSet.cs + + + pdb2mdb\CvInfo.cs + + + pdb2mdb\DataStream.cs + + + pdb2mdb\DbiDbgHdr.cs + + + pdb2mdb\DbiHeader.cs + + + pdb2mdb\DbiModuleInfo.cs + + + pdb2mdb\DbiSecCon.cs + + + pdb2mdb\Driver.cs + + + pdb2mdb\IntHashTable.cs + + + pdb2mdb\Interfaces.cs + + + pdb2mdb\MsfDirectory.cs + + + pdb2mdb\PdbConstant.cs + + + pdb2mdb\PdbDebugException.cs + + + pdb2mdb\PdbException.cs + + + pdb2mdb\PdbFile.cs + + + pdb2mdb\PdbFileHeader.cs + + + pdb2mdb\PdbFunction.cs + + + pdb2mdb\PdbLine.cs + + + pdb2mdb\PdbLines.cs + + + pdb2mdb\PdbReader.cs + + + pdb2mdb\PdbScope.cs + + + pdb2mdb\PdbSlot.cs + + + pdb2mdb\PdbSource.cs + + + pdb2mdb\PdbWriter.cs + + + pdb2mdb\SourceLocationProvider.cs + + + + + + + + + + PreserveNewest + + + Linker\Mono.Linker\Annotations.cs + + + Linker\Mono.Linker\AssemblyAction.cs + + + Linker\Mono.Linker\AssemblyResolver.cs + + + Linker\Mono.Linker\I18nAssemblies.cs + + + Linker\Mono.Linker\IXApiVisitor.cs + + + Linker\Mono.Linker\LinkContext.cs + + + Linker\Mono.Linker\MethodAction.cs + + + Linker\Mono.Linker\Pipeline.cs + + + Linker\Mono.Linker\TypePreserve.cs + + + Linker\Mono.Linker\XApiReader.cs + + + Linker\Mono.Linker.Steps\BlacklistStep.cs + + + Linker\Mono.Linker.Steps\BaseStep.cs + + + Linker\Mono.Linker.Steps\CleanStep.cs + + + Linker\Mono.Linker.Steps\IStep.cs + + + Linker\Mono.Linker.Steps\LoadI18nAssemblies.cs + + + Linker\Mono.Linker.Steps\LoadReferencesStep.cs + + + Linker\Mono.Linker.Steps\OutputStep.cs + + + Linker\Mono.Linker.Steps\RegenerateGuidStep.cs + + + Linker\Mono.Linker.Steps\ResolveFromAssemblyStep.cs + + + Linker\Mono.Linker.Steps\ResolveFromXApiStep.cs + + + Linker\Mono.Linker.Steps\ResolveFromXmlStep.cs + + + Linker\Mono.Linker.Steps\ResolveStep.cs + + + Linker\Mono.Linker.Steps\TypeMapStep.cs + + + Linker\Mono.Linker.Steps\SweepStep.cs + + + Linker\Mono.Linker.Steps\MarkStep.cs + + + + + + + + + + + + + Utilities\AndroidResource.cs + + + Utilities\Features.cs + + + Utilities\LinePreservedXmlWriter.cs + + + Utilities\Files.cs + + + Linker\Mono.Tuner\ApplyPreserveAttributeBase.cs + + + Linker\Mono.Tuner\Dispatcher.cs + + + Linker\Mono.Tuner\CustomizeActions.cs + + + Linker\Mono.Tuner\Extensions.cs + + + Linker\Mono.Tuner\FixModuleFlags.cs + + + Linker\Mono.Tuner\PrintTypeMap.cs + + + Linker\Mono.Tuner\PreserveCrypto.cs + + + Linker\Mono.Tuner\PreserveSoapHttpClients.cs + + + Linker\Mono.Tuner\Profile.cs + + + Linker\Mono.Tuner\RemoveAttributesBase.cs + + + Linker\Mono.Tuner\RemoveResources.cs + + + Linker\Mono.Tuner\RemoveSecurity.cs + + + Mono.Android\IntentFilterAttribute.cs + + + Mono.Android\MetaDataAttribute.cs + + + Mono.Android\GrantUriPermissionAttribute.cs + + + Mono.Android\Android.Content.PM.LaunchMode.cs + + + Mono.Android\Android.Content.PM.ScreenOrientation.cs + + + Mono.Android\Android.Content.PM.ConfigChanges.cs + + + Mono.Android\Android.Content.PM.UiOptions.cs + + + Mono.Android\Android.Views.SoftInput.cs + + + Mono.Android\Android.Content.PM.Protection.cs + + + Mono.Android\Android.Views.LayoutDirection.cs + + + Mono.Android\IntDefinitionAttribute.cs + + + Utilities\StringRocks.cs + + + Linker\Mono.Tuner\FilterAttributes.cs + + + Linker\Mono.Tuner\InjectSecurityAttributes.cs + + + Linker\Mono.Tuner\PrintStatus.cs + + + Linker\Mono.Tuner\RemoveSerialization.cs + + + Linker\Mono.Tuner\TunerAnnotations.cs + + + Mono.Android\UsesLibraryAttribute.cs + + + Mono.Android\UsesPermissionAttribute.cs + + + Mono.Android\PermissionAttribute.cs + + + Mono.Android\PermissionGroupAttribute.cs + + + Mono.Android\PermissionTreeAttribute.cs + + + Mono.Android\UsesFeatureAttribute.cs + + + Mono.Android\SupportsGLTextureAttribute.cs + + + Mono.Android\JavaLibraryReferenceAttribute.cs + + + Mono.Android\IncludeAndroidResourcesFromAttribute.cs + + + Mono.Android\ReferenceFilesAttribute.cs + + + Mono.Android\NativeLibraryReferenceAttribute.cs + + + Utilities\MD2Managed.cs + + + + + + + + + + + + NOTICE.txt + + + machine.config + + + MonoPackageManager.java + + + Seppuku.java + + + mscorlib.xml + + + System.xml + + + System.Data.Services.Client.xml + + + System.ServiceModel.xml + + + OpenTK.xml + + + Mono.Android.xml + + + proguard_xamarin.cfg + + + MonoPackageManager.api4.java + + + NotifyTimeZoneChanges.java + + + ApplicationRegistration.java + + + IncrementalClassLoader.java + + + Placeholder.java + + + StubApplication.java + + + + + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44} + Xamarin.AndroidTools.Aidl + + + {64CC4E44-CE3A-4319-BF3F-6CF8BD513870} + Java.Interop.Tools.Diagnostics + + + {D48EE8D0-0A0A-4493-AEF5-DAF5F8CF86AD} + Java.Interop.Tools.Cecil + + + {D18FCF91-8876-48A0-A693-2DC1E7D3D80A} + Java.Interop.Tools.JavaCallableWrappers + + + {91713046-C358-4647-B162-ED4E1442F3D8} + Xamarin.Android.Build.Utilities + + + {B17475BC-45A2-47A3-B8FC-62F3A0959EE0} + Xamarin.Android.Tools.Bytecode + + + {66CF299A-CE95-4131-BCD8-DB66E30C4BF7} + Mono.Android + False + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets new file mode 100644 index 00000000000..01f6e13ab8a --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Build.Tasks.targets @@ -0,0 +1,23 @@ + + + + + + <_GeneratedProfileClass>$(IntermediateOutputPath)Profile.g.cs + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.CSharp.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.CSharp.targets new file mode 100644 index 00000000000..684a0025382 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.CSharp.targets @@ -0,0 +1,49 @@ + + + + + + + MonoAndroid + v2.3 + SdkOnly + + Off + <_AndroidResourceDesigner>Resource.Designer.cs + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets new file mode 100755 index 00000000000..1652f21a257 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets @@ -0,0 +1,2543 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(MSBuildThisFileDirectory)Xamarin.Android.Analysis.targets + + + + + + + + + + + + + + + + + _SetLatestTargetFrameworkVersion; + $(GetReferenceAssemblyPathsDependsOn) + + + + + + + + + + + + + + + + + + + + + + + + <_XAMajorVersionNumber>1 + false + False + True + False + MonoAndroid + v$(_XAMajorVersionNumber).0 + True + True + + + + Normal + True + False + False + + False + False + True + + False + + False + False + False + + extras\android\support\multidex\library\libs\android-support-multidex.jar + + armeabi-v7a + + + + + + False + False + + + True + False + + + Off + + 23.0.0 + + + Gdb + + + <_LibraryProjectImportsDirectoryName>library_project_imports + <_NativeLibraryImportsDirectoryName>native_library_imports + <_AndroidResourcePathsCache>$(IntermediateOutputPath)resourcepaths.cache + <_AndroidSdksCache>$(IntermediateOutputPath)sdks.cache + <_AndroidLibraryImportsCache>$(IntermediateOutputPath)libraryimports.cache + <_AndroidLibraryProjectImportsCache>$(IntermediateOutputPath)libraryprojectimports.cache + + + $(EnableProguard) + False + + + {sdk.dir}tools\proguard\proguard-android.txt; + {intermediate.common.xamarin}; + {intermediate.references}; + {intermediate.application}; + @(ProguardConfiguration); + + + <_AndroidMainDexListFile>$(IntermediateOutputPath)multidex.keep + + <_PackagedResources>$(IntermediateOutputPath)android\bin\packaged_resources + + <_Android32bitArchitectures>armeabi-v7a;armeabi;x86;mips + <_Android64bitArchitectures>arm64-v8a;x86_64;mips64 + + <_AndroidSequencePointsMode Condition=" '$(AndroidManagedSymbols)' == 'True' And '$(AndroidUseDebugRuntime)' == 'False' And '$(AotAssemblies)' == 'True' And '$(DebugSymbols)' == 'True' And '$(DebugType)' == 'PdbOnly'">Offline + <_AndroidSequencePointsMode Condition=" '$(AndroidManagedSymbols)' == 'True' And '$(AndroidUseDebugRuntime)' == 'False' And '$(_AndroidSequencePointsMode)' == ''">Normal + <_AndroidSequencePointsMode Condition=" '$(_AndroidSequencePointsMode)' == ''">None + <_InstantRunEnabled Condition=" '$(_InstantRunEnabled)' == '' ">False + + + + + + + True + + + + + True + + + + + False + + + + + + + + + + + + + + + + SignAndroidPackage + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(AllowedReferenceRelatedFileExtensions); + .dll.mdb; + .exe.mdb + + + + + + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + _SetupDesignTimeBuildForBuild; + _CreatePropertiesCache; + _CheckProjectItems; + _CheckForContent; + _CheckTargetFramework; + _RemoveLegacyDesigner; + $(BuildDependsOn); + _CompileDex; + + + + + + _SetupDesignTimeBuildForBuild; + _CreatePropertiesCache; + _AddAndroidDefines; + _AddNativeLibraryArchiveToCompile; + _AddAndroidEnvironmentToCompile; + _CheckForContent; + _CheckTargetFramework; + _RemoveLegacyDesigner; + $(BuildDependsOn); + + + + + + + _SeparateAppExtensionReferences; + _PrepareWearApplication; + $(ResolveReferencesDependsOn); + + + $(CoreResolveReferencesDependsOn); + UpdateAndroidAssets; + UpdateAndroidInterfaceProxies; + UpdateAndroidResources; + $(ApplicationResolveReferencesDependsOn); + + + + + + $(PrepareForRunDependsOn); + + + + + + $(PrepareForRunDependsOn); + _CollectMonoAndroidOutputs; + + + + + + $(CleanDependsOn); + _CleanMonoAndroidIntermediateDir; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android.support.multidex.MultiDexApplication + android.app.Application + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_UploadFlagFile>$(IntermediateOutputPath)uploadflags.txt + + + + + + + + + + + + + + + + + + + + + + + + + + $(IntermediateOutputPath)assets\ + Assets + + + + + + + + + + + <_AdditionalAndroidResourceCachePaths Include="@(_AdditionalAndroidResourcePaths)" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(IntermediateOutputPath)res\ + $(IntermediateOutputPath)android\assets\ + $(IntermediateOutputPath)linksrc\ + $(IntermediateOutputPath)android\assets\ + $(IntermediateOutputPath)linkdst\ + Resources + $(IntermediateOutputPath) + $(IntermediateOutputPath)resourcecache + <_AndroidAotBinDirectory>$(IntermediateOutputPath)aot + <_AndroidResgenFlagFile>$(IntermediateOutputPath)R.cs.flag + <_AndroidComponentResgenFlagFile>$(IntermediateOutputPath)Component.R.cs.flag + <_AndroidStripFlag>$(IntermediateOutputPath)strip.flag + <_AndroidLinkFlag>$(IntermediateOutputPath)link.flag + <_AndroidApkPerAbiFlagFile>$(IntermediateOutputPath)android\bin\apk_per_abi.flag + <_AndroidDebugKeyStoreFlag>$(IntermediateOutputPath)android_debug_keystore.flag + <_RemoveRegisterFlag>$(MonoAndroidIntermediateAssetsDir)shrunk\shrunk.flag + <_AcwMapFile>$(IntermediateOutputPath)acw-map.txt + <_AndroidTypeMappingJavaToManaged>$(IntermediateOutputPath)android\typemap.jm + <_AndroidTypeMappingManagedToJava>$(IntermediateOutputPath)android\typemap.mj + $(RootNamespace) + <_AndroidLintConfigFile>$(IntermediateOutputPath)lint.xml + <_AndroidResourceDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' == 'True' ">$(IntermediateOutputPath)$(_AndroidResourceDesigner) + <_AndroidResourceDesignerFile Condition=" '$(AndroidUseIntermediateDesignerFile)' != 'True' ">$(AndroidResgenFile) + <_AndroidBuildPropertiesCache>$(IntermediateOutputPath)build.props + <_AndroidStaticResourcesFlag>$(IntermediateOutputPath)static.flag + <_AndroidResourcesCacheFile>$(IntermediateOutputPath)mergeresources.cache + + + + + <_PropertyCacheItems Include="BundleAssemblies=$(BundleAssemblies)" /> + <_PropertyCacheItems Include="AotAssemblies=$(AotAssemblies)" /> + <_PropertyCacheItems Include="AndroidAotMode=$(AndroidAotMode)" /> + <_PropertyCacheItems Include="ExplicitCrunch=$(AndroidExplicitCrunch)" /> + <_PropertyCacheItems Include="EnableProguard=$(AndroidEnableProguard)" /> + <_PropertyCacheItems Include="UseSharedRuntime=$(AndroidUseSharedRuntime)" /> + <_PropertyCacheItems Include="EmbedAssembliesIntoApk=$(EmbedAssembliesIntoApk)" /> + <_PropertyCacheItems Include="AndroidLinkMode=$(AndroidLinkMode)" /> + <_PropertyCacheItems Include="AndroidLinkSkip=$(AndroidLinkSkip)" /> + <_PropertyCacheItems Include="AndroidSdkBuildToolsVersion=$(AndroidSdkBuildToolsVersion)" /> + <_PropertyCacheItems Include="AndroidSdkPath=$(AndroidSdkDirectory)" /> + <_PropertyCacheItems Include="AndroidNdkPath=$(AndroidNdkDirectory)" /> + <_PropertyCacheItems Include="JavaSdkPath=$(JavaSdkDirectory)" /> + <_PropertyCacheItems Include="AndroidSequencePointsMode=$(_AndroidSequencePointsMode)" /> + <_PropertyCacheItems Include="XamarinAndroidVersion=$(XamarinAndroidVersion)" /> + <_PropertyCacheItems Include="AndroidManagedSymbols=$(AndroidManagedSymbols)" /> + <_PropertyCacheItems Include="AndroidUseLatestPlatformSdk=$(AndroidUseLatestPlatformSdk)" /> + <_PropertyCacheItems Include="TargetFrameworkVersion=$(TargetFrameworkVersion)" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_LibraryResourceDirs Include="@(LibraryResourceDirectories)"/> + + + + + + + + + + + + + + + + + + + + + + <_GetLibraryImportsDependsOnTargets> + _ExtractLibraryProjectImports; + _CrunchLibraryProjectImports; + _AddMultiDexDependencyJars + ;_BuildLibraryImportsCache + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_GenerateJavaDesignerForComponentDependsOnTargets> + _ValidateAndroidPackageProperties + ;_GetAdditionalResourcesFromAssemblies + ;_CreateAdditionalResourceCache + ;_CollectAdditionalResourceFiles + + + + + + + + <_AdditionalAndroidResourceCachePathsCopy Include="@(_AdditonalAndroidResourceCachePaths)"/> + + + + + + + + + <_UpdateAndroidResgenDependsOnTargets> + _CheckForDeletedResourceFile; + _ValidateAndroidPackageProperties; + _ResolveMonoAndroidSdks; + _GetLibraryImports; + _CheckDuplicateJavaLibraries; + _GetAdditionalResourcesFromAssemblies; + _CreateAdditionalResourceCache; + _GenerateAndroidResourceDir; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + $(RootNamespace) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_PrepareAssembliesDependsOnTargets> + _ResolveAssemblies + ;_ResolveSatellitePaths + ;_CheckTargetFrameworks + ;_CreatePackageWorkspace + ;_StripEmbeddedLibraries + ;_LinkAssemblies + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_CreateBaseApkDependsOnTargets> + _GetAddOnPlatformLibraries; + _ValidateAndroidPackageProperties; + _GetLibraryImports; + _CheckDuplicateJavaLibraries; + _GetAdditionalResourcesFromAssemblies; + _CreateAdditionalResourceCache; + + <_CreateBaseApkInputs> + $(MSBuildAllProjects) + ;$(IntermediateOutputPath)android\AndroidManifest.xml + ;@(_AndroidResourceDest) + ;@(_AndroidAssetsDest) + ;$(_AcwMapFile) + ;@(_LibraryResourceDirectoryStamps) + ;$(_AndroidBuildPropertiesCache) + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_CompileJavaDependsOnTargetsForJack> + _PrepareJackCompilation; + _ConvertJavaPlatformJar; + _ConvertJarToJack + + <_CompileJavaDependsOnTargets> + _AdjustJavacVersionArguments; + _GeneratePackageManagerJava; + _FindJavaStubFiles; + _AddStaticResources; + _GetMonoPlatformJarPath; + _ResolveMonoAndroidSdks; + _GetLibraryImports; + _CheckDuplicateJavaLibraries; + _CreateBaseApk; + _GetAdditionalResourcesFromAssemblies; + _CreateAdditionalResourceCache; + _DetermineJavaLibrariesToCompile; + $(_CompileJavaDependsOnTargetsForJack); + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_CompileToDalvikDependsOnTargets> + _FindCompiledJavaFiles; + _GetMonoPlatformJarPath; + _GetAdditionalResourcesFromAssemblies; + _CreateAdditionalResourceCache; + _GetLibraryImports + + <_CompileToDalvikInputs> + $(MSBuildAllProjects) + ;@(_JavaLibrariesToCompileForAppDx) + ;@(AndroidExternalJavaLibrary) + ;@(_CompiledJavaFiles) + ;@(ProguardConfiguration) + ;@(MultiDexMainDexList) + ;$(_AndroidBuildPropertiesCache) + + + + + + + + + + + + + + + + + + + + + + + + + + + <_DexFile Include="$(IntermediateOutputPath)android\bin\dex\*.dex" /> + <_DexFile Include="$(IntermediateOutputPath)android\bin\*.dex" /> + <_AndroidNativeLibraryForFastDev Include="@(AndroidNativeLibrary)" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_PrepareBuildApkDependsOnTargets> + _SetLatestTargetFrameworkVersion; + _GetLibraryImports; + _RemoveRegisterAttribute; + _StripEmbeddedLibraries; + _LinkAssemblies; + _GetAddOnPlatformLibraries; + _CompileDex; + _CompileJava; + _CreateBaseApk; + _PrepareAssemblies; + _ResolveSatellitePaths; + _CheckApkPerAbiFlag + ;_LintChecks + + + + + + + <_BuildApkEmbedInputs> + $(MSBuildAllProjects) + ;$(_PackagedResources) + ;@(_ResolvedUserAssemblies) + ;@(_ShrunkFrameworkAssemblies) + ;@(AndroidNativeLibrary) + ;@(_DexFile) + ;$(_AndroidTypeMappingJavaToManaged) + ;$(_AndroidTypeMappingManagedToJava) + ;$(_AndroidBuildPropertiesCache) + + + + + + + + + + + + + + + + + + + + + + + + + + <_CopyPackageInputs> + $(MSBuildAllProjects) + ;@(ReferencePath) + ;@(ReferenceDependencyPaths) + ;$(ApkFileIntermediate) + ;$(_AndroidBuildPropertiesCache) + + + + + + + + + + + + <_CopyPackageDependsOn> + _DefineBuildTargetAbis + ;_BuildApkEmbed + + + + + + + + + + + + + + + + + <_SymbolicateFiles Include="$(_AndroidAotBinDirectory)\**\*.msym" /> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + <_IntermediatePdbFile>$(IntermediateOutputPath)$(TargetName).pdb + + + + + + + + + + + + + + + + + <_BuildApkDependsOnTargets> + Build + ;_ResolveMonoAndroidSdks + ;_ValidateAndroidPackageProperties + ;_BuildApkEmbed + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.FSharp.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.FSharp.targets new file mode 100644 index 00000000000..e05a2ffb6db --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.FSharp.targets @@ -0,0 +1,48 @@ + + + + + + MonoAndroid + v2.3 + SdkOnly + + Off + <_AndroidResourceDesigner>Resource.Designer.fs + + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.PCLSupport.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.PCLSupport.targets new file mode 100644 index 00000000000..1e45a4c7af3 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.PCLSupport.targets @@ -0,0 +1,38 @@ + + + + + true + true + + $(ResolveReferencesDependsOn); + ImplicitlyExpandDesignTimeFacades + + + + $(ImplicitlyExpandDesignTimeFacadesDependsOn); + GetReferenceAssemblyPaths + + + + + + + <_HasReferenceToSystemRuntime Condition="'$(DependsOnSystemRuntime)' == 'true' or '%(_ResolvedProjectReferencePaths.TargetPlatformIdentifier)' == 'Portable' + or '%(ReferenceDependencyPaths.Filename)' == 'System.Runtime'">true + + + + <_DesignTimeFacadeAssemblies Include="%(DesignTimeFacadeDirectories.Identity)*.dll"/> + + + false + false + ImplicitlyExpandDesignTimeFacades + + + + + + + \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.VisualBasic.targets b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.VisualBasic.targets new file mode 100644 index 00000000000..54586bc3b82 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.VisualBasic.targets @@ -0,0 +1,44 @@ + + + + + + MonoAndroid + v2.3 + SdkOnly + false + true + + + + + + diff --git a/src/Xamarin.Android.Build.Tasks/packages.config b/src/Xamarin.Android.Build.Tasks/packages.config new file mode 100644 index 00000000000..9dbe9b01044 --- /dev/null +++ b/src/Xamarin.Android.Build.Tasks/packages.config @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/Xamarin.Android.Build.Utilities/AndroidAppManifest.cs b/src/Xamarin.Android.Build.Utilities/AndroidAppManifest.cs new file mode 100644 index 00000000000..d7afc4cabe6 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/AndroidAppManifest.cs @@ -0,0 +1,314 @@ +using System; +using System.Linq; +using System.Xml; +using System.Collections.Generic; +using System.Xml.Linq; +using System.Text.RegularExpressions; +using System.Text; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + public class AndroidAppManifest + { + XDocument doc; + XElement manifest, application, usesSdk; + + static readonly XNamespace aNS = "http://schemas.android.com/apk/res/android"; + static readonly XName aName = aNS + "name"; + + AndroidAppManifest (XDocument doc) + { + this.doc = doc; + manifest = doc.Root; + if (manifest.Name != "manifest") + throw new Exception ("App manifest does not have 'manifest' root element"); + + application = manifest.Element ("application"); + if (application == null) + manifest.Add (application = new XElement ("application")); + + usesSdk = manifest.Element ("uses-sdk"); + if (usesSdk == null) + manifest.Add (usesSdk = new XElement ("uses-sdk")); + } + + public static string CanonicalizePackageName (string packageNameOrAssemblyName) + { + if (packageNameOrAssemblyName == null) + throw new ArgumentNullException ("packageNameOrAssemblyName"); + if (string.IsNullOrEmpty (packageNameOrAssemblyName = packageNameOrAssemblyName.Trim ())) + throw new ArgumentException ("Must specify a package name or assembly name", "packageNameOrAssemblyName"); + + string[] packageParts = packageNameOrAssemblyName.Split (new[]{'.'}, StringSplitOptions.RemoveEmptyEntries); + for (int i = 0; i < packageParts.Length; ++i) { + packageParts [i] = Regex.Replace (packageParts [i], "[^A-Za-z0-9_]", "_"); + if (char.IsDigit (packageParts [i], 0) || packageParts [i][0] == '_') + packageParts [i] = "x" + packageParts [i]; + } + return packageParts.Length == 1 + ? packageParts [0] + "." + packageParts [0] + : string.Join (".", packageParts); + } + + public static AndroidAppManifest Create (string packageName, string appLabel) + { + return new AndroidAppManifest (XDocument.Parse ( + @" + + + + +")) { + PackageName = packageName, + ApplicationLabel = appLabel, + }; + } + + public static AndroidAppManifest Load (string filename) + { + return Load (XDocument.Load (filename)); + } + + public static AndroidAppManifest Load (XDocument doc) + { + return new AndroidAppManifest (doc); + } + + public void Write (XmlWriter writer) + { + doc.Save (writer); + } + + public void WriteToFile (string fileName) + { + var xmlSettings = new XmlWriterSettings () { + Encoding = Encoding.UTF8, + CloseOutput = false, + Indent = true, + IndentChars = "\t", + NewLineChars = "\n", + }; + + var tempFile = FileUtil.GetTempFilenameForWrite (fileName); + bool success = false; + try { + using (var writer = XmlTextWriter.Create (tempFile, xmlSettings)) { + Write (writer); + } + FileUtil.SystemRename (tempFile, fileName); + success = true; + } finally { + if (!success) { + try { + File.Delete (tempFile); + } catch { + //the original exception is more important than this one + } + } + } + } + + static string NullIfEmpty (string value) + { + return string.IsNullOrEmpty (value) ? null : value; + } + + public string PackageName { + get { return (string) manifest.Attribute ("package"); } + set { manifest.SetAttributeValue ("package", NullIfEmpty (value)); } + } + + public string ApplicationLabel { + get { return (string) application.Attribute (aNS + "label"); } + set { application.SetAttributeValue (aNS + "label", NullIfEmpty (value)); } + } + + public string ApplicationIcon { + get { return (string) application.Attribute (aNS + "icon"); } + set { application.SetAttributeValue (aNS + "icon", NullIfEmpty (value)); } + } + + public string ApplicationTheme { + get { return (string) application.Attribute (aNS + "theme"); } + set { application.SetAttributeValue (aNS + "theme", NullIfEmpty (value)); } + } + + public string VersionName { + get { return (string) manifest.Attribute (aNS + "versionName"); } + set { manifest.SetAttributeValue (aNS + "versionName", NullIfEmpty (value)); } + } + + public string VersionCode { + get { return (string) manifest.Attribute (aNS + "versionCode"); } + set { manifest.SetAttributeValue (aNS + "versionCode", NullIfEmpty (value)); } + } + + public string InstallLocation { + get { return (string) manifest.Attribute (aNS + "installLocation"); } + set { manifest.SetAttributeValue (aNS + "installLocation", NullIfEmpty (value)); } + } + + public int? MinSdkVersion { + get { return ParseSdkVersion (usesSdk.Attribute (aNS + "minSdkVersion")); } + set { usesSdk.SetAttributeValue (aNS + "minSdkVersion", value == null ? null : value.ToString ()); } + } + + public int? TargetSdkVersion { + get { return ParseSdkVersion (usesSdk.Attribute (aNS + "targetSdkVersion")); } + set { usesSdk.SetAttributeValue (aNS + "targetSdkVersion", value == null ? null : value.ToString ()); } + } + + int? ParseSdkVersion (XAttribute attribute) + { + var version = (string)attribute; + if (string.IsNullOrEmpty (version)) + return null; + int vn; + if (!int.TryParse (version, out vn)) + vn = AndroidVersion.MaxApiLevel; + return vn; + } + + public IEnumerable AndroidPermissions { + get { + foreach (var el in manifest.Elements ("uses-permission")) { + var name = (string) el.Attribute (aName); + if (name == null) + continue; + var lastDot = name.LastIndexOf ('.'); + if (lastDot >= 0) + yield return name.Substring (lastDot + 1); + } + } + } + + public IEnumerable AndroidPermissionsQualified { + get { + foreach (var el in manifest.Elements ("uses-permission")) { + var name = (string) el.Attribute (aName); + if (name != null) + yield return name; + } + } + } + + public bool? Debuggable { + get { return (bool?) application.Attribute (aNS + "debuggable"); } + set { application.SetAttributeValue (aNS + "debuggable", value); } + } + + public void SetAndroidPermissions (IEnumerable permissions) + { + var newPerms = new HashSet (permissions.Select (FullyQualifyPermission)); + var current = new HashSet (AndroidPermissionsQualified); + AddAndroidPermissions (newPerms.Except (current)); + RemoveAndroidPermissions (current.Except (newPerms)); + } + + void AddAndroidPermissions (IEnumerable permissions) + { + var newElements = permissions.Select (p => new XElement ("uses-permission", new XAttribute (aName, p))); + + var lastPerm = manifest.Elements ("uses-permission").LastOrDefault (); + if (lastPerm != null) { + foreach (var el in newElements) { + lastPerm.AddAfterSelf (el); + lastPerm = el; + } + } else { + var parentNode = (XNode) manifest.Element ("application") ?? manifest.LastNode; + foreach (var el in newElements) + parentNode.AddBeforeSelf (el); + } + } + + string FullyQualifyPermission (string permission) + { + //if already qualified, don't mess with it + if (permission.IndexOf ('.') > -1) + return permission; + + switch (permission) { + case "READ_HISTORY_BOOKMARKS": + case "WRITE_HISTORY_BOOKMARKS": + return string.Format ("com.android.browser.permission.{0}", permission); + default: + return string.Format ("android.permission.{0}", permission); + } + } + + void RemoveAndroidPermissions (IEnumerable permissions) + { + var perms = new HashSet (permissions); + var list = manifest.Elements ("uses-permission") + .Where (el => perms.Contains ((string)el.Attribute (aName))).ToList (); + foreach (var el in list) + el.Remove (); + } + + [Obsolete ("Use GetLaunchableFastdevActivityName or GetLaunchableUserActivityName")] + public string GetLaunchableActivityName () + { + return GetLaunchableFastDevActivityName (); + } + + /// Gets an activity that can be used to initialize the override directory for fastdev. + [Obsolete ("This should not be needed anymore; Activity execution is not part of installation.")] + public string GetLaunchableFastDevActivityName () + { + string first = null; + foreach (var a in GetLaunchableActivities ()) { + var name = (string) a.Attribute (aName); + //prefer the fastdev launcher, it's quicker + if (name == "mono.android.__FastDevLauncher") { + return name; + } + //else just use the first other launchable activity + if (first == null) { + first = name; + } + } + + return string.IsNullOrEmpty (first)? null : first; + } + + // We add a fake launchable activity for FastDev, but we don't want + // to launch that one when the user does Run or Debug + public string GetLaunchableUserActivityName () + { + return GetLaunchableActivities () + .Select (a => (string) a.Attribute (aName)) + .FirstOrDefault (name => !string.IsNullOrEmpty (name) && name != "mono.android.__FastDevLauncher"); + } + + IEnumerable GetLaunchableActivities () + { + foreach (var activity in application.Elements ("activity")) { + var filter = activity.Element ("intent-filter"); + if (filter != null) { + foreach (var category in filter.Elements ("category")) + if (category != null && (string)category.Attribute (aName) == "android.intent.category.LAUNCHER") + yield return activity; + } + } + } + + public IEnumerable GetAllActivityNames () + { + foreach (var activity in application.Elements ("activity")) { + var activityName = (string) activity.Attribute (aName); + if (activityName != "mono.android.__FastDevLauncher") + yield return activityName; + } + } + + public IEnumerable GetLaunchableActivityNames () + { + return GetLaunchableActivities () + .Select (a => (string) a.Attribute (aName)) + .Where (name => !string.IsNullOrEmpty (name) && name != "mono.android.__FastDevLauncher"); + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/AndroidLogger.cs b/src/Xamarin.Android.Build.Utilities/AndroidLogger.cs new file mode 100644 index 00000000000..4152d44d406 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/AndroidLogger.cs @@ -0,0 +1,123 @@ +using System; + +namespace Xamarin.Android.Build.Utilities +{ + public delegate void MessageHandler (string task, string message); + public delegate void TaskLogHandler (AndroidTaskLog log); + + public static class AndroidLogger + { + public static event MessageHandler Info; + public static event MessageHandler Warning; + public static event MessageHandler Error; + public static event MessageHandler Debug; + public static event TaskLogHandler Task; + + public static void LogInfo (string format, params object[] args) + { + LogInfo (string.Empty, format, args); + } + + public static void LogInfo (string task, string format, params object[] args) + { + if (Info != null) { + if (args == null || args.Length == 0) + Info (task, format); + else + Info (task, String.Format (format, args)); + } + } + + public static void LogWarning (string format, params object[] args) + { + LogWarning (string.Empty, format, args); + } + + public static void LogWarning (string task, string format, params object[] args) + { + if (Warning != null) { + if (args == null) + Warning (task, format); + else + Warning (task, String.Format (format, args)); + } + } + + public static void LogError (string format, params object[] args) + { + LogError (string.Empty, format, args); + } + + public static void LogError (string message, Exception ex) + { + message += (ex != null? System.Environment.NewLine + ex.ToString () : string.Empty); + LogError (message); + } + + public static void LogError (string task, string format, params object[] args) + { + if (Error != null) { + if (args == null || args.Length == 0) + Error (task, format); + else + Error (task, String.Format (format, args)); + } + } + + public static void LogDebug (string format, params object[] args) + { + LogDebug (string.Empty, format, args); + } + + public static void LogDebug (string task, string format, params object[] args) + { + if (Debug != null) { + if (args == null || args.Length == 0) + Debug (task, format); + else + Debug (task, String.Format (format, args)); + } + } + + public static void LogTask (AndroidTaskLog log) + { + if (Task != null) + Task (log); + } + } + + public class AndroidTaskLog + { + public string Task { get; private set; } + public string Input { get; private set; } + public string Output { get; private set; } + public DateTime StartTime { get; private set; } + public DateTime EndTime { get; private set; } + + public AndroidTaskLog (string task, string input) + { + Task = task; + Input = input; + StartTime = DateTime.Now; + } + + public AndroidTaskLog Complete (string output) + { + Output = output; + EndTime = DateTime.Now; + + return this; + } + + public AndroidTaskLog Complete (object output) + { + if (output == null) + output = ""; + Output = output.ToString(); + EndTime = DateTime.Now; + + return this; + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/AndroidSdk.cs b/src/Xamarin.Android.Build.Utilities/AndroidSdk.cs new file mode 100644 index 00000000000..522d1b25e61 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/AndroidSdk.cs @@ -0,0 +1,149 @@ +using System; +using System.Linq; +using System.Collections.Generic; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + public class AndroidSdk + { + private static AndroidSdkBase sdk; + + public static void Refresh (string androidSdkPath = null, string androidNdkPath = null, string javaSdkPath = null) + { + if (OS.IsWindows) + sdk = new AndroidSdkWindows (); + else + sdk = new AndroidSdkUnix (); + + try { + sdk.Initialize (androidSdkPath ?? sdk.PreferedAndroidSdkPath, androidNdkPath ?? sdk.PreferedAndroidNdkPath, + javaSdkPath ?? sdk.PreferedJavaSdkPath); + if (IsInstalled) { + var levels = GetInstalledPlatformVersions ().Select (l => l.ApiLevel.ToString ()).ToArray (); + string levelList; + if (levels == null || levels.Length == 0) + levelList = "(none)"; + else + levelList = string.Join (", ", levels); + AndroidLogger.LogInfo (null, "Found Android SDK. API levels: {0}", levelList); + } else { + AndroidLogger.LogInfo (null, "Did not find Android SDK"); + } + } catch (Exception ex) { + AndroidLogger.LogError ("Error finding Android/Java SDKs", ex); + } + } + + public static IEnumerable GetBuildToolsPaths (string preferredBuildToolsVersion) + { + if (!string.IsNullOrEmpty (preferredBuildToolsVersion)) { + var preferredDir = Path.Combine (AndroidSdkPath, "build-tools", preferredBuildToolsVersion); + if (Directory.Exists (preferredDir)) + return new[] { preferredDir }.Concat (GetBuildToolsPaths ().Where (p => p!= preferredDir)); + } + return GetBuildToolsPaths (); + } + + public static IEnumerable GetBuildToolsPaths () + { + ValidatePath (AndroidSdkPath); + + var buildTools = Path.Combine (AndroidSdkPath, "build-tools"); + if (Directory.Exists (buildTools)) { + var preview = Directory.EnumerateDirectories (buildTools) + .Where(x => TryParseVersion (Path.GetFileName (x)) == null) + .Select(x => x); + + foreach (var d in preview) + yield return d; + + var sorted = from p in Directory.EnumerateDirectories (buildTools) + let version = TryParseVersion (Path.GetFileName (p)) + where version != null + orderby version descending + select p; + + foreach (var d in sorted) + yield return d; + } + var ptPath = Path.Combine (AndroidSdkPath, "platform-tools"); + if (Directory.Exists (ptPath)) + yield return ptPath; + } + + static Version TryParseVersion (string v) + { + Version version; + if (Version.TryParse (v, out version)) + return version; + return null; + } + + static string ToApiName (int apiLevel) + { + // Support for N Preview + if (apiLevel == 24) + return "N"; + return apiLevel.ToString (); + } + + static string ValidatePath (string path) + { + if (String.IsNullOrEmpty (path)) + throw new InvalidOperationException ("This property is not valid when the SDK is not installed"); + return path; + } + + public static string GetPlatformDirectory (int apiLevel) + { + return Path.Combine (AndroidSdkPath, "platforms", "android-" + ToApiName (apiLevel)); + } + + public static string GetPlatformDirectory (string osVersion) + { + var level = AndroidVersion.TryOSVersionToApiLevel (osVersion); + if (level == 0) + return null; + return GetPlatformDirectory (level); + } + + public static bool IsPlatformInstalled (int apiLevel) + { + return apiLevel != 0 && Directory.Exists (GetPlatformDirectory (apiLevel)); + } + + public static IEnumerable GetInstalledPlatformVersions () + { + var knownAndInstalledSdkLevels = AndroidVersion.KnownVersions.Where (v => IsPlatformInstalled (v.ApiLevel)); + + return knownAndInstalledSdkLevels.Where (version => { + var apiLevel = MonoDroidSdk.GetApiLevelForFrameworkVersion (version.OSVersion); + return MonoDroidSdk.IsSupportedFrameworkLevel (apiLevel); + }); + } + + public static bool IsInstalled { + get { + return !string.IsNullOrEmpty (AndroidSdkPath) && !string.IsNullOrEmpty (JavaSdkPath); + } + } + + public static string AndroidNdkPath { + get { return sdk.AndroidNdkPath; } + } + + public static string AndroidSdkPath { + get { return sdk.AndroidSdkPath; } + } + + public static string JavaSdkPath { + get { return sdk.JavaSdkPath; } + } + + public static string AndroidNdkHostPlatform { + get { return sdk.NdkHostPlatform; } + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/AndroidTargetArch.cs b/src/Xamarin.Android.Build.Utilities/AndroidTargetArch.cs new file mode 100644 index 00000000000..7aebeee6fd8 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/AndroidTargetArch.cs @@ -0,0 +1,19 @@ +using System; +using System.Text.RegularExpressions; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + [Flags] + public enum AndroidTargetArch + { + None = 0, + Arm = 1, + X86 = 2, + Mips = 4, + Arm64 = 8, + X86_64 = 16, + Other = 0x10000 // hope it's not too optimistic + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/AndroidVersion.cs b/src/Xamarin.Android.Build.Utilities/AndroidVersion.cs new file mode 100644 index 00000000000..88ae13b5c82 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/AndroidVersion.cs @@ -0,0 +1,108 @@ +using System; + +namespace Xamarin.Android.Build.Utilities +{ + public class AndroidVersion + { + public static readonly int MaxApiLevel = 24; + + public AndroidVersion (int apilevel, string osVersion) + { + this.ApiLevel = apilevel; + this.OSVersion = osVersion; + } + + AndroidVersion (int apilevel, string osVersion, string codeName, Version version) + { + this.ApiLevel = apilevel; + // TODO: remove osVersion from parameter list and generate from version + this.OSVersion = osVersion; + this.CodeName = codeName; + this.Version = version; + } + + public int ApiLevel { get; private set; } + public string OSVersion { get; private set; } + public string CodeName { get; private set; } + public Version Version { get; private set; } + + public static int OSVersionToApiLevel (string osVersion) + { + int ret = TryOSVersionToApiLevel (osVersion); + if (ret == 0) + throw new ArgumentOutOfRangeException ("OS version not recognized: " + osVersion); + return ret; + } + + public static int TryOSVersionToApiLevel (string frameworkVersion) + { + // Use MonoDroidSdk.GetApiLevelForFrameworkVersion because that will translate XA versions >= 5.xx to the correct api level + var apiLevelText = MonoDroidSdk.GetApiLevelForFrameworkVersion (frameworkVersion); + int apiLevel; + int.TryParse (apiLevelText, out apiLevel); + return apiLevel; + } + + public static string ApiLevelToOSVersion (int apiLevel) + { + string ret = TryApiLevelToOSVersion (apiLevel); + if (ret == null) + throw new ArgumentOutOfRangeException ("API level not recognized: " + apiLevel); + return ret; + } + + public static string TryApiLevelToOSVersion (int apiLevel) + { + var osVersion = MonoDroidSdk.GetFrameworkVersionForApiLevel (apiLevel.ToString ()); + if (!string.IsNullOrEmpty (osVersion)) + return osVersion.TrimStart ('v'); + return null; + } + + public static string TryOSVersionToCodeName (string frameworkVersion) + { + // match on API level, the framework version might not match what we have here (>= XA 5.x uses a different version scheme) + var apiLevel = TryOSVersionToApiLevel (frameworkVersion); + + foreach (AndroidVersion version in KnownVersions) + if (version.ApiLevel == apiLevel) + return version.CodeName; + return null; + } + + public static string TryFrameworkVersionToOSVersion (string frameworkVersion) + { + // match on API level, the framework version might not match what we have here (>= XA 5.x uses a different version scheme) + var apiLevel = TryOSVersionToApiLevel (frameworkVersion); + + foreach (AndroidVersion version in KnownVersions) + if (version.ApiLevel == apiLevel) + return version.OSVersion; + return null; + } + + public static AndroidVersion[] KnownVersions = new[] { + new AndroidVersion (4, "1.6", "Donut", new Version (1, 6)), + new AndroidVersion (5, "2.0", "Eclair", new Version (2, 0)), + new AndroidVersion (6, "2.0.1", "Eclair", new Version (2, 0, 1)), + new AndroidVersion (7, "2.1", "Eclair", new Version (2, 1)), + new AndroidVersion (8, "2.2", "Froyo", new Version (2, 2)), + new AndroidVersion (10, "2.3", "Gingerbread", new Version (2, 3)), + new AndroidVersion (11, "3.0", "Honeycomb", new Version (3, 0)), + new AndroidVersion (12, "3.1", "Honeycomb", new Version (3, 1)), + new AndroidVersion (13, "3.2", "Honeycomb", new Version (3, 2)), + new AndroidVersion (14, "4.0", "Ice Cream Sandwich", new Version (4, 0)), + new AndroidVersion (15, "4.0.3", "Ice Cream Sandwich", new Version (4, 0, 3)), + new AndroidVersion (16, "4.1", "Jelly Bean", new Version (4, 1)), + new AndroidVersion (17, "4.2", "Jelly Bean", new Version (4, 2)), + new AndroidVersion (18, "4.3", "Jelly Bean", new Version (4, 3)), + new AndroidVersion (19, "4.4", "Kit Kat", new Version (4, 4)), + new AndroidVersion (20, "4.4.87", "Kit Kat + Wear support", new Version (4, 4, 87)), + new AndroidVersion (21, "5.0", "Lollipop", new Version (5, 0)), + new AndroidVersion (22, "5.1", "Lollipop", new Version (5, 1)), + new AndroidVersion (23, "6.0", "Marshmallow", new Version (6, 0)), + new AndroidVersion (24, "6.0.99", "N Preview", new Version (6, 0, 99)), + }; + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/FileUtil.cs b/src/Xamarin.Android.Build.Utilities/FileUtil.cs new file mode 100644 index 00000000000..7c81d7e4ad0 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/FileUtil.cs @@ -0,0 +1,58 @@ +using System; +using System.IO; +using System.Runtime.InteropServices; + +namespace Xamarin.Android.Build.Utilities +{ + class FileUtil + { + public static string GetTempFilenameForWrite (string fileName) + { + return Path.GetDirectoryName (fileName) + Path.DirectorySeparatorChar + ".#" + Path.GetFileName (fileName); + } + + //From MonoDevelop.Core.FileService + public static void SystemRename (string sourceFile, string destFile) + { + //FIXME: use the atomic System.IO.File.Replace on NTFS + if (OS.IsWindows) { + string wtmp = null; + if (File.Exists (destFile)) { + do { + wtmp = Path.Combine (Path.GetTempPath (), Guid.NewGuid ().ToString ()); + } while (File.Exists (wtmp)); + + File.Move (destFile, wtmp); + } + try { + File.Move (sourceFile, destFile); + } + catch { + try { + if (wtmp != null) + File.Move (wtmp, destFile); + } + catch { + wtmp = null; + } + throw; + } + finally { + if (wtmp != null) { + try { + File.Delete (wtmp); + } + catch { } + } + } + } + else { + rename (sourceFile, destFile); + } + } + + [DllImport ("libc", SetLastError=true)] + static extern int rename (string old, string @new); + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/GdbPaths.cs b/src/Xamarin.Android.Build.Utilities/GdbPaths.cs new file mode 100644 index 00000000000..e4079fc4765 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/GdbPaths.cs @@ -0,0 +1,94 @@ +using System; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + public enum AndroidDebugServer + { + /// GNU's GDB debug server (provided by Android NDK) + Gdb, + /// Facebook's Ds2 (https://github.com/facebook/ds2) + Ds2, + /// LLDB's debug server (lldb-gdbserver) + Llgs + } + + public class GdbPaths + { + public static AndroidDebugServer? GetAndroidDebugServer (string name) + { + switch ((name ?? string.Empty).ToLowerInvariant().Trim ()) + { + case "gdb": + return AndroidDebugServer.Gdb; + case "ds2": + return AndroidDebugServer.Ds2; + case "llgs": + return AndroidDebugServer.Llgs; + } + + return null; + } + + public static string GetDebugServerPath (AndroidDebugServer server, AndroidTargetArch arch, + string AndroidNdkDirectory, string SdkBinDirectory) + { + switch (server) + { + case AndroidDebugServer.Gdb: + var abi = GetAbiFromArch (arch); + return Path.Combine (AndroidNdkDirectory, "prebuilt", abi, "gdbserver", "gdbserver"); + case AndroidDebugServer.Ds2: + return Path.Combine (SdkBinDirectory, "ds2-" + arch.ToString ().ToLowerInvariant ()); + case AndroidDebugServer.Llgs: + throw new NotImplementedException (); + } + + return null; + } + + public static string GetDebugServerFileName (AndroidDebugServer server) + { + switch (server) + { + case AndroidDebugServer.Gdb: + return "libgdbserver.so"; + case AndroidDebugServer.Ds2: + return "libds2.so"; + case AndroidDebugServer.Llgs: + throw new NotImplementedException (); + } + + return null; + } + + public static AndroidTargetArch GetArchFromAbi (string abi) + { + if (abi.StartsWith ("arm64", StringComparison.OrdinalIgnoreCase) || + abi.StartsWith ("aarch64", StringComparison.OrdinalIgnoreCase)) + return AndroidTargetArch.Arm64; + if (abi.StartsWith ("arm", StringComparison.OrdinalIgnoreCase)) + return AndroidTargetArch.Arm; + if (abi.StartsWith ("mips", StringComparison.OrdinalIgnoreCase)) + return AndroidTargetArch.Mips; + if (abi.StartsWith ("x86_64", StringComparison.OrdinalIgnoreCase)) + return AndroidTargetArch.X86_64; + if (abi.StartsWith ("x86", StringComparison.OrdinalIgnoreCase)) + return AndroidTargetArch.X86; + return AndroidTargetArch.None; + } + + public static string GetAbiFromArch (AndroidTargetArch arch) + { + switch (arch) { + case AndroidTargetArch.Arm: return "android-arm"; + case AndroidTargetArch.Arm64: return "android-arm64"; + case AndroidTargetArch.Mips: return "android-mips"; + case AndroidTargetArch.X86: return "android-x86"; + case AndroidTargetArch.X86_64: return "android-x86_64"; + } + return null; + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/MonoDroidSdk.cs b/src/Xamarin.Android.Build.Utilities/MonoDroidSdk.cs new file mode 100644 index 00000000000..9781e40a755 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/MonoDroidSdk.cs @@ -0,0 +1,60 @@ +using System; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + public static class MonoDroidSdk + { + static MonoDroidSdkBase sdk; + + public static string GetApiLevelForFrameworkVersion (string framework) + { + return GetSdk ().GetApiLevelForFrameworkVersion (framework); + } + + public static string GetFrameworkVersionForApiLevel (string apiLevel) + { + return GetSdk ().GetFrameworkVersionForApiLevel (apiLevel); + } + + public static bool IsSupportedFrameworkLevel (string apiLevel) + { + return GetSdk ().IsSupportedFrameworkLevel (apiLevel); + } + + public static void Refresh (string runtimePath = null, string binPath = null, string bclPath = null) + { + if (OS.IsWindows) { + sdk = new MonoDroidSdkWindows (); + } else { + sdk = new MonoDroidSdkUnix (); + } + + try { + sdk.Initialize (runtimePath, binPath, bclPath); + } catch (Exception ex) { + AndroidLogger.LogError ("Error finding Xamarin.Android SDK", ex); + } + } + + static MonoDroidSdkBase GetSdk () + { + if (sdk == null) { + Refresh (); + } + return sdk; + } + + public static string RuntimePath { get { return GetSdk ().RuntimePath; } } + + public static string BinPath { get { return GetSdk ().BinPath; } } + + public static string FrameworkPath { get { return GetSdk ().BclPath; } } + + [Obsolete ("Do not use.")] + public static string JavaDocToMDocExe { + get { return Path.Combine (BinPath, OS.IsWindows ? "javadoc-to-mdoc.exe" : "javadoc-to-mdoc"); } + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/OS.cs b/src/Xamarin.Android.Build.Utilities/OS.cs new file mode 100644 index 00000000000..0ff07cd2185 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/OS.cs @@ -0,0 +1,227 @@ +using System; +using System.Runtime.InteropServices; +using System.IO; +using System.Text; + +namespace Xamarin.Android.Build.Utilities +{ + public class OS + { + public readonly static bool IsWindows; + public readonly static bool IsMac; + + internal readonly static string ProgramFilesX86; + + static OS () + { + IsWindows = Path.DirectorySeparatorChar == '\\'; + IsMac = !IsWindows && IsRunningOnMac (); + + if (IsWindows) { + ProgramFilesX86 = GetProgramFilesX86 (); + } + } + + //From Managed.Windows.Forms/XplatUI + static bool IsRunningOnMac () + { + IntPtr buf = IntPtr.Zero; + try { + buf = Marshal.AllocHGlobal (8192); + // This is a hacktastic way of getting sysname from uname () + if (uname (buf) == 0) { + string os = System.Runtime.InteropServices.Marshal.PtrToStringAnsi (buf); + if (os == "Darwin") + return true; + } + } catch { + } finally { + if (buf != IntPtr.Zero) + System.Runtime.InteropServices.Marshal.FreeHGlobal (buf); + } + return false; + } + + [DllImport ("libc")] + static extern int uname (IntPtr buf); + + static string GetProgramFilesX86 () + { + //SpecialFolder.ProgramFilesX86 is broken on 32-bit WinXP + if (IntPtr.Size == 8) { + return Environment.GetFolderPath (Environment.SpecialFolder.ProgramFilesX86); + } else { + return Environment.GetFolderPath (Environment.SpecialFolder.ProgramFiles); + } + } + + internal static string GetXamarinAndroidCacheDir () + { + if (IsMac) { + var home = Environment.GetFolderPath (Environment.SpecialFolder.Personal); + return Path.Combine (home, "Library", "Caches", "Xamarin.Android"); + } else if (IsWindows) { + var localAppData = Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData); + return Path.Combine (localAppData, "Xamarin.Android", "Cache"); + } else { + var home = Environment.GetFolderPath (Environment.SpecialFolder.Personal); + var xdgCacheHome = Environment.GetEnvironmentVariable ("XDG_CACHE_HOME"); + if (string.IsNullOrEmpty (xdgCacheHome)) { + xdgCacheHome = Path.Combine (home, ".cache"); + } + return Path.Combine (xdgCacheHome, "Xamarin.Android"); + } + } + } + + public static class KernelEx { + [DllImport ("kernel32.dll", CharSet = CharSet.Auto)] + static extern int GetLongPathName ( + [MarshalAs (UnmanagedType.LPTStr)] string path, + [MarshalAs (UnmanagedType.LPTStr)] StringBuilder longPath, + int longPathLength + ); + + public static string GetLongPathName (string path) + { + StringBuilder sb = new StringBuilder (255); + GetLongPathName (path, sb, sb.Capacity); + return sb.ToString (); + } + + [DllImport ("kernel32.dll", CharSet = CharSet.Auto)] + static extern int GetShortPathName ( + [MarshalAs (UnmanagedType.LPTStr)] string path, + [MarshalAs (UnmanagedType.LPTStr)] StringBuilder shortPath, + int shortPathLength + ); + + public static string GetShortPathName (string path) + { + StringBuilder sb = new StringBuilder (255); + GetShortPathName (path, sb, sb.Capacity); + return sb.ToString (); + } + } + + internal static class RegistryEx + { + const string ADVAPI = "advapi32.dll"; + + public static UIntPtr CurrentUser = (UIntPtr)0x80000001; + public static UIntPtr LocalMachine = (UIntPtr)0x80000002; + + [DllImport (ADVAPI, CharSet = CharSet.Unicode, SetLastError = true)] + static extern int RegOpenKeyEx (UIntPtr hKey, string subKey, uint reserved, uint sam, out UIntPtr phkResult); + + [DllImport (ADVAPI, CharSet = CharSet.Unicode, SetLastError = true)] + static extern int RegQueryValueExW (UIntPtr hKey, string lpValueName, int lpReserved, out uint lpType, + StringBuilder lpData, ref uint lpcbData); + + [DllImport (ADVAPI, CharSet = CharSet.Unicode, SetLastError = true)] + static extern int RegSetValueExW (UIntPtr hKey, string lpValueName, int lpReserved, + uint dwType, string data, uint cbData); + + [DllImport (ADVAPI, CharSet = CharSet.Unicode, SetLastError = true)] + static extern int RegSetValueExW (UIntPtr hKey, string lpValueName, int lpReserved, + uint dwType, IntPtr data, uint cbData); + + [DllImport (ADVAPI, CharSet = CharSet.Unicode, SetLastError = true)] + static extern int RegCreateKeyEx (UIntPtr hKey, string subKey, uint reserved, string @class, uint options, + uint samDesired, IntPtr lpSecurityAttributes, out UIntPtr phkResult, out Disposition lpdwDisposition); + + [DllImport ("advapi32.dll", SetLastError = true)] + static extern int RegCloseKey (UIntPtr hKey); + + public static string GetValueString (UIntPtr key, string subkey, string valueName, Wow64 wow64) + { + UIntPtr regKeyHandle; + uint sam = (uint)Rights.QueryValue + (uint)wow64; + if (RegOpenKeyEx (key, subkey, 0, sam, out regKeyHandle) != 0) + return null; + + try { + uint type; + var sb = new StringBuilder (2048); + uint cbData = (uint) sb.Capacity; + if (RegQueryValueExW (regKeyHandle, valueName, 0, out type, sb, ref cbData) == 0) { + return sb.ToString (); + } + return null; + } finally { + RegCloseKey (regKeyHandle); + } + } + + public static void SetValueString (UIntPtr key, string subkey, string valueName, string value, Wow64 wow64) + { + UIntPtr regKeyHandle; + uint sam = (uint)(Rights.CreateSubKey | Rights.SetValue) + (uint)wow64; + uint options = (uint) Options.NonVolatile; + Disposition disposition; + if (RegCreateKeyEx (key, subkey, 0, null, options, sam, IntPtr.Zero, out regKeyHandle, out disposition) != 0) { + throw new Exception ("Could not open or craete key"); + } + + try { + uint type = (uint)ValueType.String; + uint lenBytesPlusNull = ((uint)value.Length + 1) * 2; + var result = RegSetValueExW (regKeyHandle, valueName, 0, type, value, lenBytesPlusNull); + if (result != 0) + throw new Exception (string.Format ("Error {0} setting registry key '{1}{2}@{3}'='{4}'", + result, key, subkey, valueName, value)); + } finally { + RegCloseKey (regKeyHandle); + } + } + + [Flags] + enum Rights : uint + { + None = 0, + QueryValue = 0x0001, + SetValue = 0x0002, + CreateSubKey = 0x0004, + EnumerateSubKey = 0x0008, + } + + enum Options + { + BackupRestore = 0x00000004, + CreateLink = 0x00000002, + NonVolatile = 0x00000000, + Volatile = 0x00000001, + } + + public enum Wow64 : uint + { + Key64 = 0x0100, + Key32 = 0x0200, + } + + enum ValueType : uint + { + None = 0, //REG_NONE + String = 1, //REG_SZ + UnexpandedString = 2, //REG_EXPAND_SZ + Binary = 3, //REG_BINARY + DWord = 4, //REG_DWORD + DWordLittleEndian = 4, //REG_DWORD_LITTLE_ENDIAN + DWordBigEndian = 5, //REG_DWORD_BIG_ENDIAN + Link = 6, //REG_LINK + MultiString = 7, //REG_MULTI_SZ + ResourceList = 8, //REG_RESOURCE_LIST + FullResourceDescriptor = 9, //REG_FULL_RESOURCE_DESCRIPTOR + ResourceRequirementsList = 10, //REG_RESOURCE_REQUIREMENTS_LIST + QWord = 11, //REG_QWORD + QWordLittleEndian = 11, //REG_QWORD_LITTLE_ENDIAN + } + + enum Disposition : uint + { + CreatedNewKey = 0x00000001, + OpenedExistingKey = 0x00000002, + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/ProcessUtils.cs b/src/Xamarin.Android.Build.Utilities/ProcessUtils.cs new file mode 100644 index 00000000000..cded992fb84 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/ProcessUtils.cs @@ -0,0 +1,124 @@ +using System; +using System.Diagnostics; +using System.Threading.Tasks; +using System.IO; +using System.Threading; + +namespace Xamarin.Android.Build.Utilities +{ + public static class ProcessUtils + { + public static async Task StartProcess (ProcessStartInfo psi, TextWriter stdout, TextWriter stderr, CancellationToken cancellationToken, Action onStarted = null) + { + cancellationToken.ThrowIfCancellationRequested (); + psi.UseShellExecute = false; + psi.RedirectStandardOutput |= stdout != null; + psi.RedirectStandardError |= stderr != null; + + var process = new Process { + StartInfo = psi, + EnableRaisingEvents = true, + }; + + Task output = Task.FromResult (true); + Task error = Task.FromResult (true); + Task exit = WaitForExitAsync (process); + using (process) { + process.Start (); + if (onStarted != null) + onStarted (process); + + // If the token is cancelled while we're running, kill the process. + // Otherwise once we finish the Task.WhenAll we can remove this registration + // as there is no longer any need to Kill the process. + // + // We wrap `stdout` and `stderr` in syncronized wrappers for safety in case they + // end up writing to the same buffer, or they are the same object. + using (cancellationToken.Register (() => KillProcess (process))) { + if (psi.RedirectStandardOutput) + output = ReadStreamAsync (process.StandardOutput, TextWriter.Synchronized (stdout)); + + if (psi.RedirectStandardError) + error = ReadStreamAsync (process.StandardError, TextWriter.Synchronized (stderr)); + + await Task.WhenAll (new [] { output, error, exit }).ConfigureAwait (false); + } + // If we invoke 'KillProcess' our output, error and exit tasks will all complete normally. + // To protected against passing the user incomplete data we have to call + // `cancellationToken.ThrowIfCancellationRequested ()` here. + cancellationToken.ThrowIfCancellationRequested (); + return process.ExitCode; + } + } + + static void KillProcess (Process p) + { + try { + p.Kill (); + } catch (InvalidOperationException) { + // If the process has already exited this could happen + } + } + + static Task WaitForExitAsync (Process process) + { + var exitDone = new TaskCompletionSource (); + process.Exited += (o, e) => exitDone.TrySetResult (true); + return exitDone.Task; + } + + static async Task ReadStreamAsync (StreamReader stream, TextWriter destination) + { + int read; + var buffer = new char [4096]; + while ((read = await stream.ReadAsync (buffer, 0, buffer.Length).ConfigureAwait (false)) > 0) + destination.Write (buffer, 0, read); + } + + /// + /// Executes an Android Sdk tool and returns a result. The result is based on a function of the command output. + /// + public static Task ExecuteToolAsync (string exe, Func result, CancellationToken token, Action onStarted = null) + { + var tcs = new TaskCompletionSource (); + + var log = new StringWriter (); + var error = new StringWriter (); + + var psi = new ProcessStartInfo (exe); + psi.CreateNoWindow = true; + psi.RedirectStandardInput = onStarted != null; + + var processTask = ProcessUtils.StartProcess (psi, log, error, token, onStarted); + var exeName = Path.GetFileName (exe); + + processTask.ContinueWith (t => { + var output = log.ToString (); + var errorOutput = error.ToString (); + log.Dispose (); + error.Dispose (); + + if (t.IsCanceled) { + tcs.TrySetCanceled (); + return; + } + + if (t.IsFaulted) { + tcs.TrySetException (t.Exception.Flatten ().InnerException); + return; + } + + if (t.Result == 0) { + tcs.TrySetResult (result != null ? result (output) : default (TResult)); + } else { + var errorMessage = !string.IsNullOrEmpty (errorOutput) ? errorOutput : output; + + tcs.TrySetException (new InvalidOperationException (string.IsNullOrEmpty (errorMessage) ? exeName + " returned non-zero exit code" : string.Format ("{0} : {1}", t.Result, errorMessage))); + } + }, TaskContinuationOptions.ExecuteSynchronously); + + return tcs.Task; + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Properties/AssemblyInfo.cs b/src/Xamarin.Android.Build.Utilities/Properties/AssemblyInfo.cs new file mode 100644 index 00000000000..73f1e5319d2 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Properties/AssemblyInfo.cs @@ -0,0 +1,15 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +[assembly: AssemblyTitle ("Xamarin.Android.Build.Utilities")] +[assembly: AssemblyDescription ("")] +[assembly: AssemblyConfiguration ("")] +[assembly: AssemblyCompany ("Xamarin")] +[assembly: AssemblyProduct ("Xamarin.Android.Build.Utilities")] +[assembly: AssemblyCopyright ("Copyright © Xamarin 2011-2016")] +[assembly: AssemblyTrademark ("")] +[assembly: AssemblyCulture ("")] + +[assembly: AssemblyVersion ("1.0.0.0")] +[assembly: AssemblyFileVersion ("1.0.0.0")] + diff --git a/src/Xamarin.Android.Build.Utilities/SatelliteAssembly.cs b/src/Xamarin.Android.Build.Utilities/SatelliteAssembly.cs new file mode 100644 index 00000000000..5263303d696 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/SatelliteAssembly.cs @@ -0,0 +1,29 @@ +using System; +using System.Text.RegularExpressions; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + public class SatelliteAssembly { + // culture match courtesy: http://stackoverflow.com/a/3962783/83444 + static readonly Regex SatelliteChecker = new Regex ( + Regex.Escape (Path.DirectorySeparatorChar.ToString ()) + + "(?[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*)" + + Regex.Escape (Path.DirectorySeparatorChar.ToString ()) + + string.Format ("(?[^{0}]+.resources.dll)$", Regex.Escape (Path.DirectorySeparatorChar.ToString ()))); + + public static bool TryGetSatelliteCultureAndFileName (string assemblyPath, out string culture, out string fileName) + { + culture = fileName = null; + + var m = SatelliteChecker.Match (assemblyPath); + if (!m.Success) + return false; + + culture = m.Groups ["culture"].Value; + fileName = m.Groups ["file"].Value; + return true; + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkBase.cs b/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkBase.cs new file mode 100644 index 00000000000..1592b12a3f2 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkBase.cs @@ -0,0 +1,144 @@ +using System; +using System.Linq; +using System.IO; +using System.Collections.Generic; + +namespace Xamarin.Android.Build.Utilities +{ + abstract class AndroidSdkBase + { + string[] allAndroidSdks = null; + string[] allAndroidNdks = null; + + public string[] AllAndroidSdks { + get { + if (allAndroidSdks == null) + allAndroidSdks = GetAllAvailableAndroidSdks ().Distinct ().ToArray (); + return allAndroidSdks; + } + } + public string[] AllAndroidNdks { + get { + if (allAndroidNdks == null) + allAndroidNdks = GetAllAvailableAndroidNdks ().Distinct ().ToArray (); + return allAndroidNdks; + } + } + + public string AndroidSdkPath { get; private set; } + public string AndroidNdkPath { get; private set; } + public string JavaSdkPath { get; private set; } + public string JavaBinPath { get; private set; } + public string AndroidToolsPath { get; private set; } + public string AndroidPlatformToolsPath { get; private set; } + public string AndroidToolsPathShort { get; private set; } + public string AndroidPlatformToolsPathShort { get; private set; } + + public abstract string Adb { get; } + public abstract string Android { get; } + public abstract string Emulator { get; } + public abstract string Monitor { get; } + public abstract string ZipAlign { get; } + public abstract string JarSigner { get; } + public abstract string KeyTool { get; } + + public abstract string NdkStack { get; } + public abstract string NdkHostPlatform32Bit { get; } + public abstract string NdkHostPlatform64Bit { get; } + public abstract string Javac { get; } + + public abstract string PreferedAndroidSdkPath { get; } + public abstract string PreferedAndroidNdkPath { get; } + public abstract string PreferedJavaSdkPath { get; } + + public virtual void Initialize (string androidSdkPath = null, string androidNdkPath = null, string javaSdkPath = null) + { + AndroidSdkPath = ValidateAndroidSdkLocation (androidSdkPath) ? androidSdkPath : AllAndroidSdks.FirstOrDefault (); + AndroidNdkPath = ValidateAndroidNdkLocation (androidNdkPath) ? androidNdkPath : AllAndroidNdks.FirstOrDefault (); + JavaSdkPath = ValidateJavaSdkLocation (javaSdkPath) ? javaSdkPath : GetJavaSdkPath (); + + if (!string.IsNullOrEmpty (JavaSdkPath)) { + JavaBinPath = Path.Combine (JavaSdkPath, "bin"); + } else { + JavaBinPath = null; + } + + if (!string.IsNullOrEmpty (AndroidSdkPath)) { + AndroidToolsPath = Path.Combine (AndroidSdkPath, "tools"); + AndroidToolsPathShort = GetShortFormPath (AndroidToolsPath); + AndroidPlatformToolsPath = Path.Combine (AndroidSdkPath, "platform-tools"); + AndroidPlatformToolsPathShort = GetShortFormPath (AndroidPlatformToolsPath); + } else { + AndroidToolsPath = null; + AndroidToolsPathShort = null; + AndroidPlatformToolsPath = null; + AndroidPlatformToolsPathShort = null; + } + + if (!string.IsNullOrEmpty (AndroidNdkPath)) { + // It would be nice if .NET had real globbing support in System.IO... + string toolchainsDir = Path.Combine (AndroidNdkPath, "toolchains"); + IsNdk64Bit = Directory.EnumerateDirectories (toolchainsDir, "arm-linux-androideabi-*") + .Any (dir => Directory.Exists (Path.Combine (dir, "prebuilt", NdkHostPlatform64Bit))); + } + } + + protected abstract IEnumerable GetAllAvailableAndroidSdks (); + protected abstract IEnumerable GetAllAvailableAndroidNdks (); + protected abstract string GetJavaSdkPath (); + protected abstract string GetShortFormPath (string path); + + public abstract void SetPreferredAndroidSdkPath (string path); + public abstract void SetPreferredJavaSdkPath (string path); + public abstract void SetPreferredAndroidNdkPath (string path); + + public bool IsNdk64Bit { get; private set; } + + public string NdkHostPlatform { + get { return IsNdk64Bit ? NdkHostPlatform64Bit : NdkHostPlatform32Bit; } + } + + /// + /// Checks that a value is the location of an Android SDK. + /// + public bool ValidateAndroidSdkLocation (string loc) + { + return !string.IsNullOrEmpty (loc) && File.Exists (Path.Combine (Path.Combine (loc, "platform-tools"), Adb)); + } + + /// + /// Checks that a value is the location of a Java SDK. + /// + public virtual bool ValidateJavaSdkLocation (string loc) + { + return !string.IsNullOrEmpty (loc) && File.Exists (Path.Combine (Path.Combine (loc, "bin"), JarSigner)); + } + + /// + /// Checks that a value is the location of an Android SDK. + /// + public bool ValidateAndroidNdkLocation (string loc) + { + return !string.IsNullOrEmpty (loc) && File.Exists (Path.Combine (loc, NdkStack)); + } + + protected IEnumerable FindExecutableInPath (string executable) + { + var path = Environment.GetEnvironmentVariable ("PATH"); + var pathDirs = path.Split (new char[] { Path.PathSeparator }, StringSplitOptions.RemoveEmptyEntries); + + foreach (var dir in pathDirs) + if (File.Exists (Path.Combine (dir, (executable)))) + yield return dir; + } + + protected string NullIfEmpty (string s) + { + if (s == null || s.Length != 0) + return s; + + return null; + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkUnix.cs b/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkUnix.cs new file mode 100644 index 00000000000..93d624d3ae8 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkUnix.cs @@ -0,0 +1,244 @@ +using System; +using System.Xml; +using System.Xml.Linq; +using System.Collections.Generic; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + class AndroidSdkUnix : AndroidSdkBase + { + public override string Adb { get { return "adb"; } } + public override string Android { get { return "android"; } } + public override string Emulator { get { return "emulator"; } } + public override string Monitor { get { return "monitor"; } } + public override string ZipAlign { get { return "zipalign"; } } + public override string JarSigner { get { return "jarsigner"; } } + public override string KeyTool { get { return "keytool"; } } + public override string NdkStack { get { return "ndk-stack"; } } + + public override string NdkHostPlatform32Bit { + get { return OS.IsMac ? "darwin-x86" : "linux-x86"; } + } + public override string NdkHostPlatform64Bit { + get { return OS.IsMac ? "darwin-x86_64" : "linux-x86_64"; } + } + + public override string Javac { get { return "javac"; } } + + public override string PreferedAndroidSdkPath { + get { + var config_file = GetUnixConfigFile (); + var androidEl = config_file.Root.Element ("android-sdk"); + + if (androidEl != null) { + var path = (string)androidEl.Attribute ("path"); + + if (ValidateAndroidSdkLocation (path)) + return path; + } + return null; + } + } + + public override string PreferedAndroidNdkPath { + get { + var config_file = GetUnixConfigFile (); + var androidEl = config_file.Root.Element ("android-ndk"); + + if (androidEl != null) { + var path = (string)androidEl.Attribute ("path"); + + if (ValidateAndroidNdkLocation (path)) + return path; + } + return null; + } + } + + public override string PreferedJavaSdkPath { + get { + var config_file = GetUnixConfigFile (); + var javaEl = config_file.Root.Element ("java-sdk"); + + if (javaEl != null) { + var path = (string)javaEl.Attribute ("path"); + + if (ValidateJavaSdkLocation (path)) + return path; + } + return null; + } + } + + protected override IEnumerable GetAllAvailableAndroidSdks () + { + var preferedSdkPath = PreferedAndroidSdkPath; + if (!string.IsNullOrEmpty (preferedSdkPath)) + yield return preferedSdkPath; + + // Look in PATH + foreach (var path in FindExecutableInPath (Adb)) { + // Strip off "platform-tools" + var dir = Path.GetDirectoryName (path); + + if (ValidateAndroidSdkLocation (dir)) + yield return dir; + } + } + + protected override string GetJavaSdkPath () + { + var preferedJavaSdkPath = PreferedJavaSdkPath; + if (!string.IsNullOrEmpty (preferedJavaSdkPath)) + return preferedJavaSdkPath; + + // Look in PATH + foreach (var path in FindExecutableInPath (JarSigner)) { + // Strip off "bin" + var dir = Path.GetDirectoryName (path); + + if (ValidateJavaSdkLocation (dir)) + return dir; + } + + return null; + } + + public override bool ValidateJavaSdkLocation (string loc) + { + var result = base.ValidateJavaSdkLocation (loc); + + if (result) { + // handle apple's java stub + const string javaHomeExe = "/usr/libexec/java_home"; + + if (File.Exists (javaHomeExe)) { + // returns true if there is a java installed + var javaHomeTask = ProcessUtils.ExecuteToolAsync (javaHomeExe, + (output) => { + if (output.Contains ("(null)")) { + return false; + } + + return true; + }, System.Threading.CancellationToken.None + ); + + if (!javaHomeTask.Result) { + return false; + } + } + } + + return result; + } + + protected override IEnumerable GetAllAvailableAndroidNdks () + { + var preferedNdkPath = PreferedAndroidNdkPath; + if (!string.IsNullOrEmpty (preferedNdkPath)) + yield return preferedNdkPath; + + // Look in PATH + foreach (var path in FindExecutableInPath (NdkStack)) { + if (ValidateAndroidNdkLocation (path)) + yield return path; + } + } + + protected override string GetShortFormPath (string path) + { + // This is a Windows-ism, don't do anything for Unix + return path; + } + + public override void SetPreferredAndroidSdkPath (string path) + { + path = NullIfEmpty (path); + + var doc = GetUnixConfigFile (); + var androidEl = doc.Root.Element ("android-sdk"); + + if (androidEl == null) { + androidEl = new XElement ("android-sdk"); + doc.Root.Add (androidEl); + } + + androidEl.SetAttributeValue ("path", path); + doc.Save (UnixConfigPath); + } + + public override void SetPreferredJavaSdkPath (string path) + { + path = NullIfEmpty (path); + + var doc = GetUnixConfigFile (); + var javaEl = doc.Root.Element ("java-sdk"); + + if (javaEl == null) { + javaEl = new XElement ("java-sdk"); + doc.Root.Add (javaEl); + } + + javaEl.SetAttributeValue ("path", path); + doc.Save (UnixConfigPath); + } + + public override void SetPreferredAndroidNdkPath (string path) + { + path = NullIfEmpty (path); + + var doc = GetUnixConfigFile (); + var androidEl = doc.Root.Element ("android-ndk"); + + if (androidEl == null) { + androidEl = new XElement ("android-ndk"); + doc.Root.Add (androidEl); + } + + androidEl.SetAttributeValue ("path", path); + doc.Save (UnixConfigPath); + } + + private static string UnixConfigPath { + get { + var p = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData); + return Path.Combine (Path.Combine (p, "xbuild"), "monodroid-config.xml"); + } + } + + private static XDocument GetUnixConfigFile () + { + var file = UnixConfigPath; + XDocument doc = null; + if (!File.Exists (file)) { + string dir = Path.GetDirectoryName (file); + if (!Directory.Exists (dir)) + Directory.CreateDirectory (dir); + } else { + try { + doc = XDocument.Load (file); + } catch (Exception ex) { + AndroidLogger.LogError ("Could not load monodroid configuration file", ex); + + // move out of the way and create a new one + doc = new XDocument (new XElement ("monodroid")); + var newFileName = file + ".old"; + if (File.Exists (newFileName)) { + File.Delete (newFileName); + } + + File.Move (file, newFileName); + } + } + + if (doc == null || doc.Root == null) { + doc = new XDocument (new XElement ("monodroid")); + } + return doc; + } + + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkWindows.cs b/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkWindows.cs new file mode 100644 index 00000000000..33beb3a7e78 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Sdks/AndroidSdkWindows.cs @@ -0,0 +1,205 @@ +using System; +using System.Collections.Generic; +using System.IO; + +namespace Xamarin.Android.Build.Utilities +{ + class AndroidSdkWindows : AndroidSdkBase + { + const string MDREG_KEY = @"SOFTWARE\Novell\Mono for Android"; + const string MDREG_ANDROID_SDK = "AndroidSdkDirectory"; + const string MDREG_ANDROID_NDK = "AndroidNdkDirectory"; + const string MDREG_JAVA_SDK = "JavaSdkDirectory"; + const string ANDROID_INSTALLER_PATH = @"SOFTWARE\Android SDK Tools"; + const string ANDROID_INSTALLER_KEY = "Path"; + const string XAMARIN_ANDROID_INSTALLER_PATH = @"SOFTWARE\Xamarin\MonoAndroid"; + const string XAMARIN_ANDROID_INSTALLER_KEY = "PrivateAndroidSdkPath"; + + public override string Adb { get { return "adb.exe"; } } + public override string Android { get { return "android.bat"; } } + public override string Emulator { get { return "emulator.exe"; } } + public override string Monitor { get { return "monitor.bat"; } } + public override string ZipAlign { get { return "zipalign.exe"; } } + public override string JarSigner { get { return "jarsigner.exe"; } } + public override string KeyTool { get { return "keytool.exe"; } } + public override string NdkStack { get { return "ndk-stack.exe"; } } + public override string NdkHostPlatform32Bit { get { return "windows"; } } + public override string NdkHostPlatform64Bit { get { return "windows-x86_64"; } } + public override string Javac { get { return "javac.exe"; } } + + public override string PreferedAndroidSdkPath { + get { + var wow = RegistryEx.Wow64.Key32; + if (CheckRegistryKeyForExecutable (RegistryEx.CurrentUser, MDREG_KEY, MDREG_ANDROID_SDK, wow, "platform-tools", Adb)) + return RegistryEx.GetValueString (RegistryEx.CurrentUser, MDREG_KEY, MDREG_ANDROID_SDK, wow); + return null; + } + } + public override string PreferedAndroidNdkPath { + get { + var wow = RegistryEx.Wow64.Key32; + if (CheckRegistryKeyForExecutable (RegistryEx.CurrentUser, MDREG_KEY, MDREG_ANDROID_NDK, wow, ".", NdkStack)) + return RegistryEx.GetValueString (RegistryEx.CurrentUser, MDREG_KEY, MDREG_ANDROID_NDK, wow); + return null; + } + } + public override string PreferedJavaSdkPath { + get { + var wow = RegistryEx.Wow64.Key32; + if (CheckRegistryKeyForExecutable (RegistryEx.CurrentUser, MDREG_KEY, MDREG_JAVA_SDK, wow, "bin", JarSigner)) + return RegistryEx.GetValueString (RegistryEx.CurrentUser, MDREG_KEY, MDREG_JAVA_SDK, wow); + return null; + } + } + + protected override IEnumerable GetAllAvailableAndroidSdks () + { + var roots = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine }; + var wow = RegistryEx.Wow64.Key32; + + AndroidLogger.LogInfo ("sdk", "Looking for Android SDK.."); + + // Check for the key the user gave us in the VS/addin options + foreach (var root in roots) + if (CheckRegistryKeyForExecutable (root, MDREG_KEY, MDREG_ANDROID_SDK, wow, "platform-tools", Adb)) + yield return RegistryEx.GetValueString (root, MDREG_KEY, MDREG_ANDROID_SDK, wow); + + // Check for the key written by the Xamarin installer + if (CheckRegistryKeyForExecutable (RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow, "platform-tools", Adb)) + yield return RegistryEx.GetValueString (RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow); + + // Check for the key written by the Android SDK installer + foreach (var root in roots) + if (CheckRegistryKeyForExecutable (root, ANDROID_INSTALLER_PATH, ANDROID_INSTALLER_KEY, wow, "platform-tools", Adb)) + yield return RegistryEx.GetValueString (root, ANDROID_INSTALLER_PATH, ANDROID_INSTALLER_KEY, wow); + + // Check some hardcoded paths for good measure + var xamarin_private = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "Xamarin", "MonoAndroid", "android-sdk-windows"); + var android_default = Path.Combine (OS.ProgramFilesX86, "Android", "android-sdk-windows"); + var cdrive_default = @"C:\android-sdk-windows"; + + if (ValidateAndroidSdkLocation (xamarin_private)) + yield return xamarin_private; + + if (ValidateAndroidSdkLocation (android_default)) + yield return android_default; + + if (ValidateAndroidSdkLocation (cdrive_default)) + yield return cdrive_default; + } + + protected override string GetJavaSdkPath () + { + // check the user specified path + var roots = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine }; + const RegistryEx.Wow64 wow = RegistryEx.Wow64.Key32; + + foreach (var root in roots) { + if (CheckRegistryKeyForExecutable (root, MDREG_KEY, MDREG_JAVA_SDK, wow, "bin", JarSigner)) + return RegistryEx.GetValueString (root, MDREG_KEY, MDREG_JAVA_SDK, wow); + } + + string subkey = @"SOFTWARE\JavaSoft\Java Development Kit"; + + AndroidLogger.LogInfo ("sdk", "Looking for Java 6 SDK.."); + + foreach (var wow64 in new[] { RegistryEx.Wow64.Key32, RegistryEx.Wow64.Key64 }) { + string key_name = string.Format (@"{0}\{1}\{2}", "HKLM", subkey, "CurrentVersion"); + var currentVersion = RegistryEx.GetValueString (RegistryEx.LocalMachine, subkey, "CurrentVersion", wow64); + + if (!string.IsNullOrEmpty (currentVersion)) { + AndroidLogger.LogInfo ("sdk", " Key {0} found.", key_name); + + // No matter what the CurrentVersion is, look for 1.6 or 1.7 or 1.8 + if (CheckRegistryKeyForExecutable (RegistryEx.LocalMachine, subkey + "\\" + "1.6", "JavaHome", wow64, "bin", JarSigner)) + return RegistryEx.GetValueString (RegistryEx.LocalMachine, subkey + "\\" + "1.6", "JavaHome", wow64); + + if (CheckRegistryKeyForExecutable (RegistryEx.LocalMachine, subkey + "\\" + "1.7", "JavaHome", wow64, "bin", JarSigner)) + return RegistryEx.GetValueString (RegistryEx.LocalMachine, subkey + "\\" + "1.7", "JavaHome", wow64); + + if (CheckRegistryKeyForExecutable (RegistryEx.LocalMachine, subkey + "\\" + "1.8", "JavaHome", wow64, "bin", JarSigner)) + return RegistryEx.GetValueString (RegistryEx.LocalMachine, subkey + "\\" + "1.8", "JavaHome", wow64); + } + + AndroidLogger.LogInfo ("sdk", " Key {0} not found.", key_name); + } + + // We ran out of things to check.. + return null; + } + + protected override IEnumerable GetAllAvailableAndroidNdks () + { + var roots = new[] { RegistryEx.CurrentUser, RegistryEx.LocalMachine }; + var wow = RegistryEx.Wow64.Key32; + + AndroidLogger.LogInfo ("sdk", "Looking for Android NDK.."); + + // Check for the key the user gave us in the VS/addin options + foreach (var root in roots) + if (CheckRegistryKeyForExecutable (root, MDREG_KEY, MDREG_ANDROID_NDK, wow, ".", NdkStack)) + yield return RegistryEx.GetValueString (root, MDREG_KEY, MDREG_ANDROID_NDK, wow); + + /* + // Check for the key written by the Xamarin installer + if (CheckRegistryKeyForExecutable (RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow, "platform-tools", Adb)) + yield return RegistryEx.GetValueString (RegistryEx.CurrentUser, XAMARIN_ANDROID_INSTALLER_PATH, XAMARIN_ANDROID_INSTALLER_KEY, wow); + */ + + // Check some hardcoded paths for good measure + var xamarin_private = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), "Xamarin", "MonoAndroid"); + var android_default = Path.Combine (OS.ProgramFilesX86, "Android"); + var cdrive_default = @"C:\"; + + foreach (var basePath in new string [] {xamarin_private, android_default, cdrive_default}) + if (Directory.Exists (basePath)) + foreach (var dir in Directory.GetDirectories (basePath, "android-ndk-r*")) + if (ValidateAndroidNdkLocation (dir)) + yield return dir; + } + + protected override string GetShortFormPath (string path) + { + return KernelEx.GetShortPathName (path); + } + + public override void SetPreferredAndroidSdkPath (string path) + { + RegistryEx.SetValueString (RegistryEx.CurrentUser, MDREG_KEY, MDREG_ANDROID_SDK, path ?? "", RegistryEx.Wow64.Key32); + } + + public override void SetPreferredJavaSdkPath (string path) + { + RegistryEx.SetValueString (RegistryEx.CurrentUser, MDREG_KEY, MDREG_JAVA_SDK, path ?? "", RegistryEx.Wow64.Key32); + } + + public override void SetPreferredAndroidNdkPath (string path) + { + RegistryEx.SetValueString (RegistryEx.CurrentUser, MDREG_KEY, MDREG_ANDROID_NDK, path ?? "", RegistryEx.Wow64.Key32); + } + + #region Helper Methods + private bool CheckRegistryKeyForExecutable (UIntPtr key, string subkey, string valueName, RegistryEx.Wow64 wow64, string subdir, string exe) + { + string key_name = string.Format (@"{0}\{1}\{2}", key == RegistryEx.CurrentUser ? "HKCU" : "HKLM", subkey, valueName); + + var path = NullIfEmpty (RegistryEx.GetValueString (key, subkey, valueName, wow64)); + + if (path == null) { + AndroidLogger.LogInfo ("sdk", " Key {0} not found.", key_name); + return false; + } + + if (!File.Exists (Path.Combine (path, subdir, exe))) { + AndroidLogger.LogInfo ("sdk", " Key {0} found:\n Path does not contain {1} in \\{2} ({3}).", key_name, exe, subdir, path); + return false; + } + + AndroidLogger.LogInfo ("sdk", " Key {0} found:\n Path contains {1} in \\{2} ({3}).", key_name, exe, subdir, path); + + return true; + } + #endregion + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkBase.cs b/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkBase.cs new file mode 100644 index 00000000000..d02dc69fea2 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkBase.cs @@ -0,0 +1,280 @@ +using System; +using System.Linq; +using System.IO; +using System.Collections.Generic; +using System.Xml; + +namespace Xamarin.Android.Build.Utilities +{ + abstract class MonoDroidSdkBase + { + protected readonly static string DebugRuntime = "Mono.Android.DebugRuntime-debug.apk"; + + // I can never remember the difference between SdkPath and anything else... + [Obsolete ("Do not use.")] + public string SdkPath { get; private set; } + + // Contains mandroid + public string BinPath { get; private set; } + + // Not actually shipped... + public string IncludePath { get; private set; } + + // Contains Mono.Android.DebugRuntime-*.apk, platforms/*/*.apk. + public string RuntimePath { get; private set; } + + // Root directory for XA libraries, contains designer dependencies + public string LibrariesPath { get; private set; } + + // Contains mscorlib.dll + public string BclPath { get; private set; } + + public int SharedRuntimeVersion { get; private set; } + + // runtimePath: contains Mono.Android.DebugRuntime-*.apk + // binPath: contains mandroid + // mscorlibDir: contains mscorlib.dll + public void Initialize (string runtimePath = null, string binPath = null, string bclPath = null) + { + runtimePath = GetValidPath ("MonoAndroidToolsPath", runtimePath, ValidateRuntime, () => FindRuntime ()); + if (runtimePath != null) { + binPath = GetValidPath ("MonoAndroidBinPath", binPath, ValidateBin, () => FindBin (runtimePath)); + bclPath = GetValidPath ("mscorlib.dll", bclPath, ValidateFramework, () => FindFramework (runtimePath)); + } else { + binPath = bclPath = null; + } + + if (runtimePath == null || binPath == null || bclPath == null) { + Reset (); + return; + } + + RuntimePath = runtimePath; + #pragma warning disable 0618 + SdkPath = Path.GetFullPath (Path.Combine (runtimePath, "..", "..")); + #pragma warning restore 0618 + BinPath = binPath; + BclPath = bclPath; + LibrariesPath = FindLibraries (runtimePath); + + IncludePath = FindInclude (runtimePath); + if (IncludePath != null && !Directory.Exists (IncludePath)) + IncludePath = null; + + SharedRuntimeVersion = GetCurrentSharedRuntimeVersion (); + FindSupportedFrameworks (); + } + + static string GetValidPath (string description, string path, Func validator, Func defaultPath) + { + if (!string.IsNullOrEmpty (path) && Directory.Exists (path)) { + if (validator (path)) + return path; + AndroidLogger.LogWarning ("{0} path {1} is not valid; skipping.", description, path); + } + path = defaultPath (); + if (path != null && validator (path)) + return path; + return null; + } + + public void Reset () + { + #pragma warning disable 0618 + SdkPath = BinPath = IncludePath = RuntimePath = BclPath = null; + #pragma warning restore 0618 + SharedRuntimeVersion = 0; + } + + protected abstract string FindRuntime (); + protected abstract string FindFramework (string runtimePath); + + // Check for platform-specific `mandroid` name + protected abstract bool ValidateBin (string binPath); + + protected static bool ValidateRuntime (string loc) + { + return !string.IsNullOrWhiteSpace (loc) && + (File.Exists (Path.Combine (loc, DebugRuntime)) || // Normal/expected + File.Exists (Path.Combine (loc, "Ionic.Zip.dll"))); // Wrench builds + } + + protected static bool ValidateFramework (string loc) + { + return loc != null && File.Exists (Path.Combine (loc, "mscorlib.dll")); + } + + public string FindVersionFile () + { + #pragma warning disable 0618 + if (string.IsNullOrEmpty (SdkPath)) + return null; + #pragma warning restore 0618 + foreach (var loc in GetVersionFileLocations ()) { + if (File.Exists (loc)) { + return loc; + } + } + return null; + } + + protected virtual IEnumerable GetVersionFileLocations () + { + #pragma warning disable 0618 + yield return Path.Combine (SdkPath, "Version"); + #pragma warning restore 0618 + } + + protected abstract string FindBin (string runtimePath); + + protected abstract string FindInclude (string runtimePath); + + protected abstract string FindLibraries (string runtimePath); + + [Obsolete ("Do not use.")] + public string GetPlatformNativeLibPath (string abi) + { + return FindPlatformNativeLibPath (SdkPath, abi); + } + + [Obsolete ("Do not use.")] + public string GetPlatformNativeLibPath (AndroidTargetArch arch) + { + return FindPlatformNativeLibPath (SdkPath, GetMonoDroidArchName (arch)); + } + + [Obsolete ("Do not use.")] + static string GetMonoDroidArchName (AndroidTargetArch arch) + { + switch (arch) { + case AndroidTargetArch.Arm: + return "armeabi"; + case AndroidTargetArch.Mips: + return "mips"; + case AndroidTargetArch.X86: + return "x86"; + } + return null; + } + + [Obsolete] + protected string FindPlatformNativeLibPath (string sdk, string arch) + { + return Path.Combine (sdk, "lib", arch); + } + + static XmlReaderSettings GetSafeReaderSettings () + { + //allow DTD but not try to resolve it from web + return new XmlReaderSettings { + CloseInput = true, + DtdProcessing = DtdProcessing.Ignore, + XmlResolver = null, + }; + } + + int GetCurrentSharedRuntimeVersion () + { + string file = Path.Combine (RuntimePath, "Mono.Android.DebugRuntime-debug.xml"); + + return GetManifestVersion (file); + } + + internal static int GetManifestVersion (string file) + { + // It seems that MfA 1.0 on Windows didn't include the xml files to get the runtime version. + if (!File.Exists (file)) + return int.MaxValue; + + try { + using (var r = XmlReader.Create (file, GetSafeReaderSettings())) { + if (r.MoveToContent () == XmlNodeType.Element && r.MoveToAttribute ("android:versionCode")) { + int value; + if (int.TryParse (r.Value, out value)) + return value; + AndroidLogger.LogInfo ("Cannot parse runtime version code: ({0})", r.Value); + } + } + } catch (Exception ex) { + AndroidLogger.LogError ("Error trying to find shared runtime version", ex); + } + return int.MaxValue; + } + + internal static Version ToVersion (string frameworkDir) + { + string version = Path.GetFileName (frameworkDir); + if (!version.StartsWith ("v", StringComparison.OrdinalIgnoreCase)) { + // wat? + return new Version (); + } + version = version.Substring (1); + Version v; + if (Version.TryParse (version, out v)) + return v; + return new Version (); + } + + void FindSupportedFrameworks () + { + string bclDir = MonoDroidSdk.FrameworkPath; + string frameworksDir = Path.GetDirectoryName (bclDir); + foreach (var framework in Directory.EnumerateDirectories (frameworksDir).Select (ToVersion)) { + if (framework.Major == 0) + continue; + string apiLevel; + if (FrameworkToApiLevels.TryGetValue (framework, out apiLevel)) + SupportedFrameworks.Add (framework, apiLevel); + } + } + + readonly Dictionary SupportedFrameworks = new Dictionary (); + + static readonly Dictionary FrameworkToApiLevels = new Dictionary (AndroidVersion.KnownVersions.ToDictionary (k => k.Version, v => v.ApiLevel.ToString ())); + static readonly Dictionary LegacyFrameworkToApiLevels = new Dictionary { + { new Version (4, 5), "21" } // L Preview + }; + + public IEnumerable GetSupportedApiLevels () + { + return SupportedFrameworks.Select (e => e.Value); + } + + public string GetApiLevelForFrameworkVersion (string framework) + { + Version v; + if (!Version.TryParse (framework.TrimStart ('v'), out v)) + return null; + string apiLevel; + if (SupportedFrameworks.TryGetValue (v, out apiLevel) + || FrameworkToApiLevels.TryGetValue (v, out apiLevel) + || LegacyFrameworkToApiLevels.TryGetValue (v, out apiLevel)) + return apiLevel; + return null; + } + + public string GetFrameworkVersionForApiLevel (string apiLevel) + { + // API level 9 was discontinued immediately for 10, in the rare case we get it just upgrade the number + if (apiLevel == "9") + apiLevel = "10"; + var maxFrameworkVersion = SupportedFrameworks.Concat (FrameworkToApiLevels) + .Where (e => e.Value == apiLevel) + .OrderByDescending (e => e.Key, Comparer.Default) + .Select (e => e.Key) + .FirstOrDefault (); + if (maxFrameworkVersion != null) + return "v" + maxFrameworkVersion; + return null; + } + + /// + /// Determines if the given apiLevel is supported by an installed Framework + /// + public bool IsSupportedFrameworkLevel (string apiLevel) + { + return SupportedFrameworks.Any ((sf => sf.Value == apiLevel)); + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkUnix.cs b/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkUnix.cs new file mode 100644 index 00000000000..4a8f7709968 --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkUnix.cs @@ -0,0 +1,95 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace Xamarin.Android.Build.Utilities +{ + class MonoDroidSdkUnix : MonoDroidSdkBase + { + readonly static string[] RuntimeToFrameworkPaths = new[]{ + Path.Combine ("..", "..", "..", ".xamarin.android", "lib", "xbuild-frameworks", "MonoAndroid"), + Path.Combine ("..", "xbuild-frameworks", "MonoAndroid"), + Path.Combine ("..", "mono", "2.1"), + }; + + readonly static string[] SearchPaths = { + "/Library/Frameworks/Xamarin.Android.framework/Versions/Current/lib/mandroid", + "/Developer/MonoAndroid/usr/lib/mandroid", + "/opt/mono-android/lib/mandroid" + }; + + protected override string FindRuntime () + { + string monoAndroidPath = Environment.GetEnvironmentVariable ("MONO_ANDROID_PATH"); + if (!string.IsNullOrEmpty (monoAndroidPath)) { + string libMandroid = Path.Combine (monoAndroidPath, "lib", "mandroid"); + if (Directory.Exists (libMandroid) && ValidateRuntime (libMandroid)) + return libMandroid; + } + + // check also in the users folder + var personal = Environment.GetFolderPath (Environment.SpecialFolder.Personal); + var additionalSearchPaths = new [] { Path.Combine (personal, @".xamarin.android/lib/mandroid") }; + + return additionalSearchPaths.Concat (SearchPaths).FirstOrDefault (ValidateRuntime); + } + + protected override bool ValidateBin (string binPath) + { + return !string.IsNullOrWhiteSpace (binPath) && + File.Exists (Path.Combine (binPath, "generator")); + } + + protected override string FindFramework (string runtimePath) + { + foreach (var relativePath in RuntimeToFrameworkPaths) { + var fullPath = Path.GetFullPath (Path.Combine (runtimePath, relativePath)); + if (Directory.Exists (fullPath)) { + if (ValidateFramework (fullPath)) + return fullPath; + + // check to see if full path is the folder that contains each framework version, eg contains folders of the form v1.0, v2.3 etc + var subdirs = Directory.GetDirectories (fullPath, "v*").OrderBy (x => x).ToArray (); + foreach (var subdir in subdirs) { + if (ValidateFramework (subdir)) + return subdir; + } + } + } + + return null; + } + + protected override string FindBin (string runtimePath) + { + string binPath = Path.GetFullPath (Path.Combine (runtimePath, "..", "..", "bin")); + Console.WriteLine (binPath); + if (File.Exists (Path.Combine (binPath, "generator"))) + return binPath; + return null; + } + + protected override string FindInclude (string runtimePath) + { + string includeDir = Path.GetFullPath (Path.Combine (runtimePath, "..", "..", "include")); + if (Directory.Exists (includeDir)) + return includeDir; + return null; + } + + protected override string FindLibraries (string runtimePath) + { + return Path.GetFullPath (Path.Combine (runtimePath, "..")); + } + + protected override IEnumerable GetVersionFileLocations () + { + yield return Path.GetFullPath (Path.Combine (RuntimePath, "..", "..", "Version")); + string sdkPath = Path.GetDirectoryName (Path.GetDirectoryName (RuntimePath)); + if (Path.GetFileName (sdkPath) == "usr") + yield return Path.GetFullPath (Path.Combine (Path.GetDirectoryName (sdkPath), "Version")); + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkWindows.cs b/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkWindows.cs new file mode 100644 index 00000000000..a5905cec24c --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Sdks/MonoDroidSdkWindows.cs @@ -0,0 +1,76 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Linq; + +namespace Xamarin.Android.Build.Utilities +{ + class MonoDroidSdkWindows : MonoDroidSdkBase + { + protected override string FindRuntime () + { + string monoAndroidPath = Environment.GetEnvironmentVariable ("MONO_ANDROID_PATH"); + if (!string.IsNullOrEmpty (monoAndroidPath)) { + string libMandroid = Path.Combine (monoAndroidPath, "lib", "mandroid"); + if (Directory.Exists (libMandroid) && ValidateRuntime (libMandroid)) + return libMandroid; + } + string xamarinSdk = Path.Combine (OS.ProgramFilesX86, "MSBuild", "Xamarin", "Android"); + return Directory.Exists (xamarinSdk) + ? xamarinSdk + : OS.ProgramFilesX86 + @"\MSBuild\Novell"; + } + + static readonly string[] RuntimeToFrameworkPaths = new []{ + Path.Combine ("..", "..", "..", "Reference Assemblies", "Microsoft", "Framework", "MonoAndroid"), + Path.Combine (OS.ProgramFilesX86, "Reference Assemblies", "Microsoft", "Framework", "MonoAndroid"), + }; + + protected override string FindFramework (string runtimePath) + { + foreach (var relativePath in RuntimeToFrameworkPaths) { + var fullPath = Path.GetFullPath (Path.Combine (runtimePath, relativePath)); + if (Directory.Exists (fullPath)) { + if (ValidateFramework (fullPath)) + return fullPath; + + // check to see if full path is the folder that contains each framework version, eg contains folders of the form v1.0, v2.3 etc + var subdirs = Directory.GetDirectories (fullPath, "v*").OrderBy (x => x).ToArray (); + foreach (var subdir in subdirs) { + if (ValidateFramework (subdir)) + return subdir; + } + } + } + + return null; + } + + protected override string FindBin (string runtimePath) + { + return runtimePath; + } + + protected override bool ValidateBin (string binPath) + { + return !string.IsNullOrWhiteSpace (binPath) && + File.Exists (Path.Combine (binPath, "generator.exe")); + } + + protected override string FindInclude (string runtimePath) + { + return Path.GetFullPath (Path.Combine (runtimePath, "include")); + } + + protected override string FindLibraries (string runtimePath) + { + return Path.GetFullPath (runtimePath); + } + + protected override IEnumerable GetVersionFileLocations () + { + yield return Path.GetFullPath (Path.Combine (RuntimePath, "Version")); + } + } +} + diff --git a/src/Xamarin.Android.Build.Utilities/Xamarin.Android.Build.Utilities.csproj b/src/Xamarin.Android.Build.Utilities/Xamarin.Android.Build.Utilities.csproj new file mode 100644 index 00000000000..635201324ac --- /dev/null +++ b/src/Xamarin.Android.Build.Utilities/Xamarin.Android.Build.Utilities.csproj @@ -0,0 +1,59 @@ + + + + Debug + AnyCPU + {91713046-C358-4647-B162-ED4E1442F3D8} + Library + Xamarin.Android.Build.Utilities + Xamarin.Android.Build.Utilities + v4.5 + + + true + full + false + ..\..\bin\$(Configuration)\lib\xbuild\Xamarin\Android + DEBUG; + prompt + 4 + false + + + full + true + ..\..\bin\$(Configuration)\lib\xbuild\Xamarin\Android + prompt + 4 + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks.csproj b/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks.csproj index 4f8b135c3e4..f667bb116f8 100644 --- a/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks.csproj +++ b/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks.csproj @@ -35,6 +35,7 @@ + diff --git a/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateProfile.cs b/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateProfile.cs new file mode 100644 index 00000000000..c7c9fdda82a --- /dev/null +++ b/src/Xamarin.Android.Tools.BootstrapTasks/Xamarin.Android.Tools.BootstrapTasks/GenerateProfile.cs @@ -0,0 +1,47 @@ +using System; +using System.IO; +using System.Linq; +using System.Text; +using Microsoft.Build.Framework; +using Microsoft.Build.Utilities; + +namespace Xamarin.Android.Tools.BootstrapTasks +{ + public class GenerateProfile : Task + { + [Required] + public ITaskItem[] Files { get; set; } + + [Required] + public ITaskItem OutputFile { get; set; } + + public override bool Execute () + { + Log.LogMessage (MessageImportance.Low, "Task GenerateProfile"); + Log.LogMessage (MessageImportance.Low, "\tOutputFile : {0}", OutputFile); + Log.LogMessage (MessageImportance.Low, "\tFiles : "); + foreach (var file in Files) { + Log.LogMessage (MessageImportance.Low, "\t\t{0}", file.ItemSpec); + } + + var sb = new StringBuilder (); + sb.AppendLine ("using System.Collections.Generic;"); + sb.AppendLine (); + sb.AppendLine ("namespace Xamarin.Android.Tasks {"); + sb.AppendLine ("\tpublic partial class Profile {"); + sb.AppendLine ("\t\t// KEEP THIS SORTED ALPHABETICALLY, CASE-INSENSITIVE"); + sb.AppendLine ("\t\tpublic static readonly string [] SharedRuntimeAssemblies = new []{"); + foreach (var file in Files.Select(x => Path.GetFileName (x.ItemSpec)).Distinct().OrderBy(x => x)) { + sb.AppendFormat ("\t\t\t\"{0}\"," + Environment.NewLine, file); + } + sb.AppendLine ("\t\t};"); + sb.AppendLine ("\t}"); + sb.AppendLine ("}"); + + File.WriteAllText (OutputFile.ItemSpec, sb.ToString ()); + + return !Log.HasLoggedErrors; + } + } +} + diff --git a/src/Xamarin.AndroidTools.Aidl/AidlAst.cs b/src/Xamarin.AndroidTools.Aidl/AidlAst.cs new file mode 100644 index 00000000000..caaabc01ea1 --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/AidlAst.cs @@ -0,0 +1,170 @@ +using System; +using System.Linq; +using Irony.Parsing; + +namespace Xamarin.AndroidTools.Aidl +{ + public class CompilationUnit + { + public CompilationUnit (TypeName package, TypeName [] imports, ITypeDeclaration [] types) + { + Package = package; + Imports = imports; + Types = types; + } + + public TypeName Package { get; private set; } + public TypeName [] Imports { get; private set; } + public ITypeDeclaration [] Types { get; private set; } + } + + public interface ITypeDeclaration + { + } + + public class Parcelable : ITypeDeclaration + { + public Parcelable (TypeName name) + { + Name = name; + } + + public TypeName Name { get; private set; } + } + + public class Interface : ITypeDeclaration + { + public Interface (string modifier, string name, Method [] methods) + { + Modifier = modifier; + JavaName = name; + Name = name [0] == 'I' ? name : 'I' + name; + Methods = methods ?? new Method [0]; + } + + public string Modifier { get; private set; } + public string JavaName { get; private set; } + public string Name { get; private set; } + public Method [] Methods { get; private set; } + } + + public class Method + { + public Method (string modifier, TypeName returnType, string name, Argument [] args) + { + Modifier = modifier; + ReturnType = returnType; + JavaName = name; + Name = Util.ToPascalCase (name); + Arguments = args ?? new Argument [0]; + } + + public string Modifier { get; private set; } + public TypeName ReturnType { get; private set; } + public string JavaName { get; private set; } + public string Name { get; private set; } + public Argument [] Arguments { get; private set; } + } + + public class Argument + { + public Argument (string modifier, TypeName type, string name) + { + Modifier = modifier; + Type = type; + Name = name; + } + + public string Modifier { get; private set; } + public TypeName Type { get; private set; } + public string Name { get; private set; } + } + + public class TypeName + { + public TypeName (string [] identifiers) + { + if (identifiers == null) + throw new ArgumentNullException ("identifiers"); + if (identifiers.Any (i => i == null)) + throw new ArgumentException ("'identifiers' contain one or more null values: {0}", String.Concat (identifiers)); + this.Identifiers = identifiers; + } + + public TypeName (string [] identifiers, TypeName [] genericArguments) + { + if (identifiers == null) + throw new ArgumentNullException ("identifiers"); + if (identifiers.Any (i => i == null)) + throw new ArgumentException ("'identifiers' contain one or more null values: {0}", String.Concat (identifiers)); + if (genericArguments != null && genericArguments.Any (g => g == null)) + throw new ArgumentException (String.Format ("With {0} elementType, 'genericArguments' contain one or more null values", String.Concat (".", identifiers))); + this.Identifiers = identifiers; + this.GenericArguments = genericArguments; + } + + public string [] Identifiers { get; private set; } + //public TypeName ElementType { get; private set; } + public TypeName [] GenericArguments { get; private set; } + public int ArrayDimension { get; internal set; } + + public string GetNamespace () + { + return GetFormattedIdentifiers (Identifiers.Length - 1); + } + + public string GetPackage () + { + return GetJavaIdentifiers (Identifiers.Length - 1); + } + + public override string ToString () + { + string baseName = GetFormattedIdentifiers (Identifiers.Length); + if (GenericArguments != null) + baseName += "<" + String.Join (",", (from g in GenericArguments select g.ToString ()).ToArray ()) + ">"; + for (int i = 0; i < ArrayDimension; i++) + baseName += " []"; + return baseName; + } + + public string ToJavaString () + { + string baseName = GetJavaIdentifiers (Identifiers.Length); + if (GenericArguments != null) + baseName += "<" + String.Join (",", (from g in GenericArguments select g.ToJavaString ()).ToArray ()) + ">"; + for (int i = 0; i < ArrayDimension; i++) + baseName += " []"; + return baseName; + } + + string GetJavaIdentifiers (int count) + { + return String.Join (".", Identifiers.Take (count)); + } + + string GetFormattedIdentifiers (int count) + { + return String.Join (".", (from n in Identifiers.Take (count) select Util.ToPascalCase (n)).ToArray ()); + } + } + + static class Util + { + public static string ToPascalCase (string s) + { + switch (s) { + case "int": + case "string": + case "void": + case "long": + case "float": + case "double": + case "byte": + case "char": + return s; + } + return Char.ToUpper (s [0]) + s.Substring (1); + } + } +} diff --git a/src/Xamarin.AndroidTools.Aidl/AidlCompiler.cs b/src/Xamarin.AndroidTools.Aidl/AidlCompiler.cs new file mode 100644 index 00000000000..372cb6b927f --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/AidlCompiler.cs @@ -0,0 +1,82 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using Irony.Parsing; +using Mono.Cecil; + +namespace Xamarin.AndroidTools.Aidl +{ + public class AidlCompiler + { + public class Result + { + public Result () + { + LogMessages = new Dictionary (); + } + + public IDictionary LogMessages { get; private set; } + } + + Dictionary units = new Dictionary (); + string output_path; + string output_ns; + + public event Action FileWritten; + + public Result Run (ConverterOptions opts, Func resolveAssembly) + { + return Run (opts, resolveAssembly, ((path, sourceFile) => { + var file = Path.GetFileNameWithoutExtension (sourceFile); + file = Path.Combine (output_path, /*file [0] == 'I' ? file : 'I' +*/ file) + ".cs"; + return file; + })); + } + + public Result Run (ConverterOptions opts, Func resolveAssembly, Func outputFilenameSelector) + { + var result = new Result (); + var database = new BindingDatabase (opts.References, resolveAssembly); + var lang = new LanguageData (new AidlGrammar () { LanguageFlags = LanguageFlags.Default | LanguageFlags.CreateAst }); + var parser = new Parser (lang); + output_path = opts.OutputDirectory; + output_ns = opts.OutputNS; + foreach (var file in opts.InputFiles) { + var pt = parser.Parse (File.ReadAllText (file), file); + if (pt.HasErrors ()) + foreach (var l in pt.ParserMessages) + result.LogMessages.Add (file, l); + else + units.Add (file, (CompilationUnit) pt.Root.AstNode); + } + if (result.LogMessages.Any (e => e.Value.Level == Irony.ErrorLevel.Error)) + return result; + + if (output_path != null && !Directory.Exists (output_path)) + Directory.CreateDirectory (output_path); + output_path = output_path ?? "./"; + + var parcelables = new List (); + foreach (var u in units.Values) + foreach (Parcelable t in u.Types.Where (t => t is Parcelable)) + parcelables.Add (u.Package == null ? t.Name : new TypeName (u.Package.Identifiers.Concat (t.Name.Identifiers).ToArray ())); + + foreach (var pair in units) { + string file = outputFilenameSelector (output_path, pair.Key); + var sw = new StringWriter (); + new CSharpCodeGenerator (sw, database).GenerateCode (pair.Value, parcelables, opts); + string csharp = sw.ToString (); + if (pair.Value.Package != null && output_ns != null) + csharp = csharp.Replace (pair.Value.Package.ToString (), output_ns); + using (var fw = File.CreateText (file)) + fw.Write (csharp); + FileWritten (file, csharp); + } + + return result; + } + } +} + diff --git a/src/Xamarin.AndroidTools.Aidl/AidlParser.cs b/src/Xamarin.AndroidTools.Aidl/AidlParser.cs new file mode 100644 index 00000000000..ebe7765e30f --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/AidlParser.cs @@ -0,0 +1,220 @@ +using System; +using System.Linq; +using Irony.Parsing; +using Irony.Ast; + +namespace Xamarin.AndroidTools.Aidl +{ + [Language ("AIDL", "1.0", "AIDL pseudo grammar")] + public partial class AidlGrammar : Grammar + { + NonTerminal DefaultNonTerminal (string label) + { + var nt = new NonTerminal (label); + nt.AstConfig.NodeCreator = delegate { throw new NotImplementedException (label); }; + return nt; + } + + KeyTerm Keyword (string label) + { + var ret = ToTerm (label); + ret.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + node.AstNode = node.Token.ValueString; + }; + return ret; + } + + AstNodeCreator CreateArrayCreator () + { + return delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = (from n in node.ChildNodes select (T) n.AstNode).ToArray (); + }; + } + + void SelectSingleChild (AstContext ctx, ParseTreeNode node) + { + ProcessChildren (ctx, node); + if (node.ChildNodes.Count == 1) + node.AstNode = node.ChildNodes.First ().AstNode; + } + + void ProcessChildren (AstContext ctx, ParseTreeNode node) + { + foreach (var cn in node.ChildNodes) { + if (cn.Term.AstConfig.NodeCreator != null) + cn.Term.AstConfig.NodeCreator (ctx, cn); + } + } + + void DoNothing (AstContext ctx, ParseTreeNode node) + { + // do nothing. + } + + public AidlGrammar () + { + CommentTerminal single_line_comment = new CommentTerminal ("SingleLineComment", "//", "\r", "\n"); + CommentTerminal delimited_comment = new CommentTerminal ("DelimitedComment", "/*", "*/"); + + NonGrammarTerminals.Add (single_line_comment); + NonGrammarTerminals.Add (delimited_comment); + + IdentifierTerminal identifier = TerminalFactory.CreateCSharpIdentifier ("Identifier"); + identifier.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + node.AstNode = node.Token.ValueString; + }; + + KeyTerm keyword_package = Keyword ("package"); + KeyTerm keyword_import = Keyword ("import"); + KeyTerm keyword_parcelable = Keyword ("parcelable"); + KeyTerm keyword_interface = Keyword ("interface"); + KeyTerm keyword_oneway = Keyword ("oneway"); + KeyTerm keyword_in = Keyword ("in"); + KeyTerm keyword_out = Keyword ("out"); + KeyTerm keyword_inout = Keyword ("inout"); + + var compile_unit = DefaultNonTerminal ("compile_unit"); + var opt_package_decl = DefaultNonTerminal ("opt_package_declaration"); + var package_decl = DefaultNonTerminal ("package_declaration"); + var imports = DefaultNonTerminal ("imports"); + var import = DefaultNonTerminal ("import"); + var type_decls = DefaultNonTerminal ("type_decls"); + var type_decl = DefaultNonTerminal ("type_decl"); + var parcelable_decl = DefaultNonTerminal ("parcelable_decl"); + var interface_decl = DefaultNonTerminal ("interface_declaration"); + var interface_external_decl = DefaultNonTerminal ("interface_external_declaration"); + var interface_local_decl = DefaultNonTerminal ("interface_local_declaration"); + var interface_body = DefaultNonTerminal ("interface_body"); + var interface_members = DefaultNonTerminal ("interface_members"); + var method_decl = DefaultNonTerminal ("method_decl"); + var opt_interface_modifier = DefaultNonTerminal ("opt_interface_modifier"); + var opt_method_modifier = DefaultNonTerminal ("opt_method_modifier"); + var argument_decls = DefaultNonTerminal ("argument_declarations"); + var argument_decl = DefaultNonTerminal ("argument_declaration"); + var type_name = DefaultNonTerminal ("type_name"); + var dotted_identifier = DefaultNonTerminal ("dotted_identifier"); + var array_type = DefaultNonTerminal ("array_type"); + var generic_type = DefaultNonTerminal ("generic_type"); + var generic_arguments = DefaultNonTerminal ("generic_arguments"); + var opt_parameter_modifier = DefaultNonTerminal ("opt_parameter_modifier"); + +// + + compile_unit.Rule = opt_package_decl + imports + type_decls; + imports.Rule = MakeStarRule (imports, null, import); + compile_unit.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = new CompilationUnit ((TypeName) node.ChildNodes [0].AstNode, + (TypeName []) node.ChildNodes [1].AstNode, + (ITypeDeclaration []) node.ChildNodes [2].AstNode); + }; + + opt_package_decl.Rule = package_decl | Empty; + opt_package_decl.AstConfig.NodeCreator = SelectSingleChild; + package_decl.Rule = keyword_package + type_name + ";"; + package_decl.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = node.ChildNodes [1].AstNode; + }; + + imports.Rule = MakeStarRule (imports, null, import); + imports.AstConfig.NodeCreator = CreateArrayCreator (); + + import.Rule = keyword_import + type_name + ";"; + import.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = node.ChildNodes [1].AstNode; + }; + + type_decls.Rule = MakePlusRule (type_decls, type_decl); + type_decls.AstConfig.NodeCreator = CreateArrayCreator ();; + type_decl.Rule = parcelable_decl | interface_decl; + type_decl.AstConfig.NodeCreator = SelectSingleChild; + + parcelable_decl.Rule = keyword_parcelable + dotted_identifier + ";"; + parcelable_decl.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = new Parcelable ((TypeName) node.ChildNodes [1].AstNode); + }; + + interface_decl.Rule = interface_local_decl | interface_external_decl; + interface_decl.AstConfig.NodeCreator = SelectSingleChild; + interface_external_decl.Rule = opt_interface_modifier + keyword_interface + dotted_identifier + ";"; + interface_external_decl.AstConfig.NodeCreator = DoNothing; + interface_local_decl.Rule = opt_interface_modifier + keyword_interface + identifier + interface_body; + interface_body.Rule = "{" + interface_members + "}"; + interface_local_decl.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = new Interface ((string) node.ChildNodes [0].AstNode, + (string) node.ChildNodes [2].AstNode, + (Method []) node.ChildNodes [3].AstNode); + }; + interface_body.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = (Method []) node.ChildNodes [1].AstNode; + }; + opt_interface_modifier.Rule = Empty | keyword_oneway; + opt_interface_modifier.AstConfig.NodeCreator = SelectSingleChild; + + interface_members.Rule = MakeStarRule (interface_members, null, method_decl); + interface_members.AstConfig.NodeCreator = CreateArrayCreator (); + + method_decl.Rule = opt_method_modifier + type_name + identifier + "(" + argument_decls + ")" + ";"; + method_decl.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = new Method ( + (string) node.ChildNodes [0].AstNode, + (TypeName) node.ChildNodes [1].AstNode, + (string) node.ChildNodes [2].AstNode, + (Argument []) node.ChildNodes [4].AstNode); + }; + + opt_method_modifier.Rule = Empty | keyword_oneway; + opt_method_modifier.AstConfig.NodeCreator = SelectSingleChild; + + argument_decls.Rule = MakeStarRule (argument_decls, ToTerm (","), argument_decl); + argument_decls.AstConfig.NodeCreator = CreateArrayCreator (); + + argument_decl.Rule = opt_parameter_modifier + type_name + identifier; + argument_decl.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = new Argument ((string) node.ChildNodes [0].AstNode, (TypeName) node.ChildNodes [1].AstNode, (string) node.ChildNodes [2].AstNode); + }; + + opt_parameter_modifier.Rule = Empty | keyword_in | keyword_out | keyword_inout; + opt_parameter_modifier.AstConfig.NodeCreator = SelectSingleChild; + + type_name.Rule = dotted_identifier | array_type | generic_type; + type_name.AstConfig.NodeCreator = SelectSingleChild; + + array_type.Rule = type_name + "[" + "]"; + array_type.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + var tn = (TypeName) node.ChildNodes [0].AstNode; + tn.ArrayDimension++; + node.AstNode = tn; + }; + + generic_type.Rule = dotted_identifier + "<" + generic_arguments + ">"; + generic_type.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + node.AstNode = new TypeName (((TypeName) node.ChildNodes [0].AstNode).Identifiers, (TypeName []) node.ChildNodes [2].AstNode); + }; + + generic_arguments.Rule = MakePlusRule (generic_arguments, ToTerm (","), type_name); + generic_arguments.AstConfig.NodeCreator = CreateArrayCreator (); + + dotted_identifier.Rule = MakePlusRule (dotted_identifier, ToTerm ("."), identifier); + dotted_identifier.AstConfig.NodeCreator = delegate (AstContext ctx, ParseTreeNode node) { + ProcessChildren (ctx, node); + var last = node.ChildNodes.Last ().Term.Name; + last = Char.ToUpper (last [0]) + last.Substring (1); + node.AstNode = new TypeName ((from n in node.ChildNodes select n.Token.ValueString).ToArray ()); + }; + + this.Root = compile_unit; + } + } +} + diff --git a/src/Xamarin.AndroidTools.Aidl/BindingDatabase.cs b/src/Xamarin.AndroidTools.Aidl/BindingDatabase.cs new file mode 100644 index 00000000000..df3d71bd8e8 --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/BindingDatabase.cs @@ -0,0 +1,63 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Mono.Cecil; + +namespace Xamarin.AndroidTools.Aidl +{ + public class BindingDatabase + { + Dictionary nsmap; + Dictionary regs; + List asses = new List (); + + public BindingDatabase (IEnumerable assemblies, Func resolveAssembly) + { + foreach (var assfile in assemblies) { + var ass = resolveAssembly (assfile); + if (ass == null) + throw new InvalidOperationException ("Failed to resolve specified assembly"); + asses.Add (ass); + } + Initialize (assemblies, resolveAssembly); + } + + public BindingDatabase (IEnumerable asses) + { + this.asses.AddRange (asses); + } + + public IDictionary NamespaceMappings { + get { return nsmap; } + } + + public IDictionary RegisteredTypes { + get { return regs; } + } + + void Initialize (IEnumerable assemblies, Func resolveAssembly) + { + var d = new Dictionary (); + nsmap = d; + var r = new Dictionary (); + regs = r; + + foreach (var ass in asses) { + if (!ass.CustomAttributes.Any (a => a.AttributeType.FullName != "Android.Runtime.NamespaceMappingAttribute")) + continue; // irrelevant assembly. + foreach (var att in ass.CustomAttributes) { + if (att.AttributeType.FullName != "Android.Runtime.NamespaceMappingAttribute") + continue; + string java = (string) att.Properties.First (p => p.Name == "Java").Argument.Value; + string cs = (string) att.Properties.First (p => p.Name == "Managed").Argument.Value; + d [java] = cs; + } + foreach (var md in ass.Modules) + foreach (var td in md.Types.Where (t => t.IsPublic || t.IsNestedPublic)) + foreach (var att in td.CustomAttributes.Where (a => a.AttributeType.FullName == "Android.Runtime.RegisterAttribute")) + r [((string) att.ConstructorArguments [0].Value).Replace ('/', '.').Replace ('$', '.')] = td.FullName; + } + } + } +} + diff --git a/src/Xamarin.AndroidTools.Aidl/CSharpCodeGenerator.cs b/src/Xamarin.AndroidTools.Aidl/CSharpCodeGenerator.cs new file mode 100644 index 00000000000..c83c4b16753 --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/CSharpCodeGenerator.cs @@ -0,0 +1,661 @@ +using System; +using System.Collections.Generic; +using Microsoft.CSharp; +using System.IO; +using System.Linq; + +namespace Xamarin.AndroidTools.Aidl +{ + public class CSharpCodeGenerator + { + class NameResoltionCache + { + BindingDatabase database; + CompilationUnit unit; + Dictionary cache = new Dictionary (); + IList parcelable_names; + + public NameResoltionCache (BindingDatabase database, CompilationUnit unit, IList parcelableNames) + { + this.database = database; + this.unit = unit; + parcelable_names = parcelableNames; + cache ["IBinder"] = "Android.OS.IBinder"; + cache ["CharSequence"] = "Java.Lang.ICharSequence"; + cache ["List"] = "Android.Runtime.JavaList"; + cache ["Map"] = "Android.Runtime.JavaDictionary"; + } + + public bool IsParcelable (TypeName type) + { + var jn = ToFullJavaName (type); + jn = type.ArrayDimension > 0 ? jn.Substring (0, jn.Length - 3) : jn; + foreach (var p in parcelable_names) + if (p.ToJavaString () == jn) + return true; + return false; + } + + bool IsSupportedPrimitiveJavaType (string name) + { + switch (name) { + case "boolean": + case "byte": + case "char": + case "int": + case "long": + case "float": + case "double": + case "void": + case "String": + return true; + } + return false; + } + + public string GetCSharpNamespace (TypeName name) + { + string dummy, javapkg = name != null ? name.GetPackage () : String.Empty; + return database.NamespaceMappings.TryGetValue (javapkg, out dummy) ? dummy : name.GetNamespace (); + } + + public string ToCSharpNamespace (TypeName package) + { + string dummy, javapkg = package != null ? package.ToJavaString () : String.Empty; + return package == null ? String.Empty : database.NamespaceMappings.TryGetValue (javapkg, out dummy) ? dummy : package.ToString (); + } + + public string ToFullJavaName (TypeName javaType) + { + string java = javaType.ToJavaString (); + string suffix = javaType.ArrayDimension > 0 ? " []" : ""; + java = javaType.ArrayDimension > 0 ? java.Substring (0, java.Length - 3) : java; + if (IsSupportedPrimitiveJavaType (java)) + return java; + foreach (var imp in unit.Imports) { + int idx = java.IndexOf ('.'); + string head = idx < 0 ? java : java.Substring (0, idx); + if (imp.Identifiers.Length > 0 && head == imp.Identifiers.Last ()) + return imp.ToJavaString () + (idx < 0 ? "" : java.Substring (idx)) + suffix; + } + // FIXME: implement lookup with wildcard import + + // not found. + return (unit.Package != null ? unit.Package.ToJavaString () + '.' : null) + java + suffix; + } + + public string ToCSharp (TypeName javaType) // java name could be ns-less, or even partial ("import foo.Bar;" + "Bar.Baz" => "foo.Bar.Baz") + { + string java = javaType.ToJavaString (); + string suffix = javaType.ArrayDimension > 0 ? " []" : ""; + java = javaType.ArrayDimension > 0 ? java.Substring (0, java.Length - 3) : java; + string cs; + Func filter = (ret) => ret == "sbyte []" ? "byte []" : ret; + + if (cache.TryGetValue (java, out cs)) + return filter (cs + suffix); + if (IsSupportedPrimitiveJavaType (java)) + return filter ((java == "boolean" ? "bool" : java == "byte" ? "sbyte" : java) + suffix); + var javaFullName = ToFullJavaName (javaType); + javaFullName = javaFullName.Substring (0, javaFullName.Length - suffix.Length); + if (database.RegisteredTypes.TryGetValue (javaFullName, out cs)) { +//Console.WriteLine ("Found in lookup " + javaFullName + " / " + cs); + cache [java] = cs; + + return filter (cs + suffix); + } + + var p = parcelable_names.FirstOrDefault (pp => pp.ToJavaString () == javaFullName); + if (p != null) { + var decentNS = GetCSharpNamespace (p); + var stupidNS = p.GetNamespace (); + cache [java] = cs = (stupidNS != decentNS ? p.ToString ().Replace (stupidNS, decentNS) : p.ToString ()); +//Console.WriteLine ("Found in parcelable lookup " + java + " / " + cs); + return filter (cs + suffix); + } + + string csns = javaFullName != java ? GetCSharpNamespace (javaType) : String.Empty; + cache [java] = cs = csns + (string.IsNullOrEmpty (csns) ? "" : ".") + javaType.Identifiers.Last (); +//Console.WriteLine ("NOT Found in lookup " + java + " / " + cs); + return filter (cs + suffix); + } + } + + TextWriter w; + BindingDatabase database; + + public CSharpCodeGenerator (TextWriter writer, BindingDatabase database) + { + w = writer; + this.database = database; + } + + CompilationUnit unit; + NameResoltionCache name_cache; + string self_ns; + + public void GenerateCode (CompilationUnit unit, IList parcelableNames, ConverterOptions opts) + { + w.WriteLine ("// This file is automatically generated and not supposed to be modified."); + w.WriteLine ("using System;"); + w.WriteLine ("using Boolean = System.Boolean;"); + w.WriteLine ("using String = System.String;"); + w.WriteLine ("using List = Android.Runtime.JavaList;"); + w.WriteLine ("using Map = Android.Runtime.JavaDictionary;"); + + this.unit = unit; + name_cache = new NameResoltionCache (database, unit, parcelableNames); + self_ns = name_cache.ToCSharpNamespace (unit.Package); + + if (unit.Imports != null) { + var nss = new List (); + foreach (var imp in unit.Imports) { + string dummy, pkg = imp.GetPackage (); + string ns = database.NamespaceMappings.TryGetValue (pkg, out dummy) ? dummy : imp.GetNamespace (); + if (nss.Contains (ns)) + continue; + nss.Add (ns); + w.WriteLine ("using " + ns + ";"); + } + } + if (unit.Package != null) { + w.WriteLine (); + w.WriteLine ("namespace {0}", self_ns); + w.WriteLine ("{"); + } + + foreach (var type in unit.Types) { + if (type is Parcelable) { + if (type is Parcelable) { + switch (opts.ParcelableHandling) { + case ParcelableHandling.Ignore: + continue; + case ParcelableHandling.Error: + throw new InvalidOperationException ("Parcelable AIDL cannot be converted to C#"); + case ParcelableHandling.Stub: + StubParcelable ((Parcelable) type); + break; + } + } + } + else if (type is Interface) + GenerateCode ((Interface) type); + } + + if (unit.Package != null) + w.WriteLine ("}"); + + this.unit = null; + } + + public void StubParcelable (Parcelable type) + { + string csNS = name_cache.GetCSharpNamespace (type.Name); + if (csNS.Length > 0) + w.WriteLine ("namespace {0} {{", csNS); + string name = type.Name.Identifiers.Last (); + w.WriteLine (@" + public class {0} : global::Java.Lang.Object, global::Android.OS.IParcelable + {{ + public static global::Android.OS.IParcelableCreator Creator; // FIXME: implement + + public virtual int DescribeContents () + {{ + throw new NotImplementedException (); + }} + + // LAMESPEC: Android IParcelable interface is bogus by design. It does not expose + // this method, while aidl tool explicitly expects this method and generates such + // code that invokes it(!) + // Seealso: http://code.google.com/p/android/issues/detail?id=21777 + public virtual void ReadFromParcel (global::Android.OS.Parcel parcel) + {{ + throw new NotImplementedException (); + }} + + public virtual void WriteToParcel (global::Android.OS.Parcel parcel, global::Android.OS.ParcelableWriteFlags flags) + {{ + throw new NotImplementedException (); + }} + }}", name); + if (csNS.Length > 0) + w.WriteLine ("}"); + } + + public void GenerateCode (Interface type) + { + w.WriteLine ("\tpublic interface " + type.Name + " : global::Android.OS.IInterface"); + w.WriteLine ("\t{"); + foreach (var m in type.Methods) + GenerateCode (m); + w.WriteLine ("\t}"); + + w.WriteLine (@" + public abstract class {0}Stub : global::Android.OS.Binder, global::Android.OS.IInterface, {1}{2}{0} + {{ + const string descriptor = ""{3}{2}{4}""; + public {0}Stub () + {{ + this.AttachInterface (this, descriptor); + }} + + public static {1}{2}{0} AsInterface (global::Android.OS.IBinder obj) + {{ + if (obj == null) + return null; + var iin = (global::Android.OS.IInterface) obj.QueryLocalInterface (descriptor); + if (iin != null && iin is {1}{2}{0}) + return ({1}{2}{0}) iin; + return new Proxy (obj); + }} + + public global::Android.OS.IBinder AsBinder () + {{ + return this; + }} + + protected override bool OnTransact (int code, global::Android.OS.Parcel data, global::Android.OS.Parcel reply, int flags) + {{ + switch (code) {{ + case global::Android.OS.BinderConsts.InterfaceTransaction: + reply.WriteString (descriptor); + return true;", +// end of long formatted output... + type.Name, + self_ns, + unit.Package != null ? "." : String.Empty, + unit.Package != null ? unit.Package.ToJavaString () : null, + type.JavaName); + + foreach (var method in type.Methods) { + bool isVoidReturn = method.ReturnType.ToString () == "void"; + w.WriteLine (@" + case Transaction{0}: {{ + data.EnforceInterface (descriptor);", method.Name); + for (int i = 0; method.Arguments != null && i < method.Arguments.Length; i++) { + var a = method.Arguments [i]; + w.WriteLine ("\t\t\t\t{0} {1} = default ({0});", ToOutputTypeName (name_cache.ToCSharp (a.Type)), "arg" + i); + if (a.Modifier == null || a.Modifier.Contains ("in")) + w.WriteLine ("\t\t\t\t{0}", GetCreateStatements (a.Type, "data", "arg" + i)); + } + string args = String.Join (", ", (from i in Enumerable.Range (0, method.Arguments.Length) select "arg" + i).ToArray ()); + if (isVoidReturn) + w.WriteLine ("\t\t\t\tthis.{0} ({1});", method.Name, args); + else + w.WriteLine ("\t\t\t\tvar result = this.{0} ({1});", method.Name, args); + if (method.Modifier == null || !method.Modifier.Contains ("oneway")) + w.WriteLine ("\t\t\t\treply.WriteNoException ();"); + if (!isVoidReturn) + w.WriteLine ("\t\t\t\t{0}", GetWriteStatements (method.ReturnType, "reply", "result", "global::Android.OS.ParcelableWriteFlags.ReturnValue")); + for (int i = 0; method.Arguments != null && i < method.Arguments.Length; i++) { + var a = method.Arguments [i]; + if (a.Modifier == null || a.Modifier.Contains ("out")) + w.WriteLine ("\t\t\t\t{0}", GetWriteStatements (a.Type, "data", "arg" + i, "global::Android.OS.ParcelableWriteFlags.None")); + } + w.WriteLine ("\t\t\t\treturn true;"); + w.WriteLine ("\t\t\t\t}"); + } + w.WriteLine (@" + }} + return base.OnTransact (code, data, reply, flags); + }} + + public class Proxy : Java.Lang.Object, {1}{2}{0} + {{ + global::Android.OS.IBinder remote; + + public Proxy (global::Android.OS.IBinder remote) + {{ + this.remote = remote; + }} + + public global::Android.OS.IBinder AsBinder () + {{ + return remote; + }} + + public string GetInterfaceDescriptor () + {{ + return descriptor; + }}", + type.Name, self_ns, unit.Package != null ? "." : String.Empty); + foreach (var method in type.Methods) { + string args = JoinArguments (method); + w.WriteLine (@" + public {0} {1} ({2}) + {{ + global::Android.OS.Parcel __data = global::Android.OS.Parcel.Obtain (); +", ToOutputTypeName (name_cache.ToCSharp (method.ReturnType)), method.Name, args); + bool isOneWay = type.Modifier == "oneway"; + bool hasReturn = method.ReturnType.ToString () != "void"; + if (!isOneWay) + w.WriteLine ("\t\t\t\tglobal::Android.OS.Parcel __reply = global::Android.OS.Parcel.Obtain ();"); + if (hasReturn) + w.WriteLine ("{0} __result = default ({0});", ToOutputTypeName (name_cache.ToCSharp (method.ReturnType))); + w.WriteLine (@" + try { + __data.WriteInterfaceToken (descriptor);"); + foreach (var arg in method.Arguments) + if (arg.Modifier == null || arg.Modifier.Contains ("in")) + w.WriteLine ("\t\t\t\t\t" + GetWriteStatements (arg.Type, "__data", SafeCSharpName (arg.Name), "global::Android.OS.ParcelableWriteFlags.None")); + w.WriteLine ("\t\t\t\t\tremote.Transact ({1}Stub.Transaction{0}, __data, {2}, 0);", + method.Name, + type.Name, + isOneWay ? "null" : "__reply"); + if (!isOneWay) + w.WriteLine ("\t\t\t\t\t__reply.ReadException ();"); + if (hasReturn) + w.WriteLine ("\t\t\t\t\t{0}", GetCreateStatements (method.ReturnType, "__reply", "__result")); + foreach (var arg in method.Arguments) + if (arg.Modifier != null && arg.Modifier.Contains ("out")) + w.WriteLine ("\t\t\t\t\t{0}", GetReadStatements (arg.Type, "__reply", SafeCSharpName (arg.Name))); + if (hasReturn) + w.WriteLine (@" + } finally { + __reply.Recycle (); + __data.Recycle (); + } + return __result;"); + else + w.WriteLine (@" + } finally { + __data.Recycle (); + }"); + w.WriteLine (@" + } +"); + } + w.WriteLine (@" + }"); // end of Proxy + + for (int i = 0; i < type.Methods.Length; i++) { + var method = type.Methods [i]; + w.WriteLine (@" + internal const int Transaction{0} = global::Android.OS.Binder.InterfaceConsts.FirstCallTransaction + {1};", method.Name, i); + } + foreach (var method in type.Methods) + w.WriteLine (@" + public abstract {0} {1} ({2});", + ToOutputTypeName (name_cache.ToCSharp (method.ReturnType)), + method.Name, + JoinArguments (method)); + w.WriteLine (@" + }"); // end of Stub + } + + public void GenerateCode (Method method) + { + w.Write ("\t\t{0} {1} (", ToOutputTypeName (name_cache.ToCSharp (method.ReturnType)), method.Name); + bool written = false; + if (method.Arguments != null) + foreach (var a in method.Arguments) { + if (written) + w.Write (", "); + else + written = true; + w.Write ("{0} {1}", ToOutputTypeName (name_cache.ToCSharp (a.Type)), SafeCSharpName (a.Name)); + } + w.WriteLine (");"); + } + + string GetCreateStatements (TypeName type, string parcel, string arg) + { + string csname = name_cache.ToCSharp (type); + switch (csname) { + case "String": + return String.Format ("{0} = {1}.ReadString ();", arg, parcel); + case "String []": + return String.Format ("{0} = {1}.CreateStringArray (); {1}.ReadStringArray ({0});", arg, parcel); + case "bool": + return String.Format ("{0} = {1}.ReadInt () != 0;", arg, parcel); + case "bool []": + return String.Format ("{0} = {1}.CreateBooleanArray (); {1}.ReadBooleanArray ({0});", arg, parcel); + // FIXME: I'm not sure if aidl should support byte... + case "sbyte": + return String.Format ("{0} = {1}.ReadByte ();", arg, parcel); + case "byte []": + return String.Format ("{0} = {1}.CreateByteArray (); {1}.ReadByteArray ({0});", arg, parcel); + case "char": + return String.Format ("{0} = (char) {1}.ReadInt ();", arg, parcel); + case "char []": + return String.Format ("{0} = {1}.CreateCharArray (); {1}.ReadCharArray ({0});", arg, parcel); + case "int": + return String.Format ("{0} = {1}.ReadInt ();", arg, parcel); + case "int []": + return String.Format ("{0} = {1}.CreateIntArray (); {1}.ReadIntArray ({0});", arg, parcel); + case "long": + return String.Format ("{0} = {1}.ReadLong ();", arg, parcel); + case "long []": + return String.Format ("{0} = {1}.CreateLongArray (); {1}.ReadLongArray ({0});", arg, parcel); + case "float": + return String.Format ("{0} = {1}.ReadFloat ();", arg, parcel); + case "float []": + return String.Format ("{0} = {1}.CreateFloatArray (); {1}.ReadFloatArray ({0});", arg, parcel); + case "double": + return String.Format ("{0} = {1}.ReadDouble ();", arg, parcel); + case "double []": + return String.Format ("{0} = {1}.CreateDoubleArray (); {1}.ReadDoubleArray ({0});", arg, parcel); + // FIXME: are JavaList for List and JavaDictionary for Map always appropriate? + case "List": + case "Android.Runtime.JavaList": + if (type.GenericArguments != null) { + switch (name_cache.ToCSharp (type.GenericArguments.First ())) { + case "String": + case "Java.Lang.ICharSequence": + return String.Format ("{0} = (global::Android.Runtime.JavaList) {1}.CreateStringArrayList ();", arg, parcel); + case "Android.OS.IBinder": + return String.Format ("{0} = (global::Android.Runtime.JavaList) {1}.CreateBinderArrayList ();", arg, parcel); + default: + return String.Format ("{0} = (global::Android.Runtime.JavaList) {1}.CreateTypedArrayList ({2}.Creator);", arg, parcel, ToOutputTypeName (name_cache.ToCSharp (type.GenericArguments [0]))); + } + } + return String.Format ("{0} = (global::Android.Runtime.JavaList) {1}.ReadArrayList ((global::Java.Lang.ClassLoader) null);", arg, parcel); + case "Map": + case "Android.Runtime.JavaDictionary": + return String.Format ("{0} = {1}.ReadHashMap ();", arg, parcel); + case "Android.OS.IParcelable": + return String.Format ("{0} = {1}.ReadInt () != 0 ? ({2}) global::Android.OS.Bundle.Creator.CreateFromParcel ({1}) : null;", arg, parcel, ToOutputTypeName (csname)); + case "Android.OS.IBinder": + return String.Format ("{0} = {1}.ReadStrongBinder ();", arg, parcel); + case "Android.OS.IBinder []": + return String.Format ("{0} = {1}.CreateBinderArray ();", arg, parcel); + case "Java.Lang.ICharSequence": + return String.Format ("{0} = {1}.ReadInt () != 0 ? (global::Java.Lang.ICharSequence) global::Android.Text.TextUtils.CharSequenceCreator.CreateFromParcel ({1}) : null;", arg, parcel); + default: + if (name_cache.IsParcelable (type)) { + if (type.ArrayDimension > 0) // ParcelableCreator + return String.Format ("{0} = global::System.Array.ConvertAll ({1}.CreateTypedArray ({2}.Creator), __input => ({2}) __input);", arg, parcel, ToOutputTypeName (csname.Substring (0, csname.Length - 3))); + else + return String.Format ("{0} = {1}.ReadInt () != 0 ? ({2}) global::Android.OS.Bundle.Creator.CreateFromParcel ({1}) : null;", arg, parcel, ToOutputTypeName (csname)); + } + else if (type.ArrayDimension > 0) + throw new NotSupportedException (String.Format ("AIDL does not support creating this array type: {0}", type)); + else + // interfaces + return String.Format ("{0} = {2}Stub.AsInterface ({1}.ReadStrongBinder ());", arg, parcel, ToOutputTypeName (csname)); + } + } + + string GetReadStatements (TypeName type, string parcel, string arg) + { + string csname = name_cache.ToCSharp (type); + switch (csname) { + case "String []": + return String.Format ("{1}.ReadStringArray ({0});", arg, parcel); + case "bool []": + return String.Format ("{1}.ReadBooleanArray ({0});", arg, parcel); + // FIXME: I'm not sure if aidl should support byte... + case "byte []": + return String.Format ("{1}.ReadByteArray ({0});", arg, parcel); + case "char []": + return String.Format ("{1}.ReadCharArray ({0});", arg, parcel); + case "int []": + return String.Format ("{1}.ReadIntArray ({0});", arg, parcel); + case "long []": + return String.Format ("{1}.ReadLongArray ({0});", arg, parcel); + case "float []": + return String.Format ("{1}.ReadFloatArray ({0});", arg, parcel); + case "double []": + return String.Format ("{1}.ReadDoubleArray ({0});", arg, parcel); + case "Android.OS.IParcelable": + return String.Format ("{0} = {1}.ReadInt () != 0 ? ({2}) global::Android.OS.Bundle.Creator.CreateFromParcel ({1}) : null;", arg, parcel, ToOutputTypeName (csname)); + // FIXME: are JavaList for List and JavaDictionary for Map always appropriate? + case "List": + case "Android.Runtime.JavaList": + if (type.GenericArguments != null) { + switch (name_cache.ToCSharp (type.GenericArguments.First ())) { + case "String": + case "Java.Lang.ICharSequence": + return String.Format ("{1}.ReadStringList ((global::System.Collections.Generic.IList) {0});", arg, parcel); + case "Android.OS.IBinder": + return String.Format ("{1}.ReadBinderList ((global::System.Collections.Generic.IList) {0});", arg, parcel); + default: + return String.Format ("{1}.ReadTypedList ({0}, {2}.Creator);", arg, parcel, ToOutputTypeName (name_cache.ToCSharp (type.GenericArguments [0]))); + } + } + return String.Format ("{0} = {1}.ReadList ();", arg, parcel); + case "Map": + case "Android.Runtime.JavaDictionary": + return String.Format ("{0} = {1}.ReadMap ();", arg, parcel); + case "Android.OS.IBinder": + return String.Format ("{0} = {1}.ReadStrongBinder ();", arg, parcel); + case "Android.OS.IBinder []": + return String.Format ("{1}.ReadBinderArray ({0});", arg, parcel); + default: + if (name_cache.IsParcelable (type)) { + if (type.ArrayDimension > 0) // ParcelableCreator + return String.Format ("{1}.ReadTypedArray ({0}, {2}.Creator);", arg, parcel, ToOutputTypeName (csname.Substring (0, csname.Length - 3))); + else + return String.Format ("if ({1}.ReadInt () != 0) {0}.ReadFromParcel ({1});", arg, parcel); + } + else if (type.ArrayDimension > 0) + throw new NotSupportedException (String.Format ("AIDL does not support reading this array type: {0}", type)); + else + // interfaces + return String.Format ("{0} = {2}Stub.AsInterface ({1}.ReadStrongBinder ());", arg, parcel, ToOutputTypeName (csname)); + } + } + + string GetWriteStatements (TypeName type, string parcel, string arg, string parcelableWriteFlags) + { + string csname = name_cache.ToCSharp (type); + switch (csname) { + case "String": + return parcel + ".WriteString (" + arg + ");"; + case "String []": + return parcel + ".WriteStringArray (" + arg + ");"; + case "bool": + return parcel + ".WriteInt (" + arg + " ? 1 : 0);"; + case "bool []": + return parcel + ".WriteBooleanArray (" + arg + ");"; + // FIXME: I'm not sure if aidl should support byte... + case "sbyte": + return parcel + ".WriteByte (" + arg + ");"; + case "byte []": + return String.Format ("{1}.WriteByteArray ({0});", arg, parcel); + case "char": + return parcel + ".WriteInt ((int) " + arg + ");"; + case "char []": + return parcel + ".WriteCharArray (" + arg + ");"; + case "int": + return parcel + ".WriteInt (" + arg + ");"; + case "int []": + return parcel + ".WriteIntArray (" + arg + ");"; + case "long": + return parcel + ".WriteLong (" + arg + ");"; + case "long []": + return parcel + ".WriteLongArray (" + arg + ");"; + case "float": + return parcel + ".WriteFloat (" + arg + ");"; + case "float []": + return parcel + ".WriteFloatArray (" + arg + ");"; + case "double": + return parcel + ".WriteDouble (" + arg + ");"; + case "double []": + return parcel + ".WriteDoubleArray (" + arg + ");"; + // FIXME: are JavaList for List and JavaDictionary for Map always appropriate? + case "List": + case "Android.Runtime.JavaList": + if (type.GenericArguments != null) { + switch (name_cache.ToCSharp (type.GenericArguments.First ())) { + case "String": + case "Java.Lang.ICharSequence": + return String.Format ("{1}.WriteStringList ((global::System.Collections.Generic.IList) {0});", arg, parcel); + case "Android.OS.IBinder": + return String.Format ("{1}.WriteBinderList ((global::System.Collections.Generic.IList) {0});", arg, parcel); + default: + return String.Format ("{1}.WriteTypedList ({0});", arg, parcel, ToOutputTypeName (name_cache.ToCSharp (type.GenericArguments [0]))); + } + } + return String.Format ("{1}.WriteList ({0});", arg, parcel); + case "Map": + case "Android.Runtime.JavaDictionary": + return String.Format ("{1}.WriteMap ({0});", arg, parcel); + case "Android.OS.Bundle": + case "Android.OS.IParcelable": + return String.Format ("if ({0} != null) {{ {1}.WriteInt (1); {0}.WriteToParcel ({1}, {2}); }} else {1}.WriteInt (0);", arg, parcel, parcelableWriteFlags); + case "Android.OS.IBinder": + return String.Format ("{1}.WriteStrongBinder ({0});", arg, parcel); + case "Android.OS.IBinder []": + return String.Format ("{1}.WriteBinderArray ({0});", arg, parcel); + case "Java.Lang.ICharSequence": + return String.Format ("if ({0} != null) {{ {1}.WriteInt (1); global::Android.Text.TextUtils.WriteToParcel ({0}, {1}, {2}); }} else {1}.WriteInt (0);", arg, parcel, parcelableWriteFlags); + default: // interfaces + if (name_cache.IsParcelable (type)) { + if (type.ArrayDimension > 0) + return String.Format ("{1}.WriteTypedArray ({0}, {2});", arg, parcel, parcelableWriteFlags); + else + return String.Format ("if ({0} != null) {{ {1}.WriteInt (1); {0}.WriteToParcel ({1}, {2}); }} else {1}.WriteInt (0);", arg, parcel, parcelableWriteFlags); + } + else if (type.ArrayDimension > 0) + throw new NotSupportedException (String.Format ("AIDL does not support writing this array type: {0}", type)); + else + return String.Format ("{1}.WriteStrongBinder (((({0} != null)) ? ({0}.AsBinder ()) : (null)));", arg, parcel); + } + } + + // FIXME: should this be used? + string GetCreatorName (TypeName type) + { + string csname = name_cache.ToCSharp (type); + switch (csname) { + case "String": + case "Java.Lang.ICharSequence": + return "Android.OS.Parcel.StringCreator"; + case "List": + case "Android.Runtime.JavaList": + return "Android.OS.Parcel.ArrayListCreator"; + default: + if (name_cache.IsParcelable (type)) + return csname + ".Creator"; + return String.Empty; + } + } + + string [] cs_units; + + string ToOutputTypeName (string name) + { + if (cs_units == null) + cs_units = unit.Package == null ? new string [0] : name_cache.ToCSharpNamespace (unit.Package).Split ('.'); + int idx = name.IndexOf ('.'); + if (idx < 0) + return name; + if (Array.IndexOf (cs_units, name.Substring (0, idx)) < 0) + return name; + return "global::" + name; + } + + static CSharpCodeProvider csp = new CSharpCodeProvider (); + static string SafeCSharpName (string name) + { + return csp.IsValidIdentifier (name) ? name : "@" + name; + } + + string JoinArguments (Method m) + { + return m.Arguments != null ? String.Join (", ", (from a in m.Arguments select ToOutputTypeName (name_cache.ToCSharp (a.Type)) + " " + SafeCSharpName (a.Name)).ToArray ()) : null; + } + } +} + diff --git a/src/Xamarin.AndroidTools.Aidl/ConverterOptions.cs b/src/Xamarin.AndroidTools.Aidl/ConverterOptions.cs new file mode 100644 index 00000000000..057e6b84a8d --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/ConverterOptions.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; + +namespace Xamarin.AndroidTools.Aidl +{ + public enum ParcelableHandling + { + Ignore, + Error, + Stub + } + + public class ConverterOptions + { + public bool Verbose; + public ParcelableHandling ParcelableHandling; + public string OutputDirectory; + public string OutputNS; + public List InputFiles = new List (); + public List References = new List (); + } +} + diff --git a/src/Xamarin.AndroidTools.Aidl/Xamarin.AndroidTools.Aidl.csproj b/src/Xamarin.AndroidTools.Aidl/Xamarin.AndroidTools.Aidl.csproj new file mode 100644 index 00000000000..f477b733f96 --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/Xamarin.AndroidTools.Aidl.csproj @@ -0,0 +1,61 @@ + + + + Debug + AnyCPU + {D27AD8F7-7710-40BE-B03B-55EFBEC13C44} + Library + Xamarin.AndroidTools.Aidl + Xamarin.AndroidTools.Aidl + + + True + full + False + ..\..\bin\$(Configuration)\lib\xbuild\Xamarin\Android + DEBUG; + prompt + 4 + False + + + none + True + ..\..\bin\$(Configuration)\lib\xbuild\Xamarin\Android + prompt + 4 + False + + + + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.dll + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.Mdb.dll + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.Pdb.dll + + + ..\..\packages\Mono.Cecil.0.9.6.1\lib\net40\Mono.Cecil.Rocks.dll + + + ..\..\packages\Irony.0.9.1\lib\net40\Irony.dll + False + + + + + + + + + + + + + + + diff --git a/src/Xamarin.AndroidTools.Aidl/packages.config b/src/Xamarin.AndroidTools.Aidl/packages.config new file mode 100644 index 00000000000..bd1a9fa7cb3 --- /dev/null +++ b/src/Xamarin.AndroidTools.Aidl/packages.config @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file