Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

java - Adding external library in Android studio 0.3.6

So I am fairly new to Java, and I am trying to add this library to my project. The problem is that Android studio 0.3.6 doesn't have a simple way of doing that and all the answers I searched either reference an older version of Android Studio, or describe how to import an external project (source code, not jar file).

After reading a little, I got to the conclusion that manually adding the jar file would be the best way (manual copy/paste and gradle edits) but as I said, I'm fairly new to this technology and don't know where to place the file nor what lines I need to add to the gradle files.

Can someone help me?

UPDATE 1:
I finally made the IDE recognize the .jar file (I get autocomplete and class recognition). The new problem is that I get the following error when compiling: Gradle: package com.google.gson does not exist. Here are the steps I took to import the library:

  1. Creat a folder called libs in the main directory (src/main/libs should be the result)
  2. Copy the .jar file in that directory
  3. add the following line to the dependencies section in the build.gradle file in your project: compile files('libs/gson-2.2.4.jar'). It should look something like this now:

    dependencies { compile 'com.android.support:support-v13:+' compile files('libs/gson-2.2.4.jar') }

  4. Recompile the project (not sure if necessary, but I did it)
  5. Right click on the libs folder and select "Add as Library"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Since the GSON library is available in MavenCentral, there's an easy way to add it that avoids having to download an archive file and save it in your project.

Go to Project Structure > Modules > Your module name > Dependencies and click on the + button to add a new dependency. Choose Maven dependency from the list:

Adding a Maven dependency

You'll get a dialog box where you can enter search terms or the fully-qualified Maven coordinate string. Since GSON is a common library for Android developers to use, it's actually given in this dialog as an example, with the fully-qualified name. You can type it in:

Adding GSON dependency

Hit OK on both dialogs and you should be good to go.

With these Maven dependencies, the build system will automatically download the library and cache it if hasn't done so already; it takes care of that for you.

If you had a library that wasn't available on MavenCentral, you could save the archive in a libs folder in your project, and from that module dependencies dialog, add a File dependency instead of a Maven dependency to take care of it.

If you edit your build.gradle file by hand, you need to click on the "Sync Project with Gradle Files" button in the toolbar to force Android Studio to pick up the changes and update your project. If you go through the Project Structure dialog, that's unnecessary.

There are lots of conflicting answers to this issue in Stack Overflow because the functionality for this is in flux as the necessary features are implemented; it has been really broken before. These instructions should work properly for 0.3.6, and things will get a little easier in 0.3.7 and later.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...