gpt4 book ai didi

java - 从 Java 调用 Kotlin(找不到符号)

转载 作者:行者123 更新时间:2023-12-01 23:19:59 25 4
gpt4 key购买 nike

我正在尝试从 Java 调用 Kotlin 中的函数。
文档说这很容易。到目前为止,我一直在关注这篇文章。

https://kotlinlang.org/docs/java-to-kotlin-interop.html#package-level-functions

当输入 Java 时,IDE 可以识别类,但是当我编译它时却说错误:找不到符号

我们非常欢迎任何帮助。

应用.kt

package org.cocos2dx.cpp

fun getTime() { /*...*/ }

AppActivity.java

    public void test1() {
org.cocos2dx.cpp.AppKt.getTime();
}

项目结构

/proj.android
/app
/src
/org.cocos2dx.cpp
app.kt
AppActivity

项目级build.gradle

buildscript {
ext.kotlin_version = "1.5.0"
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.3.8'
classpath 'com.google.firebase:firebase-crashlytics-gradle:2.7.0'
}
}

allprojects {
repositories {
google()
mavenCentral()
}
}

task clean(type: Delete) {
delete rootProject.buildDir
}

模块级build.gradle

import org.gradle.internal.os.OperatingSystem

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'

android {
compileSdkVersion PROP_COMPILE_SDK_VERSION.toInteger()

defaultConfig {
applicationId "demo.1"
minSdkVersion PROP_MIN_SDK_VERSION
targetSdkVersion PROP_TARGET_SDK_VERSION
versionCode 43
versionName "0.43"

externalNativeBuild {
cmake {
targets 'MyGame'
arguments "-DCMAKE_FIND_ROOT_PATH=", "-DANDROID_STL=c++_static", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE"
cppFlags "-frtti -fexceptions -fsigned-char"
}
}

ndk {
abiFilters = []
abiFilters.addAll(PROP_APP_ABI.split(':').collect{it as String})
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

sourceSets.main {
java.srcDir "src"
res.srcDir "res"
manifest.srcFile "AndroidManifest.xml"
assets.srcDir "../../Resources"
}

externalNativeBuild {
cmake {
path "../../CMakeLists.txt"
}
}

signingConfigs {
release {
if (project.hasProperty("RELEASE_STORE_FILE")) {
storeFile file(RELEASE_STORE_FILE)
storePassword RELEASE_STORE_PASSWORD
keyAlias RELEASE_KEY_ALIAS
keyPassword RELEASE_KEY_PASSWORD
}
}
}

buildTypes {
release {
debuggable false
jniDebuggable false
renderscriptDebuggable false
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
if (project.hasProperty("RELEASE_STORE_FILE")) {
signingConfig signingConfigs.release
}
}

debug {
debuggable true
jniDebuggable true
renderscriptDebuggable true
}
}
}

android.applicationVariants.all { variant ->
def project_root_folder = "${projectDir}/../.."
def dest_assets_folder = "${projectDir}/assets"

delete dest_assets_folder
def targetName = variant.name.capitalize()
def copyTaskName = "copy${targetName}ResourcesToAssets"

tasks.register(copyTaskName) {
copy {
from "${buildDir}/../../../Resources"
into "${buildDir}/intermediates/assets/${variant.dirName}"
exclude "**/*.gz"
}
}
tasks.getByName("pre${targetName}Build").dependsOn copyTaskName
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(':libcocos2dx')
implementation 'androidx.core:core:1.0.0'
implementation platform('com.google.firebase:firebase-bom:28.1.0')
implementation 'com.google.firebase:firebase-crashlytics-ktx'
implementation 'com.google.firebase:firebase-analytics-ktx'
}

错误

error: cannot find symbol
org.cocos2dx.cpp.AppKt.getTime();
^
symbol: class AppKt
location: package org.cocos2dx.cpp

最佳答案

您的代码看起来不错;但你需要有项目级别的build.gradle类路径:

类路径“org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version”

buildscript {
ext.kotlin_version = "1.5.0"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

}
}

以及 build.gradle 模块级别中的 Kotlin 标准库依赖项:

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}

关于java - 从 Java 调用 Kotlin(找不到符号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68230211/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com