有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在离线模式中使用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.安卓.tools.build:gradle:2.2.2.
 Required by:
     :demo:unspecified
  > No cached version of com.安卓.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) 个答案

  1. # 1 楼答案

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