Flutter app built with build apk command fails to run platform specific code
Solution 1
It seems when you use flutter build apk
, gradle doesn't include the libs you added in the built apk.
You need to do a couple of checks:
- Check if you are using proguard optimization and
minifyEnabled true
. If yes, try to setminifyEnabled false
, else follow the second check - Check if you are using
implementation
keyword when adding dependencies you your project. If yes, try to replace it withapi
I hope this helps
Solution 2
It reads: ClassNotFoundException: com.razorpay.G__G_
because you've obfuscated the Activity
name. Add this to your Android ProGuard/R8 configuration file to keep all Activity
class-names:
-keep public class * extends android.app.Activity
The verbose output might show some more warnings to be fixed:
-verbose
For Flutter, you may also need these:
-keep class io.flutter.app.** { *; }
-keep class io.flutter.plugin.** { *; }
-keep class io.flutter.util.** { *; }
-keep class io.flutter.view.** { *; }
-keep class io.flutter.** { *; }
-keep class io.flutter.plugins.** { *; }
If you're looking for a reputable source, see the ProGuard manual or the reference card.
Yahya Ayash Luqman
Updated on December 21, 2022Comments
-
Yahya Ayash Luqman over 1 year
I have a flutter app that runs certain platform specific code using method channel. This code also uses external libraries through gradle dependencies. Everything works fine when built using android studio for both debug and release mode. But when built using
flutter build apk
command, the app crashes when trying to execute the platform code. These are the logs I am getting fromadb logcat
from one of the method:05-30 02:04:53.302 4398 4398 E AndroidRuntime: Process: com.**.**, PID: 4398 05-30 02:04:53.302 4398 4398 E AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.**.**/com.razorpay.CheckoutActivity}: java.lang.ClassNotFoundException: com.razorpay.G__G_ 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:107) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.os.Looper.loop(Looper.java:214) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7397) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: Caused by: java.lang.ClassNotFoundException: com.razorpay.G__G_ 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.O__Y_.G__G_(:1100) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.b1.<clinit>(Unknown Source:1683) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.b1.a(Unknown Source:0) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.c.f(Unknown Source:11) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.g.c(Unknown Source:0) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.k.onCreate(Unknown Source:70) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.w0.onCreate(Unknown Source:110) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at com.razorpay.CheckoutActivity.onCreate(Unknown Source:0) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7993) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.Activity.performCreate(Activity.java:7982) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3246) 05-30 02:04:53.302 4398 4398 E AndroidRuntime: ... 11 more
Here com.razorpay is one such external libraries I am using. I get the same error for other libraries too. So basically it crashes when trying to use any external libraries. Any idea what could be the problem?
EDIT:
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.**.**"> <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /> <application android:name="io.flutter.app.FlutterApplication" android:icon="@mipmap/ic_launcher" android:label="**" android:requestLegacyExternalStorage="true"> <activity android:name=".IncomingCallActivity" android:screenOrientation="portrait" android:theme="@style/NoActionBar" /> <activity android:name=".TokboxActivity" android:label="Incoming Call" android:configChanges="orientation|screenSize|smallestScreenSize|screenLayout" android:supportsPictureInPicture="true" android:theme="@style/NoActionBar" /> <activity android:name=".MainActivity" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" android:hardwareAccelerated="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme" android:windowSoftInputMode="adjustResize"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> <meta-data android:name="flutterEmbedding" android:value="2" /> <service android:name=".MessagingService" android:exported="false"> <intent-filter> <action android:name="com.google.firebase.MESSAGING_EVENT" /> </intent-filter> </service> </application> </manifest>