problem with android library project - classes not found - java

I think i do not understand Android Library Project correctly. I'm having the following issue:
i have a library project in a package a.b.c.lib
i have a application which is uses the library project in package a.b.c
i have another application which is uses the library project in package a.b.c.pro
Everything is OK with the application in package a.b.c, however i hit the following issue (during runtime) in the a.b.c.pro app:
java.lang.NoClassDefFoundError: a.b.c.lib.c1$c2
the same class is found OK in the a.b.c app.
So, what is the right way to define packages in this case?

There's no right way. Every way is right. It should work so I think the problem is not in packages. Do you use Proguard for obfuscation? It can remove some classes which are used in your app. Also such errors happen because of some bugs in the toolchain. Try to clean and rebuild you app .

Related

NoClassDefFoundError at Runtime on class inside .AAR in Android Studio

I've been researching for many hours about a possible solution to this but whatever I try it just fails. Here's the explained situation:
I have this library project on Android Studio which generates the app-debug.aar file.
Then I add this new module in the implementing project, following the wizard to import .JAR/.AARs I pick the .aar and update the dependency for :app in the Project Structure.
Once the project is cleaned (even using ./gradlew clean--Mac OS X) I can make references of this class and its methods in any of the activities.
Now at runtime, once built and installed on the real devices the app will crash throwing the aforementioned Runtime Exception.
Note: I'm importing the .aar because I would need to protect the code and it has resources so a .jar is out of the question. And if I import the library module (with source and all instead of an.aar) then the app has no issues at runtime.
I've tried everything and if anyone can throw some light on what this could be, it'll be highly appreciated.
The class extends ViewGroup and is instantiated at runtime in case that implies anything. The code was given to me so major changes might not be possible unless it's absolutely necessary.
Thanks beforehand to all!
Armando
For those, who are still looking for the solution, following two options worked for me to solve the exact same problem as OP has mentioned.
Include the (problematic) library dependency in the target module as well, i-e the in my case i was including the protobuf-lite as a dependency to my library module but getting noclassdefFound error when lib imported as .aar in app module. As a workaround, i added the protobuf-lite dependency to my app module too and it worked like charm.
Second option that worked for me that instead of adding the gradle dependency `compile 'com.google.protobuf:protobuf-lite:3.0.1', i downloaded the protobuf-jar from maven and added manually to the libs of my library module, and the problem got solved.
Don't know what's wrong with the gradle plugin, but hope it helps someone else looking for the same problem.
The NoClassDefFound error has actually happening to a backward-compatibility library being used by this class I mentioned on the question ("Class A"). No details were given other than Class A couldn't be found and later on found out that another class had a similar issue BUT was pointing at that compat lib with the same exception and I noticed that Class A was also calling its methods and implementing its callbacks. By bumping up the min version (to use the native API) I could overcome that issue. It was indeed a problem of including that backward-comp library in the .aar or so it seems. Should I understand this issue a lot better I'll update this "answer."

Problems with JUnit testing in IntelliJ IDEA - Class not found

I'm trying to do some JUnit testing in IntelliJ IDEA. When I have a pure Java project the testing works fine. However I have a Android project I want to test on. The project contains both pure Java files and Android files. This error comes from trying to test a non Android class in a Android project. The non-android class is in the same test package as the Android test classes.
When I have my dependencies structured like this:
I get the following error:
I looked at another thread here on Stackoverflow and that said to put JUnit above the Android API in the dependencies list. When I try that I only get:
Class not found: "com.edit.mouseophonic.app.tests.TestClass"
Process finished with exit code 1
I have no idea how to solve this.
All help appreciated!
You have two options:
Use additional module for andoidTests which you can create in intellij idea http://www.jetbrains.com/idea/webhelp/testing-android-applications.html
Another option using maven-android-plugin https://code.google.com/p/maven-android-plugin/ and use appropriate dependencies. I prefer second.

Eclipse Not Finding Classes added to the build Path (Java Dynamic Web Project)

I am taking over a web project from my school. I am trying to deploy the project in eclipse. The project uses a group of libraries (Namely javax.mail.* , com.sun.mail.*, org.joda.* and org.apache.*). The project can not compile because it is unable to locate these. I have them in the src folder and then added the three top level folders/packages to the build path. The import statements work. But getting the joda.time.CLASSXYZ does not work (Then I instantiate the class CLASSXYZ). (The error is "The type org.joda.time.base.BaseDateTime cannot be resolved. It is indirectly referenced from required .class files" the import was joda.time.* so the class should have been imported)
I've looked at the other threads and most of them just explain how to add folders to the build path. I have Apache tomcat set up with eclipse. It works with other projects that I have deployed.
This problem has gotten pretty frustrating as its preventing me from starting the project. Any help you be much appreciated.
Cheers
Unless you use maven you need to download additional libraries:
javax.mail: http://www.oracle.com/technetwork/java/javamail/index.html (also included in Java EE)
org.apache http://commons.apache.org/
org.joda http://joda-time.sourceforge.net/
I suggest to place them in a folder 'lib' and add the jar-files to your build class path. (context menu on your project).
The other threads you mentioned are pointing you in the right direction, and provide a good practice to get in to (especially once you start having more than one project running in your eclipse ide).
So right click project - Build Path - Configure Build Path... , then in the Libraries tab, click Add External JARs and add in the jar for org.joda from the link in the other answer here. This way you can have all your external jar libraries in one spot and reuse them across multiple projects. There's also about a dozen different ways to do the same thing in eclipse, but this is the most direct I think. I'm on version Helios (your's might be slightly different). I try not to use source files of external code libraries unless I want to play around, debugging their code - not a bad thing to do - but I never have the time to spare for that.
Also I'd double check that the other libraries you mentioned aren't being automatically linked in or included as part of the JRE system library. Especially if you are setup in a Dynamic Web Project, it would surprise me if org.apache wasn't already there... but I could easily be wrong there, have been many times before & eclipse was usually involved ;-)

Project setup for creating third party libraries for Android

I am creating a library for Android that others can include in their own project. So far I have been working on it as a normal Java project with JDK 1.6 setup as system library. This works just fine in Eclipse when I add the android.jar.
The issue comes when I try to my build script. I am running Gradle and doing a normal compile and test build cycle. My thoughts were that it does not matter if I compile it with a normal JDK, since this is not a standalone application. The benefits by creating a normal Java project is that Gradle does support this much better. My project also does not contain any UI at all. However, the problem is that of course android.jar and the JDK contains lots of the same classes and I think that this is what messes up my build script. Everything crashes when running the tests (the tests are in the same project under src/test/java).
My question is, how should I create this project that is meant to be included in Android projects as a third party library? Should I create it as an Android project in Eclipse even though I am only creating a library that does not use any of the UI features? Also, should the tests be in a separate project?
Thanks for all responses!
Have you looked at the Android plugin for Gradle? It incorporates the ProGuard tool to package only necessary classes into your Android APK file, so might address your scenario. Have a look here: https://github.com/jvoegele/gradle-android-plugin/wiki
I will start with the simple question, the one regarding the test project. My experience is that is is better to have a separate test project. I have done this with success in many Java ME project, where the problems are similar. The test project only need to import the source code, not the libraries. Then there should be no problems with duplicate classes.
The other question is a little more difficult. My intuition tells me that the core project should be an Android project.

How can I instruct IntelliJ Idea to not include certatin dependencies (android)

My project layout is
/ServerModule
/commonClasses
/AndroidClient
/needsToIncludeSomeCommonClasses
but as soon as i want to compile and build my AndroidClient IntelliJ adds the server libs to the compile classpath of my android project and I get a UNEXPECTED TOP-LEVEL EXCEPTION because of multiple classes that are present in android and normal java.
My question is how can i set up my project without duplicating the server code?
It seems to be a problem with the android-plugin.
It includes all the serverside libs in the build.
I found a solution.
I need to make a new module with common classes only and link this with the other two modules. Not very pretty but it works
I would ask IntelliJ support, http://www.jetbrains.com/support/idea/index.html they are usually very responsive or ask their Community Support Forum. http://www.jetbrains.com/devnet/

Categories