我想在Gradle Kotlin DSL中使用内置的JUnit 5,因为在构建过程中我得到了这个警告:
WARNING: The junit-platform-gradle-plugin is deprecated and will be discontinued in JUnit Platform 1.3.
Please use Gradle's native support for running tests on the JUnit Platform (requires Gradle 4.6 or higher):
https://junit.org/junit5/docs/current/user-guide/#running-tests-build-gradle
Run Code Online (Sandbox Code Playgroud)
这个链接告诉我放
test {
useJUnitPlatform()
}
Run Code Online (Sandbox Code Playgroud)
在我的build.gradle,但是语法是build.gradle.kts什么?
我当前的构建文件是
import org.gradle.api.plugins.ExtensionAware
import org.junit.platform.gradle.plugin.FiltersExtension
import org.junit.platform.gradle.plugin.EnginesExtension
import org.junit.platform.gradle.plugin.JUnitPlatformExtension
group = "com.example"
version = "0.0"
// JUnit 5
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath("org.junit.platform:junit-platform-gradle-plugin:1.2.0")
}
}
apply {
plugin("org.junit.platform.gradle.plugin")
}
// …Run Code Online (Sandbox Code Playgroud) 我在尝试对我的 gradle 文件使用 Kotlin DSL 时遇到错误。
在build.gradle(app)我有一个函数来检索存储在文件中的 api 密钥中keys.properties,Groovy 中的函数如下:
// Retrieve key api
def getApiKey() {
def keysFile = file("keys.properties")
def keysProperties = new Properties()
keysProperties.load(new FileInputStream(keysFile))
def apiKey = keysProperties['API_KEY']
return apiKey
}
Run Code Online (Sandbox Code Playgroud)
当切换到 Kotlin DSL 时,我天真地将函数更改如下:
// Retrieve key for TMDB api
fun getApiKey() {
val keysFile = file("keys.properties")
val keysProperties = Properties()
keysProperties.load(FileInputStream(keysFile))
val apiKey = keysProperties["API_KEY"]
return apiKey
}
Run Code Online (Sandbox Code Playgroud)
然后构建返回以下错误:
.../app/build.gradle.kts:13:26: Unresolved reference: Properties
Run Code Online (Sandbox Code Playgroud)
有谁知道如何解决这个问题?
按照 #bam bam 的建议,添加导入import …
我想将此文件转换为旧的 build.gradle 文件,我正在这样做,但有很多错误。搜索了太多谷歌链接,但什么也没发生。我想将此文件转换为旧的 build.gradle 文件,我正在这样做,但有很多错误。搜索了太多谷歌链接,但什么也没发生。
import com.android.build.gradle.api.ApplicationVariant
plugins {
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
}
android {
compileSdkVersion(29)
defaultConfig {
minSdkVersion(14)
targetSdkVersion(29)
versionCode = 1
versionName = "0.1.1"
externalNativeBuild {
cmake {
}
}
}
externalNativeBuild {
cmake {
setPath(File("${projectDir}/src/main/cpp/CMakeLists.txt"))
}
}
sourceSets {
getByName("main") {
assets.srcDirs("src/main/assets", "build/ovpnassets")
}
getByName("debug") {
}
getByName("release") {
}
}
lintOptions {
enable("BackButton", "EasterEgg")
warning("ImpliedQuantity", "MissingQuantity")
disable("MissingTranslation", "UnsafeNativeCodeLocation")
}
buildTypes {
getByName("release") {
if (project.hasProperty("icsopenvpnDebugSign")) {
logger.warn("property icsopenvpnDebugSign set, using debug signing for release")
signingConfig = …Run Code Online (Sandbox Code Playgroud) 我的 Firebase 分发配置有问题。build.gradle这是我的Kotlin DSL的一部分
flavorDimensions("dim")
productFlavors {
create("fl1") {
applicationIdSuffix = ".fl1"
setDimension("dim")
firebaseAppDistribution {
releaseNotes = "$name"
groups = "group-fl1"
}
}
create("fl2") {
applicationIdSuffix = ".fl2"
setDimension("dim")
firebaseAppDistribution {
releaseNotes = "$name"
groups = "group-fl2"
}
}
}
Run Code Online (Sandbox Code Playgroud)
风味 1 和风味 2 上传到 2 个不同的 Firebase 项目 - 因此我有两个google-services.json文件:src/fl1和src/fl2.
根据观察,Firebase App Distribution 插件始终使用第二个块中的配置firebaseDistribution。看起来这不是设置为风味而是全局设置。例如,当我调用assembleFl1Debug appDistributionUploadFl1Debug正确的 .apk 时,正确的 Firebase 项目会出现,但发行说明和组都不正确。有人遇到过类似的问题吗?
当前的build.gradle.kts:
android {
// ...
variantFilter {
ignore = run {
val isSim = flavors[0].name == "sim"
val isNet = !isSim
val isU = flavors[1].name == "custom"
val isMain = !isU
val buildType = buildType.name
val isDebug = buildType == "debug"
val isRelease = buildType == "release"
(isSim && isRelease) ||
(isSim && isU)
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何在新版本的插件中创建类似的配置?
更新:使用答案,上面的代码如下:
android {
androidComponents.beforeVariants {
it.enabled = run {
val isSim = it.productFlavors[0].second == "sim"
val isNet = !isSim
val isU = …Run Code Online (Sandbox Code Playgroud) 在原生 android 项目中,我们可以定义 BuildConfig 变量,这些变量可以根据所选的构建类型(调试或发布)进行更改。为此,我们可以在应用程序级 gradle 文件中添加以下代码
buildTypes {
release {
buildConfigField 'String', "BASE_URL", '"https://stackoverflow.com/"'
}
debug {
buildConfigField 'String', "BASE_URL", '"https://qa.stackoverflow.com/"'
}
}
Run Code Online (Sandbox Code Playgroud)
我期待创建这样的全局配置变量,可以从共享模块以及 Android 和 iOS 模块(如果可能的话)访问。我怎样才能做到这一点?
我在 gradle 中使用目录版本时遇到问题,我正在尝试将其应用到我的项目中。我从这里举了一个例子https://github.com/android/nowinandroid但是当我收到如下错误时:
Extension of type 'LibraryExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, LibrariesForLibs, VersionCatalogsExtension, BasePluginExtension, DefaultArtifactPublicationSet, SourceSetContainer, ReportingExtension, JavaPluginExtension, JavaToolchainService, NamedDomainObjectContainer<BaseVariantOutput>, BaseAppModuleExtension, ApplicationAndroidComponentsExtension, KotlinAndroidProjectExtension, KotlinTestsRegistry]
at org.gradle.internal.extensibility.ExtensionsStorage.getHolderByType(ExtensionsStorage.java:88)
at org.gradle.internal.extensibility.ExtensionsStorage.configureExtension(ExtensionsStorage.java:70)
at org.gradle.internal.extensibility.DefaultConvention.configure(DefaultConvention.java:189)
at AndroidLibraryConventionPlugin.apply(AndroidLibraryConventionPlugin.kt:30)
at AndroidLibraryConventionPlugin.apply(AndroidLibraryConventionPlugin.kt:10)Run Code Online (Sandbox Code Playgroud)
我试图实现的代码是这样的:
internal fun Project.configureKotlinAndroid(
commonExtension: CommonExtension<*, *, *, *>,
) {
commonExtension.apply {
compileSdk = 32
defaultConfig {
minSdk = 21
}
compileOptions {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
} …Run Code Online (Sandbox Code Playgroud)我有一个简单的 gRPC 客户端,由 Gradle 使用 ShadowJar 打包在 jar 中。当我使用main()IntelliJ 运行时,RPC 发送成功。当我使用 运行它时java -jar,出现异常:
更新:我想我已经确定 IntelliJ 和 ./gradlew ShadowJar 提供了同一组类,但顺序不同。我怀疑这会导致不同的行为,但我真的不明白为什么,或者在任何一种情况下如何控制类路径的顺序。如果我能弄清楚哪些类实际上与这里相关,那将会有很大的帮助。完整扩展包括 19k 类文件。
Exception in thread "main" io.grpc.StatusException: UNKNOWN
at io.grpc.Status.asException(Status.java:550)
at io.grpc.kotlin.ClientCalls$rpcImpl$1$1$1.onClose(ClientCalls.kt:296)
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:562)
at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:70)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:743)
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:722)
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37)
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.nio.channels.UnsupportedAddressTypeException
at java.base/sun.nio.ch.Net.checkAddress(Net.java:146)
at java.base/sun.nio.ch.Net.checkAddress(Net.java:157)
at java.base/sun.nio.ch.SocketChannelImpl.checkRemote(SocketChannelImpl.java:816)
at java.base/sun.nio.ch.SocketChannelImpl.connect(SocketChannelImpl.java:839)
at io.netty.util.internal.SocketUtils$3.run(SocketUtils.java:91)
at io.netty.util.internal.SocketUtils$3.run(SocketUtils.java:88)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:569)
at io.netty.util.internal.SocketUtils.connect(SocketUtils.java:88)
at io.netty.channel.socket.nio.NioSocketChannel.doConnect(NioSocketChannel.java:322)
at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.connect(AbstractNioChannel.java:248)
at io.netty.channel.DefaultChannelPipeline$HeadContext.connect(DefaultChannelPipeline.java:1342)
at io.netty.channel.AbstractChannelHandlerContext.invokeConnect(AbstractChannelHandlerContext.java:548) …Run Code Online (Sandbox Code Playgroud) 我正在使用 Android Studio Giraffe。新的推荐方法是使用 Koltin 构建而不是 groovy。因此,当我创建新项目时,当我打开突出显示的库时,模块和项目级别的 build.gradle.kts 文件都以红色突出显示。有一个名为 libs.version.toml 的单独版本目录文件
红色高亮
库.版本.toml
[versions]
com-android-application = "8.1.0-alpha11"
org-jetbrains-kotlin-android = "1.8.10"
core-ktx = "1.9.0"
junit = "4.13.2"
androidx-test-ext-junit = "1.1.5"
espresso-core = "3.5.1"
lifecycle-runtime-ktx = "2.6.1"
activity-compose = "1.7.0"
compose-bom = "2023.03.00"
[libraries]
core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "core-ktx" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-test-ext-junit = { group = "androidx.test.ext", name = "junit", version.ref = "androidx-test-ext-junit" …Run Code Online (Sandbox Code Playgroud) 示例项目(使用 kotlin DSL 在 Gradle 8.1.1 中编写):
https://github.com/tribbloid/scaffold-gradle-kts/tree/16bf3acffca7b83a7e64dbb248a9512caba87e71
该项目有 3 个以上子模块:
include(":lightweight-dependency:core")
include(":lightweight-dependency:extra") // depends on :lightweight-dependency:core
include(":core") // depends on lightweight-dependency:extra
...
Run Code Online (Sandbox Code Playgroud)
编译的时候,出现了如下错误:
FAILURE: Build failed with an exception.
* What went wrong:
Circular dependency between the following tasks:
:core:compileJava
+--- :core:compileJava (*)
+--- :core:compileKotlin
| +--- :core:compileJava (*)
| +--- :core:compileKotlin (*)
| \--- :core:compileScala
| +--- :core:compileJava (*)
| +--- :core:compileKotlin (*)
| \--- :core:compileScala (*)
\--- :core:compileScala (*)
(*) - details omitted (listed previously)
Run Code Online (Sandbox Code Playgroud)
这是有问题的,因为:lightweight-dependency:core …