我用 Android Studio 2.2
和 Gradle
在离线模式。 Gradle Home
的值是 /path/to/gradle/gradle-2.14.1
.我可以运行 Android 项目,但现在我想运行一个 Java 标准类来测试一些 Java 代码,然后再在 Android 项目中使用它们。所以我关注了this answer .但是当我运行类时,我收到了这样的错误:
Error:Gradle: A problem occurred configuring root project 'demo'.
> Could not resolve all dependencies for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:2.2.2.
Required by:
:demo:unspecified
> No cached version of com.android.tools.build:gradle:2.2.2 available for offline mode.
这里还有
build.gradle
的内容Java库:apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
我该如何解决这个问题? (我不想使用其他 IDE 或为 Gradle 启用在线模式)
最佳答案
您必须下载com.android.tools.build:gradle:2.2.2
...
这需要互联网。包gradle-2.14.1
和 that is Gradle itself, and not the Android Gradle plugin 不是一回事.
不过,尚不清楚您为什么将该插件应用到标准 Java 模块上。
所有你需要的是
apply plugin: 'java'
换句话说,Gradle 只是构建 Android 代码。除此之外,它与 Android 无关,如果设置正确,您可以独立于 Android 运行 Java 项目。
Gradle - Java Plugin
例如,
java-code/build.gradle
apply plugin: 'java'
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
test {
testLogging {
// Show that tests are run in the command-line output
events 'passed'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
}
app/build.gradle
apply plugin: 'com.android.application'
evaluationDependsOn(':java-code')
...
dependencies {
compile project(":java-code")
compile fileTree(dir: 'libs', include: ['*.jar'])
...
}
settings.gradle
include ':app', ':java-code'
关于java - 使用离线 gradle 运行标准 java 项目时出现 Android Studio 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599361/