When launching my Flutter app using the pytorch_mobile
plugin with minifyEnabled
and shrinkResources
set to true, I get the following error:
...
F/xxx(25186): java_vm_ext.cc:594] JNI DETECTED ERROR IN APPLICATION: JNI NewGlobalRef called with pending exception java.lang.ClassNotFoundException: Didn't find class "org.pytorch.PyTorchAndroid" on path: DexPathList[[zip file "/data/app/~~hrC1Awe0qTN_lMvYeHVsZQ==/xxx-iLzf33Fatdto0s-3rgbmzA==/base.apk"],nativeLibraryDirectories=[/data/app/~~hrC1Awe0qTN_lMvYeHVsZQ==/xxx-iLzf33Fatdto0s-3rgbmzA==/lib/arm64, /data/app/~~hrC1Awe0qTN_lMvYeHVsZQ==/xxx-iLzf33Fatdto0s-3rgbmzA==/base.apk!/lib/arm64-v8a, /system/lib64, /system/system_ext/lib64]]
...
I have the following in the android/app/build.gradle
android.buildTypes
to try to solve this issue in debug before solving it in release:
...
debug {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
...
I tried many solutions in the proguard-rules.pro
file:
-optimizationpasses 1
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-dump class_files.txt
-printseeds seeds.txt
-printusage unused.txt
-printmapping mapping.txt
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-allowaccessmodification
-keepattributes *Annotation*
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable
-repackageclasses ''
-keep class org.pytorch.** { *; }
-keep interface org.pytorch.** { *; }
-keep class io.fynn.pytorch_mobile.** { *; }
-keep interface io.fynn.pytorch_mobile.** { *; }
-keep class io.fynn.pytorch_mobile.** { *; }
-keep interface io.fynn.pytorch_mobile.** { *; }
-keep class io.fynn.** { *; }
-keep interface io.fynn.** { *; }
-keep class libpytorch.** { *; }
-keep interface libpytorch.** { *; }
-keep class libpytorch*.** { *; }
-keep interface libpytorch*.** { *; }
-keep class org.pytorch.proguard.UsedBy*
-keep @org.pytorch.proguard.UsedBy* class * {
<init>();
}
-keepclassmembers class * {
@org.pytorch.proguard.UsedBy* *;
}
-keep class org.pytorch.*
-keep @org.pytorch.* class * { *; }
-keepclassmembers class * {
@org.pytorch.* *;
}
-keep class * {
*;
}
-keep class org.pytorch.** { *; }
-keep class com.facebook.jni.** { *; }
-keep interface org.pytorch.** { *; }
-keep interface com.facebook.jni.** { *; }
-keep class org.pytorch.PyTorchAndroid** { *; }
-keep interface org.pytorch.PyTorchAndroid** { *; }
-keep class org.pytorch.PyTorchAndroid.** { *; }
-keep interface org.pytorch.PyTorchAndroid.** { *; }
-keep class org.pytorch.pytorchandroid** { *; }
-keep interface org.pytorch.pytorchandroid** { *; }
-keep class org.pytorch.pytorchandroid.** { *; }
-keep interface org.pytorch.pytorchandroid.** { *; }
-keep class org.pytorch.jni.** { *; }
-keep interface org.pytorch.jni.** { *; }
-keep class org.pytorch.**
-keep class org.pytorch.*
#-keep class org.pytorch.PyTorchAndroid
#@keep class org.pytorch.** { *; }
#@keep class com.facebook.jni.** { *; }
#@keep interface org.pytorch.** { *; }
#@keep interface com.facebook.jni.** { *; }
#@keep class org.pytorch.PyTorchAndroid** { *; }
#@keep interface org.pytorch.PyTorchAndroid** { *; }
#@keep class org.pytorch.PyTorchAndroid.** { *; }
#@keep interface org.pytorch.PyTorchAndroid.** { *; }
#@keep class org.pytorch.pytorchandroid** { *; }
#@keep interface org.pytorch.pytorchandroid** { *; }
#@keep class org.pytorch.pytorchandroid.** { *; }
#@keep interface org.pytorch.pytorchandroid.** { *; }
#@keep class org.pytorch.jni.** { *; }
#@keep interface org.pytorch.jni.** { *; }
#@keep class org.pytorch.**
#@keep class org.pytorch.*
#@keep class org.pytorch.PyTorchAndroid
-keeppackagenames *pytorch*,*PyTorch*,**pytorch**,**PyTorch**
-libraryjars pytorch_android-1.8.0.aar
But I still get the above error and the app crashes.
There is no solution for now in the related plugin thread that would allow optimization: https://github.com/fynnmaarten/flutter_pytorch_mobile/issues/25
My mistake was the wrong location of the proguard-rules.pro
file. It must be in the /android/app/
folder.