Flutter应用在Android上启动时崩溃

时间:2018-07-18 16:55:02

标签: android flutter

我已经在iOS上测试了我的第一个Flutter应用,并且运行良好。现在,我第一次在Android模拟器中运行它,然后收到此错误。

07-18 11:42:10.757 4264-4264/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.akamojo.liga, PID: 4264
    java.lang.RuntimeException: Unable to instantiate application com.akamojo.liga.liga: java.lang.ClassNotFoundException: Didn't find class "com.akamojo.liga.liga" on path: DexPathList[[zip file "/data/app/com.akamojo.liga-1/base.apk"],nativeLibraryDirectories=[/data/app/com.akamojo.liga-1/lib/x86, /data/app/com.akamojo.liga-1/base.apk!/lib/x86, /vendor/lib, /system/lib]]
        at android.app.LoadedApk.makeApplication(LoadedApk.java:578)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4680)
        at android.app.ActivityThread.-wrap1(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        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)

对我来说,这似乎很可疑,因为它正在寻找“ com.akamojo.liga.liga”类。这是对的吗?我的applicationId是“ com.akamojo.liga”。 Flutter框架是否正在创建一个称为“ liga”的起始类?

这是build.gradle。

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

def keystorePropertiesFile = rootProject.file("key.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

android {
    compileSdkVersion 27

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        applicationId "com.akamojo.liga"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

这是AndroidManifest.xml文件。

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akamojo.liga">

    <!-- The INTERNET permission is required for development. Specifically,
         flutter needs it to communicate with the running application
         to allow setting breakpoints, to provide hot reload, etc.
    -->
    <uses-permission android:name="android.permission.INTERNET"/>

    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <application
        android:name="liga"
        android:label="liga"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- This keeps the window background of the activity showing
                 until Flutter renders its first frame. It can be removed if
                 there is no splash screen (such as the default splash screen
                 defined in @style/LaunchTheme). -->
            <meta-data
                android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                android:value="true" />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

3 个答案:

答案 0 :(得分:0)

在您的mainActivity.kt文件中添加以下代码

override fun onCreate(savedInstanceState: Bundle?) {
  FlutterMain.startInitialization(this); //Added line
  super.onCreate(savedInstanceState)
  GeneratedPluginRegistrant.registerWith(this)
}

答案 1 :(得分:0)

我的应用正在运行,但是在更新Dart插件后,我开始收到此错误。

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo com.package.name.MainActivity

在我的情况下,这是因为Dart和Flutter插件不兼容。 我运行了flutter upgrade命令,应用再次开始正常运行。

因此请确保同时更新Dart和Flutter插件。

答案 2 :(得分:0)

首先, 文件>项目结构>选择Project SDK最新API平台,然后转到模块(依赖项),然后选择最新API平台,应用,确定。 第二, 在build-gradle => compileSdkVersion 28中,targetSdkVersion 28更改为30。 第三, 在build-gradle => GradleException中更改FileNotFoundException

就这样。