美文网首页
Kotlin Native

Kotlin Native

作者: 我有的似乎只能是等待等待 | 来源:发表于2018-12-06 21:17 被阅读0次

    1. Kotlin Native简介

    kotlin native.png

    Kotlin/Native is a technology for compiling Kotlin code to native binaries, which can run without a virtual machine. It is an LLVM based backend for the Kotlin compiler and native implementation of the Kotlin standard library.

    Target Platforms

    Kotlin/Native supports the following platforms:

    • iOS (arm32, arm64, emulator x86_64)
    • MacOS (x86_64)
    • Android (arm32, arm64)
    • Windows (mingw x86_64)
    • Linux (x86_64, arm32, MIPS, MIPS little endian)
    • WebAssembly (wasm32)

    根据官网描述,kotlin native支持以上的平台,这得益于llvm,这意味着,你可以使用kotlin编写出来的代码编程成各个平台的代码,供各种语言去使用。

    同样的kotlin还支持调用以上平台的代码,这也就是说如果你写了一个c/c++的库,比如常用的ffmpeg,opengl等等,都可以使用kotlin去调用。

    2. 使用kotlin Native编译成平台直接可运行程序

    1. 首先我们先要下载kotlin Native的代码,https://github.com/JetBrains/kotlin-native(这里建议只下载某一个分支的代码就可以,没必要全部下载)

    First, download dependencies:

    ./gradlew dependencies:update
    

    Then, build the compiler and libraries:(最好使用下面的一条命令进行编译,节省时间)

    ./gradlew bundle
    

    The build can take about an hour on a Macbook Pro. To run a shorter build with only the host compiler and libraries, run:

    ./gradlew dist distPlatformLibs
    

    To include Kotlin compiler in composite build and build against it, use the kotlinProjectPath project property:

    ./gradlew dist -PkotlinProjectPath=path/to/kotlin/project
    

    After that, you should be able to compile your programs like this:

    export PATH=./dist/bin:$PATH kotlinc hello.kt -o hello
    

    For an optimized compilation, use -opt:

    kotlinc hello.kt -o hello -opt
    

    3. 下载并编译完kotlin native的工具,我们就可以使用该工具进行kotlin native的开发,当然建议最好使用mac或者linux进行开发,省时省力。

    4. 然后我们采用的开发工具是IDEA,我们先用IDEA创建一个gradle工程,为什么要创建一个gradle工程呢?因为我们要使用gradle构建工具强大的自动构建功能帮我们进行编译。但是kotlin官网更建议我们去编写makefile文件或者脚本文件去编译,这就各有所好了。

    5. 创建一个空的gradle工程后,先在工程的build.gradle工程中加入以下代码:

    buildscript { 
          repositories { 
                mavenCentral() 
                //增加kotlin代码库的maven地址 
                maven { 
                        url "https://dl.bintray.com/jetbrains/kotlin-native-dependencies" 
                } 
          } 
    
    dependencies { 
                // 这里是kotlin native的插件,这个版本就要到它的仓库里去查看,可以选择最新的,               
                // 如果是发烧友的话可以使用beta版本,这里选择了当前最新的稳定版                       
                classpath "org.jetbrains.kotlin:kotlin-native-gradle-plugin:1.3.10"
          } 
    } 
    
    // konan 就是用来编译 Kotlin 为 native 代码的插件 
    apply plugin: 'konan' 
    
    konanArtifacts { 
               //konanArtifacts 配置我们的项目
               //第一个参数: kotlin native编译后生成的输出产物名称。
              //第二个参数: 这里另外说一下,因为会生成不同平台的东西,所以一套代码可能会编译成不同平台的运行程序
             // 如果targets数组不填,默认会生成当前平台的运行程序。
             //srcFiles: 编译的目标文件,即kotlin代码。
           program('Main',targets: ['android_arm64','linux']) { 
                    srcFiles 'src/main/kotlin/Main.kt'
           }
     }
    
    

    6. 当然Main.kt的内容可以很简单:

    fun main(args: Array<String>) {
             println("Hello Kotlin Native!!!") 
    }
    

    但是这个文件不能直接运行,需要使用gradle工具去编译成可运行文件,

    打开gradle工具栏,执行build任务,就可以在工程的build目录下找到对应的可执行程序

    gradle build任务 生成的exe可执行程序 命令行运行程序

    在window平台下,直接在命令行下敲入 Main.exe 便可以执行该程序。

    相关文章

      网友评论

          本文标题:Kotlin Native

          本文链接:https://www.haomeiwen.com/subject/otcacqtx.html