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

时间:2017-03-04 17:59:54

标签: java android gradle

我在离线模式中使用Android Studio 2.2GradleGradle 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.

此处还有Java库build.gradle的内容:

apply plugin: 'java'

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

sourceCompatibility = "1.7"
targetCompatibility = "1.7"

我如何解决这个问题? (我不想使用其他IDE或为Gradle启用在线模式)

1 个答案:

答案 0 :(得分:2)

您必须下载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'