如何在Android Studio中手动添加依赖项

4
我试图多次向我的项目添加依赖项,但每次都会出现错误。我想添加的依赖项是'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'
}

1
从Github克隆项目并将该项目作为模块添加到您的项目中。 - SaravInfern
为什么它不工作?手动添加库通常不是一个好主意。而且看起来你已经手动添加了circleimage "compile files('lib/de.hdodenhof/circleimageview/1.3.0/jars/classes.jar')"。而且它被添加了两次,这将导致失败。要么添加为jar文件,要么添加为外部依赖项,即使用“compile files”,删除“compile 'de.hd...”部分,反之亦然。 - zapl
@zapl 我在第一次中移除了“de.hd...”并且 gradle 同步我的项目没问题,但在运行时会因为这个库出现 forceclose 错误。 - afzali
@afzali,你遇到了什么错误?([编辑]你的问题并包括你得到的错误消息-在logcat窗口:https://dev59.com/tmQn5IYBdhLWcg3wcm57#28476685)。更重要的是,当你按照预期的方式操作(并删除`compile files`),然后你又遇到了什么错误?这应该是你优先解决的问题。而不是寻找有很多缺点的变通方法。 - zapl
5个回答

6

下载你所需的库的jar/aar文件

将文件复制到你的应用程序文件夹中的libs目录下 对于*.jar文件: 在你的gradle文件的依赖项中添加以下代码

 compile files('libs/library.jar')

对于*.aar文件:

尝试从项目结构/新模块/从aar/jar导入

祝好运


3

在您的项目面板中展开GradleScripts选项卡。您将看到两个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'
}

1

只需将此内容复制到依赖项中

 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"/>


我的代码包含这个! - afzali
是的。然后您需要同步Gradle文件。在Studio中有一个按钮可以执行此操作。 - Phillen
删除该行代码:compile files('lib/de.hdodenhof/circleimageview/1.3.0/jars/classes.jar') - Phillen

0

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

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

然后构建该项目。它应该能够成功构建。您可以参考this blogthis SO thread以获取更详细的解决方案。


0
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....

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接