什么是.kotlin_builtins文件,我可以从我的uberjars中忽略它们吗? [英] What are .kotlin_builtins files and can I omit them from my uberjars?

查看:1788
本文介绍了什么是.kotlin_builtins文件,我可以从我的uberjars中忽略它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为在Kotlin编写的应用程序集成proguard和gradle build。我发现proguard正在剥离Kotlin标准库(因为它应该在我简单的Hello World程序中),但它会在我的jar文件中留下一堆文件,其文件扩展名为 .kotlin_builtins 。当我配置我的gradle任务来排除这些文件时,该程序仍然可以正常工作。什么是这些文件,它们必须随我的可执行文件uberjar一起提供?



这是我的 build.gradle 文件的内容以供参考:

  buildscript {
ext.kotlin_version ='1.0.5'
ext.shadow_version ='1.2 .4'

仓库{
mavenCentral()
maven {
urlhttps://plugins.gradle.org/m2/
}
flatDir dirs:gradle / proguard
}

依赖项{
classpathorg.jetbrains.kotlin:kotlin-gradle-plugin:$ kotlin_version
classpathcom.github.jengelman.gradle.plugins:shadow:$ shadow_version
classpath:proguard:

}

apply plugin: 'kotlin'
apply plugin:'application'
apply plugin:'com.github.johnrengelman.shadow'

mainClassName ='namespace.MainKt'

defaultTasks'run'

存储库{
mavenCentral()
}

依赖关系{
compileorg.jetbrains.kotlin:kotlin-stdlib:$ kotlin_version
testCompilejunit:junit:4.12
testCompileorg.jetbrains.kotlin:kotlin-test-junit:$ kotlin_version
}

shadowJar {
排除'kotlin / ** / *。kotlin_builtins'
排除'.keep'
}

任务minify(类型:proguard.gradle.ProGuardTask,dependsOn:'shadowJar'){
libraryjars$ {System.getProperty('java.home')} / lib / rt.jar

injars'build / libs / artful-all.jar'
outjars'build / libs / artful-all.out.jar'

printmapping'build / libs / out。 map'

keepclasseswithmembers'public class * {\
public static void main(java.lang.String []); \
}'

assumenosideeffects'class kotlin.jvm.internal.Intrinsics {\
static void checkParameterIsNotNull(java.lang.Object,java.lang.String); \
}'
}


解决方案

这些文件包含用于声明标准(内置)Kotlin类的数据,这些类未被编译为 .class 文件,而是映射到现有类型上平台(在这种情况下,JVM)。例如, kotlin / kotlin.kotlin_builtins 包含包 kotlin 中的非物理类信息: Int 字符串 Enum Annotation code>,集合等等。

使用这些文件有两种主要情况:



1)编译器从类路径中的 kotlin-runtime 查找它们以确定哪些内置声明可用。
$ b 2)反射库( kotlin-reflect )将这些文件加载​​为资源,以便为构建在声明中。例如, String :: class.members 返回类 kotlin.String 的所有成员,就像Kotlin编译器会看到这些成员(尽管事实上没有 kotlin / String.class 文件,并且它已被擦除为 java.lang.String in bytecode)。

第一点显然不适用于你的情况。如果您不使用内置类的反射,我认为完全从结果jar中排除 .kotlin_builtins 文件是安全的。


I'm working on integrating proguard to my gradle build for an application written in Kotlin. I'm finding that proguard is stripping out the Kotlin standard library (as it should in my simple Hello World program) but it's leaving a bunch of files in my jar that have the file extension .kotlin_builtins. When I configure my gradle task to exclude those files, the program still appears to work fine. What are those files and must they ship with my executable uberjar?

Here's my build.gradle file's contents for reference:

buildscript {
   ext.kotlin_version = '1.0.5'
   ext.shadow_version = '1.2.4'

   repositories {
    mavenCentral()
    maven {
        url "https://plugins.gradle.org/m2/"
    }
    flatDir dirs: "gradle/proguard"
   }

   dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "com.github.jengelman.gradle.plugins:shadow:$shadow_version"
    classpath ":proguard:"
   }
}

apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'com.github.johnrengelman.shadow'

mainClassName = 'namespace.MainKt'

defaultTasks 'run'

repositories {
    mavenCentral()
}

dependencies {
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    testCompile "junit:junit:4.12"
    testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}

shadowJar {
    exclude 'kotlin/**/*.kotlin_builtins'
    exclude '.keep'
}

task minify(type: proguard.gradle.ProGuardTask, dependsOn: 'shadowJar') {
    libraryjars "${System.getProperty('java.home')}/lib/rt.jar"

    injars  'build/libs/artful-all.jar'
    outjars 'build/libs/artful-all.out.jar'

    printmapping 'build/libs/out.map'

    keepclasseswithmembers 'public class * { \
        public static void main(java.lang.String[]); \
    }'

    assumenosideeffects 'class kotlin.jvm.internal.Intrinsics { \
        static void checkParameterIsNotNull(java.lang.Object, java.lang.String); \
    }'
}

解决方案

These files contain data for declarations of standard ("built-in") Kotlin classes which are not compiled to .class files, but rather are mapped to the existing types on the platform (in this case, JVM). For example, kotlin/kotlin.kotlin_builtins contains the information for non-physical classes in package kotlin: Int, String, Enum, Annotation, Collection, etc.

There are two main scenarios when these files are used:

1) The compiler looks them up from kotlin-runtime on the classpath to determine which built-in declarations are available.

2) The reflection library (kotlin-reflect) loads these files as resources to provide reflection capabilities for built-in declarations. For example, String::class.members returns all members of the class kotlin.String exactly in the same way as the Kotlin compiler sees those members (despite the fact that there's no kotlin/String.class file and it's erased to java.lang.String in bytecode).

The first point is clearly not applicable in your case. And if you don't use reflection on built-in classes, I think it's safe to exclude .kotlin_builtins files completely from the resulting jar.

这篇关于什么是.kotlin_builtins文件,我可以从我的uberjars中忽略它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆