无法使用Android Studio构建Flutter应用

8e2ybdfx  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(200)

我尝试使用Android Studio构建Flutter应用程序,但出现了一个错误消息。

> Task :app:compileFlutterBuildDevelopmentRelease
lib/main.dart: Error: Error when reading 'lib/main.dart': The system cannot find the file specified.

package:my_app/main.dart: Error: No 'main' method found.
Try adding a method named 'main' to your program.
Target kernel_snapshot failed: Exception

在这一步,我在Android Studio中单击build按钮来构建项目。我知道build.gradle中有一个特定的配置来指示入口点,这有助于Android Studio构建项目。请帮我编写运行项目的配置
项目中的build.gradle文件

android {
    namespace "com.my.app"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {

        applicationId "com.my.app"
       
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        if (System.getenv("ANDROID_KEYSTORE_PATH")) {
            release {
                storeFile file(System.getenv("ANDROID_KEYSTORE_PATH"))
                keyAlias System.getenv("ANDROID_KEYSTORE_ALIAS")
                keyPassword System.getenv("ANDROID_KEYSTORE_PRIVATE_KEY_PASSWORD")
                storePassword System.getenv("ANDROID_KEYSTORE_PASSWORD")
            }
        } else {
            release {
                keyAlias keystoreProperties['keyAlias']
                keyPassword keystoreProperties['keyPassword']
                storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
                storePassword keystoreProperties['storePassword']
            }
        }
    }

    flavorDimensions "default"
    productFlavors {
        production {
            dimension "default"
            applicationIdSuffix ""
            manifestPlaceholders = [
                    appName               : "App Name",
                    'flutterMainDartEntry': 'lib/main_production.dart'
            ]
        }
        staging {
            dimension "default"
            applicationIdSuffix ".stg"
            manifestPlaceholders = [
                    appName               : "[STG] App Name",
                    'flutterMainDartEntry': 'lib/main_staging.dart',
            ]
        }
        development {
            dimension "default"
            applicationIdSuffix ".dev"
            manifestPlaceholders = [
                    appName               : "[DEV] App Name",
                    'flutterMainDartEntry': 'lib/main_development.dart',
            ]
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt')
        }
        debug {
            signingConfig signingConfigs.debug
        }
    }
}
rjee0c15

rjee0c151#

如果你的flutter编译从main.dart文件开始,那么它将在android studio上工作。
或者您没有更改主.dart文件。所以你必须在Android Studio同步Gradle文件。因此,同步Gradle并等待同步完成。如果同步失败,那么你将无法在android studio上运行flutter项目。

相关问题