3

I've included opencv in my android app using the following statements:

compile group: 'org.bytedeco', name: 'javacv', version: '0.11'
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'

Now only two out of the four are used, which is a waste of space and probably also speed. Is there a way to only load/compile libraries which belong to an architecture? I've read Gradle android build for different processor architectures but this one uses the libs folder and therefore has its own includes. I have all libraries imported through gradle.

Community
  • 1
  • 1
Gooey
  • 4,740
  • 10
  • 42
  • 76

1 Answers1

5

You could use gradle flavors (documentation).

productFlavors {
    arm {
        ...
    }

    x86 {
        ...
    }

    all {
        ...
    }
}

...

dependencies {
    // For arm
    armCompile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-arm'
    armcompile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-arm'

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

    // For all
    allCompile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-arm'
    allcompile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-arm'
    allCompile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.11-0.11', classifier: 'android-x86'
    allCompile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.6.1-0.11', classifier: 'android-x86'
}

Then use the build variant you want.

Médéric
  • 938
  • 4
  • 12
  • Does this automatically select the right architecture? Or is this about creating 2 different APK's? – Gooey May 22 '15 at 10:53
  • It's create two apks depending of selected flavor. You can add another one with all architectures. You can see my edited answer – Médéric May 22 '15 at 12:43
  • But I would like one apk that people download from the google play store and then depending on their architecture, compile/load the right libs.. – Gooey May 22 '15 at 12:54
  • 1
    Compilation is done before uploading apk on the play store.... Check http://developer.android.com/google/play/publishing/multiple-apks.html#SupportedFilters to see how to use CPU architecture filter with different apk's – Médéric May 22 '15 at 13:02