Mysteries of importing libraries (eg, Picasso) to Android Studio

前端 未结 1 1922
后悔当初
后悔当初 2021-01-21 07:58

Android Studio, it\'s 0.5.9 (on a Mac, fwiw),

(1) What (\"the hell\") is the difference between the inner and outer build.gradle file? I\'m confused why

相关标签:
1条回答
  • 2021-01-21 08:19

    Answering your questions out of order:

    1. The outer build.gradle has a comment in it which reads:

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

    Inner build.gradle files are per-module; it's in the inner modules where you do most of your work in terms of adding dependencies and setting things up. Most of the time you don't modify the outer one. Unfortunately it's confusing and it's easy to make mistakes, and when you do, you get really bizarre error messages. It's a work in progress.

    2. With a project you've created recently, if it has this its build.gradle file:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
    

    you shouldn't need to do the right-click "Add as library" nor should you have to otherwise modify your build file. Just drag the library into the libs folder IN THE FILESYSTEM and it will pick it up; you may have to click the "Sync Project with Gradle Files" button in the toolbar until the feature is implemented to detect that change automatically.

    I just tried dragging a jar file from the filesystem into the libs folder of the IDE window and got that "Refactoring cannot be performed" message. This looks like a bug, and I've filed https://code.google.com/p/android/issues/detail?id=70784 for it. But if you do the copy in the OS instead of the IDE, it will pick it up, except perhaps needing to sync with Gradle files as I said.

    5. The Project Structure dialog is essentially a wizard for modifying your build.gradle files. Those build files are the source of truth, and the Project Structure UI reads from and writes to them. If the UI does what you want, then use it in good health and don't modify the build files by hand.

    Having said that, if you want to add dependencies by modifying the build files:

    4. To add Picasso, you could modify your dependencies block in the module-level build.gradle file to look like this:

    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.squareup.picasso:picasso:2.3.1'
        // Maybe there's other stuff in here too
    }
    

    3.Those instructions are for Maven, which is a completely different build system. It's possible to use it in Android Studio, but you'll find the IDE fights you a lot since it's not set up to do that.

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