running python in android app with chaquopy yields fatal error - android

I'm try to run python code in my android app using Chaquopy, I'm also using the moviepy library (I'm converting a mp4 file to a gif file). When I try to do the conversion I get the following error message:
2019-02-06 15:46:47.586 3924-3924/com.udacity.gradle.builditbigger E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.udacity.gradle.builditbigger, PID: 3924
com.chaquo.python.PyException: AttributeError: module 'moviepy.audio.fx.all' has no attribute 'audio_fadein'
at <python>.moviepy.editor.<module>(<string>:1)
at <python>.moviepy.editor.<module>(editor.py:80)
at <python>.java.android.importer.load_module_impl(importer.py:435)
at <python>.java.android.importer.load_module(importer.py:353)
at <python>.importlib._bootstrap._load_backward_compatible(<frozen importlib._bootstrap>:626)
at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:656)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:955)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:971)
at <python>.java.chaquopy.import_override(import.pxi:18)
at <python>.gif_convert.<module>(gif_convert.py:1)
at <python>.java.android.importer.load_module_impl(importer.py:435)
at <python>.java.android.importer.load_module(importer.py:353)
at <python>.importlib._bootstrap._load_backward_compatible(<frozen importlib._bootstrap>:626)
at <python>.importlib._bootstrap._load_unlocked(<frozen importlib._bootstrap>:656)
at <python>.importlib._bootstrap._find_and_load_unlocked(<frozen importlib._bootstrap>:955)
at <python>.importlib._bootstrap._find_and_load(<frozen importlib._bootstrap>:971)
at <python>.importlib._bootstrap._gcd_import(<frozen importlib._bootstrap>:994)
at <python>.importlib.import_module(__init__.py:126)
at <python>.chaquopy_java.Java_com_chaquo_python_Python_getModule(chaquopy_java.pyx:154)
at com.chaquo.python.Python.getModule(Native Method)
at com.udacity.gradle.builditbigger.newPost.visualMediaPost.VisualMediaPostFragment.moveFile(VisualMediaPostFragment.java:247)
at com.udacity.gradle.builditbigger.camera.LifeCycleCamera.getFilePath(LifeCycleCamera.java:1161)
at com.udacity.gradle.builditbigger.camera.LifeCycleCamera.stopRecordingVideo(LifeCycleCamera.java:1078)
at com.udacity.gradle.builditbigger.newPost.visualMediaPost.VisualMediaPostFragment.lambda$onCreateView$5(VisualMediaPostFragment.java:162)
at com.udacity.gradle.builditbigger.newPost.visualMediaPost.-$$Lambda$VisualMediaPostFragment$WS1G2y9p8sE-re_eOWFa7Rr0OCU.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:6897)
at android.view.View$PerformClick.run(View.java:26089)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
I've found a fix for this problem (it involves changing the python code that would normally be generated by android studio, so any changes made to the python files will be overwritten during build time.
The python code is shown below
from moviepy.editor import VideoFileClip
def convert(path):
clip = VideoFileClip(path)
return clip.write_gif("output.gif", fps=24)
The java code used to start the pyhton code:
Python.start(new AndroidPlatform(getActivity()));
Python py = Python.getInstance();
PyObject gifConvert = py.getModule("gif_convert");
PyObject gif = gifConvert.callAttr("convert", file.getAbsolutePath());
Log.i("gif type", gif.toString());
Any help would be greatly appreciated.

Older versions of Chaquopy had a problem with pkgutil.iter_modules. This has been fixed in Chaquopy 6.2.1.

Related

Kotlin Multiplatform + Sentry

I want to use Sentry for my Multiplatform Kotlin project.
Sentry for Kotlin Multiplatform
So I was trying to prepare the setup of Sentry with Kotlin Multiplatform.
I was following the guide, made a mavenLocal() build etc.
Now I wanted to add it to the project.
However, when starting my (Android) project, even before starting the onCreate() of the Application I get a crash stating:
Unable to get provider io.sentry.android.core.SentryInitProvider:
java.lang.IllegalArgumentException: DSN is required. Use empty string to disable SDK.
So to be clear this happens already when adding api("io.sentry:sentry-kotlin-multiplatform:0.0.1") to the dependencies. Long before the init call.
All I did was:
Configure shared build.gradle.kts and add
val commonMain by getting {
dependencies {
api("io.sentry:sentry-kotlin-multiplatform:0.0.1")
Adding the sample configuration
fun initializeSentry(context: Context) {
Sentry.init(context) { sentryOptions -> setDsnHere... }
}
and in the Android Application:
initializeSentry(this)
Then tried to run it. But I don't even get to the init call.
The configuration should be reached and the initializeSentry(this) block executed. (I can accept if it fails there but I don't even get that far.)
Instead the app fails stating
E/AndroidRuntime: FATAL EXCEPTION: main
Process: xyz, PID: 23056
java.lang.RuntimeException: Unable to get provider io.sentry.android.core.SentryInitProvider: java.lang.IllegalArgumentException: DSN is required. Use empty string to disable SDK.
at android.app.ActivityThread.installProvider(ActivityThread.java:7467)
...
even before anything else.
So the mere adding to the gradle file causes the crash already (even if the other code is commented out).
-------- Full Stack trace-------
java.lang.RuntimeException: Unable to get provider io.sentry.android.core.SentryInitProvider: java.lang.IllegalArgumentException: DSN is required. Use empty string to disable SDK.
at android.app.ActivityThread.installProvider(ActivityThread.java:7467)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:6973)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6744)
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2133)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.IllegalArgumentException: DSN is required. Use empty string to disable SDK.
at io.sentry.Sentry.initConfigurations(Sentry.java:201)
at io.sentry.Sentry.init(Sentry.java:170)
at io.sentry.Sentry.init(Sentry.java:118)
at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:87)
at io.sentry.android.core.SentryAndroid.init(SentryAndroid.java:56)
at io.sentry.android.core.SentryInitProvider.onCreate(SentryInitProvider.java:27)
at android.content.ContentProvider.attachInfo(ContentProvider.java:2451)
at android.content.ContentProvider.attachInfo(ContentProvider.java:2421)
at io.sentry.android.core.SentryInitProvider.attachInfo(SentryInitProvider.java:44)
at android.app.ActivityThread.installProvider(ActivityThread.java:7462)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:6973) 
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6744) 
at android.app.ActivityThread.-$$Nest$mhandleBindApplication(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2133) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:201) 
at android.os.Looper.loop(Looper.java:288) 
at android.app.ActivityThread.main(ActivityThread.java:7872) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
Turns out the manual had forgotten to mention the auto configuration.
Add this line to your Manifest:
<meta-data android:name="io.sentry.auto-init" android:value="false" />
and proceed with the tutorial.
For Reference:
https://github.com/getsentry/sentry-kotlin-multiplatform/issues/45

Android Studio and SMBJ

I am trying to make a File List from my SMB Shared Folder with Android Studio.
I want to press a Button in my Android App and then get the List stored as *.txt in my root or SD Card Root.
I am using Android Studio API Version Level 30 and SMBJ Library.
But I'm failing in connection for my SMB Share.
When I press the button with the code its crashing without giving a Message on my Samsung Galaxy S20.
I found that the crash happens on following 2 lines:
AuthenticationContext ac = new AuthenticationContext("admin", "password123".toCharArray(),null);
Session session = connection.authenticate(ac);
I am not sure if I need the domain.
I have no domain installed and tried some different things.
SMBClient client = new SMBClient();
try {
Connection connection = client.connect("192.168.1.75");
AuthenticationContext ac = new AuthenticationContext("admin", "password123".toCharArray(),null);
Session session = connection.authenticate(ac);
//DiskShare share = (DiskShare) session.connectShare("Intel");
} catch (Exception e) {
txt.setText(e.toString());
}
The following is from the SMBJ Website on GIT:
https://github.com/hierynomus/smbj
SMBClient client = new SMBClient();
try (Connection connection = client.connect("SERVERNAME")) {
AuthenticationContext ac = new AuthenticationContext("USERNAME", "PASSWORD".toCharArray(), "DOMAIN");
Session session = connection.authenticate(ac);
// Connect to Share
try (DiskShare share = (DiskShare) session.connectShare("SHARENAME")) {
for (FileIdBothDirectoryInformation f : share.list("FOLDER", "*.TXT")) {
System.out.println("File : " + f.getFileName());
}
}
}
This is the error log I get:
2022-04-25 11:45:27.475 22865-22865/com.example.filme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.filme, PID: 22865
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/slf4j/LoggerFactory;
at com.hierynomus.smbj.SMBClient.<clinit>(SMBClient.java:119)
at com.example.filme.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:7792)
at android.widget.TextView.performClick(TextView.java:16112)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7769)
at android.view.View.access$3800(View.java:910)
at android.view.View$PerformClick.run(View.java:30213)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8663)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.slf4j.LoggerFactory" on path: DexPathList[[dex file "/data/data/com.example.filme/code_cache/.overlay/base.apk/classes3.dex", dex file "/data/data/com.example.filme/code_cache/.overlay/base.apk/classes.dex", zip file "/data/app/~~SMMMBpkYv1Fu0fT2H1M_sA==/com.example.filme-ovE-fLeCY69AhQvQzyn1cA==/base.apk"],nativeLibraryDirectories=[/data/app/~~SMMMBpkYv1Fu0fT2H1M_sA==/com.example.filme-ovE-fLeCY69AhQvQzyn1cA==/lib/arm64, /system/lib64, /system/system_ext/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:218)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.hierynomus.smbj.SMBClient.<clinit>(SMBClient.java:119) 
at com.example.filme.MainActivity$1.onClick(MainActivity.java:36) 
at android.view.View.performClick(View.java:7792) 
at android.widget.TextView.performClick(TextView.java:16112) 
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119) 
at android.view.View.performClickInternal(View.java:7769) 
at android.view.View.access$3800(View.java:910) 
at android.view.View$PerformClick.run(View.java:30213) 
at android.os.Handler.handleCallback(Handler.java:938) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loopOnce(Looper.java:226) 
at android.os.Looper.loop(Looper.java:313) 
at android.app.ActivityThread.main(ActivityThread.java:8663) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 
2022-04-25 11:45:27.518 22865-22865/com.example.filme I/Process: Sending signal. PID: 22865 SIG: 9
tried to install library slf4j-android-1.7.9.jar
but dont works for now.
Edit 12:46 o clock:
I added now many libraries and now its going close to work but not fully.
No library is missing but the following error occurs:
android.os.NetworkOnMainThreadException error
I read that I must do asynchronous processing.
But I'm too stupid to write this asynchronous code.. can anyone help?
Now its working.
I needed following LIBRARIES:
asn-one-0.6.0.jar
bcprov-jdk15on-1.70.jar
mbassador-1.3.2.jar
slf4j-api-1.7.9.jar
slf4j-nop-1.7.9.jar
smbj-0.11.5.jar

TFlite object detector app crashes just before making predictions

I tried several methods to deploy the tflite model on android, flutter, kotlin, etc, but andriod app keeps crashing when I want to make predictions. This was an error I got trying out the object detection codelab:
2022-01-01 15:52:55.965 2971-3034/org.tensorflow.codelabs.objectdetection E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
Process: org.tensorflow.codelabs.objectdetection, PID: 2971
java.io.FileNotFoundException: This file can not be opened as a file descriptor; it is probably compressed
at android.content.res.AssetManager.nativeOpenAssetFd(Native Method)
at android.content.res.AssetManager.openFd(AssetManager.java:938)
at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromFdAndOptions(TaskJniUtils.java:65)
at org.tensorflow.lite.task.vision.detector.ObjectDetector.createFromFileAndOptions(ObjectDetector.java:147)
at org.tensorflow.codelabs.objectdetection.MainActivity.runObjectDetection(MainActivity.kt:127)
at org.tensorflow.codelabs.objectdetection.MainActivity.access$runObjectDetection(MainActivity.kt:48)
at org.tensorflow.codelabs.objectdetection.MainActivity$setViewAndDetect$1.invokeSuspend(MainActivity.kt:150)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
2022-01-01 15:52:56.014 2971-3034/org.tensorflow.codelabs.objectdetection I/Process: Sending signal. PID: 2971 SIG: 9
This issue was caused by android compressing the tflite model during the build. To solve this issue i had to instruct aapt not to compress the file.
go to build.gradle, at the app level, inside the andriod{} block, add
aaptOptions {
noCompress "model.tflite"
}
please rename model.tflite to your custom name if your tflite model is not named "model.tflite"

app crashes on production but not in development - android app

My Project structure:
1- Kotlin app lib.
2-Java app calling methods from Kotlin lib.
From Java app I'm calling some methods from Kotlin lib. in development all works good and methods called successfully. after generating apk app starts with splash screen then crashes after getting location,
specifically when calling methods from Kotlin lib.
I think I missed something when generating apk with App and lib.
I tried this example and another about proguardFiles but no new result:
https://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-multi-project-build/
the error:
2020-05-28 13:22:29.970 339-339/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.app, PID: 339
java.lang.NoSuchMethodError: No static method asAttributeSet(Lh/a/a/a;)Landroid/util/AttributeSet; in class Landroid/util/Xml; or its super classes (declaration of 'android.util.Xml' appears in /system/framework/framework.jar:classes2.dex)
at c.a.n.g.inflate()
at ui.activity.MainActivity.onCreateOptionsMenu()
at android.app.Activity.onCreatePanelMenu(Activity.java:3183)
at androidx.fragment.app.d.onCreatePanelMenu()
at c.a.n.i.onCreatePanelMenu()
at androidx.appcompat.app.f$j.onCreatePanelMenu()
at c.a.n.i.onCreatePanelMenu()
at androidx.appcompat.app.i.o()
at androidx.appcompat.app.i$a.run()
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

How to build and use PJSIP for different architectures than armeabi?

I succesfully compiled PJSIP library for Android. Sample project works without any problems when I build for default architecture armeabi. Now I tried to compile library for arch such as armeabi-v7a and arm64-v8a. Unfortunately when I add libraries to project in appropriate folders (jnLibs/armeabi-v7a and jniLibs/arm64-v8a) application is not starting...
To setup build for different archs I use this command:
NDK_TOOLCHAIN_VERSION=4.9 TARGET_ABI=armeabi-v7a ./configure-android --use-ndk-cflags
I have this error on app launch:
07-04 12:28:46.079 15317-15317/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.futuremind.omili, PID: 15317
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "getifaddrs" referenced by "/data/app/com.futuremind.omili-1/lib/arm/libpjsua2.so"...
at java.lang.Runtime.loadLibrary(Runtime.java:372)
at java.lang.System.loadLibrary(System.java:1076)
at com.futuremind.omili.MyApp.<clinit>(MyApp.java:293)
at com.futuremind.omili.MainActivity.onCreate(MainActivity.java:87)
at android.app.Activity.performCreate(Activity.java:6289)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2535)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:152)
at android.app.ActivityThread.main(ActivityThread.java:5497)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Thanks for any help! :)
I was able to fix this error by updating the AndroidManifest.xml in "pjsip-apps/src/swig/java/android/app/src/main" to match my android target -- in my case to "23". Then "make clean" and another "make" in "pjsip-apps/src/swig", and rebuilding my app.

Categories

Resources