I have a Android project which uses libraries with interdependencies:
Android Application depends on the following native libraries
module tess-two (containing tesseract and leptonica)
module opencv4android
module mylibrary
mylibrary also depends on tess-two and opencv4android
The following is the build.gradle file of my Android Application:
apply plugin: 'com.android.application'
dependencies {
compile fileTree(include: '*.jar', dir: 'libs')
compile project(':libraries:opencv4android')
compile project(':libraries:tess-two')
compile project(':libraries:mylibrary')
}
android {
compileSdkVersion 19
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 14
targetSdkVersion 19
}
// packagingOptions {
// exclude 'lib/armeabi/liblept.so'
// exclude 'lib/armeabi/libtess.so'
// exclude 'lib/armeabi-v7a/liblept.so'
// exclude 'lib/armeabi-v7a/libtess.so'
// exclude 'lib/mips/liblept.so'
// exclude 'lib/mips/libtess.so'
// exclude 'lib/x86/liblept.so'
// exclude 'lib/x86/libtess.so'
// }
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
A gradle build on this configuration fails with duplicate files during packaging of APK error due to liblept.so and libtess.so being copied twice.
When I uncomment the packaging options I can build successfully, however, I get an UnsatisfiedLinkError at runtime since liblept.so and libtess.so are not contained in the apk at all.
How can I get liblept.so and libtess.so to be included in the apk exactly once?
I think I found the solution, however, I do not quite understand it.
The build.gradle files of both modules mylibrary and tess-two contained a definition for
android.sourceSets.main.jniLibs.srcDirs = ['libs']
Commenting this definition in tess-two resulted in libtess.soand liblept.so being copied to the apk only once.
If someone understands this solution please leave a comment or answer.
Edit:
In the meantime I found a better solution:
I added the following code to my main applications gradle.build file:
packagingOptions { // otherwise libtess.so and liblept.so are copied to apk twice resulting in an error
pickFirst('lib/*/liblept.so')
pickFirst('lib/*/libtess.so')
}
Related
i have a prebuilt libraries that contains jar files and .so files and i newly converted my project from eclipse to android studio
my libs directory in eclipse is
libs
****armeabi
****x86
****jar files
so armeabi and x86 contains the .so files and when i converted project to android studio the app build very well at first but some classes in the .so files are not initialized and giving error
java.lang.ExceptionInInitializerError
Caused by: java.lang.UnsatisfiedLinkError: Couldn't load nmsp_speex from loader dalvik.system.PathClassLoader[dexPath=/data/app/project.app-2.apk,libraryPath=/data/app-lib/project.app-2]: findLibrary returned null
and i searched about this error , people says that you must add .so files in jni folder and a already did that ad found build error when android studio tried to build the ndk and the error says
Error:Execution failed for task ':project:compileDebugNdk'.
com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'E:\Android Index\android-ndk-r10d\ndk-build.cmd'' finished with non-zero exit value 1
and here is my build.gradle file
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':appcompat')
compile project(':facebook')
compile project(':google-play-services_lib')
compile project(':main')
compile project(':pagerslidingtabstrip')
compile 'com.android.support:multidex:1.0.0'
}
android {
compileSdkVersion 21
buildToolsVersion "21.0.0"
defaultConfig
{
minSdkVersion 9
targetSdkVersion 21
multiDexEnabled true
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
jni.srcDirs = ['src/main/jni', 'src/main/jni/']
}
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
so any help on this
if you don't have any NDK source code inside your project (you're not generating the .so files, you're only using them), then remove the jni folder and put your .so files into src/main/jniLibs/armeabi and src/main/jniLibs/x86.
btw you placed your ndk directory under a path that contains a space. You should move it to somewhere else or delete this blank space.
I had the same issue.I placed my .so files under
src->
main->
jniLibs->armeabi-> .so file
and removed the line containing
jni.srcDirs = ['src/main/jni', 'src/main/jni/']
from my app's build.gradle.And it works fine.
Hope this helps someone.
I'm trying to import an Android project build on Eclipse into my Android Studio IDE. I'm following the steps to import properly the project with a build.gradle file but when I'm trying to compile, I get this weird error: Plugin with id 'android' not found. I have this version of Android Studio: Android Studio (Preview) 0.4.3. Here is my build.gradle:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:DesignDialogLibrary')
compile project(':workspace:HoloColorPickerLib')
compile project(':workspace:MbAdBleamSdkReaderLibrary')
compile project(':workspace:ViewPagerLibrary')
compile project(':workspace:PullToRefreshLibrary')
compile project(':workspace:SlidingMenuLib')
}
android {
compileSdkVersion 17
buildToolsVersion "18.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
You're missing this block in your build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
This block tells Gradle where to find the "android" plugin that you're missing.
It's standard in projects created in newer versions of Android Studio to put this in the build.gradle that's in your project's root directory and omit it from the one that's in your module's directory, so look there and see if looks like it's in order.
If you don't have that root-level build.gradle file and you've only got one directory and one build file, then add this block to that file. Be aware, however, that Android Studio currently has a few bugs that prevent the Project Structure dialog from working properly with projects in this single-module format, though I believe they'll otherwise build and run fine.
I'm trying to build my android library project, which contains sub test project. Command to build: gradle clean connectedCheck
After task :dexTest I got error:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lorg/json/simple/ItemList;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/accessibilityservice/AccessibilityServiceInfoCompat;
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lorg/apache/commons/lang3/CharRange$1;
Command gradle clean build ends successful. So my problem is that i can't run my tests. When i change plugin, in my build.gradle file, from 'android-library' to 'android' everything works fine.
Also, i'm added --info param and got this, before error
command: /home/username/adt-bundle/sdk/build-tools/18.1.1/dx --dex --output /home/username/android/build/libs/android-test.dex /home/username/android/build/classes/test /home/username/android/build/dependency-cache/test /home/username/android/libs/json_simple.jar /home/username/android/build/bundles/debug/classes.jar /home/username/android/build/bundles/debug/libs/json_simple.jar /home/username/android/libs/android-support-v4.jar /home/username/android/build/bundles/debug/libs/commons-lang3-3.1.jar /home/username/android/build/bundles/debug/libs/android-support-v4.jar /home/username/android/libs/commons-lang3-3.1.jar
What was that? Why it uses libraries from folder build/bundles/debug/libs/? This is the reason why I had error java.lang.IllegalArgumentException but why gradle takes libraries from that dir i don't understand.
Here my build.gradle file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.3'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 18
buildToolsVersion "18.1.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 18
testPackageName "ua.cooperok.stringcalc.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets {
main {
manifest.srcFile file('AndroidManifest.xml')
java.srcDirs = ['src']
resources.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest {
manifest.srcFile file('tests/AndroidManifest.xml')
java.srcDirs = ['tests/src']
resources.srcDirs = ['tests/src']
res.srcDirs = ['tests/res']
assets.srcDirs = ['tests/assets']
}
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
}
Maybe it's a bug in android-library plugin?
While I do not know exact answer to the question, my colleagues from JetBrains supporting gradle in IntelliJ IDEA advised to ask the question at https://code.google.com/p/android/ because it's clearly an issue (and may be a bug) in Google Android Gradle Plugin for Eclipse, given that you work in Eclipse, as far as I recall from our conversation.
Now problem is no longer relevant. In build tools from version 0.7.2 it is fixed.
Piece of updates from tech doc
- Solve issue with local jar when testing library projects.
I am currently attempting to switch my project over to using gradle builds. While using the old build system I linked to the private/internal/hidden android API's by compiling the source and linking the classes.jar files before the Android libraries. I am currently trying to do the same thing in gradle with no luck at all.
With my current build.gradle file I receive the following error:
Gradle: A problem occurred evaluating project ':Launcher2'.
> Could not find method external() for arguments [file collection] on project ':Launcher2'.
If I remove these lines before "apply plugin: android"
dependencies {
compile files('libraries/classes.jar')
}
I receive errors about missing com.android.internal and com.android.common packages which are located in my classes.jar file.
Here's my build.gradle file.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.6'
}
}
dependencies {
compile files('libraries/classes.jar')
}
plugin: 'android'
android {
buildToolsVersion "17.0"
compileSdkVersion 10
dependencies {
compile fileTree(dir: 'libraries', include: '*.jar')
compile project(':ActiveAndroid')
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
}
}
Edit: After reading the gradle documentation a bit more I was able to successfully link the libraries into the project but the order of dependencies is wrong. The equivalent to what I am trying to do is adding both .jar files to the top of the classpath so that they are declared before the android SDK jar files.
I was digging through the source for the android gradle plugin, it seems that this may not be possible. The code seems incomplete and will not build. If anyone has any insight I would really appreciate it. Otherwise it looks like I will have to switch back to using Ant for now.
I'm trying to switch from eclipse to android studio for my android development.
However, I still haven't found the right way to import my existing project.
I don't know if it is important: but I'm using a mac
I did the export step in eclipse, imported this gradle build in Android Studio, but when I try to build my project, it gives me this error:
Gradle:
FAILURE: Could not determine which tasks to execute.
* What went wrong:
Task 'assembleDebug' not found in project ':ProjectName'.
* Try:
Run gradle tasks to get a list of available tasks.
Could not execute build using Gradle installation '/Users/<username>/Development/Build/gradle-1.6'.
This is the build.gradle file that eclipse gave me:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.+'
}
}
apply plugin: 'android'
dependencies {
compile project(':ProjectName:library:ActionBarSherlock')
compile project(':ProjectName:library:facebook')
compile files('../../../../../../../ProjectName/libs/gcm.jar')
compile files('../../../../../../../ProjectName/libs/libGoogleAnalyticsV2.jar')
compile files('../../../../../../../ProjectName/libs/commons-lang3-3.1.jar')
compile files('../../../../../../../ProjectName/libs/actionbarsherlock-plugin-maps-4.2.0.jar')
compile files('../../../../../../../NiteOwl/libs/volley.jar')
compile project(':ProjectName:library:PullToRefresh')
compile project(':ProjectName:library:google-play-services_lib')
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
I've seen a lot of possible solutions, but none of them worked for me, any idea what I'm doing wrong?
The path to the project has no spaces in it
jar libraries => ProjectName/libs
android libraries => ProjectName/library
all of this was working in eclipse
We recently moved our project to gradle as well. We ran into issues due to the library projects.
To solve it we added a settings.gradle file in the root of your project with
include ':libs:actionbarsherlock'
include ':yourprojectname'
Add all your library projects from eclipse into the settings.gradle
We also made a build.gradle file for each of the library Projects.
AFAIK the export from eclipse doesn't deal well with library projects.
In the build.gradle at the root of your project (the one that's probably mostly empty), add the following line:
task assemble{}
Found at https://code.google.com/p/android/issues/detail?id=57531
I've had this error appear when I had a stray empty flavors stanza in my build.gradle:
productFlavors {
}