GithubHelp home page GithubHelp logo

xamguard's Introduction

XamGuard adds a new step to the build process where the compiled Java code (from bound libraries) is stripped of unused code by Proguard.

Why would I need this?

Building applications with Xamarin.Android is a breeze and one can use various components to speed up the development even more. Problem with using a lot of libraries is that the final APK size grows rapidly to something that is hard to justify to the users downloading it. Xamarin team has built an awesome IL linker, which removes unused .NET code from your app.

When using Android support libraries or other Java binding libraries, they not only have large .NET assemblies but large Java class files. Most of the time you will only be using a small subset of those libraries, thus forcing your users to waste room on their phones for that dead code.

The Android SDK bundles Proguard with it and when developing in Java it is very easy to let it strip out the unused Java code (just like Xamarin does for IL). Out of the box, Xamarin.Android doesn't support running Proguard for the Java code, that's where XamGuard steps in.

For example, the JAR file for Google Play Services is around 1.4M. If you only use GCM from it, then after stripping unused code from it, it will only take up 27K.

Give it to me now!

The easies way to get the binaries is to look for XamGuard in your NuGet package manager.

After getting the binaries, the first thing you need to do is to add a line to your Android .csproj file. This line imports the new build steps to run Proguard. You need to add if after the line importing Novell.MonoDroid.CSharp.targets. Like so:

  <Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
  <Import Project="..\packages\XamGuard.1.0.1\Proguard.targets" Condition="'$(Configuration)' != 'Debug'" />

You should double check that the path to Proguard.targets file is correct. The next step is to configure Proguard.

Setting up Proguard

The NuGet package adds a Proguard.cfg to your project folder. This is the file where you add project specific Proguard configurations. The template is usually good enough for most cases.

You do need to edit it, whenever you use some Android views from XML files. In that case you need to instruct Proguard explicitly to keep them, like so:

-keep class android.support.v4.view.PagerTitleStrip { *; }
-keep class android.support.v4.view.ViewPager { *; }

It is possible that in some cases XamGuard fails to detect that some Java function is used by managed code. XamGuard also does not attempt to detect JNIEnv usages in your own code. If that is the case, you should check out Proguard manual on how to best configure Proguard for your usecase.

In short, you should always test your release builds before giving them to your users as there might have been something that was removed and will cause your app to crash.

Building from source

To build XamGuard from source you need to open the solution and build in Release mode. After that you should move the files from Proguard.Build.Tasks/bin/Release/ to your solution folder where your Android project can import the Proguard.target. That's it.

Contributing

All contributions are more than welcome. Fork the code, add your changes and create a pull request. The usual Github workflow. :)

License

XamGuard is licensed under MIT license.

xamguard's People

Contributors

decavoid avatar kjeremy avatar roosmaa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.