Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter app built with build apk command fails to run platform specific code

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 from adb 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>
like image 529
Yahya Ayash Luqman Avatar asked May 29 '20 23:05

Yahya Ayash Luqman


People also ask

How can I convert my flutter app to APK?

The solution is easy. Just run flutter clean and run flutter build apk after that and it generates the updated app apk. If you want to install it directly just run flutter install after the build command.


1 Answers

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:

  1. Check if you are using proguard optimization and minifyEnabled true. If yes, try to set minifyEnabled false, else follow the second check
  2. Check if you are using implementation keyword when adding dependencies you your project. If yes, try to replace it with api

I hope this helps

like image 193
abd3llatif Avatar answered Sep 29 '22 14:09

abd3llatif