Adding JAR files to your testing classpath: - java

Am performing a junit test on one of my methods in a class in android. and according to the info at http://code.google.com/appengine/docs/java/tools/localunittesting.html i should add the libraries below to my classpath.
${SDK_ROOT}/lib/impl/appengine-api.jar
${SDK_ROOT}/lib/impl/appengine-api-labs.jar
${SDK_ROOT}/lib/impl/appengine-api-stubs.jar
My question is, where in u.s should i get this libraries and how should i add them? am using android studio running on ubuntu 15 64bit.
am fairly new in android and GAE things!
what i have tried
this is what i have added to my modlue's build.gradle
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
testCompile 'com.google.appengine:appengine-testing:1.8.2'
testCompile 'com.google.appengine:appengine-api-labs:1.8.2'
testCompile 'com.google.appengine:appengine-api-stubs:1.8.2'
testCompile 'com.google.appengine:appengine-tools-sdk:1.8.2'
testCompile 'org.json:json:20141113'
}
When i try to run my junit test class. this is what am getting
com.googlecode.objectify.SaveException: Error saving
com.top.ash.backend.entities.Ameggedon#589b3632: No API environment is registered for this thread.
com.googlecode.objectify.impl.EntityMetadata.save(EntityMetadata.java:95)
at com.googlecode.objectify.impl.WriteEngine.save(WriteEngine.java:75)
at com.googlecode.objectify.impl.SaverImpl.entities(SaverImpl.java:60)
at com.googlecode.objectify.impl.SaverImpl.entity(SaverImpl.java:35)
org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.NullPointerException: No API environment is registered for this thread.

Note: I know nothing about GAE, but here is how you can add jars from folder to test classpath.
Create a new folder in the same directory as libs folder (in this picture it is called testlibs). In this directory you should put those jars.
Now, add to the build.gradle of the module (not of the project), inside dependencies block:
dependencies {
...
testCompile fileTree(dir: 'testlibs', include: ['*.jar'])
}
Now in your tests (Junit, not instrumentation) you could use the methods from the jar.

Related

Problem with gradle and external jar - jar not found

I have a problem with Gradle. I want to use an externally provided Jar: itext-2.1.7.js6.jar, it is a patched version used by JasperReports.
In the project structure, I have a /libs directory, containing 2 files: fonts.jar and itext-2.1.7.js6.jar
Part of my gradle.build file looks like this:
compile fileTree(dir: 'libs', include: ['*.jar'])
implementation group: 'net.sf.jasperreports', name: 'jasperreports', version: '6.7.1'
implementation group: 'net.sf.jasperreports', name: 'jasperreports-functions', version: '6.8.0'
implementation group: 'joda-time', name: 'joda-time', version: '2.9.9'
//implementation group: 'hu.blackbelt.bundles.itext', name: 'com.lowagie.itext', version: '2.1.7-1'
The last dependency is commented out cause I want to use the external JAR instead of the original iText.
When I am trying to run my app I get:
BUILD FAILED in 0s
1 actionable task: 1 executed
Could not find com.lowagie:itext:2.1.7.js6.
Required by:
project : > net.sf.jasperreports:jasperreports:6.7.1
What am I doing wrong? Everything is fine with my fonts.jar, so I think that it should be the same with iText, but it isn't...
As can be seen in the pom file of jasperreports, there is an explicit dependency on com.lowagie:itext:2.1.7.js6 which can also be found in a specific Jasper Reports repository
The reason this works transparently in Maven is that Maven will use the repositories defined in a pom file, while Gradle will not use them, as they could be a source of insecurity.
You have two options:
Add that repository, potentially using repository filtering to only serve these artifacts from there and then remove the file from libs
Add an exclude to jasperreportsso that it no longer tries to fetch that dependency. And instead the runtime code will use the version found in libs. You just forget about transitive dependency management in this case, for example when upgrading jasperreports and getting (or not) an updated version from their dependency declaration.

Failed to resolve: com.android.support:support-v4:26.0.2

when i use this library :
compile 'co.ronash.android:pushe-base:1.4.0'
I get this error in gradle:
Failed to resolve: com.android.support:support-v4:26.0.2
I can't fix it.
There is a solution to be there library Ignored
'com.android.support:support-v4:26.0.2' from 'co.ronash.android:pushe-base:1.4.0' ?
Because I have already compiled a newer version of support-v4 library.
All my dependencies code in gradle :
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.constraint:constraint-layout:1.0.0-alpha7'
compile 'com.android.support:appcompat-v7:26.0.0-alpha1'
compile 'com.android.support:support-v4:26.+' //--> its ok and no problem
compile 'uk.co.chrisjenx:calligraphy:2.2.0'
compile 'co.ronash.android:pushe-base:1.4.0' //--->this code is error Failed to resolve: com.android.support:support-v4:26.0.2
compile 'com.google.android.gms:play-services-gcm:11.0.4'
compile 'com.google.android.gms:play-services-location:11.0.4'
testCompile 'junit:junit:4.12'
}
There are a few reasons which can cause this kind of problem.
Checkout these solution, I guess it might help you:
1. Remove Pushe and try adding support-v4 library with version of 26.0.2 to make sure it gets downloaded and cached to your system.
compile 'com.android.support:support-v4:26.0.2' // or implementation
If it still can't be resolved then you have a problem getting it from it's repository server. checkout build.gradle(project:your_prj) and make sure this block is valid.
allprojects {
repositories {
google() // or maven { url 'https://maven.google.com/' } for lower gradles
jcenter()
}
}
If your country is included in sanction you might need to use a VPN to be able to get them. You can also use a proxy like Fod(also donate if you can since it's opensource) or shecan.
Remember all support libraries better be 26.0.2 to avoid conflict and crashing. Your support libraries are 26.0.0-alpha1 which is lower.
After you successfully added and cached support-v4 then try to add Pushe to your dependencies again.
! And also remember that pushe has a support-v4 bundled in it and it's not really needed for you to add it yourself
And Make sure you have
<uses-sdk tools:overrideLibrary="co.ronash.pushe" />
in you manifest. Therefore you can override it's libraries and dependencies.
At the end keep your SDK update.
Edit:
Current version of Pushe is using android support libraries. If you attend to use AndroidX be sure to enable jettifier in order to do runtime conversion.

com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: android/support/v4/os/ParcelableCompat.class [duplicate]

I am not sure what this error means.
Execution failed for task ':excelSior:packageAllDebugClassesForMultiDex'.
> java.util.zip.ZipException: duplicate entry: android/support/v4/util/TimeUtils.class
I am currently using android-support-v4.jar for my libraries
dependencies {
compile project(':addThisSDK')
compile project(':centeredContentButton')
compile project(':googleplayservices_lib')
compile files('libs/adxtag2.4.6.jar')
compile files('libs/android-support-v4.jar')
compile files('libs/aws-android-sdk-1.7.1.1-debug.jar')
compile files('libs/commons-lang-2.6.jar')
compile files('libs/crittercism_v4_4_0_sdkonly.jar')
compile files('libs/dd-plist.jar')
compile files('libs/FiksuAndroidSDK_4.1.1.jar')
compile files('libs/iqengines-sdk-barcode.jar')
compile files('libs/irEventTracker-1.2.jar')
compile files('libs/jolt-core-0.0.7.jar')
compile files('libs/json-utils-0.0.7.jar')
compile files('libs/jsoup-1.7.2.jar')
compile files('libs/kooaba-api-v4-java.jar')
compile 'com.android.support:multidex:1.0.0'
}
Error does not show up during gradle sync. Just when I try to run the application
What could be the problem?
You've probably fixed this by now but just so this doesn't stay unanswered,
Try adding this to your build.gradle:
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
You need to check that you have inserted v4 library and compile library? You must not repeat library in your app or your dependence program.
delete the repeat library so that just one V4 remains.
in your app dir build.gradle file
add this command:
android{
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
all*.exclude group: 'com.android.support', module: 'support-annotations'
}
}
it works for me! You can try it!
I also came across this kind issue when re import old eclipse project. It occurred some old dependency as jar file in the project.
just remove
compile fileTree(dir: 'libs', include: '*.jar')
in gradle file
and add dependency in the gradle file.
It works for me ..
In my case the mentioned "duplicate entry" error arised after settingmultiDexEnable=true in the build.gradle.
In order to fully resolve the error first of all have a look at Configure Apps with Over 64K Methods (espescially "Configuring Your App for Multidex with Gradle").
Furthermore search for occurences of the class that causes the "duplicate entry" error using ctrl+n in Android Studio. Determine the module and dependency that contains the duplicate and exclude it from build, e.g.:
compile ('org.roboguice:roboguice:2.0') {
exclude module: 'support-v4'
}
I had to try multiple module labels till it worked. Excluding "support-v4" solves issues related to "java.util.zip.ZipException: duplicate entry: android/support/v4/*"
My understanding is that there are duplicate references to the same API (Likely different version numbers). It should be reasonably easy to debug when building from the command line.
Try ./gradlew yourBuildVariantName --debug from the command line.
The offending item will be the first failure. An example might look like:
14:32:29.171 [INFO] [org.gradle.api.Task] INPUT: /Users/mydir/Documents/androidApp/BaseApp/build/intermediates/exploded-aar/theOffendingAAR/libs/google-play-services.jar
14:32:29.171 [DEBUG] [org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter] Finished executing task ':BaseApp:packageAllyourBuildVariantNameClassesForMultiDex'
14:32:29.172 [LIFECYCLE] [class org.gradle.TaskExecutionLogger] :BaseApp:packageAllyourBuildVariantNameClassesForMultiDex FAILED'
In the case above, the aar file that I'd included in my libs directory (theOffendingAAR) included the Google Play Services jar (yes the whole thing. yes I know.) file whilst my BaseApp build file utilised location services:
compile 'com.google.android.gms:play-services-location:6.5.87'
You can safely remove the offending item from your build file(s), clean and rebuild (repeat if necessary).
check your dependencies versions, you must have compatible versions put attention specially to com.google packages, must have same version like:
compile 'com.google.android.gms:play-services:8.3.0'
compile 'com.google.android.gms:play-services-maps:8.3.0'
Both are 8.3.0, if you have another version compilation will throw that exception.
Simple Remove Your Jar file from dependencies gardle.project as v7
and run your project
For Expose.class Error i.e
java.util.zip.ZipException: duplicate entry: com/google/gson/annotations/Expose.class
use the below code
configurations {
all*.exclude module: 'gson'
}
find out the lib depends on the support v4, and exclude it.
code in build.gradle is like this:
androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.1') {
// http://stackoverflow.com/a/30931887/5210
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
In my situation, the lib 'espresso' has a jar called support-v4 and in my project 'app' have the same support-v4, exclude the support-v4 when import espresso.
PS: it seems compile project can not work with the exclude
This is because you have added a library and given its dependency on a module more than once.
In my case, I had added a library as a module and as a gradle dependency both.
Removing one source of adding library (I removed gradle dependency) solved my problem.
For Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug' com.android.build.api.transform.TransformException java.util.zip.ZipException duplicate entry com/google/gson/annotations/Expose.class
Here is what I did:
1) Delete the gson-2.5.jar file.
2) configurations { all*.exclude module: 'gson-2.5' }
I have faced this issue as i have manually copied the jar in libs as well as mentioned the dependency in gradle file. You also check in your project structure, whether the same jar file is copied in any other folder like libs or in project folder.
I had the same problem after upgrading the android SDK. I was able to run the application in the buildToolsVersion '23.0.1', I got the same error when I changed to buildToolsVersion '24.0.3'
I resolved the problem by updating my java version from 1.7 to 1.8 with compileSdkVersion 24
This problem cost me one whole day. I finally downgraded the firebase-ui library version from 2.0.0 to 1.2.0 and added the following code inside Project level build.gradle file:
allprojects {
repositories {
jcenter()
// Add the following code to your project level build.gradle
maven {
url 'https://maven.fabric.io/public'
}
}
}
I also have the issue because of i have compile 'com.android.support:appcompat-v7:24.0.0-alpha1' but i added recyclerview liberary compile 'com.android.support:recyclerview-v7:24.0.2'..i changed the version as same as compat like (24.0.2 intead of 24.0.0).
i got the answer..may be it will help for someone.
In my case the mentioned "duplicate entry" error arised after settingmultiDexEnable=true in the build.gradle.
and exact error which i was getting was below :
Error:Execution failed for task
':android:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException:
java.util.zip.ZipException: duplicate entry:
com/google/android/gms/internal/zzqx.class
So first thing I search for class which causes "duplicate entry" error using ctrl+n in Android Studio and searched for com/google/android/gms/internal/zzqx.class and then it was showing 2 entries for gms class with one version 8.4.0 and 1 with version 11.6.0 .
To fix it i made both to use 11.6.0 and it was fixed example
earlier
compile "com.google.android.gms:play-services-games:11.6.0"
compile "com.google.android.gms:play-services-auth:8.4.0"
compile "com.google.android.gms:play-services-ads:11.6.0"
After
compile "com.google.android.gms:play-services-games:11.6.0"
compile "com.google.android.gms:play-services-auth:11.6.0"
compile "com.google.android.gms:play-services-ads:11.6.0"
Rebuilding Fixed .
In my case exact error was below
':android:transformClassesWithJarMergingForDebug'.
com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: com/google/android/gms/internal/zzqx.class
I was using another version of google apis i.e. in one modules gradle file
if (!project.hasProperty('gms_library_version')) {
ext.gms_library_version = '8.6.0'
}
however in other modules version 11.6.0 as below
compile "com.google.android.gms:play-services-ads:11.6.0"
compile "com.google.android.gms:play-services-games:11.6.0"
compile "com.google.android.gms:play-services-auth:11.6.0"
However to find this i did a ctrl + n in android studio and entered class name zzqx.class and then it displayed 2 jar files being pulled for this class and then i understood that somewhere i am using version 8.6.0 . On changing 8.6.0 to 11.6.0 and rebuilding the project the issue was fixed .
Hope this helps .
More on this here
https://www.versionpb.com/tutorials/step-step-tutorials-libgdx-basic-setup-libgdx/implementing-google-play-services-leaderboards-in-libgdx/
For me something similar happened when I had accidently added
apply plugin: 'kotlin-android'
to my android library module. Removing the line fixes the issue.
I tried all the above solutions but not working for me. I tried update libraries by goto project structure > app. And it works for me! Hope this answer helpful to someone.
Try this:
android {
configurations {
all*.exclude module: 'PhotoView' //去除重复依赖库
}
}

NoClassDefFoundError: android.support.design.internal.NavigationMenuPresenter

After adding the new dependencies in the build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.thebluealliance:spectrum:0.5.0'
compile 'com.github.sillebille:dynamic-calendar:1.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:design:23.0.1'
compile 'com.android.support:support-v4:23.0.1'}
Caused the following exception
Caused by: java.lang.NoClassDefFoundError: android.support.design.internal.NavigationMenuPresenter
at android.support.design.widget.NavigationView.<init>(NavigationView.java:80)
at android.support.design.widget.NavigationView.<init>(NavigationView.java:92)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
at android.view.LayoutInflater.createView(LayoutInflater.java:611) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:280)
Is there any problem the added library or some proguard has to be added to make this work?
A "NoClassDefFoundError" means that you're trying to use a class that either isn't defined, or doesn't exist. Double check that any libraries you're using are installed correctly if they're open source, and if you're quite sure that everything is correct, it might just be a problem with the API. I've run into this error loads of times because API's are either riddled with errors or are simply not complete. Probably not what you wanted to hear, but I hope it helps!

How to convert a non-gradle JUnit project (that uses Hibernate and EntityManager) to a gradle Junit project and keep it working

I have a JPA/Hibernate project that implements a number of repository interfaces (I use DDD) and I have another project that tests the first one, by using JUnit 4. My IDE is Eclipse Luna. I learned later that I could made a single project with both runtime and testing code, but at this moment, I have them separated in those two projects.
Everything works perfectly so far.
Now, I decided to convert my test project in a Gradle project. After converting, I created this build.gradle file
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
repositories {
mavenLocal()
mavenCentral()
}
jar {
baseName = 'dominio.hibernate.tests'
version = '0.1.0'
}
dependencies {
compile 'junit:junit:4.11'
compile 'org.hamcrest:hamcrest-all:1.3'
runtime fileTree(dir: '../dominio/build/libs', include: '*.jar')
compile fileTree(dir: '../dominio/build/libs', include: '*.jar')
runtime fileTree(dir: '../dominio.hibernate/build/libs', include: '*.jar')
compile fileTree(dir: '../dominio.hibernate/build/libs', include: '*.jar')
compile 'postgresql:postgresql:9.0-801.jdbc4'
compile 'org.hibernate:hibernate-core:4.3.6.Final'
compile 'org.hibernate:hibernate-c3p0:4.1.0.Final'
compile "javax.enterprise:cdi-api:1.0-SP1"
compile "javax.servlet:servlet-api:2.5"
runtime "org.jboss.weld:weld-core:1.0.1-SP1"
compile project(':Dominio')
compile project(':DominioHibernate')
}
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
The compile works perfectly, but when I run the tests, iether in Eclipse or by Gradle, I have the folowing exception
javax.persistence.PersistenceException: No Persistence provider for EntityManager named unidades
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at dominio.hibernate.tests.HibernateUnidadeOrganizacionalRepositoryTests.setUp(HibernateUnidadeOrganizacionalRepositoryTests.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
... the rest of the stack
I stress that nothing changed in the Java code and it ran perfectly before Gradle came into the scene.
So, it appears that after converting the project to a Gradle one, persistence.xml is not being reachable anymore during the JUnit run, and I can't get the EntityManager instance I need to perform the tests.
I browsed a large number of posts about problems relating JUnit, resource files and Gradle, but no one of them could give me an idea about how to fix the problem.
Any ideas? Thanks in advance!
To successfully run tests that make use of Hibernate, Hibernate resources and related code have to placed in the same output directory:
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
This is only a problem for tests, as they have the output directories on their class path, rather than the Jar/War. (This avoids the need to build a Jar/War for running tests.)
Thanks to Peter Niederwiser answer and comments, this post in StackOverflow and some reading on Gradle documentation about Java plugin I was able to figure it out what was wrong and fix it. Detais below:
Peter suggestions on updating the resourceDir for both main and test source sets were done (see Peter's answer and his comments)
There was a Hibernate package missing for runtime environment; I updated the dependency block to add it:
dependencies {
...
compile 'org.hibernate:hibernate-entitymanager:4.3.6.Final'
runtime 'org.hibernate:hibernate-entitymanager:4.3.6.Final'
...
}
Only this two steps combined fixed my problem. The META-INF folder was copied to the right place and the (now present) Hibernate package was there to handle persistence.xml in order to create the EntityManager instance my testing code needed.

Categories

Resources