Android Studio: Add the jar as a library?

This translation from: Android Studio: the Add JAR AS Library?

I'm trying to use the new Android Studio but I can not seem to get it working correctly. I'm trying to use the new Android Studio, but I can not seem to get it to work.

At The'm a using the I Gson Library to the serialize / the Deserialize JSON-Objects. I am using Gson library to serialize / deserialize JSON objects. But the library somehow is not included in the build. But somehow the library is not included in the build.

A new new Project HAD the Created the I with the Just A MainActivity . I created a only MainActivity new projects. Copied gson-2.2.3.jar in The / libs Folder and added IT AS A Library dependancy (Click-right> AS the Add Library). Copy / libs folder gson-2.2.3.jar and add to the library dependencies (right click -> Add to library). This includes the jar in android studio so it can be referenced from the source files. This includes android studio in the jar, so you can reference it from the source file.

When I try to run the project it can not compile so I added: When I try to run the project, it can not be compiled, so I added:

compile files('libs/gson-2.2.3.jar')

to the dependencies in de .gradle file. to dependency de .gradle file. But that IT compiles correctly & the After the when running the Application at The the I GET A ClassDefNotFoundException. After it compiles correctly, but when you run the application, I get one ClassDefNotFoundException.

Does anyone know what I'm doing wrong ? Does anyone know what I did wrong?


#1st Floor

Reference: https://stackoom.com/question/17gX9/Android-Studio- Add the jar as a library


#2nd Floor

IIRC, Simply a using "the Add Library AS" IT IS not enough to for the compile with at The Project. IIRC, just use the "Add to Library" is not enough to use the project to compile.

The Check Intellij's Help the About Adding the Libraries to Project A check Intellij on adding a library to the project to help

The part that should interest you the most is this: part you are most interested in:

(The In File > Project Structure) and the Open The Module1 Settings Tab the Dependencies The SELECT. "(In the File > Project Structureopen module settings, and select)" dependencies "tab.

On the Dependencies tab, click add and select Library. On the "dependencies" tab, click "Add" and select "Library."

In the Choose Libraries dialog, select one or more libraries and click Add Selected. In the "Choose Library" dialog box, select one or more libraries, and then click the "Add Selected."

If the library does not show up in the dialog, add it in the Libraries settings, right below Modules. If the library does not appear in the dialog box, add them to the "library" setting, located under the "Module."

Should not need to the Add by You compile files()anymore, and at The Library Should BE Properly added to your Project. You should not need to add compile files(), correct and should be added to the project library.


#3rd floor

I've been struggling with the same thing for many hours, trying to get the Gson jar to work no less. I've been struggling for several hours on the same thing, trying to get Gson jar continue working. I finally cracked it - here are the steps I took: I finally cracked it - this is the steps I take:

  1. At The JAR Gson PUT (in My Case, gson-2.2.4.jar) INTO at The libsFolder to Gson jar (in my case, gson-2.2.4.jar) into the libsfolder
  2. Right click it and hit 'Add as library' right-click it and click "Add to Library"
  3. That of Ensure compile files('libs/gson-2.2.4.jar')IS in your build.gradleFile (or compile fileTree(dir: 'libs', include: '*.jar')IF you are a using MANY jar Files) If you are using a lot of jar files, make sure compile files('libs/gson-2.2.4.jar')the build.gradlefile (or compile fileTree(dir: 'libs', include: '*.jar'))

    Edit: the Use implementation files('libs/gson-2.2.4.jar')(or implementation fileTree(dir: 'libs', include: '*.jar')) in Android Studio 3.0+ editing: Use in Android Studio 3.0+ in implementation files('libs/gson-2.2.4.jar')(or implementation fileTree(dir: 'libs', include: '*.jar'))

  4. A Clean Build the Do (CAN Probably you do in the this Android Studio Fine, But to the make the Sure Terminal A to the I in the Navigated at The root of My Folder App and typed gradlew clean. The I'm the X-ON Mac OS, the Command Might at The Different BE ON your System do a clean build (you can do this, but I want to make sure that the file to the root of my application folder and type in the terminal navigation in Android Studio gradlew clean. I'm on Mac OS X, the command may be different on your system

After I did the above four, it started working fine. After I finished the four, it starts to work. I think the 'Add as library' step was the one I'd previously missed, and it did not work until I cleaned it either. I think the 'Add to Library' step is the step that I missed before, until I cleaned it before it down.

[Edit - added at The build.gradlethe STEP Which Also Necessary AS IS Others have have pointed OUT] [edit - added build.gradlesteps, it is also necessary, as others have pointed out]


#4th floor

On Mac OS X: On Mac OS X:

  1. Add jar as library (drag jar to libs, right click add as lib) Add jar as a library (the jar onto the libs, right-click add as lib)

  2. Add compile statement to build.grade add the statement to compilebuild.grade

  3. The Install gradle v1.6(use the homebrew) mounted gradle v1.6(self-made program)

    • brew install gradle brew安装gradle
    • gradle -v gradle -v
    • if not v1.6, upgrade homebrew if not v1.6, upgrade homebrew
  4. gradle clean (rebuild android did not work) gradle clean(重建android没有用)

This sorted me out. This rule me out.


#5th Floor

Here are the instructions for adding as a library to a module a local jar file: The following is added to the local file jar as a library module description:

  1. Create a 'libs' folder in the top level of the module directory (the same directory that contains the 'src' directory) to create a 'libs' folder (containing 'src' directory in the same directory) in the top-level directory module

  2. The the In build.gradle filethe Add your Dependencies The following SO closure that has: the build.gradle filefollowing is added to your dependencies closure having:

     dependencies { // ... other dependencies compile files('libs/<your jar's name here>') } 
  3. Should have have already Setup Studio Android gradlew A wrapper. Android Studio should already be set up gradlew wrapper. The Command Line at The the From, the Navigate to your Project at The Top Level of (at The Directory that has A gradlewFile). From the command line, navigate to the top level of the project (with a gradlewcatalog file).

    RUN ./gradlew assemble. Run ./gradlew assemble. This should compile the project with the library . This library should be used to build the project. You may need to fix errors in your build.gradle file as necessary. You may need to repair build.gradle errors in the file as needed.

  4. In order to have Android Studio recognize the local jar files as libraries for support while coding in the IDE, you need to take a few more steps: In order for Android Studio when encoding in the IDE local jar file identified as support libraries, you You need to take the following steps:

    4.1. 4.1. Module in the Click ON at The Right left at The Hand and the Choose Panel Open Library Settings. Right-click in the left panel module, and then choose " Open Library Settings.

    4.2. 4.2. The left of Panel The ON Dialog, the Choose Libraries. In the left panel of the dialog box, select " Libraries.

    4.3. 4.3. At The the Click +Sign from above at The Panel SECOND, at The left -> Java Click on the left side of the second panel at the top +number ->Java

    menu

    4.4. 4.4. Select your local jar and add it to the project. Select Local jar and add it to the project.

  5. May need to RUN at The by You above ./gradlewthe Command One More Time You may need to re-run the above ./gradlewcommand


#6th floor

'... the compile Files' Used to Work for Me,. But not the any More 'compiled file ......' used to work for me, but no longer yes. after much pain, I found that using this instead works: After much suffering, I found that using it can work:

compile fileTree(dir: 'libs', include: '*.jar')

I have no idea why that made a difference, but, at least the damn thing is working now. I do not know why a difference, but at least the damn thing now is working.

Original articles published 0 · won praise 73 · views 550 000 +

Guess you like

Origin blog.csdn.net/w36680130/article/details/105309374