How do I add a library project to Android Studio?

前端 未结 30 3991
梦谈多话
梦谈多话 2020-11-21 04:24

How do I add a library project (such as Sherlock ABS) to Android Studio?

(Not to the old ADT Eclipse-based bundle, but to the new Android Studio.)

相关标签:
30条回答
  • 2020-11-21 05:10

    A simple way to add a JAR file as a library to your Android Studio project:

    a) Copy your *.jar files

    b) Paste into the libs directory under your projects:

    Enter image description here

    c) Add to build.gradle:

    dependencies {
        ...
        compile files('libs/ScanAPIAndroid.jar', 'libs/ScanAPIFactoryAndroid.jar', .., ..)
    }
    

    b) If your project from example com.example.MYProject and libraries com.example.ScanAPI has the same namespace com.example, Android Studio will check your build and create all necessary changes in your project. After that you can review these settings in menu File -> Project Structure.

    c) If your project and libraries have a different namespace you have to right click on the library and select option "Add as Library" and select the type what you need.

    Remember the "Project structure" option is not doing any auto changes in "build.gradle" in the current version of Android Studio (0.2.3). Maybe this feature will be available in the next versions.

    0 讨论(0)
  • 2020-11-21 05:11

    Option 1: Drop Files Into Project's libs/directory

    The relevant build.gradle file will then update automatically.

    Option 2: Modify build.gradle File Manually

    Open your build.gradle file and add a new build rule to the dependencies closure. For example, if you wanted to add Google Play Services, your project's dependencies section would look something like this:

    dependencies {
         compile fileTree(dir: 'libs', include: ['*.jar'])
         compile 'com.google.android.gms:play-services:6.5.+'
       }
    

    Option 3: Use Android Studio's User Interface

    In the Project panel, Control + click the module you want to add the dependency to and select Open Module Settings.

    Enter image description here

    Select the Dependencies tab, followed by the + button in the bottom-left corner. You can choose from the following list of options:

    • Library Dependency
    • File Dependency
    • Module Dependency

    You can then enter more information about the dependency you want to add to your project. For example, if you choose Library Dependency, Android Studio displays a list of libraries for you to choose from.

    Once you've added your dependency, check your module-level build.gradle file. It should have automatically updated to include the new dependency.

    Source

    0 讨论(0)
  • 2020-11-21 05:11
    1. Press F4 to show Project Structure, click libraries or Global libraries, and click + to add the JAR file.
    2. Click Modules what you want add jar, select the Dependencies tab, click +, and add Library.
    0 讨论(0)
  • 2020-11-21 05:13

    I have just found an easier way (rather than writing directly into the .gradle files).

    This is for Android Studio 1.1.0.

    1. Menu File -> New Module...:

      Import Existing Project

      Click on "Import Existing Project".

    2. Select the desired library and the desired module.

    3. Click finish. Android Studio will import the library into your project. It will sync gradle files.

    4. Add the imported module to your project's dependencies.

      Right click on the app folder -> Open Module settings -> go to the dependencies tab -> Click on the '+' button -> click on Module Dependency.

      The library module will be then added to the project's dependencies.

      Add library module

    5. ???

    6. Profit

    0 讨论(0)
  • 2020-11-21 05:13

    To add to the answer: If the IDE doesn't show any error, but when you try to compile, you get something like:

    No resource found that matches the given name 'Theme.Sherlock.Light'
    

    Your library project is probably compiled as an application project. To change this, go to:

    Menu File -> Project structure -> Facets -> [Library name] -> Check "Library module".

    0 讨论(0)
  • 2020-11-21 05:13

    https://www.dropbox.com/s/1e3eteu3h0pmkf7/Android%20studio%20_doc.doc?dl=0 is the Dropbox link of how to add a JAR file and library project in the latest version of Android Studio 1.0.1.

    Please see the documentation with screenshots. It's very easy for a new user.

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