Bug 14932 - obfuscation before create apk
Summary: obfuscation before create apk
Status: RESOLVED DUPLICATE of bug 13865
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 4.8.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-09-23 12:27 UTC by Jon Goldberger [MSFT]
Modified: 2013-11-18 13:26 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Test Project (6.43 MB, application/zip)
2013-09-23 12:27 UTC, Jon Goldberger [MSFT]
Details
Android Tools Log (1.22 MB, application/octet-stream)
2013-09-23 12:28 UTC, Jon Goldberger [MSFT]
Details


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 13865

Description Jon Goldberger [MSFT] 2013-09-23 12:27:20 UTC
Created attachment 4961 [details]
Test Project

Hi,
I use Xamarin Studio with Mono for Android.
I have problem with obfuscation my Android application.

So, I add this external command in my project in Release mode, that it launch after Generation:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<ConsolePause>false</ConsolePause>
<AndroidKeyStore>True</AndroidKeyStore>
<AndroidSigningKeyStore>XXX</AndroidSigningKeyStore>
<AndroidSigningStorePass>XXX</AndroidSigningStorePass>
<AndroidSigningKeyAlias>XXX</AndroidSigningKeyAlias>
<AndroidSigningKeyPass>XXX</AndroidSigningKeyPass>
<AndroidSupportedAbis>armeabi,armeabi-v7a</AndroidSupportedAbis>
<EmbedAssembliesIntoApk>True</EmbedAssembliesIntoApk>
<MandroidI18n>west</MandroidI18n>
<CustomCommands>
<CustomCommands>
<Command type="AfterBuild" command="Obfuscate.bat" workingdir="" externalConsole="True" />
</CustomCommands>
</CustomCommands>
</PropertyGroup>

My file Obfuscate.bat obfuscate the dll from Bin folder, after that I copy obfuscated file into Bin and Obj/assembly folder:

copy "obj\Release\assemblies\Mono.Android.dll" "bin\Release\Mono.Android.dll"
copy "obj\Release\assemblies\Mono.Android.Export.dll" "bin\Release\Mono.Android.Export.dll"
copy "obj\Release\assemblies\Mono.Android.Support.v4.dll" "bin\Release\Mono.Android.Support.v4.dll"
copy "obj\Release\assemblies\Mono.Security.dll" "bin\Release\Mono.Security.dll"
copy "obj\Release\assemblies\mscorlib.dll" "bin\Release\mscorlib.dll"

"C:\Program Files (x86)\LogicNP Software\Crypto Obfuscator For .Net 2013 R2\co.exe" projectfile= obfuscator.obproj
pause

copy "CryptoObfuscator_Output\xx.dll" "bin\Release\assemblies\xx.dll"
copy "CryptoObfuscator_Output\xx.dll" "obj\Release\assemblies\xx.dll"

The external command is called, but when Xamarin Studio start create APK, new generation start without call my external command.
After that, the DLL into BIN and OBJ folder are not obfuscated.
Comment 1 Jon Goldberger [MSFT] 2013-09-23 12:28:16 UTC
Created attachment 4962 [details]
Android Tools Log
Comment 3 Jonathan Pryor 2013-09-23 17:01:44 UTC
There are _three_ mechanisms to do post-build commands:

1. The <CustomCommands/> element, as is used here.

    This is what Xamarin Studio's Build > Custom Commands section uses.

2. The $(PostBuildEvent) MSBuild property. This is what Visual Studio generates.

3. Overriding the $(BuildDependsOn) MSBuild property.

The problem is that (1) is only supported by the Xamarin Studio build system, not Visual Studio, MSBuild, or `xbuild`, and Xamarin.Android projects are always built with MSBuild/xbuild. Consequently, Obfuscate.bat will not be invoked except when using the Xamarin Studio Build command, which will not be the case when e.g. Running the application on a device.

Note: the Changing Xamarin Studio to use $(PostBuildEvent) is tracked at Bug #13865.

The fix is to use either (2) or (3).

Using $(PostBuildEvent) would consist of adding the following XML fragment as a child of the <Project/> element:

  <PropertyGroup>
    <PostBuildEvent>Obfuscate.bat</PostBuildEvent>
  </PropertyGroup>


Using the $(BuildDependsOn) MSBuild property allows execution of MSBuild _targets_ (as opposed to commands), and requires two parts, setting the $(BuildDependsOn) property, and then providing the target.

The $(BuildDependsOn) property is set via a <PropertyGroup/>, and contains the MSBuild targets to execute:

  <PropertyGroup>
    <BuildDependsOn>
      $(BuildDependsOn);
      ObfuscateAssemblies
    </BuildDependsOn>
  </PropertyGroup>

The second part requires defining the target used in $(BuildDependsOn):

  <Target Name="ObfuscateAssemblies">
    <Exec Command="Obfuscate.bat"
        WorkingDirectory="$(ProjectDir)"
    />
  </Target>

*** This bug has been marked as a duplicate of bug 13865 ***