gpt4 book ai didi

安卓工作室 : JavaExec: Could not find or load main class

转载 作者:行者123 更新时间:2023-11-29 23:54:18 29 4
gpt4 key购买 nike

作为构建脚本的一部分,我想在打包 APK 之前运行一些 Java 代码。

当我尝试构建我的项目时,出现错误:

Error: Could not find or load main class net.bgreco.myapp.build.BuildScript

BuildScript.java 位于 app/src/main/java/net/bgreco/myapp/build/

在我收到上述错误消息之前,BuildScript.class 确实已成功编译,并且我可以从命令行运行它而不会出现错误。

app/build.gradle :

task runBuildScript(type: JavaExec) {
main = 'net.bgreco.myapp.build.BuildScript'
}

tasks.whenTaskAdded { theTask ->
if (theTask.name.contains('package')) {
theTask.dependsOn 'runBuildScript'
}
}

最佳答案

this answer 的帮助下,我通过如下修改类路径使其工作。我还需要引用一些 Android 库,所以我包含了 android.jar 的路径。

app/build.gradle :

task runBuildScript(type: JavaExec) {
String flavor, buildType
(flavor, buildType) = getCurrentFlavorAndBuildType()
classpath files("build/intermediates/classes/" + flavor + "/" + buildType, "${android.getSdkDirectory().getAbsolutePath()}\\platforms\\${android.compileSdkVersion}\\android.jar")
main = 'net.bgreco.myapp.build.BuildScript'
args = [flavor] // Not needed unless you want to know the current flavor from within the BuildScript
}

tasks.whenTaskAdded { theTask ->
if (theTask.name.contains('package')) {
theTask.dependsOn 'runBuildScript'
}
}

def getCurrentFlavorAndBuildType() {
Gradle gradle = getGradle()
String tskReqStr = gradle.getStartParameter().getTaskRequests().toString()

Pattern pattern;

if (tskReqStr.contains("assemble"))
pattern = Pattern.compile("assemble(\\w+)(Release|Debug)")
else
pattern = Pattern.compile("generate(\\w+)(Release|Debug)")

Matcher matcher = pattern.matcher(tskReqStr)

if (matcher.find())
return [matcher.group(1).toLowerCase(), matcher.group(2).toLowerCase()]
else {
println "NO MATCH FOUND"
return ["", ""];
}
}

关于安卓工作室 : JavaExec: Could not find or load main class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50536660/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com