Skip to content

Instantly share code, notes, and snippets.

@maiatoday
Created August 22, 2013 21:10
Show Gist options
  • Save maiatoday/6312827 to your computer and use it in GitHub Desktop.
Save maiatoday/6312827 to your computer and use it in GitHub Desktop.
Trying to get OpenCV working with gradle and android studio
here is a topic we known as: "https://gist.github.com/khernyo/4226923", But i have try this method in gradle plugin 0.4.0+, it doesn't copy the so libs to apk.
So I'm confused. But I figured out a new way to hack this before gradle support.
The way is: use jar to copy so libs. Is it amazing?right?. Let me show how-to:
If you have the following structure:
project
|---libs
|---armeabi/xxx.so
|---xxx.jar
|---xxx.jar
1.We archive the armeabi folder to zip named:armeabi.zip with the folder in zip like: lib/armeabi/xxx.so
2.Rename armeabi.zip to armeabi.jar and put armeabi.jar into libs folder with the other jars.
3.And when we call
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
in our build.gradle file, it will extract the so libs to the apks.
As we know, only class files will be packed into classes.dex file and other files will be extracted to the apk as the structure in the jars.
So, That's how-to hack. Easy and amazing. Cool,right?
or also this:
And just made some changes to make it even more cleaner.
You don't need to rename the .zip file to .jar, just add it with a normal compile file dependency on build.gradle script. So, you would make a foo.zip file with a structure similar to this:
foo.zip ->
|--/lib
|--|--/armeabi
|--|--|--*.so
|--|--/x86
|--|--|--*.so
put it in your libs folder and then add it to gradle using compile files('libs/foo.zip'):
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+' //gradle plugin update for Andoid Studio 0.2.+
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile files('libs/foo.zip') //zip file should be in Module's /libs folder (not the Project's /lib)
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
while gradle does the build it just unzip the file you added preserving its structure.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment