At my current job we are using ILMerge to combine multiple separate .net assemblies into a single .exe file. Recently we introduced a new 3rd party library which is obfuscated. It turns out that obfuscated libraries cannot be merged with other libraries with tools such as ILMerge. The merge will appear to complete successfully, but in my case an unusual runtime error was produced when attempting to invoke any code from the 3rd party assembly. What to do?
I did a little digging and experimenting. I found that it is easy to exclude an assembly from being merged among the others. Just leave it out of the list of assemblies to merge but make sure it is included with the
/lib switch. /lib provides ILMerge with libraries that are referenced by the others but not necessarily included. So by using /lib and simply not including the library with the others in the ILMerge arguments, that assembly will not be merged but will need to remain in the same directory as the final merged assembly in order for it to work.
Below is a basic example, where the variable
%DirPath% is where all the DLLs are found (bin directory basically). Passing the DirPath to the /lib switch provides all DLLs to ILMerge. Then passing %DirPath%\LibA.dll at the end of the command merges LibA.dll in with ConsoleApp1.exe.
ILMerge.exe /lib:%DirPath% /target:exe /targetplatform:v4,C:\Windows\Microsoft.NET\Framework64\v4.0.30319 /wildcards /out:%DirPath%\FINAL.exe %DirPath%\ConsoleApp1.exe %DirPath%\LibA.dll
The result of the above is:
- One file named FINAL.exe which internally contains LibA.dll
- Anything else in %DirPath% that is referenced by LibA or ConsoleApp1.exe must reside in the same directory as FINAL.exe in order for FINAL.exe to work
You can find my full working example here: https://github.com/jeffwmair/ilmerge-examples
More about ILMerge here: https://github.com/dotnet/ILMerge