Android Could not find class, Nested jars issues? - java

I have two projects, the first is a library (Marked as "isLibrary" in the eclipse properties -> android settings) and the second is an android application. The android application uses the library and everything works ok.
I am now trying to include a jar file in the library and access it's functionality in a class (we will call it ClassA) also found again in the library. Now in the android application I create a new instance of the ClassA and tell it to do some stuff that will use functions provided by the jar I added in the library. I end up with this error:
"Android could no find class 'com.blah.blah.blah' etc."
I checked the .classpath in the library and it is exporting the jar properly. What could I be missing?
Thanks in advance.

I think that while you can use any "lib" folder you want in eclipse, and add it to the classpath, I'm pretty sure for Android to package things correctly the jar should be placed in a folder called libs.

Related

IntelliJ ctrl-click references a .class file not a .java file

I have the following line of code in a .groovy file for testing:
GenerateShipConfirmsForBatch gscb = new GenerateShipConfirmsForBatch();
Ctrl-clicking on the GenerateShipConfirmsForBatch takes me to GenerateShipConfirmsForBatch.class in a .jar, and not the .java file, even though I have the class correctly imported at the top. I want it to reference the .java file so it will pick up changes I make to the .java file.
Any help would be greatly appreciated!
First guess - Wrong import
What you wrote seems to me like you have class with the same name in 2 different packages or in imported project instead of in open-able java class. When you import class be careful that you import the one you want to use.
Eg. annotation class Service is wildly used in different packages.
import org.springframework.stereotype.Service;
import com.google.web.bindery.requestfactory.shared.Service;
Just check that you are using the correct import.
Second guess - Incorrectly set modules
If you are having the multi-module application you have to set correctly the parent project to properly address this issue as well as child projects where the links should be as well.
In Maven it is done using pom.xml. It is very nicely addressed in Maven - Guide to Working with Multiple Modules.
In Gradle it is done using build.gradle. You can read more about it Gradle Multi-Module Project Setup.
Basics about classes
Local class
Idea is linking local .java files in preference instead of .class therefore if this is happening I'd recommend reinstalling Idea as I cannot find the correct approach.
Linked class (from external library)
If you have imported external library it WILL link to .class as it is decompiled from .jar file.
What you can do is either download .jar with source codes, if you are using Maven Projects click on Download Sources and/or Documentation.
Just because you have the class imported at the top does not mean that you can view the source code (e.g., .java file). If this class is coming from a dependency defined in your pom.xml or build.gradle file then you likely won't have access to view the source code. However, if this is a separate module you have at the top level of your project, then you'll be able to view the .java file. If this library is open source then I'd suggest cloning it in your project and adding it as a module. That will solve your problem.
You can install Java Decompiler IntelliJ Plugin from here: https://plugins.jetbrains.com/plugin/7100-java-decompiler-intellij-plugin
It allows you to display all the Java sources during your debugging process, even if you do not have them all

Android Studio: How to import java class from outside the project?

I'm working on an android game that requires a server. We've set up a git in which there are three folders: Client, Server and Shared:
Client contains the Android-Studio project.
Server contains an Eclipse project which is our server and database.
Shared contains java classes that I would like to use in both the Eclipse and Android-Studio project.
In Eclipse I successfully loaded the files from Shared by importing them as "file system". (It created a package and everything works great)
In Android-Studio, I can't find a solution that doesn't require me to copy-paste the classes in the project. The aim here is to have those file at only one location as they're susceptible to change but must me identical in both the projects... So those solutions don't fit my needs.
I'm looking for something like a way to link files from outside the project. (I managed to link libraries from inside the project already but not from outside)
The simple solution is to copy the desired java files into your project structure. Of course, you may have to change package names manually.
Found a solution that might not be optimal but at least in my case it solved the problem.
In Android-Studio:
Create a folder called something like "CommonEclipseAndroid" following a path similar to this one: project/Android/app/src/main/java/com/example/myapplication/CommonEclipseAndroid
Paste the code you need to use in both project inside this folder.
In Eclipse:
Right click on your project: Properties > Java Build Path > Source > Link Source and choose the folder we've just created.
Now your projects use the same code. You only need to modify it once for both projects and there's no risk of error.
Hope I helped :).
You can import the shared folder as a module .
go to file> new> import module
Now use the same imported folder in eclipse as well.

Library properly imported (compiles and import statements are correct), but object cannot access it's methods

I'm attempting to build an android app in Android Studio that integrates the jReddit Java Wrapper. I've cloned the git repository, added a build.gradle file, added the proper dependencies and referenced the jReddit module in my android app's project structure.
I'm able to create any of the objects in the jReddit library, but I cannot use any of the methods for those objects. I get a "Cannot resolve symbol 'xxx'" when I try to use a method. Currently the project structure is set up like below:
Project Title
App
src
libs
jReddit
src
libs
So the app's code is in the src folder and any .jar dependencies are in the libs folder. The jReddit project has in it's build.gradle file "apply plugin: 'java'" and is listed as a dependency in the app's build.gradle file. Finally the libs folder in the jReddit project tree has the jars that IT depends on (which are also referenced in its respective build gradle).
Ultimately, everything compiles nicely, is imported nicely (in the app source code), there are no errors instantiating a new object from the jReddit library, but when I go to do anything with that object, Android Studio cannot find any methods. See the screen shots below to see what I'm talking about:
http://i.imgur.com/Hm09S3W.png
I'm completely dumbfounded with what to do here. I've never encountered anything like this. Does anyone have any suggestions?
So this is a very old post but I never followed up on this. It turns out, I had done everything correctly, but I was attempting to access the objects methods outside the scope of a function (either main or otherwise). Moving the code into the main function fixed the issue.

How to create jar file from Android Library project

I have a Android library project need to convert this in to a jar file and use it on my Android project.like we are doing on Adds network integrations,Is there any way to achieve this?.
An Android project can't really fit into a .jar file, unless you just wanna use the compiled java files, which you would certainly have done it via a normal Java library project instead of a Android Library Project in first place.
Android Library Projects are packaged into aar files. Read more about it here: http://tools.android.com/tech-docs/new-build-system/aar-format and it is automatically handled by Gradle builds and late Android Maven scripts...this last one used to use a 'apklib' format which has been deprecated to be replaced by aar format. You can read more about it here: https://code.google.com/p/maven-android-plugin/wiki/ApkLib
I've done this before.
But, due to my requirements, i only needed implementation (no android specific resources). Although you can still include Android actual code (if the project you're going to use it in is also an Android project in which you can load the SDK which includes the classes you're exporting in the jar)
Now the .jar file that is automatically generated in the bin folder, will not allow you to do this because it includes all the Android's project resources (which is not understood in the Jar format). Therefore, you must exclude all except the src folder. Note : Make sure you also exclude the files in the root folder of the project (i.e, AndroidManifest.xml, etc .. )
In Eclipse : Project properties -> Export -> Jar -> (Include only src folder)
If you're project library includes native code ( NDK ), there is a few other workaround that you need to do to get it to work. If this is the case, comment below, i'll help you out.
Cheers

Referencing External Java and Android Libraries

I am trying to reference a Java and an Android library (that I created) to use within my Android application built in Eclipse.
I am referencing the Java library via Properties-->Java Build Path-->Projects-->Add.
I am referencing the Android library via Properties-->Android-->Library-->Add.
Both of these are shown within the windows after adding them. I also moved the Java library to the top of the list within Properties-->Java Build Path-->Order and Export.
My project compiles fine but when I run the application on my device I get a NoClassDefFoundError from the Java library. The class is in there and should work.
Did I miss something that I need to link in order to get this running correctly?
I am referencing the Java library via Properties-->Java Build Path-->Projects-->Add.
This is incorrect. Please undo that, and then copy the JAR into libs/ within your project. If you are on the R22 or higher version of the tools, you may also need to go to "Order and Export" in your build path dialog and check "Android Private Libraries".
What you did only added the JAR to the compile path. Putting the JAR in libs/ will add it to the compile path and package the JAR's contents in your APK, so those classes are available at runtime.

Categories

Resources