Add external jar to android project in eclipse

I am creating a simple Android project in eclipse that uses google-api-java-client . I am trying to follow this tutorial in order to run it and run it. I was looking for SO to answer the question of how to add JAR to an Android project in Eclipse. Most of them recommend adding JARs to the lib/ folder in an Android project, and then adding these JARs to the project build path. I have done these two things. The project compiles perfectly (in any case, Eclipse does not complain about any errors). But when I run the Android application in my emulator, I get a ClassDefNotFoundError whenever I try to instantiate any class in the google-api-java-client JAR. for instance

 new com.google.api.client.http.apache.ApacheHttpTransport(); 

Raises a ClassDefNotFoundError .

Here is the code that causes the error:

 package com.mycom.android; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; public class SearchRunner implements OnClickListener { private static final String API_KEY = "AIzaSyA1Mg3xWXfoov4HdPUzYY2NwTxuvCev1-E"; private static final String PLACES_SEARCH_URL = ""; @Override public void onClick(View v) { EditText editText = (EditText) v; String searchText = editText.getText().toString(); runSearch(searchText); } protected void runSearch(String searchText) { new com.google.api.client.http.apache.ApacheHttpTransport(); } } 

Here is a more complete output from Eclipse LogCat:

 12-21 15:42:11.402: E/dalvikvm(3412): Could not find class 'com.google.api.client.http.apache.ApacheHttpTransport', referenced from method com.mycom.android.SearchRunner.runSearch 12-21 15:42:11.402: W/dalvikvm(3412): VFY: unable to resolve new-instance 70 (Lcom/google/api/client/http/apache/ApacheHttpTransport;) in Lcom/mycom/android/SearchRunner; 12-21 15:42:11.402: D/dalvikvm(3412): VFY: replacing opcode 0x22 at 0x0000 12-21 15:42:11.402: D/dalvikvm(3412): DexOpt: unable to opt direct call 0x00ca at 0x02 in Lcom/mycom/android/SearchRunner;.runSearch 12-21 15:42:11.682: I/MapActivity(3412): Handling network change notification:CONNECTED 12-21 15:42:11.682: E/MapActivity(3412): Couldn't get connection factory client 12-21 15:42:11.791: D/gralloc_goldfish(3412): Emulator without GPU emulation detected. 12-21 15:42:12.532: D/dalvikvm(3412): GC_CONCURRENT freed 102K, 3% free 10520K/10823K, paused 5ms+7ms 12-21 15:42:18.422: D/AndroidRuntime(3412): Shutting down VM 12-21 15:42:18.422: W/dalvikvm(3412): threadid=1: thread exiting with uncaught exception (group=0x409951f8) 12-21 15:42:18.482: E/AndroidRuntime(3412): FATAL EXCEPTION: main 12-21 15:42:18.482: E/AndroidRuntime(3412): java.lang.NoClassDefFoundError: com.google.api.client.http.apache.ApacheHttpTransport 12-21 15:42:18.482: E/AndroidRuntime(3412): at com.mycom.android.SearchRunner.runSearch(SearchRunner.java:20) 
+4
source share
3 answers

I solved the problem by following these steps step by step. When I initially started creating the project:

  • I imported the JAR files from the google-api-java-client project into the lib/ project folder
  • I right-click lib/google-api-client... and select Build Path> Add to Build Path

At this point, in Eclipse, a group of libraries appears in my Linked Libraries project:

 jsr305-1.3.9.jar gson-1.7.1.jar guava-r09.jar junit-4.8.2.jar httpclient-4.0.3.jar httpcore-4.0.1.jar commons-logging-1.1.1.jar commons-codec-1.3.jar jackson-core-asl-1.9.1.jar xpp3-1.1.4c.jar protobuf-java-2.2.0.jar google-http-client-1.6.0-beta.jar google-oauth-client-1.6.0-beta.jar google-api-client-1.6.0-beta.jar 

However, when I went to Project> Properties> Java Build Path> Libraries, I saw that only google-api-client... was specified google-api-client... When I tried to start the project, I received a NoClassDefFounError for ApacheHttpTransport , as indicated in the original post. This is because this class is defined in google-http-client !. By manually adding google-http-client to the build path, the problem is resolved.

Lesson learned: just because the JAR is listed in the Eclipse linked libraries project does not mean that the JARs are included in the project creation path.

+5
source

maybe import? does ctrl + shift + o do anything?

If not, I'm not sure that you should instantiate a good class with this error

0
source

Yes, place the jar in the lib folder and add the project to the build path (the properties section of your project). Eclipse will clean and build after. You should see the jar file used as it appears in the β€œlibrary” in the Android library.

For reference, use the import of some.package.com. Since letroll says ctrl + shift + o should sort the import for you.

0
source

Source: https://habr.com/ru/post/1387459/


All Articles