[英]Android Studio: JavaSE app
我正在研究一個應在Android以及桌面(JavaSE)上運行的項目。 為此,我將其分為3個Java模塊:
為什么使用Android Studio(AS)很明顯。 除了JavaSE模塊的“運行”按鈕外,幾乎所有事情都可以工作。 問題很簡單:類路徑不完整。 該模塊會編譯,但不會運行。 當我將其構建到JAR中時,它運行良好。 JAR包含所有必需的依賴項。 唯一的問題是在JavaSE模塊上使用時該死的“運行”按鈕。
頂級build.gradle :(幾乎由AS生成)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
CommonCore build.gradle:
apply plugin: 'java-library'
targetCompatibility="1.8"
sourceCompatibility="1.8"
dependencies {
api 'org.nanohttpd:nanohttpd:2.3.1'
api 'com.google.code.gson:gson:2.8.1'
api 'org.apache.commons:commons-text:1.1'
api files('src/main/resources')
}
DesktopApp build.gradle:
apply plugin: 'java'
targetCompatibility="1.8"
sourceCompatibility="1.8"
dependencies {
implementation project(':CommonCore')
implementation 'org.xerial:sqlite-jdbc:3.21.0.1'
}
jar {
manifest {
attributes 'Main-Class': 'myproject.Main'
}
from {
configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
AndroidApp build.gradle :(跳過了實際的android設置以使其簡短)
apply plugin: 'com.android.application'
dependencies {
implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation project(':CommonCore')
}
android { ... }
當我運行它時,我得到:
Exception in thread "main" java.lang.NoClassDefFoundError: fi/iki/elonen/NanoHTTPD
顯然,JVM從CommonCore中找不到依賴項“ org.nanohttpd:nanohttpd:2.3.1”。 檢查類路徑后,我看到:
/Applications/Android Studio.app/Contents/jre/jdk/<a ton of unused JARs>
/Users/bamboo/AndroidStudioProjects/MyProject/DesktopApp/build/classes/java/main
/Users/bamboo/AndroidStudioProjects/MyProject/CommonCore/build/classes/java/main
Gradle管理的依賴項缺少條目。
我做錯了什么? AS必須看到它們,因為自動完成效果很好。
將依賴項放入gradle中的runtime()
內,例如:
dependencies {
runtime(
[group: 'org.nanohttpd', name: 'nanohttpd', version: '2.3.1'],
)
}
解決該問題的一種方法是將NetBeans與Gradle插件一起用於JavaSE開發。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.