I have three projects in my Eclipse workspace:
I would like the Android application to use the library project as the dependency.
I have read a few responses to similar questions about adding library file dependency to an Android application and I tried to follow them:
Method #1:
Project->Properties->Java Build Path->Projects-->Add and add the library project.
Now, I can use the classes from the library project. Compiler does not produce any warning. However, when I run the application on the emulator, I get a class-not-defined error.
Method #2:
Copy the generated .jar file into "libs" directory of the Android app.
This works. There is no class-not-defined exception. However, I cannot attach the source and therefore cannot step through the library code. Plus, each time the source changes, I will have to manually copy the .jar file over.
Method #3:
Go to Properties of the library project. You will see "Android" in the left pane. Select it and mark "IsLibrary" field.
In my case, however, there is no "Android" property in the left pane. I guess this property is shown only if it is a genuine Android library project.
I am stuck. I would appreciate it if someone can tell me how to best deal with this situation.
Here is a summary of what I wish to achieve:
Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.
The android developers recommendation is that you assign unique prefixes for your resource names in each android library. Java libraries cannot reference any android SDK classes such as android.
For developing the android application using eclipse IDE, you need to install the Eclipse. you can download it from this location download the Eclipse. Eclipse classic version is recommended but we are using the Eclipse IDE for JavaEE Developers.
Android Studio is faster than Eclipse. There is no need to add a plugin to Android Studio but if we use Eclipse then we do need to. Eclipse needs many resources to start but Android Studio does not. Android Studio is based on IntelliJ's Idea Java IDE and Eclipse uses the ADT Plugin to develop Android applications.
Open your Eclipse installation, and let’s get started! 1) Open the File menu. Select New followed by Project. 2) In the New Project wizard, select Android followed by Android Project. Click Next. 3) Enter a name for your project (in this example, we’ll use AndroidApp) and select the location where your project will be stored. Click Next.
In the Package Explorer, right-click the dependent project and select Properties. In the Properties window, select the "Android" properties group at left and locate the Library properties at right. Click Add to open the Project Selection dialog.
Your library should be available for your project. Click on “ Import Existing Project “. Step 2: Select the desired library and the desired module. Then click finish. Android Studio will import the library into your project and will sync Gradle files. Step 3: In the next step you need to add the imported module to your project’s dependencies.
To use your Android library's code in another app module, proceed as follows: Navigate to File > Project Structure > Dependencies. In the Declared Dependencies tab, click and select Library Dependency in the dropdown. In the Add Library Dependency dialog, use the search box to find the library to add.
Method#1 seems so far the option to go for, but you need to study why you're getting that ClassNotFoundException - unpack the APK (since it's an archive), undex it and see why the library classes are not included.
From experience, if you're working with Eclipse, I believe you're getting that exception because you didn't check the library as exported when you're adding it to Android project - after you've added it to build path, do the following: Project Props -> Build Path -> Order and Export, here make sure your library project is selected. There is a known issue/frustration about this ClassNotFoundException and the work-around is to select the export tab. I believe this article contains more details or this SO topic.
When it comes to debugging the jar code, you need to include in the libs folder a properties file that has the same naming as your jar file (ex. if the library jar file is mylib.jar
, you need to have there a mylib.jar.properties
file). That file should have a src
entry pointing to the path where the source code lives (either as jar file or file system). Eclipse is a bit stupid when you're adding this file so you need to restart it. It can contain a doc
entry as well pointing to where the javadoc lives. Here's an example of my own:
src=../docs/spring-android-rest-template-1.0.1.RELEASE-sources.jar
doc=../docs/spring-android-rest-template-1.0.1.RELEASE-javadoc.jar
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With