【问题标题】:How to add dependency to Android Studio manually如何手动将依赖项添加到 Android Studio
【发布时间】:2016-10-09 20:43:32
【问题描述】:

我尝试了几次向我的项目添加依赖项,每次都报错 我要添加它们的依赖项是 'de.hdodenhof:circleimageview:1.3.0' 和 'com.github.bumptech.glide:glide:3.6.1' 所以我想下载这些并手动添加到我的项目中是否可能?如果是,如何?

这是我的应用程序的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '24.0.0 rc4'

    defaultConfig {
        applicationId "ir.esfandune.material"
        minSdkVersion 23
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    packagingOptions {
        exclude 'classes.dex'
    }
}

repositories {
    jcenter()
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

}

dependencies {
    compile 'com.android.support:design:23.0.0'
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:cardview-v7:23.0.0'
    compile 'com.android.support:recyclerview-v7:23.0.0'
    compile 'com.android.support:support-v4:23.0.0'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.github.rey5137:material:1.2.1.6-SNAPSHOT'
    compile project(':material-dialogs')
    compile files('lib/de.hdodenhof/circleimageview/1.3.0/jars/classes.jar')
    //*** added from orginal source
    compile 'de.hdodenhof:circleimageview:1.3.0'
    compile 'com.github.bumptech.glide:glide:3.6.1'
}

【问题讨论】:

  • 从 github 克隆项目并将项目作为模块添加到您的项目中
  • 为什么不工作?手动添加库并不是一个好主意。看来您已经“手动”添加了 circleimage compile files('lib/de.hdodenhof/circleimageview/1.3.0/jars/classes.jar')。它也被添加了两次,这将失败。添加为 jar 或作为外部依赖项,即使用 compile files 删除 compile ' de.hd... 部分,反之亦然。
  • @zapl 我第一次删除了 'de.hd... 并且 gradle 同步 myproject 很好,但在运行时给这个库的 forceclose 错误
  • @afzali 你遇到了什么错误? (edit 您的问题并包括您收到的错误消息 - 在 logcat 窗口中:stackoverflow.com/a/28476685/995891)。更重要的是,当您以预期的方式执行此操作(并删除 compile files 一个)时,您会遇到什么错误?这应该是您优先解决的问题。找不到有很多缺点的解决方法。

标签: java android android-studio github dependency-injection


【解决方案1】:

下载你想要的库的 jar/aar 文件

将文件复制到应用文件夹中的 libs 目录中 对于 *.jar 文件: 将此代码添加到您的 gradle 文件的依赖项中

 compile files('libs/library.jar')

对于 *.aar 文件: 尝试从项目结构/新模块/从 aar/jar 导入

祝你好运

【讨论】:

    【解决方案2】:

    在您的项目面板中展开 GradleScripts 选项卡。你会在那里看到 2 build.gradle 文件。打开第二个 build.gradle(module:app) 文件。在文件末尾,您将看到如下部分:

    dependencies {
        compile 'com.android.support:appcompat-v7:23.0.0'
    }
    

    在此处添加一个新的依赖项,如下所示:

    dependencies {
        compile 'com.android.support:appcompat-v7:23.0.0'
    
        //manually added dependency
        compile 'com.android.support:design:23.0.0'
    }
    

    【讨论】:

      【解决方案3】:

      只需将其复制到依赖项中

       compile 'de.hdodenhof:circleimageview:1.3.0'
      

      然后按“使用 gradle 文件同步项目”按钮

      然后粘贴下面的代码在任何你想要的地方使用它

      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/profile_image"
      android:layout_width="96dp"
      android:layout_height="96dp"
      android:src="@drawable/profile"
      app:civ_border_width="2dp"
      app:civ_border_color="#FF000000"/>
      

      【讨论】:

      • 我的鳕鱼包含这个!
      • 是的。然后你必须同步 gradle 文件。在工作室中有一个按钮可以这样做。
      • abd 删除行 compile files('lib/de.hdodenhof/circleimageview/1.3.0/jars/classes.jar')
      【解决方案4】:

      我遇到了类似的问题,正如@SaravInfern 在 cmets 中指出的那样,我通过将项目作为模块导入来解决它。

      1. 克隆项目。
      2. 文件 -> 新建 -> 导入模块,然后按照步骤操作。
      3. 导入该模块后,让 gradle 下载所有必要的依赖项。
      4. 打开这个新模块的gradle文件,添加apply plugin: 'com.android.library'(之前可能是com.android.library)
      5. 在依赖项块中,添加:compile project(":app") 其中,app 是导入模块的名称。

      然后构建项目。它应该成功构建。更详细的解决方案可以参考this blog和this SO thread。

      【讨论】:

        【解决方案5】:
        Yes you can manually update the dependencies in build.gradle system 
        
        Just copy any of the existing dependency and rename with yours required dependency..
        
        Example:
        
        dependencies {
            compile 'com.android.support:design:23.0.0'// this is default dependency 
            compile 'de.hdodenhof:circleimageview:1.3.0' // this is your dependency..
         }
        
        Issues your get..
        1. android machine warn you to use implements instand of complie..(its a warning ignore it)
        2. de.hdodenhof:circleimageview:1.3.0 change of version...(use latest if your using latest then change to lower version).
        
        .@Sunil, Try this It will work....
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-03-16
          • 2014-10-06
          • 1970-01-01
          • 2019-07-01
          • 2016-11-22
          • 2019-04-11
          • 2017-05-21
          • 2015-05-10
          相关资源
          最近更新 更多