繁体   English   中英

尝试 Flutter hello world 代码时出现“无法应用插件 [id 'kotlin-android']”

[英]Getting “Failed to apply plugin [id 'kotlin-android']” when trying Flutter hello world code

我正在尝试 flutter 的 hello world 代码。 从这里关注官方文档。

Flutter 版本:flutter_linux_1.20.1-stable

我创建了 flutter 项目并点击运行:

flutter create myapp
cd myapp
flutter run

但它抛出了这个错误信息:

Launching lib/main.dart on R40 in debug mode...
                                                                        
FAILURE: Build failed with an exception.                                
                                                                        
* Where:                                                                
Build file '/home/sjs/flutter-projects/myapp/android/app/build.gradle' line: 25
                                                                        
* What went wrong:                                                      
A problem occurred evaluating project ':app'.                           
> Failed to apply plugin [id 'kotlin-android']                          
   > Could not create an instance of type org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension.
      > Could not generate a decorated class for class org.jetbrains.kotlin.gradle.dsl.KotlinAndroidProjectExtension.
         > org/jetbrains/kotlin/gradle/plugin/KotlinTarget              
                                                                        
* Try:                                                                  
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
                                                                        
* Get more help at https://help.gradle.org                              
                                                                        
BUILD FAILED in 18s                                                     
Running Gradle task 'assembleDebug'...                                  
Running Gradle task 'assembleDebug'... Done                        22.1s
Exception: Gradle task assembleDebug failed with exit code 1

这是位于“/home/sjs/flutter-projects/myapp/android/app/”中的 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.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

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

android {
    compileSdkVersion 28

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

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.myapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

这是flutter doctor -v output:

[✓] Flutter (Channel stable, 1.20.1, on Linux, locale en_US.UTF-8)
    • Flutter version 1.20.1 at /home/sjs/flutter
    • Framework revision 2ae34518b8 (2 days ago), 2020-08-05 19:53:19 -0700
    • Engine revision c8e3b94853
    • Dart version 2.9.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
    • Android SDK at /home/sjs/Android/Sdk
    • Platform android-30, build-tools 30.0.1
    • Java binary at: /home/sjs/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Android Studio (version 4.0)
    • Android Studio at /home/sjs/android-studio
    • Flutter plugin version 48.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)

[✓] Connected device (1 available)
    • R40 (mobile) • 00114604032517 • android-arm • Android 9 (API 28)

• No issues found!

好的,在这里找到解决方案: https://github.com/react-native-community/react-native-webview/issues/1407#issuecomment-634436481

我不得不编辑<project-folder>/android/app/build.gradle文件。 我已经更换了线路

ext.kotlin_version = '1.3.50'

有了这个

ext.kotlin_version = '1.3.72'

然后运行flutter run

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM