标签: gradle-kotlin-dsl

将sourceDir添加到Java sourceSet

根据Gradle Kotlin插件页面

Kotlin源可以与同一文件夹或不同文件夹中的Java源混合使用。默认约定是使用其他文件夹。如果不使用默认约定,则应更新相应的sourceSets属性。

我想在src / main / kotlin中添加一些Java文件,并让compileJava任务对其进行编译。我试图提出以下程序块来实现它,但是没有骰子。

java {
    val kotlinSrcDir: File = File(projectDir, "src/main/kotlin")
    sourceSets["main"].java.srcDirs.add(kotlinSrcDir)
    val javasrcdirs: Set<File> = sourceSets["main"].java.srcDirs
    println(javasrcdirs)
}
Run Code Online (Sandbox Code Playgroud)

是什么赋予了?

gradle-kotlin-dsl

1
推荐指数
1
解决办法
626
查看次数

在 Gradle Kotlin DSL 中使用 S3 Maven 存储库的 IAM 凭证

我已经按照此处的说明配置了 S3 支持的 Maven 存储库,例如:

repositories {
    maven {
        url "s3://myCompanyBucket/maven2"
        authentication {
           awsIm(AwsImAuthentication) // load from EC2 role or env var
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试将我的脚本转换为使用 Kotlin DSL 而不是 groovy,但无法找出等效的代码,特别是该authentication部分的代码。

上面的 Groovy 代码片段的等效 Kotlin DSL 是什么?

gradle gradle-kotlin-dsl

1
推荐指数
1
解决办法
1962
查看次数

在 Kotlin DSL 中为 gradle 定义维度

我正在将我的转换build.gradle为 Kotlin DSL。我在应用程序中有 2 个构建风格,但我不知道如何设置这些风格的维度:

flavorDimensions("type")

productFlavors {
    create("free") {
        buildConfigField("boolean", "IS_DONATE", false.toString())
        dimension = "type"
    }
    create("donate") {
        buildConfigField("boolean", "IS_DONATE", true.toString())
        dimension = "type"
    }
}
Run Code Online (Sandbox Code Playgroud)

dimension = "type"部分是失败; 你如何设置每种口味的维度?

android-gradle-plugin gradle-kotlin-dsl

1
推荐指数
1
解决办法
1481
查看次数

不生成 BuildConfig - Gradle Kotlin DSL

要禁用BuildConfigGroovy DSL 生成,可以执行以下操作:

afterEvaluate {
    generateReleaseBuildConfig.enabled = false
    generateDebugBuildConfig.enabled = false
}
Run Code Online (Sandbox Code Playgroud)

我试图在使用 Gradle 的 Kotlin DSL (build.gradle.kts) 时找到等效项。有人有这样的运气吗?

android android-gradle-plugin android-buildconfig gradle-kotlin-dsl

1
推荐指数
1
解决办法
3625
查看次数

如何从 Kotlin 使用 Apollo Android,但没有 Android?

我想使用 Apollo Android 来查询 GitHub GraphQL api,使用 Kotlin 和 Gradle Kotlin DSL,但不在Android 上。

错误信息

我确实遇到了一个特定的问题,但这可能是因为我在其他地方的设置有问题。

总而言之,我有一个ViewLogin.graphql文件,从中ViewLoginQuery生成了 a,但是当我尝试使用ViewLoginQuery.builder()then时unresolved reference: builder出现了问题。

设置

正如https://github.com/apollographql/apollo-android/issues/1573中提到的,应该可以在没有 Android 的情况下使用 Apollo Android。

环境

我正在使用 Arch Linux 和 IntelliJ,并安装了 JS GraphQL IntelliJ 插件。

Gradle Kotlin DSL 构建文件

在编写构建文件时,我已经遇到了一个问题:我无法避免使用已弃用的 buildscript块。

https://plugins.gradle.org/plugin/com.apollographql.apollo中显示的插件显然是https://github.com/apollographql/apollo-android/issues/1573#issuecomment-575613238中提到的孵化插件尚未准备好。

以下构建文件似乎正确应用了该插件:

buildscript {
    repositories {
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
        jcenter()
    }
    dependencies {
        classpath("com.apollographql.apollo:apollo-gradle-plugin:1.2.2")
    }
}

apply(plugin = "com.apollographql.android") …
Run Code Online (Sandbox Code Playgroud)

kotlin apollo-android gradle-kotlin-dsl

1
推荐指数
1
解决办法
5465
查看次数

Gradle 指南,构建 Java Web 应用程序 - Gradle 构建抛出 java.lang.IllegalStateException

我正在尝试通过“构建 Java Web 应用程序”Gradle 指南 (kotlin dsl),但 gradle 在构建时抛出异常。我真的很感激任何帮助使本指南工作。我正在使用 gradle 6.2.2。我已经克隆了 gradle / guides 并尝试在那里构建具有相同结果的示例。

settings.gradle.kts

rootProject.name = "webdemo"
Run Code Online (Sandbox Code Playgroud)

build.gradle.kts

plugins {
    war
    id("org.gretty") version "2.2.0"
}

repositories {
    jcenter()
}

gretty {
    integrationTestTask = "test"
}

dependencies {
    providedCompile("javax.servlet:javax.servlet-api:3.1.0")
    testCompile("junit:junit:4.12")
    testCompile("org.mockito:mockito-core:2.7.19")
    testCompile("io.github.bonigarcia:webdrivermanager:1.6.1")
    testCompile("org.seleniumhq.selenium:selenium-java:3.3.1")
}
Run Code Online (Sandbox Code Playgroud)

例外

% ./gradlew build                                                                                                                                                                                                                          (git)-[master] 
Exception in thread "Thread-447" java.lang.IllegalStateException: The configuration :grettyNoSpringBoot was resolved from a thread not managed by Gradle.
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.resolveToStateOrLater(DefaultConfiguration.java:565)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.access$1900(DefaultConfiguration.java:141)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.getSelectedArtifacts(DefaultConfiguration.java:1246)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.visitContents(DefaultConfiguration.java:1237)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.getFiles(DefaultConfiguration.java:1226)
        at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.getFiles(DefaultConfiguration.java:484)
        at …
Run Code Online (Sandbox Code Playgroud)

java gradle gretty gradle-kotlin-dsl

1
推荐指数
1
解决办法
1766
查看次数

如何将私有签名信息转换为 Kotlin DSL?

我正在迁移到 Kotlin DSL。我已经关注了大多数流行的博客来进行最初的设置。现在是时候重构build.gradle文件了。我有私人签名信息逻辑,如下所示。

if (project.hasProperty('propertyfile') && project.hasProperty('key.store')) {

  def keystorePropertiesFile = rootProject.file(project.getProperty('propertyfile'))
  def keystoreProperties = new Properties()
  keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

  signingConfigs {
    release {
        keyAlias keystoreProperties['key.alias']
        keyPassword keystoreProperties['key.alias.password']
        storeFile file(project.getProperty('key.store'))
        storePassword keystoreProperties['key.store.password']
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我不确定如何将这部分转换为与 Kotlin DSL 等效的部分。浏览了有关该主题的相关帖子和博客,但找不到任何相关内容。有人可以帮我分享一些关于如何做到这一点的想法吗?

android build.gradle android-gradle-plugin gradle-kotlin-dsl

1
推荐指数
1
解决办法
5388
查看次数

gradle kotlin DSL 扩展自

我该如何重新表述:

testCompile.extendsFrom compileOnly
Run Code Online (Sandbox Code Playgroud)

Gradle Groovy DSL 与其基于 Kotlin 的等效项?

configurations {
        testCompile{
            extendsFrom(compileOnly)
        }
    }
Run Code Online (Sandbox Code Playgroud)

我上面的方法失败了。

build gradle kotlin gradle-kotlin-dsl kotlin-dsl

1
推荐指数
1
解决办法
2595
查看次数

Gradle Kotlin DSL - 当父级中定义 java.sourceCompatibility 时构建脚本失败,但添加到子模块时构建成功

我正在尝试使用 gradle (用于构建脚本的 Kotlin DSL)为多模块 springBoot 应用程序设置一个新的存储库

作为同一部分,我试图声明所有子项目所需的通用配置和依赖项。这样做时,我尝试为父文件块sourceCompatility中的所有子项目定义subprojectsbuild.gradle.kts

当我尝试使用上述配置编译我的项目时,构建失败并出现以下异常

* What went wrong:
Extension with name 'java' does not exist. Currently registered extension names: [ext]
Run Code Online (Sandbox Code Playgroud)

但是,如果我将该行移至java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11子模块的build.gradle.kts文件,那么它就会成功编译,并且应用程序将按预期启动。

我不明白我在这里缺少什么。请帮助我理解这一点。

家长build.gradle.kts

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("java")
    id("idea")
    id("war")
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}

subprojects {
    group = "com.company.example"
    version = "0.0.1"

    java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

    repositories {
        mavenCentral()
        maven { url = uri("http://nexus.pentaho.org/content/groups/omni/") }
    }

    apply() {
        plugin("java")
        plugin("idea") …
Run Code Online (Sandbox Code Playgroud)

java gradle kotlin build.gradle gradle-kotlin-dsl

1
推荐指数
1
解决办法
6712
查看次数

如何在 Kotlin Gradle dsl 中使用 Firebase BoM?

这看起来如何 kotlin DSL:

dependencies {
  // Import the BoM for the Firebase platform
  implementation platform('com.google.firebase:firebase-bom:26.2.0')
}
Run Code Online (Sandbox Code Playgroud)

以上是Firebase 文档中当前推荐的

android firebase gradle-kotlin-dsl

1
推荐指数
1
解决办法
280
查看次数

构建用于开发用 Kotlin 编写的 Gradle 插件的配置

我想构建一个用 Kotlin 编写的 Gradle 插件并将其发布到 Gradle 插件门户。但是,我不清楚需要什么构建配置来实现这一目标。关于 Gradle 使用的“嵌入式 Kotlin”,有很多令人困惑的信息和奇怪的错误。

\n

插件开发指南没有 \xe2\x80\x99t 任何 Kotlin + 的示例build.gradle.kts(只有一个简单的使用java-gradle-plugin

\n

\xe2\x80\x99s一些关于嵌入式 Kotlin 的技术细节,但是 \xe2\x80\x99s 并没有明确明确的需求,并且比实际的 \xe2\x80\x9chow 到 \xe2\x80\ 更抽象的技术实现x9d 指南。

\n

使用gradle init(v8.0.2) 创建一个手动设置 Kotlin 版本的项目,因此它可能与嵌入的 Kotlin 版本不匹配

\n
// generated by `gradle init` command\n\nplugins {\n    // Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins\n    `java-gradle-plugin`\n\n    // Apply the Kotlin JVM plugin to add support …
Run Code Online (Sandbox Code Playgroud)

gradle kotlin gradle-plugin gradle-kotlin-dsl

1
推荐指数
1
解决办法
759
查看次数

Android 中的模块化

我正在尝试了解 Android 中的模块化是如何工作的。我正在使用 Kotlin DSL。

在每个 build.gradle.kts 文件中,我是否必须添加默认配置,例如 minSdk、目标 SDK 等?换句话说,我是否只能在应用程序模块 build.gradle.kts 文件中设置配置,而在其他模块中(例如功能)是否可以只声明依赖项?

NowInAndroid 应用程序的 build.gradle.kts 文件具有以下代码:

plugins {
    id("nowinandroid.android.feature")
    id("nowinandroid.android.library.compose")
    id("nowinandroid.android.library.jacoco")
}

android {
    namespace = "com.google.samples.apps.nowinandroid.feature.bookmarks"
}

dependencies {
    implementation(libs.androidx.compose.material3.windowSizeClass)
}

Run Code Online (Sandbox Code Playgroud)

它只声明依赖项和插件,但我认为这些插件中有不同的配置。

我看到的另一种方法是使用继承其他 build.gradle 文件

apply {
 (path to build.gradle) file
}
Run Code Online (Sandbox Code Playgroud)

但使用该方法时,我无法声明名称空间,因此我必须将其添加到 build.gradle 文件中,该文件在不同功能之间共享,如果没有它,我将无法访问给定功能模块中的资源。

android gradle android-gradle-plugin gradle-kotlin-dsl

0
推荐指数
1
解决办法
930
查看次数

gradle-kotlin-dsl 中的 Android productFlavors

我已经在一个 android 项目中试验了 gradle-kotlin-dsl。我设法让它工作,但我被困在如何定义productFlavors

android {
    compileSdkVersion(Config.Android.compileSdkVersion)
    buildToolsVersion(Config.Android.buildToolsVersion)

    defaultConfig {
        minSdkVersion(Config.Android.minSdkVersion)
        targetSdkVersion(Config.Android.targetSdkVersion)
        versionCode = Config.Version.code
        versionName = Config.Version.name
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }

    flavorDimensions("dimension")

    productFlavors {
        //product flavors here
    }
}
Run Code Online (Sandbox Code Playgroud)

经过一番调查,我得到了解决方案,只需使用以下create方法:

productFlavors {
    create("flavor1") {
        //flavor configurations here
    }
    create("flavor2") {
        //flavor configurations here
    }
}
Run Code Online (Sandbox Code Playgroud)

android gradle kotlin gradle-kotlin-dsl

-1
推荐指数
1
解决办法
1586
查看次数