flutter / flutter/website

Android: Problem building Flutter project

Open
#11,753 0 comments 0 reactions 0 assignees View on GitHub
e2-days from.page-issue p2-medium
Dominant language
Dart
Stars
3.1k
Forks
3.5k
Avg merge
2d 4h
Merged PRs (30d)
95

Description

### Page URL

https://docs.flutter.dev/deployment/android/

### Page source

https://github.com/flutter/website/tree/main/src/content/deployment/android.md

### Describe the problem

I have created a new Flutter project (using the `flutter create` command), which by default uses the **gradle.kts** (Kotlin DSL) configuration and does not use Groovy. However, when building the APK with the command `flutter build apk`, I encounter the following error:

```
Plugin [id: 'com.android.application'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
```

### Environment and Setup:
- **Operating System:** Windows 11 Pro 64-bit
- **Flutter Version:** 3.29.0 (Stable)
- **Android SDK Version:** 35.0.1
- **Gradle Version:** 8.4 (as specified in `gradle-wrapper.properties`)
- **IDE:** Visual Studio Code (with occasional use of Android Studio)
- **Keystore:** Correctly configured via `key.properties`.

### Steps and Changes Taken:
1. **Plugin Setup:**
- In my `android/app/build.gradle.kts`, I have the following plugins block:
```kotlin
plugins {
id("com.android.application")
id("kotlin-android")
id("dev.flutter.flutter-gradle-plugin")
}
```
- Additionally, I tried managing the plugins via a `pluginManagement` block in my `settings.gradle.kts`, but the error persists.

2. **Changing Gradle and Plugin Versions:**
- I have tested several versions of Gradle, such as 8.2.0 and 8.1.0.
- I also tried specifying plugin versions directly in the `plugins` block (e.g., version 8.0.2 for the Android plugin and 1.8.20 for the Kotlin plugin), but the issue is still unresolved.

3. **Kotlin Plugin Warning:**
- I get a warning in the build output that the embedded Kotlin version (1.9.10) does not match the requested version (1.9.20). While this doesn't seem to directly cause the failure, it might indicate some configuration mismatch.

4. **Server Issue:**
- Although Flutter connects to a Chinese server (flutter_io.cn) during build, packages are downloaded correctly, so it doesn’t appear to be a server issue.

### Summary of the Problem:
Even though my project is created with **gradle.kts** by default, and I have not migrated or changed from Groovy, the main issue lies in not finding the `com.android.application` plugin during the build process. All typical fixes (setting plugins in the `settings` and `build` files, changing Gradle and plugin versions, checking Android SDK and other settings) have been tried, but the problem still persists.

Could you please guide me on whether there is a specific configuration required when using Flutter with Kotlin DSL, or if I’ve missed something in this configuration that might be causing the issue?

Thank you for your attention and assistance.

Best regards,
[Ramin ....

### Expected fix

**Expected Outcome:**
My goal is for the `flutter build apk` process to complete successfully without encountering errors, especially the error regarding the **`com.android.application` plugin not being found**. I would like the build to finish without any issues so I can generate the APK for my Flutter project.

### Additional context

PS D:\afghan> flutter build apk
Flutter assets will be downloaded from https://storage.flutter_io.cn/. Make sure you trust this source!

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\afghan\android\app\build.gradle.kts' line: 4

* What went wrong:
Plugin [id: 'com.android.application'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

* 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 13s
Running Gradle task 'assembleRelease'... 14.2s
Gradle task assembleRelease failed with exit code 1
PS D:\afghan> cd android
PS D:\afghan\android> .\gradlew clean
Starting a Gradle Daemon, 2 incompatible and 2 stopped Daemons could not be reused, use --status for details

> Configure project :gradle
WARNING: Unsupported Kotlin plugin version.
The `embedded-kotlin` and `kotlin-dsl` plugins rely on features of Kotlin `1.9.10` that might work differently than in the requested version `1.9.20`.

FAILURE: Build failed with an exception.

* Where:
Build file 'D:\afghan\android\app\build.gradle.kts' line: 4

* What went wrong:
Plugin [id: 'com.android.application'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)

* 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 9s
PS D:\afghan\android> flutter doctor
Flutter assets will be downloaded from https://storage.flutter_io.cn/. Make sure you trust this source!
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.26100.3194], locale en-US)
[√] Windows Version (11 Pro 64-bit, 24H2, 2009)
[√] Android toolchain - develop for Android devices (Android SDK version 35.0.1)
[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.12.4)
[√] Android Studio (version 2024.2)
[√] VS Code (version 1.97.2)
[√] Connected device (3 ava

//android/app/build.gradle.kts

import java.util.Properties
import java.io.FileInputStream

plugins {
id("com.android.application")
id("kotlin-android")
id("dev.flutter.flutter-gradle-plugin")
}

val keystoreProperties = Properties()
val keystorePropertiesFile = rootProject.file("key.properties")
if (keystorePropertiesFile.exists()) {
keystoreProperties.load(FileInputStream(keystorePropertiesFile))
}

android {
namespace = "com.example.my_now"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion

compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

kotlinOptions {
jvmTarget = JavaVersion.VERSION_11.toString()
}

sourceSets {
getByName("main") {
java.srcDirs("src/main/kotlin")
}
}

signingConfigs {
create("release") {
keyAlias = keystoreProperties["keyAlias"] as String?
keyPassword = keystoreProperties["keyPassword"] as String?
storeFile = keystoreProperties["storeFile"]?.let { file(it) }
storePassword = keystoreProperties["storePassword"] as String?
}
}

buildTypes {
getByName("release") {
signingConfig = signingConfigs.getByName("release")
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
getByName("debug") {
signingConfig = signingConfigs.getByName("debug")
}
}

defaultConfig {
applicationId = "com.example.my_now"
minSdk = 21
targetSdk = 34
versionCode = 1
versionName = "1.0"
}
}

flutter {
source = "../.."
}

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:2.0.20")
}

// settings.gradle.kts
pluginManagement {
val flutterSdkPath = run {
val properties = java.util.Properties()
file("local.properties").inputStream().use { properties.load(it) }
val flutterSdkPath = properties.getProperty("flutter.sdk")
require(flutterSdkPath != null) { "flutter.sdk not set in local.properties" }
flutterSdkPath
}

includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

repositories {
google()
mavenCentral()
gradlePluginPortal()
}
}

include(":app")

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://[services.gradle.org/distributions/gradle-8.4-bin.zip](http://services.gradle.org/distributions/gradle-8.4-bin.zip)

//key.properties

storePassword=#####
keyPassword=######
keyAlias=upload
storeFile=$env:android/upload-keystore.jks

### I would like to fix this problem.

- [ ] I will try and fix this problem on docs.flutter.dev.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.