Android Studio (Gradle) 找不到 Mockito
Android Studio (Gradle) cannot find Mockito
我尝试在 Android Studio 1.2.2 中使用 Mockito,但出现以下错误:
Error:(50, 17) Failed to resolve: org.mockito:mockito-core:1.10.19
手动添加依赖后同步Gradle时出现错误。这是我的模块 Gradle 文件中的依赖项:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
testCompile 'org.mockito:mockito-core:1.10.19'
}
谁能帮我解决这个问题?
相关问题:
- 我需要先手动下载 Mockito 吗?
- 如果可以,我应该把它放在哪里?
注:评论对解决上述问题很有帮助。然而,这让我陷入了另一个我无法解决的问题。但更新到 Android Studio 1.3 解决了它。我现在是 运行 来自 Android Studio 的 Mockito。
尝试将 testCompile
替换为 androidTestCompile
,它在导入 Mockito 库时对我有用。
但是,如果您仅包含 mockito-core,则可能 运行 出现运行时错误。您需要添加到 gradle:
androidTestCompile "org.mockito:mockito-core:1.10.19"
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
如果您在使用 dexcache 时遇到错误,请将此行放入您的 setUp()
(假设您使用的是 InstrumentalTestCase
)
System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
我遇到了类似的问题,手动添加 mockito jar 文件为我解决了问题。
为此,首先在您的应用程序目录中创建一个名为 "libs" 的目录。请注意,此目录应与 src/main 和构建目录处于同一级别。
接下来,下载 mockito jar 文件并将其粘贴到 libs 目录中。
将其包含在应用级别 build.gradle 文件中的依赖项中:
dependencies {
compile files('libs/add-your-jar-file-name-here')
}
同步 gradle 就可以了。
请参阅 this 答案以获取带有快照的更详细答案。
确保测试文件位于 $your_module/src/test/java
或 $your_module/src/androidTest/java
目录下。
androidTestCompile 现已替换为 androidTestImplementation
dependencies {
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
}
我尝试在 Android Studio 1.2.2 中使用 Mockito,但出现以下错误:
Error:(50, 17) Failed to resolve: org.mockito:mockito-core:1.10.19
手动添加依赖后同步Gradle时出现错误。这是我的模块 Gradle 文件中的依赖项:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.0'
testCompile 'org.mockito:mockito-core:1.10.19'
}
谁能帮我解决这个问题?
相关问题:
- 我需要先手动下载 Mockito 吗?
- 如果可以,我应该把它放在哪里?
注:评论对解决上述问题很有帮助。然而,这让我陷入了另一个我无法解决的问题。但更新到 Android Studio 1.3 解决了它。我现在是 运行 来自 Android Studio 的 Mockito。
尝试将 testCompile
替换为 androidTestCompile
,它在导入 Mockito 库时对我有用。
但是,如果您仅包含 mockito-core,则可能 运行 出现运行时错误。您需要添加到 gradle:
androidTestCompile "org.mockito:mockito-core:1.10.19"
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"
如果您在使用 dexcache 时遇到错误,请将此行放入您的 setUp()
(假设您使用的是 InstrumentalTestCase
)
System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
我遇到了类似的问题,手动添加 mockito jar 文件为我解决了问题。
为此,首先在您的应用程序目录中创建一个名为 "libs" 的目录。请注意,此目录应与 src/main 和构建目录处于同一级别。 接下来,下载 mockito jar 文件并将其粘贴到 libs 目录中。
将其包含在应用级别 build.gradle 文件中的依赖项中:
dependencies {
compile files('libs/add-your-jar-file-name-here')
}
同步 gradle 就可以了。
请参阅 this 答案以获取带有快照的更详细答案。
确保测试文件位于 $your_module/src/test/java
或 $your_module/src/androidTest/java
目录下。
androidTestCompile 现已替换为 androidTestImplementation
dependencies {
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
}