Android Studio and openimaj - java

I am currently trying to build an android studio project using a dependency from the openimaj Java library.
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.mapinguari.myapplication"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'org.openimaj:openimaj:1.3.1'
}
Is my module build file. It reports no errors when syncing to the IDE.
However when I try to construct any of the classes in one of the source files the Android Studio does not recognize any of the classes from the openimaj dependency.
Any Help much appreciated.
Thank you!

I think it might be because you've specified a non-jar OpenIMAJ dependency (specifically you've told it to link against the OpenIMAJ master pom file, which only contains references to the different sub-modules). You probably need to actually choose the specific modules that you want - for example if your application is doing image processing, then add the dependency org.openimaj:image-processing:1.3.1.
Edit:
It seems that the batik svg libraries have a circular dependency somewhere that breaks Gradle (see https://issues.apache.org/jira/browse/BATIK-1098). This is what causes an eventual StackOverflowError. Additionally, something is pulling in xml-apis which will conflict with Android. Assuming you don't mind not have SVG image support, then the following should work:
repositories {
mavenCentral()
maven {
url "http://maven.openimaj.org"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:18.+'
compile('org.openimaj:image-processing:1.3.1') {
exclude group: 'org.apache.xmlgraphics'
exclude group: 'xml-apis'
}
}
You might also want to add additional exclusions for dependencies that are not needed in your app - it seems that just including org.openimaj:image-processing pulls in lots of things that are almost certainly not going to be needed (I've created an issue for that here: https://github.com/openimaj/openimaj/issues/97).

Related

Android aar library doesn't contain dependencies

i saw similar questions, but not found accepted answers.
Problem - i have my own android library with some tiny functions.
My library uses others - f.e. Hawk (no sql database).
My library gradle file:
apply plugin: 'com.android.library'
buildscript {
repositories {
maven { url "https://www.jitpack.io" }
}
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
minSdkVersion 18
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.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:26.0.2'
compile 'com.github.orhanobut:hawk:1.23'
testCompile 'junit:junit:4.12'
}
Library works fine. And if i use it as project inside another project - it work too. But when i generate *.aar file (with gradle -> assembleRelease) and include into separate project - it fails. Project see ONLY MY library's class. Hawk com.orhanobut.hawk package and ofc others (if i will use then) are not visible. So ClassNotFoundException comes.
If i remove
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'library.pro'
the result doesnt change.
I tried to add the following line into proguard file (library.pro)
-keep class com.orhanobut.hawk {public *;}
-keep class com.orhanobut.hawk.* {public *;}
Result is the same.
So i have two question:
1 - what should i do to make my main project see my library's third party dependencies?
2 - is is possible to obfuscate my library's code (only mine, not dependencies)?
what should i do to make my main project see my library's third party dependencies?
The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the module.
It means that, if you are importing a aar file using a flatDir repository you have to specify the dependencies also in your project.
You should use a maven repository, private or public, to avoid the issue.
In this case, gradle downloads the dependencies using the pom file which will contains the dependencies list.
Another way to solve the problem of dependencies is to get the jar files of the dependencies you want to use and place them in the libs folder of your module. This will copy all the your dependency jars into your library's jar or aar.
Note that I have emphasized jar because you cannot include aar file in libs folder, gradle still doesn't support aar file inside aar out of the box. There are a few gradle plugins like fataar which solve that problem.

How can I repair my Android Studio IDE? (Error 23,24 ; Error 26,13 , missing classes and render problems)

I have downloaded Android Studio and it has been since then giving me problems. I've reinstalled it 3 times and I've looked up those problems but I just can't find a solution!
Here is how it looks like:
It seems like I can't get passed the 2 errors shown below (23,24 ; 26,13). Note that if I click on "Install repository and sync project", it won't work.
Also my design window looks bad as I have render problems and missing classes.
You are missing the toolsets required for Gradle to sync your project. You need to install those appcompat and espresso libraries. You said the "Install repository and sync project" button "won't work", but can you clarify what that means? What exactly happens when you click that button?
It might help to share your build.gradle file.
Here is my build.gridle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.black.prima"
minSdkVersion 15
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(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:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
I had the same problem, I tried a lot of these suggestions. I tried tried re-installing over and over and tried to delete all the files left over, I have my account on my windows computer and a guest account set up on my computer so I tried a fresh install on the guest account and noticed also that I downloaded from was not the official site so I made sure I downloaded it from the official site and the new studio came up with no errors

Android Kinvey java.lang.NoClassDefFoundError

I have an android application that used kinvey and it worked just fine with me suddenly i got
java.lang.OutOfMemoryError: GC overhead limit exceeded
so i put
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
in my gradle file but then i got java.lang.NoClassDefFoundError in the line when i create new client
mKinveyClient = new Client.Builder(AP_ID,App_Secret, this).build();
i tried to clean project ,clean gradle file , updated my kinvey sdk from 1.5 to 1.6 Any suggestions?
here is my build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
dexOptions { //for the error java.lang.OutOfMemoryError: GC overhead limit exceeded
incremental true
javaMaxHeapSize "4g"
}
defaultConfig {
applicationId "com.example.dell.augmentedreality"
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
multiDexEnabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
repositories {
jcenter()
flatDir {
dirs 'libs'
}
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile files('src/main/java/Vuforia.jar')
compile(name:'kinvey-android-2.10.6', ext:'aar') {changing= true}
compile 'com.android.support:multidex:1.0.0'
compile 'com.gordonwong:material-sheet-fab:1.2.1'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:recyclerview-v7:23.0.1'
///////////////////////////for animating the text
///////////////////////
compile 'com.nineoldandroids:library:2.4.0'
compile 'com.daimajia.easing:library:1.0.1#aar'
compile 'com.daimajia.androidanimations:library:1.1.3#aar'
///////////////google cloud messaging
compile 'com.google.android.gms:play-services:8.4.0'
}
Edit:in the image below my jars files
I think the problem is that you copied both the kinvey JAR file and the AAR file into the libs/ folder. You only need the AAR.
The class that is claimed to be missing, com.google.api.client.json.JsonObjectParser, is contained within google-http-client-1.19-0.jar, so adding that JAR file to the correct libs/ directory in the module and including these lines in you build.gradle should properly find that class without error.
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile(name:'kinvey-android-*', ext:'aar')
// other stuff
}
With regards to the OutOfMemory error, you most likely exceeded the Android Dex method limit because of how many dependencies you've included. Most specifically, the last line of your gradle file for the play-services dependency. If you read that documentation, you'll see the note that says
Note: If the number of method references in your app exceeds the 65K limit, your app may fail to compile. You may be able to mitigate this problem when compiling your app by specifying only the specific Google Play services APIs your app uses, instead of all of them.
Since you seem to only want cloud messaging, you can just include that one, so replace this line.
compile 'com.google.android.gms:play-services:8.4.0'
With this one
compile 'com.google.android.gms:play-services-gcm:8.4.0'
And any other Play Services you want to include.
i got java.lang.NoClassDefFoundError
You have included wrong Dependant jar. Add appropriate Dependant version of jar by using maven or gradle.

How to include JAR dependency into an AAR library

Summary:
I have an AAR file that depends on a JAR file, when I build the AAR project, it doesn't contain the JAR code.
Details:
I have a Java SDK library project that contains code that we use for Java web projects and such, this library is created using Gradle and resides in an internal nexus server (as a JAR).
The goal is to provide an "Android configured" version of this JAR library through an AAR library that multiple Android Applications can use and minimize the effort (and boilerplate code) to implement it. This AAR file is also uploaded to the nexus server to be used by the Android Application projects.
My AAR project includes a gradle dependency for my Java SDK library (the JAR) but when built, the AAR doesn't include any classes from it.
Code:
This is the Java SDK project's gradle file:
apply plugin: 'java'
//noinspection GroovyUnusedAssignment
sourceCompatibility = 1.7
version = '1.1.1'
repositories {
mavenCentral()
}
jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile 'org.apache.directory.studio:org.apache.commons.io:2.4'
compile 'org.springframework:spring-web:3.1.1.RELEASE'
compile 'com.google.code.gson:gson:2.3'
}
This is the gradle file for my AAR Project, note that I removed the Maven repository declarations to my nexus server from it. I guess it shouldn't matter for the sake of this question.
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "2.2.2"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile ('com.mycompany:javasdk:1.1.1')
}
This is the gradle file for my Android Project, again I removed the nexus server references:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.mycompany.application1"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile ('com.mycompany:androidsdk:2.2.2#aar')
}
NOTE: I initially solved the issue by adding the JAR in the lib directory of the AAR project, but this is undesired. It makes having a nexus server useless. It would be good that we can just bump the JAR's version number in the AAR project's gradle file and the update happens automatically at compile time.
NOTE2: I tried adding transitive=true to my AAR dependency in the Android Project but it didn't solved anything, the real issue is that when building the AAR project, the JAR project code doesn't get bundled.
You can add this task:
task copyLibs(type: Copy) {
from configurations.compile
into 'libs'
}
Dependencies will be downloaded from your Nexus, but when you need package the library, execute this task first and jar files will be copied and included inside final aar.
By default, AAR does not include any dependencies. Solution mentioned by #Hector should work for gradle plugin < 3.0. For Gradle plugin 3.0+, try custom config as mentioned here.
android { ... }
// Add a new configuration to hold your dependencies
configurations {
myConfig
}
dependencies {
....
myConfig 'com.android.support:appcompat-v7:26.1.0'
myConfig 'com.android.support:support-v4:26.1.0'
...
}
task copyLibs(type: Copy) {
from configurations.myConfig
into "libs"
}
None of the suggestions helped me for Gradle 4.6, so I wasted the whole day inventing my own.
Eventually I found a good Gist and modified it for my version of Gradle:
https://gist.github.com/stepio/824ef073447eb8d8d654f22d73f9f30b
The upper ones didn't work for me in android tools 3.6.2 with Gradle. 5.6.1 . For anyone having the same issue, using https://github.com/kezong/fat-aar-android in the end worked fine for me.

How do I import an external library into Android Studio?

I need to use the commons-codec-1.9.jar from the commons-codec-1.9 library supplied by Apache. I've used this as a library in another project in NetBeans, so I already had the required .jar file that I needed to import. I copied the jar file from the NetBeans project and put it in my app project (app:libs folder) and right-clicked and chose the "Add as library..." option. I added it to my project dependencies as well via File > Project Structure > Dependencies. I created the import for the library in the classes I've written that use the library, and I get no errors and the code builds. However, when I run the app, it breaks and throws a NoSuchMethod exception... even though it is there.
Here are the contents of my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion '20.0.0'
defaultConfig {
applicationId "com.example.<path_stuff>"
minSdkVersion 17
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/commons-codec-1.9.jar')
}
Here is my settings.gradle
include ':app'
I have also recently been getting a build error after I removed the commons-codec stuff from the libs folder and tried to re-add it. The error says something along the lines of "Build failed: could not find task 'assemble' in project " as if Android Studio thinks this is another project. Can someone please help me understand what is going on here and where I am going wrong?
No such method error details:
java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64String
Edit: Realized I included the incorrect build.gradle file. Updated settings.gradle to remove the include statement. Included correct build.gradle and NoSuchMethod error.
Remove include ':libs:commons-codec-1.9: from your settings.gradle.
Add compile fileTree(dir: 'libs', include: '*.jar') to your dependencies block in your module's build.gradle file.
android {
...
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
This will automatically compile any .jar file in your libs folder into your project.

Categories

Resources