在Kotlin中使用JNI桥接时出现未解决的引用

huangapple go评论50阅读模式
英文:

Unresolved reference in Kotlin using JNI-Bridge

问题

我想在Kotlin的可组合函数中调用startOsc()函数,但出现了这个错误:Unresolved reference: startOsc

这是我的Kotlin代码:

class MainActivity : ComponentActivity() {

    private external fun startEngine()
    private external fun stopEngine()
    private external fun startOsc() // <-- 这里定义了函数

    companion object {
        init {
            System.loadLibrary("pitch_andoid")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TwoNotesView()
        }
        startEngine()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopEngine()
    }
}

@Composable
fun TwoNotesView() {

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.cyan))
    ) {
        Row {
            Box (
                modifier = Modifier
                    .clickable {
                        startOsc() // <-- 这是错误的那一行
                    }
                    ) {
                AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // 这是其他可组合部分,我为简单起见省略了
            }
        }
    }
}

CMakeLists.txt可能是正确的,因为startEngine()stopEngine()没有未解决的引用。这是startOsc()函数的jni-bridge.cpp代码:

JNIEXPORT void JNICALL
Java_com_example_pitch_1andoid_MainActivity_startOsc(JNIEnv *env, jobject/* this */) {
    audioEngine->setToneOn(true);
}

我拼写错误了项目的名称,这就是为什么所有东西都叫做"andoid"而不是"android"。抱歉。

编辑:错误应该是将函数从MainActivity传递到可组合函数中。我尝试在MainActivity中调用函数,并没有出错。

英文:

I want to call the startOsc()function inside of a Composable in Kotlin and get this error: Unresolved reference: startOsc

This is my Kotlin code:

class MainActivity : ComponentActivity() {

    private external fun startEngine()
    private external fun stopEngine()
    private external fun startOsc() // <-- here the function is defined

    companion object {
        init {
            System.loadLibrary("pitch_andoid")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TwoNotesView()
        }
        startEngine()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopEngine()
    }
}

@Composable
fun TwoNotesView() {

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.cyan))
    ) {
        Row {
            Box (
                modifier = Modifier
                    .clickable {
                        startOsc() // <-- this is the line of the error 
                    }
                    ) {
                AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // this is some other composable I left out for simplicity
            }
        }
    }
}

The CMakeLists.txt is probably correct, since there is no Unresolved Reference for startEngine() and stopEngine().
Here is the code of the jni-bridge.cpp for the startOsc()function:

JNIEXPORT void JNICALL
Java_com_example_pitch_1andoid_MainActivity_startOsc(JNIEnv *env, jobject/* this */) {
    audioEngine->setToneOn(true);
}

I made a spelling mistake naming the project, that's why everything is called "andoid" instead of "android", Sorry.

edit: the error has to be passing down the function from MainActivity to the Composable. I tried calling the function inside MainActivity and got no error.

答案1

得分: 0

通过以下方式传递 startOsc() 来解决这个问题:

class MainActivity : ComponentActivity() {

    private external fun startEngine()
    private external fun stopEngine()
    external fun startOsc() // <-- 删除 'private'

    companion object {
        init {
            System.loadLibrary("pitch_andoid")
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TwoNotesView(::startOsc) // <-- 作为参数传递 startOsc()
        }
        startEngine()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopEngine()
    }
}

@Composable
fun TwoNotesView(startOsc: () -> Unit) { // <-- 添加 startOsc() 作为参数

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.cyan))
    ) {
        Row {
            Box (
                modifier = Modifier
                    .clickable {
                        startOsc()
                    }
                    ) {
                AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // 这是我为简单起见略去的另一个可组合部分
            }
        }
    }
}
英文:

Solved it by passing down startOsc() like this:

class MainActivity : ComponentActivity() {

    private external fun startEngine()
    private external fun stopEngine()
    external fun startOsc() // &lt;-- deleted &#39;private&#39;

    companion object {
        init {
            System.loadLibrary(&quot;pitch_andoid&quot;)
        }
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            TwoNotesView(::startOsc) // &lt;-- pass down startOsc() as parameter
        }
        startEngine()
    }

    override fun onDestroy() {
        super.onDestroy()
        stopEngine()
    }
}

@Composable
fun TwoNotesView(startOsc: () -&gt; Unit) { // &lt;-- add startOsc() as parameter

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(colorResource(id = R.color.cyan))
    ) {
        Row {
            Box (
                modifier = Modifier
                    .clickable {
                        startOsc()
                    }
                    ) {
                AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // this is some other composable I left out for simplicity
            }
        }
    }
}

huangapple
  • 本文由 发表于 2023年7月10日 18:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76652839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定