Skip to content

Configuring Proguard for JavaCV

Nackloose edited this page Mar 26, 2018 · 3 revisions

By default, proguard optimzes code and reduces the amount of code to make your Android application faster and smaller. Unfortunately, proguard removes annotations be default, including the @Platform annotation used by javaCV. To make proguard preserve these annotation, follow the steps below:

Upgrade your proguard in the Android SDK

At the time of typing (9/5/2015, dd/mm/yyyy), the Android SDK ships with proguard 4.7. This version is known to have some issues regarding obfuscating. The latest release is proguard 5.1 (or higher). So the first step is to upgrade proguard.

  1. Download the most recent version here: http://proguard.sourceforge.net/
  2. Extract the proguard directory from the e.g. zip file.
  3. Replace or merge the .../sdk/tools/proguard sub directory with the downloaded proguard directory, restart Android Studio if you have it open.

Every time you upgrade buildtools or other aspects of the sdk your proguard MAY be reset to the version being shipped with the sdk. Follow this Stackoverflow post to figure out what your proguard version is: http://stackoverflow.com/questions/5013163/how-can-i-check-upgrade-proguard-version-whn-using-it-in-eclipse-for-android-dev

Configure proguard rules

Now we have to set a variety of rules to tell proguard not to optimize certain parts of javacv.

In your local proguard-rules.txt or proguard-rules.pro file, add the following lines:

-keepattributes *Annotation*

# JavaCV
-keep @org.bytedeco.javacpp.annotation interface * {
    *;
}

-keep @org.bytedeco.javacpp.annotation.Platform public class *

-keepclasseswithmembernames class * {
    @org.bytedeco.* <fields>;
}

-keepclasseswithmembernames class * {
    @org.bytedeco.* <methods>;
}

-keepattributes EnclosingMethod
-keep @interface org.bytedeco.javacpp.annotation.*,javax.inject.*

-keepattributes *Annotation*, Exceptions, Signature, Deprecated, SourceFile, SourceDir, LineNumberTable, LocalVariableTable, LocalVariableTypeTable, Synthetic, EnclosingMethod, RuntimeVisibleAnnotations, RuntimeInvisibleAnnotations, RuntimeVisibleParameterAnnotations, RuntimeInvisibleParameterAnnotations, AnnotationDefault, InnerClasses
-keep class org.bytedeco.javacpp.** {*;}
-dontwarn java.awt.**
-dontwarn org.bytedeco.javacv.**
-dontwarn org.bytedeco.javacpp.**

# end javacv

Note that some of these lines may be redundant and can be removed, but at time of writing, these rules are known to work.

Gradle settings

For completeness, the following lines can be added to make sure you have all the presets for opencv and ffmpeg for both the arm and x86 architectures:

compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-x86'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-x86'

You can replace the 2.4.11-0.11 version for opencv and the 2.6.1-0.11 version for ffmpeg with the most recent versions of opencv and ffmpeg respectively. More can be found here: https://github.com/bytedeco/javacpp-presets

The latest release of opencv and ffmpeg can be found on the maven central repository

If you need more presets, you can copy the lines above and adjust the name and version number where needed.