我正在尝试使用 ShadowJar 构建一个 fatjar。我的应用程序和 gradle 代码如下。我使用 Gradle 5.0 进行构建。当我运行 ./gradlew run 时,代码有效。当我运行“gradle Shadowjar”,并在“build/lib”文件夹中使用“java -jar”运行 fatjar 时,出现以下错误。
我猜依赖项没有加载到 fatjar 中?我还使用 Groovy 构建 Gradle 文件,但遇到了同样的错误。
我没有在 fatjar 文件中包含所有依赖项,这是否正确?如果是这种情况,您知道如何修改 Gradle 文件以确保包含此文件吗?
Gradle Kotlin DSL
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
// Apply the Kotlin JVM plugin to add support for Kotlin on the JVM.
id("org.jetbrains.kotlin.jvm").version("1.3.21")
id("com.github.johnrengelman.shadow") version "5.0.0"
// Apply the application plugin to add support for building a CLI application.
application
}
repositories {
// Use jcenter for resolving your dependencies.
// …
Run Code Online (Sandbox Code Playgroud) 我有一个工作构建,包括数据绑定,但是在将我的 Gradle 构建脚本迁移到 Kotlin DSL 后,我现在每次使用时都会遇到未解决的符号错误import androidx.databinding.DataBindingUtil
我的build.properties.kts
包含以下内容:
plugins {
id ("com.android.application")
kotlin ("android")
kotlin ("android.extensions")
id ("de.mannodermaus.android-junit5")
}
android {
lintOptions.isAbortOnError = false
compileSdkVersion(28)
defaultConfig {
// ...
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles (getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
dataBinding.isEnabled = true
sourceSets {
getByName("main").java.srcDir("src/main/kotlin")
getByName("test").java.srcDir("src/test/kotlin")
}
}
dependencies {
// ...
}
Run Code Online (Sandbox Code Playgroud)
启用数据绑定的正确方法是dataBinding.isEnabled = true
,还是我需要做其他事情?(我知道我可以通过返回 Groovy 来“解决”这个问题,但这感觉有点像放弃!)
我正在尝试将我的 gradle 文件切换到 Kotlin DSL。我的项目正在调用 API。
在build.gradle(app)
我有一个函数来检索存储在另一个文件中的 api 密钥keys.properties
。
在出现一些问题(例如)之后,我重写了函数以获取密钥。我在中编写了以下函数build.gradle.kts
:
import import java.io.File
fun readFileLineByLineUsingForEachLine2(fileName: String): HashMap<String, String>{
val items = HashMap<String, String>()
File(fileName).forEachLine {
items[it.split("=")[0]] = it.split("=")[1]
}
return items
}
Run Code Online (Sandbox Code Playgroud)
然后我设置一个变量来保存特定键的值:
buildConfigField(String!, "API_KEY", returnMapOfKeys()["API_KEY"])
Run Code Online (Sandbox Code Playgroud)
修复了一些错误后,我遇到了以下问题:
app/build.gradle.kts:49:36: Expecting ')'
Run Code Online (Sandbox Code Playgroud)
上面带有buildConfigField
.
有人知道这个错误在哪里吗?
或者有人知道如何使用 Kotlin DSL 从文件中检索密钥?
在我的 中build.gradle.kts
,我想编写一个使用外部类的函数:StrSubstitutor
来自 Apach Commons Text。但是,尽管我在运行时可以看到该库,但找不到导入./gradlew dependencies
。
文件build.gradle.kts
内容如下:
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.apache.commons.text.StringSubstitutor // Import not found
plugins {
val kotlinVersion = "1.3.61"
kotlin("jvm") version "$kotlinVersion"
kotlin("kapt") version "$kotlinVersion"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
repositories {
mavenCentral()
}
dependencies {
implementation("org.apache.commons:commons-text:1.8")
// SourceSets
sourceSets.main {
withConvention(KotlinSourceSet::class) {
kotlin.srcDirs("src/main/kotlin")
}
}
sourceSets.test {
withConvention(KotlinSourceSet::class) {
kotlin.srcDirs("src/main/kotlin")
}
}
}
tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
} …
Run Code Online (Sandbox Code Playgroud) 我想选择性地应用一些插件来缩短开发时间的构建时间。
我的意思是我有这个:
app/gradle.build.kts
:
plugins {
id("com.android.application")
id("com.google.firebase.crashlytics")
}
Run Code Online (Sandbox Code Playgroud)
但我不想一直应用 firebase crashlytics 插件(以及其他插件,如性能监视器),但如果我尝试访问块project
的内部,plugins
我会得到:
plugins {
id("com.android.application")
if (!project.hasProperty("build_fast")) {
id("com.google.firebase.crashlytics")
}
}
Run Code Online (Sandbox Code Playgroud)
我得到:
'val Build_gradle.project: Project' can't be called in this context by implicit receiver. Use the explicit one if necessary
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
我可以访问系统环境变量,System.getenv("CI")
但从 android studio 替换这些值目前对我来说有点麻烦,有人可以展示一种更新这些变量的方法吗?
我该如何使用 来做到这一点project.hasPropperty("")
?
迁移到 gradle 7.x 时出现以下错误
* What went wrong:
An exception occurred applying plugin request [id: 'no.nils.wsdl2java', version: '0.12']
> Failed to apply plugin 'no.nils.wsdl2java'.
> Configuration with name 'compile' not found.
Run Code Online (Sandbox Code Playgroud)
下面是在build.gradle中添加的wsdl2java
plugins {
id("no.nils.wsdl2java") version "0.12"
`java-library`
}
buildscript {
dependencies {
classpath("no.nils:wsdl2java:0.12")
}
}
Run Code Online (Sandbox Code Playgroud)
如何解决这个问题?
在常规构建脚本中,
dependencies {
implementation project(':nameless-core')
}
Run Code Online (Sandbox Code Playgroud)
我们可以将同一项目中的另一个模块添加到依赖项中。kotlin dsl 构建脚本可以吗?
我是一名初学者gradle
,希望在我的 Kotlin 项目中使用koin。
但是,我收到以下错误
Execution failed for task ':compileTestKotlin'.
> Error while evaluating property 'filteredArgumentsMap' of task ':compileTestKotlin'
> Could not resolve all files for configuration ':testCompileClasspath'.
> Could not resolve org.jetbrains.kotlin:kotlin-test-junit5:1.6.20.
Required by:
project : > org.jetbrains.kotlin:kotlin-test:1.6.20
> Module 'org.jetbrains.kotlin:kotlin-test-junit5' has been rejected:
Cannot select module with conflict on capability 'org.jetbrains.kotlin:kotlin-test-framework-impl:1.6.20' also provided by [org.jetbrains.kotlin:kotlin-test-junit:1.6.10(junitApi)]
> Could not resolve org.jetbrains.kotlin:kotlin-test-junit:1.6.10.
Required by:
project : > io.insert-koin:koin-test:3.2.0-beta-1 > io.insert-koin:koin-test-jvm:3.2.0-beta-1
> Module 'org.jetbrains.kotlin:kotlin-test-junit' has been rejected:
Cannot …
Run Code Online (Sandbox Code Playgroud) 当我在 Android 插件项目中将 kotlin-gradle-plugin 版本从 1.5.0 升级到 1.7.20 时,出现此异常。
\n\n我使用这段代码build.gradle
compileGroovy.classpath += files(compileKotlin.destinationDir)\n
Run Code Online (Sandbox Code Playgroud)\n怎么解决\xef\xbc\x9f
\n我的 evn 是:
\nandroid android-gradle-plugin gradle-plugin gradle-kotlin-dsl kotlin-gradle-plugin
Groovy 的 Android 文档 ( https://developer.android.com/guide/topics/resources/app-languages )
android {
...
defaultConfig {
resourceConfigurations += ["en", "en-rGB", "fr", "ja", "b+zh+Hans+MO", "b+zh+Hant+MO"]
}
}
Run Code Online (Sandbox Code Playgroud)
迁移到 Kotlin DSL 后如何替换它(https://developer.android.com/build/migrate-to-kotlin-dsl)?