I'm trying use JavaCV with Android Studio and Gradle. I wrote such code fragment:
repositories {
mavenCentral()
maven {
url "http://maven2.javacv.googlecode.com/git/"
}
}
dependencies {
compile files('libs/android-support-v4.jar')
compile group: 'com.googlecode.javacpp', name: 'javacpp', version: '0.5'
compile group: 'com.googlecode.javacv', name: 'javacv', version: '0.5'
}
and I see imported libraries in dir External Libraries. So I tried to run this:
...
IplImage zdjecie=cvLoadImage(Environment.getExternalStorageDirectory().getPath()+ "/1.bmp");
cvSaveImage(Environment.getExternalStorageDirectory().getPath()
+ "/2.bmp", zdjecie);
...
and I got error:
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load jniopencv_core: findLibrary returned null
...because I don't know what should I do with opencv's (and others) .so files.
So how should we use JavaCV in Android Studio?
1.WARNING:
That's not enough!:
dependencies { compile group: 'org.bytedeco', name: 'javacv', version: '0.9'}
2.EDIT: Sorry for mistake, my recent solution which
I posted here and which told only about line above was wrong. But I checked it out and this works for me:
a)Add dependencies
dependencies {
compile group: 'org.bytedeco', name: 'javacv', version: '0.9'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '2.4.9-0.9', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '2.3-0.9', classifier: 'android-arm'
}
b) Create jniLibs dir inside your project (on the same level as normal libs dir. EDIT: If
you have some troubles try moving jniLibs to app/src/main).
c) Add required .so files extracted from opencv-android-arm.jar and ffmpeg-android-arm.jar (or only this files which you really need) to created jniLibs dir. (If you don't know about what
I'm talking you can download javacv-0.9-bin.zip from JavaCV page and inside it
you can find these 2 .jars).
Inserting inside the build.gradle dependencies worked for me:
compile ‘org.bytedeco:javacv:+’
compile group: ‘org.bytedeco.javacpp-presets’, name: ‘opencv’, version: ‘2.4.10–0.10', classifier: ‘android-arm’
compile group: ‘org.bytedeco.javacpp-presets’, name: ‘ffmpeg’, version: ‘2.5.1–0.10', classifier: ‘android-arm’
For an up-to-date solution (JavaCV 1.1 and Android Studio 2.1.1) please see my step-by-step instructions in my Gist. JavaCV 1.2 currently doesn't like Android 6.
This tut will use JavaCV 1.1, version 1.2 currently has SIGSEGV issue. JavaCV 1.1 comes with FFmpeg 2.8.1.
Obtain the prebulit binaries here.
Extract it and copy these files:
javacpp.jar (essential).
javacv.jar (essential).
ffmpeg.jar (essential if you use FFmpeg).
ffmpeg-android-arm.jar (for arm CPU).
ffmpeg-android-x86.jar (optional for x86 CPU).
into the libs folder of your project (app/libs in my case).
Click on app -> F4 -> Dependencies -> + -> File dependencies -> choose all the previous .jar.
Temporarily, because of this issue, we need to set targetSdkVersion 22 the app's build.gradle.
Possibly need to set these packagingOptions in the build.gradle:
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.test"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'
//might need these if you use openCV
//exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
//exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
}
}
Unfortunately, I had some problems with user2645214's solution (still getting java.lang.UnsatisfiedLinkError), but I found another one, so I decided to share it with those who would have the same problem.
Since release 0.7.3 there is another way to include your *.so files - you can just put them in /src/main/jniLibs (just create jniLibs directory if you don't have it) and it should work.
Also my build.gradle file looks like:
apply plugin: 'android'
repositories {
mavenCentral()
maven { url 'http://maven2.javacv.googlecode.com/git/' }
}
dependencies {
compile 'com.android.support:appcompat-v7:20.+'
compile 'com.googlecode.javacpp:javacpp:0.7'
compile 'com.googlecode.javacv:javacv:0.7'
}
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId "com.example.agp.testapplication2"
minSdkVersion 15
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
I faced this issue on Windows 7 x64.
Here is a section from my gradle.build file which resolved the problem:
compile("org.bytedeco:javacv:0.11")
compile("org.bytedeco.javacpp-presets:opencv:2.4.11-0.11:windows-x86_64")
compile("org.bytedeco.javacpp-presets:ffmpeg:2.6.1-0.11:windows-x86_64")
EDIT. Solution in this post is deprecated and may contain some errors. Please see recent, correct post at the bottom of the page.
Ok, I did it on my own :). So here is what we should do to use JavaCV in Android Studio:
1) First we should add such dependencies to build.gradle inside project:
dependencies {
compile group: 'com.googlecode.javacpp', name: 'javacpp', version: '0.7'
compile group: 'com.googlecode.javacv', name: 'javacv', version: '0.7'}
(I mean: build.gradle inside our module directory)
Thanks that we get javacv.jar and javacpp.jar inside external libraries (if not, we
should restart Android Studio).
2) If we follow the Eclipse instruction on
javacv official site
we are now in point 4.:
Extract all the *.so files rom javacv-android-arm.jar,
opencv-2.4.8-android-arm.jar, and ffmpeg-2.1.1-android-arm.jar
directly into the newly created "libs/armeabi" folder, without
creating any of the subdirectories found in the JAR files.
We have to extract all *.so files which we need, but then we should join them to our project in other special way:
3rd answer here says about it,
but generally, we should:
Create new 'armeabi' directory
Put interesting .so files into 'armeabi' directory
Create armeabi.zip
Change extension of armeabi.zip to armeabi.jar
Put armeabi.jar to 'libs' directory
Make sure that build.gradle of our module contains such dependency:
compile fileTree(dir: 'libs', include: ['*.jar'])
3) Enjoy your OpenCV and JavaCv in Android Studio (and don't forget to add
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
to your AndroidManifest.xml if you want to read/save image from/to sdcard and try thanks this,
that your project works well).
/////////////////////////////////////////////////////////
If something is wrong, please write about it in comments.
If you try out with javacv 1.2, we have to do some additional steps so at to get this working. Here is my build.gradle file
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "org.audiorecording"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.properties'
exclude 'META-INF/maven/org.bytedeco.javacpp-presets/ffmpeg/pom.xml'
//might need these if you use openCV
exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.properties'
exclude 'META-INF/maven/org.bytedeco.javacpp-presets/opencv/pom.xml'
}
}
repositories {
mavenCentral()
}
configurations {
all*.exclude group: 'org.bytedeco', module: 'javacpp-presets'
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'org.bytedeco:javacv:1.2'
compile group: 'org.bytedeco.javacpp-presets', name: 'opencv', version: '3.1.0-1.2', classifier: 'android-arm'
compile group: 'org.bytedeco.javacpp-presets', name: 'ffmpeg', version: '3.0.2-1.2', classifier: 'android-arm'
}
Related
I came across a problem while implementing an Android plugin for Unity based on EasyPermissions. The issue was due to
Caused by: java.lang.ClassNotFoundException: Didn't find class "pub.devrel.easypermissions.EasyPermissions$PermissionCallbacks"
as the relevant classes were not packed by Android Studio into my AAR file.
I tried various ways:
Just compile in Android Studio and hope the resulting AAR will be fine FAIL
Download an AAR from jCentral and add it to Unity - FAIL
So finally I unzipped the EasyPlugin AAR and took the classes.jar file thats inside and added this to Unity. This worked. Yet its quite a manual process.
What is the tinly little bit I need to add to my gradle file to get this done automatically?
I am using:
https://github.com/googlesamples/easypermissions
Unity 2017.1 (should not matter)
Android Studio 2.3.3
Gradle task used: build->assemble
My Gradle File:
apply plugin: 'com.android.library'
android {
compileSdkVersion 26
buildToolsVersion "27.0.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26.+'
testCompile 'junit:junit:4.12'
compile 'pub.devrel:easypermissions:1.0.1'
provided files('unity/classes.jar')
}
PS. There was a second issue when overriding UnityPlayerActivity but for that a clause provided instead of compile was the solution.
Check above comments.
If you use an external library its enough to add its AAR into Plugins/Android directory in Unity.
For some reason I added it in a way that went unnoticed by Unity and Unity was not adding it into resulting APK file.
Error:Execution failed for task ':app:transformClassesWithJarMergingForRelease'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/v4/widget/CursorAdapter$MyDataSetObserver.class - List item
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
dexOptions {
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "com.omairm.hoops"
minSdkVersion 14
targetSdkVersion 23
multiDexEnabled true
ndk {
moduleName "player_shared"
}
}
sourceSets {
main {
jni.srcDirs = []
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile ('com.google.android.gms:play-services:+'){exclude module: 'Support-v4'}
compile files('libs/PTAdRevMob.jar')
compile files('libs/dagger-1.2.2.jar')
compile files('libs/javax.inject-1.jar')
compile files('libs/nineoldandroids-2.4.0.jar')
compile files('libs/support-v4-19.0.1.jar')
}
Do I need to delete the CursorAdapter.class?
You should add something similar to this to your build.gradle, where the duplicate file is added to exclude
android {
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
all*.exclude group: 'com.android.support', module: 'support-annotations'
}
}
do i need to delete the cursoradapter.class
It's good that you are reading the error and recognize a least part of what it's telling you, but you can't just delete a class from a JAR file. The build process that Gradle goes through is only throwing the error at the first class conflict it found, and there would be more if you were to only focus on that one.
My suggestion would be to find the correct and current libraries for each of the JAR files you have and remove the jar files as you go.
Also, try to actually use a number value rather than only a plus for the version because you don't want to be using alpha / beta releases of a library that'll change every few weeks
If you search around Maven you can replace most of those JAR files
dependencies {
compile "com.android.support:appcompat-v7:26+"
// Don't use just a plus here
compile ('com.google.android.gms:play-services:+'){exclude module: 'support-v4'}
compile files('libs/PTAdRevMob.jar')
compile group: 'com.squareup.dagger', name: 'dagger', version: '1.2.2'
// compile group: 'com.nineoldandroids', name: 'library', version: '2.4.0'
}
It's also worth mentioning that nineoldandroids is deprecated. Since you're using minSdkVersion 14, I don't think you even need it.
Dagger has also had many releases since version 1.2 but it already includes javax inject framework (see compiled dependencies https://mvnrepository.com/artifact/com.squareup.dagger/dagger/1.2.2)
You'll definitely want to take a look at Google Play | Selectively compiling APIs into your executable too.
Note that if PTAdRevMob.jar has any classes that still conflict with any other library here, you'll still get an error, and will probably need to switch to a different Ad provider
Android studio has automatically update the following components to API 26:
ROM - SDK to API 26
Android SDK Build-Tools 26
Android Emulator 26.0.3
Android SDK Platforms-Tools 26.0.0
Android SDK Tools 26.0.2
My gradle 2.3.3:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion '26.0.0'
defaultConfig {
applicationId "com.mycompany.myapplication"
minSdkVersion 15
targetSdkVersion 26
versionCode 4
versionName "0.0.4"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
/////////If I change it to 26.0.0 it gives errors.///////////
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-vector-drawable:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
/////////////////////////////////////////////////////////////
testCompile 'junit:junit:4.12'
}
I have two questions (see commented line in Gradle):
How can I know the latest version of these libraries (I simply throw random numbers and try to up them until I found the latest version).
In the excluded group there is 'com.android.support', if I delete that do I need to specify those libraries? I see many people including 'com.android.support' libraries this way so there is a reason I guess to do so.
I searched in developer.android.com and the latest version is 24.2.0 (so really old I have been using 25.3.1).
If you really want to use the 26 version you could use the + character at the end of your dependencies.
compile 'com.android.support:appcompat-v7:26+'
This way, grade will take the latest version beginning with 26.?.? of your dependency. But you will get a warning from Android Studio, because between two different versions the behavior could eventually change and then produce random effect.
To know the latest version, just take a look here : https://developer.android.com/topic/libraries/support-library/revisions.html
And to finish, if you include 'com.android.support', you will include the whole library. And you certainly not need all the stuff included. If you only use recycler view just add :
compile 'com.android.support:recyclerview-v7:26.0.0-beta2'
Check https://developer.android.com/topic/libraries/support-library/revisions.html and set language to English at footer to check latest version of support library. Currently is 25.4.0 (stable) or 26.0.0 (beta 2)
Try this:
allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}
Thanks for reporting your issue.upcoming 6.0.0 release includes support for Java 8 features, therefore in order to run it (or any of the betas) you need to upgrade your project to include Java 8 support by adding this into you app's gradle file:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
I got a project which I imported from Eclipse to Android Studio. In Eclipse everything worked well.
It contains a main module (a project in Eclipse) which uses packages from a library module (library project in Eclipse). Since the migration did not went well, I have created a library module manually and just copied all the source code to the newly created module.
The problem is that the main module doesn't seem to find the packages from the library module and when I rebuild the project I get errors like "package bla bla does not exist".
Here is the main module gradle.build:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.pointer.mamagoose"
minSdkVersion 9
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':linphoneclean')
compile 'com.android.support:support-v4:25.0.0'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.google.android.gms:play-services:9.4.0'
compile files('libs/firebase-client-android-2.5.0.jar')
compile files('libs/apache-httpcomponents-httpclient.jar')
compile files('libs/apache-httpcomponents-httpcore.jar')
compile files('libs/android-support-v7-recyclerview.jar')
}
linphoneclean is the library module.
The entire project's settings.gradle:
include ':linphoneclean'
include ':tigris'
This is the build.gradle of the library module:
apply plugin: 'com.android.library'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.0'
testCompile 'junit:junit:4.12'
compile 'com.google.code.gson:gson:2.6.2'
compile files('libs/commons-lang3-3.4.jar')
compile files('libs/linphone.jar')
compile files('libs/firebase-client-android-2.5.0.jar')
}
The structure of the library module includes for example folder:
src/main/com/pointer/linphone (and inside there are all the java files with a deceleration of package com.pointer.linphone, Yet I still get an error saying >"package com.pointer.linphone does not exist).
What am I doing wrong?
After fighting with the same issue for hours here is what worked for me.
I've created a fresh project with blank activity, added a library module with a dummy class, defined the dependency. Verified that it works by importing the dummy class in the app. Then I copied all my relevant code from the real project.
My thinking is that it was probably issue with IDE's iml files, since starting from scratch and copying stuff over worked.
See properly source file contains both java folder and res folder,add java files in java packages and res in res folder.Add Activity name in Manifest file see here ,add necessary libraries files in Gradle file.Clean and rebuild the project in the Android studio.
https://developer.android.com/guide/topics/manifest/manifest-element.html
Maybe you can check if the library's AndroidManifest has defined the package correctly, like:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
package="com.pointer.linphone">
I wish to use appium for automation testing (I'm using Android Studio for dev).I added a gradle dependency for appium.However, I am unable to run the app due to following warnings and non zero exit errors.
Warning:Dependency org.apache.httpcomponents:httpclient:4.4.1 is ignored for debug as it may be conflicting with the internal version provided by Android.
In case of problem, please repackage it with jarjar to change the class packages
And error as:
> Error:Execution failed for task ':app:preDexDebug'.
> java.lang.IllegalArgumentException: Source
> D:\Learning\GDLearning\app\build\intermediates\pre-dexed\debug\websocket-client-9.2.11.v20150529-344b13c53e6f4871f43101e675dc9239cd9ab46b.jar
> and destination
> D:\Learning\GDLearning\app\build\intermediates\pre-dexed\debug\websocket-client-9.2.11.v20150529-344b13c53e6f4871f43101e675dc9239cd9ab46b.jar
> must be different
My build.gradle file is:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.gdlearning"
minSdkVersion 11
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
packagingOptions{
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.1'
compile('com.google.api-client:google-api-client-android:1.20.0') {
exclude group: 'org.apache.httpcomponents'
}
compile('com.google.apis:google-api-services-tasks:v1-rev41-1.20.0') {
exclude group: 'org.apache.httpcomponents'
}
compile 'com.google.android.gms:play-services-ads:8.3.0'
compile 'io.appium:java-client:3.2.0'
}
Any suggestion/ help would be great!I explored on SO , tried a few things but it didnt worked.
You don't need to push appium dependencies to your app Build.gradle files.
All you need to do is follow the instructions at official page.
http://appium.io/slate/en/master/?ruby#quick-start
Maybe you should:
1) Download Appium
2) Setting Evironment variables (ANDROID_HOME, JAVA_HOME, ..)
3) Play with the Appium
4) Write some test code