java - 使用离线 gradle 运行标准 java 项目时出现 Android Studio 错误

标签 java android gradle

我用 Android Studio 2.2Gradle在离线模式。 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.1that 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/

相关文章:

java - 从java中的字符串中获取<img>值

java - 卡夫卡和 OpenJDK

android - CKEditor 4.4.7 不显示在任何移动浏览器上

java - 如何下载Android Gradle项目外部依赖到文件夹?

java - Gradle 似乎什么都不做

java - 两个 android 设备之间的 TCP 连接(即使使用 NAT)

java - JQuery 在 tomcat 中不工作

android - 如何将多部分表单数据和图像上传到android中的服务器?

android - 使用 android volley 库解析 JSON 数组中的 JSON 对象

react-native - Firebase - Gradle 构建失败 - com.google.android.gms :play-services-measurement-base