chaquo / chaquopy

Chaquopy: the Python SDK for Android
https://chaquo.com/chaquopy/
MIT License
791 stars 130 forks source link

callAttr method gives an error. #1005

Closed furkanbalci0 closed 10 months ago

furkanbalci0 commented 10 months ago

Chaquopy version

14.0.2

Devices or emulators where the issue happens

All devices and emulators.

Relevant parts of your code

Event:APP_SCOUT_HANG Thread:main backtrace:
                                             at com.chaquo.python.PyObject.callAttrThrowsNative(Native Method)
                                             at com.chaquo.python.PyObject.callAttrThrows(PyObject.java:232)
                                             at com.chaquo.python.PyObject.callAttr(PyObject.java:221)
                                             at com.speedreadtube.app.data.remote.YoutubeRepository.getTranscripts(YoutubeRepository.kt:18)
                                             at com.speedreadtube.app.MainViewModel$getTranscripts$2.invokeSuspend(MainViewModel.kt:24)
                                             at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                             at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
                                             at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:280)
                                             at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
                                             at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
                                             at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source:1)
                                             at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking$default(Builders.kt:38)
                                             at kotlinx.coroutines.BuildersKt.runBlocking$default(Unknown Source:1)
                                             at com.speedreadtube.app.MainViewModel.getTranscripts(MainViewModel.kt:23)
                                             at com.speedreadtube.app.ui.lobby.LobbyFragment$populateUi$1.invokeSuspend(LobbyFragment.kt:43)
                                             at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                             at kotlinx.coroutines.DispatchedTaskKt.resume(DispatchedTask.kt:235)
                                             at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:168)
                                             at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:474)
                                             at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl(CancellableContinuationImpl.kt:508)
                                             at kotlinx.coroutines.CancellableContinuationImpl.resumeImpl$default(CancellableContinuationImpl.kt:497)
                                             at kotlinx.coroutines.CancellableContinuationImpl.resumeUndispatched(CancellableContinuationImpl.kt:595)
                                             at kotlinx.coroutines.android.HandlerContext$scheduleResumeAfterDelay$$inlined$Runnable$1.run(Runnable.kt:19)
                                             at android.os.Handler.handleCallback(Handler.java:942)
                                             at android.os.Handler.dispatchMessage(Handler.java:99)
                                             at android.os.Looper.loopOnce(Looper.java:211)
                                             at android.os.Looper.loop(Looper.java:300)
                                             at android.app.ActivityThread.main(ActivityThread.java:8232)
                                             at java.lang.reflect.Method.invoke(Native Method)
                                             at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1028)
00:43:59.042 eedreadtube.app          I  Thread[6,tid=20603,WaitingInMainSignalCatcherLoop,Thread*=0xb40000775443d000,peer=0x13541490,"Signal Catcher"]: reacting to signal 3

Describe your issue

Sometimes the app works and sometimes it doesn't. It happens completely randomly. I do this entirely in mainthread.

mhsmith commented 10 months ago

Please post the exception message (not just the stack trace), and the relevant parts of your code.

furkanbalci0 commented 10 months ago

Sure,

Repositroy Class

class YoutubeRepository {
    fun getTranscripts(videoId: String): String {
        val python = Python.getInstance()
        val pythonFile = python.getModule("youtube")
        val result: PyObject = kotlin.run { pythonFile.callAttr("get_transcripts", videoId) }

        val moshi = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
        //List adapter
        val jsonAdapter: JsonAdapter<List<TranscriptModel>> = moshi
            .adapter<List<TranscriptModel>>(Types.newParameterizedType(List::class.java, TranscriptModel::class.java))
            .lenient()
        val list = jsonAdapter.fromJson(result.toString())!!

        val stringBuilder = StringBuilder()
        for (transcript in list) {

            val text = transcript.text
            stringBuilder.append("$text ")
        }

        //replace all new lines with spaces
        stringBuilder.replace(Regex("[\\n\\r]"), " ")

        return stringBuilder.toString()
    }
}

src/main/python/youtube.py

from youtube_transcript_api import YouTubeTranscriptApi

def get_transcripts(video_id):
    try:
        transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
        for transcript in transcript_list:
            return transcript.fetch()
    except Exception as e:
        return {"error": str(e)}

gradle: app (root)

plugins {
    id 'com.android.application' version '8.1.2' apply false
    id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
    id 'com.chaquo.python' version '14.0.2' apply false
}

gradle: module

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'com.chaquo.python'
}

android {
    namespace 'com.speedreadtube.app'
    compileSdk 34

    defaultConfig {
        applicationId "com.speedreadtube.app"
        minSdk 24
        targetSdk 33
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        ndk {
            abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
        }
        python {
            version "3.11"
            buildPython "/Library/Frameworks/Python.framework/Versions/3.11/bin/python3"
            pip {
                // A requirement specifier, with or without a version number:
                install "youtube_transcript_api"
            }

        }
    }

    sourceSets {
        main {
            python.srcDir "src/main/python"
        }
    }

    buildFeatures {
        viewBinding true
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'
    implementation 'com.google.android.material:material:1.10.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'

    //Moshi
    implementation 'com.squareup.moshi:moshi:1.14.0'
    implementation 'com.squareup.moshi:moshi-kotlin:1.14.0'

    //Navigation
    implementation "androidx.navigation:navigation-fragment-ktx:2.7.4"
    implementation "androidx.navigation:navigation-ui-ktx:2.7.4"

    //Coroutines
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4'
    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.2'

    //Lottie
    implementation 'com.airbnb.android:lottie:6.1.0'

    //BlurKit
    implementation 'jp.wasabeef:blurry:4.0.1'

    implementation("com.squareup.okhttp3:logging-interceptor:4.11.0")
    implementation("com.squareup.okhttp3:okhttp:4.11.0")
    implementation("com.squareup.okhttp3:okhttp-urlconnection:4.9.1")
}
mhsmith commented 10 months ago

You still haven't posted the actual exception message. Please try to find it and understand it yourself before asking for help.

It should be in in the log near the stack trace, and if the exception comes from Python code, there should be some <python> lines there too.

This stack trace is in an unusual format, which my searches suggest may be connected to Xiaomi MIUI devices. You may get more useful information from a different device.

furkanbalci0 commented 10 months ago

I need to be able to access the python code in order to throw any exceptions, but since I cannot access the python code anyway, it gives the above error.

Where should “buildPython” be in Gradle?

Currently: "/Library/Frameworks/Python.framework/Versions/3.11/bin/python3"

mhsmith commented 10 months ago

You've closed the issue: does this mean you've solved your problem? If so, please post a comment explaining how you solved it, as this may be useful to other people.

furkanbalci0 commented 10 months ago

I'm sorry for keeping you busy. I have concluded that the problem is not caused by you.