2017-03-04 62 views
-1

我使用Android Studio 2.2Gradle離線模式Gradle Home的值是/path/to/gradle/gradle-2.14.1。我可以運行Android項目,但現在我想在運行Android項目之前運行Java標準類來測試一些Java代碼。所以我跟着this answer。但是,當我運行類,我得到了這樣的錯誤:Android Studio在運行帶離線gradle的標準java項目時出錯

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或啓用在線模式的搖籃)

+0

@ cricket_007權 – njzk2

回答

2

您必須下載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' 

換句話說,搖籃只需構建Android的代碼。除此之外,它與Android無關,如果您正確設置了Java,則可以獨立於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' 
+0

是的,這是驚人的我。但爲什麼Android Studio可以運行Android項目? – hasanghaforian

+0

你剛纔問爲什麼IDE製作Android項目可以運行它們嗎? –

+0

謝謝你的回覆。現在我有兩個問題:1-我必須添加'apply plugin:'java''? 2-爲什麼Android工作室想要使用不同版本的'Gradle'來運行Android項目和'Java'? – hasanghaforian

相關問題