Android Studio: Add jar as library?

后端 未结 30 2365
天命终不由人
天命终不由人 2020-11-21 05:54

I\'m trying to use the new Android Studio but I can\'t seem to get it working correctly.

I\'m using the Gson library to serialize/deserialize JSON-o

相关标签:
30条回答
  • 2020-11-21 06:35

    With Android Studio 3+:

    You should just be able to simply copy the jar file to the libs folder right under the app folder.

    ... myproject\app\libs\myfile.jar

    Then select Project Files from the drop-down on the Projects window, right click on the project, select Synchronize to see the file in Project Files. It will automatically add the dependencies in the gradle file (Module:app).

    dependencies {
    ...
        implementation files('libs/myfile.jar')
    

    Here is another solution:

    Go to the Project Files view (select Project Files from the dropdown).

    Select New... Directory, create a folder named libs right under app.

    Open up File Explorer, copy and paste your jar file into the libs folder.

    In Android Studio, right click on the jar file, and select Add as a Library... from the popup menu.

    You should see the file listed in the dependencies list in the gradle file:

    dependencies {
    ...
        implementation files('libs/myfile.jar')
    }
    

    Open up your java file, and add the import statement there:

    import com.xxx.xxx;
    
    0 讨论(0)
  • 2020-11-21 06:36

    Like many before pointed out you shall add

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

    to your build.gradle file.

    However I have a project in Android Studio that was migrated from Eclipse and in this case the "libs" folder is named "lib" so for me removing the "s" solved the problem.

    0 讨论(0)
  • 2020-11-21 06:37

    All these solutions are outdated. It's really easy now in Android Studio:

    File > New Module...

    The next screen looks weird, like you are selecting some widget or something but keep it on the first picture and below scroll and find "Import JAR or .AAR Package"

    Then take Project Structure from File menu.Select app from the opened window then select dependencies ,then press green plus button ,select module dependency then select module you imported then press OK

    0 讨论(0)
  • 2020-11-21 06:38

    Easy steps to add external library in Android Studio

    1. If you are in Android View in project explorer, change it to Project view as below

    1. Right click the desired module where you would like to add the external library, then select New > Directroy and name it as 'libs'
    2. Now copy the blah_blah.jar into the 'libs' folder
    3. Right click the blah_blah.jar, Then select 'Add as Library..'. This will automatically add and entry in build.gradle as compile files('libs/blah_blah.jar') and sync the gradle. And you are done

    Please Note : If you are using 3rd party libraries then it is better to use dependencies where Gradle script automatically downloads the JAR and the dependency JAR when gradle script run.

    Ex : compile 'com.google.android.gms:play-services-ads:9.4.0'

    Read more about Gradle Dependency Mangement

    0 讨论(0)
  • 2020-11-21 06:41

    My answer is basically gathering some of the right but incomplete answers provided above.

    1. Open build.gradle
    2. Add the following:

      dependencies {
      compile 'com.android.support:appcompat-v7:19.+'
      compile fileTree(dir: 'libs', include: ['*.jar'])
      compile 'com.google.code.gson:gson:2.3'
      }
      

      This will allow support for two different ways of adding dependencies. The compile fileTree(dir: 'libs', include: ['*.jar']) (as @Binod mentioned) tells the compiler to look under the folder libs for ANY jar. It is a good practice to create such a folder 'libs' which will contain the jar packages that our application needs to use.

    But this will also allow support for Maven dependency. The compile 'com.google.code.gson:gson:2.3' (as mentioned by @saneryee) it is another recommended way to add dependencies that are in a central remote repository and not in our /libs "local repository". It is basically telling gradle to look for that version of that package and it's telling the compiler to consider it when compiling the project (having it in the classpath)

    PS: I use both

    0 讨论(0)
  • 2020-11-21 06:42

    I found Dependency Manager of Android Studio quite handy and powerful for managing 3rd party dependencies (like gson mentioned here). Providing step by step guide which worked for me (NOTE: These steps are tested for Android Studio 1.6 and onward versions on Windows platform).

    Step-1: Goto "Build > Edit Libraries and Dependencies..." it would open up the dialog "Project Structure"

    Step-2: Select "app" and then select "Dependencies" tab. Then select "Add > 1 Library dependency"

    Step-3: "Choose Library Dependency" dialog would be shown, specify "gson" in search and press the "search button"

    Step-4: The desired dependency would be shown in search list, select com.google.code.gson:gson:2.7 (this is the latest version at the time when I wrote the answer), press OK

    Press OK on "Project Structure" dialog. Gradle would update your build scripts accordingly.

    Hope this would help :)

    0 讨论(0)
提交回复
热议问题