Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flutter 3.7.12 with native plugin fails with AGP version 8.0+ #125181

Closed
2 tasks done
maxlapides opened this issue Apr 20, 2023 · 28 comments
Closed
2 tasks done

Flutter 3.7.12 with native plugin fails with AGP version 8.0+ #125181

maxlapides opened this issue Apr 20, 2023 · 28 comments
Assignees
Labels
a: plugins Support for writing, building, and running plugin packages f: integration_test The flutter/packages/integration_test plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels.

Comments

@maxlapides
Copy link

maxlapides commented Apr 20, 2023

Is there an existing issue for this?

Steps to reproduce

  1. Create a new Flutter Application (empty)
  2. Use Android Studio's "AGP Upgrade Assistant" to upgrade to Gradle 8
  3. flutter pub add device_info_plus (or any other package that has a native component like mobile_scanner)
  4. Gradle sync

Expected results

Gradle sync success

Actual results

Gradle sync failure:

Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

android {
    namespace 'com.example.namespace'
}

If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

Code sample

Code sample
name: flutter_application_1
description: A new Flutter project.
publish_to: 'none'
version: 0.1.0

environment:
  sdk: '>=2.19.6 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  flutter_timezone: ^1.0.4

dev_dependencies:
  flutter_test:
    sdk: flutter
  flutter_lints: ^2.0.0

flutter:
  uses-material-design: true

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.7.12, on macOS 13.3.1 22E261 darwin-arm64, locale en-US)
    • Flutter version 3.7.12 on channel stable at /Users/maxlapides/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (2 days ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/maxlapides/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • ANDROID_HOME = /Users/maxlapides/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E222b
    • CocoaPods version 1.12.0

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.77.3)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.62.0

[✓] Connected device (4 available)
    • sdk gphone64 arm64 (mobile) • emulator-5554                        • android-arm64  • Android 13 (API 33) (emulator)
    • iPhone 14 Pro Max (mobile)  • 988C1D17-7F2A-48E1-A3D3-3F77767A91D1 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-4 (simulator)
    • macOS (desktop)             • macos                                • darwin-arm64   • macOS 13.3.1 22E261 darwin-arm64
    • Chrome (web)                • chrome                               • web-javascript • Google Chrome 112.0.5615.121

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
@huycozy huycozy added the in triage Presently being triaged by the triage team label Apr 20, 2023
@Shakle

This comment was marked as off-topic.

@ellet0

This comment was marked as off-topic.

@vbuberen
Copy link

In the error there is a clear mention that now you need to add namespace in android section.

More info in the official docs:
https://developer.android.com/build/releases/gradle-plugin#namespace-dsl
https://developer.android.com/build/configure-app-module#set-namespace

@MacLeeIn
Copy link

MacLeeIn commented Apr 20, 2023

Version 3.7.12 working perfectly for me. AGP version 7.4.2 and Gradle 8.0

Put your namespace on android/app/build.gradle file android tag

https://developer.android.com/build/configure-app-module

@huycozy
Copy link
Member

huycozy commented Apr 20, 2023

Thanks for the report! I also can see this issue when adding device_info_plus plugin. Using Android Studio Flamingo | 2022.2.1. But after adding namespace as the suggestion above, Gradle can sync successfully.

For anyone facing this issue, please try it to see if it works.

@huycozy huycozy added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Apr 20, 2023
@Shakle
Copy link

Shakle commented Apr 20, 2023

Version 3.7.12 working perfectly for me. AGP version 7.4.2 and Gradle 8.0

Put your namespace on android/app/build.gradle file android tag

https://developer.android.com/build/configure-app-module

You don't have dependencies which use native code

@ellet0
Copy link
Contributor

ellet0 commented Apr 20, 2023

Version 3.7.12 working perfectly for me. AGP version 7.4.2 and Gradle 8.0

Put your namespace on android/app/build.gradle file android tag

https://developer.android.com/build/configure-app-module

Hi, this namespace need to be added on all build.gradle of the plugins

@Shakle
Copy link

Shakle commented Apr 20, 2023

juliansteenbakker/mobile_scanner#602

mobile scanner will soon be updated

@stuartmorgan stuartmorgan added platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels. package flutter/packages repository. See also p: labels. a: plugins Support for writing, building, and running plugin packages P1 High-priority issues at the top of the work list f: integration_test The flutter/packages/integration_test plugin and removed waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds in triage Presently being triaged by the triage team labels Apr 20, 2023
@stuartmorgan
Copy link
Contributor

stuartmorgan commented Apr 20, 2023

I tested one of our plugins with the namespace added but the package left in AndroidManifest.xml, and built it with an example app that was set to AGP 4.2/Gradle 6.7.1 (which predates namespace support according to the docs), as well as AGP 8.0/Gradle 8.0.0, and both worked.

So it appears that old versions of AGP are fine with namespace existing, and new versions are fine with the duplicate entry in AndroidManfest.xml, in which case we can easily support AGP 8+ without breaking support for AGP <7, which means this should be straightforward for plugin authors to fix.

My testing was incorrect, see my comments below. It's true for 4.2, but not 4.1.

On the Flutter side, we'll need to:

  • Update all of the plugins in flutter/packages to include the namespace.
  • Update the integration_test plugin in flutter/flutter to include the namespace.
    • Ideally we would cherry-pick this to beta and/or stable; since it's used as an SDK-sourced package, apps that depend on integration_test will be blocked from updating to 8.0 until they are using a Flutter version that has the addition.
  • Update the plugin template in flutter/flutter to include the namespace.

Optional changes:

  • Add tooling support to catch this error and add a Flutter error message telling people to report the issue to the author of the relevant plugins.
  • Add the namespace to the app template in flutter/flutter (we won't need to do that until we update the AGP version in the template).

@reidbaker reidbaker changed the title Flutter 3.7.12 with native plugin fails with Gradle 8 Flutter 3.7.12 with native plugin fails with AGP version 7.3+ Apr 20, 2023
@reidbaker
Copy link
Contributor

Changed the title since the issue is more closely related to AGP than it is to gradle.

@stuartmorgan stuartmorgan changed the title Flutter 3.7.12 with native plugin fails with AGP version 7.3+ Flutter 3.7.12 with native plugin fails with AGP version 8.0+ Apr 20, 2023
@stuartmorgan
Copy link
Contributor

Adjusting title; the breaking change is in AGP 8.0

itsjustkevin pushed a commit that referenced this issue May 1, 2023
- Smallest change required to support host apps using agp 8.0 (#125657)

Original issue #125181
Cherry pick issue #125688

*If you had to change anything in the [flutter/tests] repo, include a
link to the migration guide as per the [breaking change policy].*

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [ ] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.
auto-submit bot pushed a commit that referenced this issue May 8, 2023
- Update Gradle/AGP version and add namespace plus dependencies. 

#125181
@reidbaker
Copy link
Contributor

Final State:

  • AGP 8.0 requires the entire plugin ecosystem to add namespace to their build.gradle files.
  • This comment provides a way for plugin authors to provide a backwards compatible namespace declaration.
  • The root of the issue for AGP 8.0 adoption is based on plugins not based on flutter. The exception to that is if your app uses integration_test which is published as part of flutter. To use AGP 8.0 and run integration_tests you must update to flutter 3.10.

Closing issue.

stuartmorgan added a commit to stuartmorgan/flutter that referenced this issue May 9, 2023
Adds the `namespace` property necessary for AGP 8 compatibility to the
plugin templates, with the conditional logic to ensure that it doesn't
break AGP <4.2, so that new plugins will be maximally compatible.

Part of flutter#125181
auto-submit bot pushed a commit that referenced this issue May 12, 2023
Adds the `namespace` property necessary for AGP 8 compatibility to the plugin templates, with the conditional logic to ensure that it doesn't break AGP <4.2, so that new plugins will be maximally compatible.

Part of #125181
@EphraimNetWorks
Copy link

EphraimNetWorks commented May 19, 2023

You can also add this under the subprojects block in your root build.gradle (android/build.gradle). It checks if namespace in plugin hasn't been set and sets it using package value from it's AndroidManifest.xml

subprojects {
    //other config
    afterEvaluate {
        // check if `android` block is available and namespace isn't set
        if(it.hasProperty('android') && it.android.namespace == null){
            def manifest = new XmlSlurper().parse(file(it.android.sourceSets.main.manifest.srcFile))
            def packageName = manifest.@package.text()
            print("Setting ${packageName} as android namespace")
            android.namespace= packageName
        }
    }
}

There is also the possibility of running into kotlin jvm compatibility issues for same plugins which are yet to be migrated. Setting kotlin.jvm.target.validation.mode=warning in your gradle.properties file solves the error. Read more about it here https://kotlinlang.org/docs/gradle-configure-project.html#check-for-jvm-target-compatibility-of-related-compile-tasks.

For my case affected packages(package_info_plus inclusive like in your case) are working fine. You might want keep track of these packages so when they are fully migrated you can undo these changes.

@ellet0
Copy link
Contributor

ellet0 commented May 19, 2023

It's simple, you don't even need to edit flutter android plugin (android native code)
edit your build.gradle (project module) to the following:

subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
    afterEvaluate {
        // check if android block is available

        if (it.hasProperty('android')) {

            if (it.android.namespace == null) {
                def manifest = new XmlSlurper().parse(file(it.android.sourceSets.main.manifest.srcFile))
                def packageName = manifest.@package.text()
                println("Setting ${packageName} as android namespace")
                android.namespace = packageName
            }

            def javaVersion = JavaVersion.VERSION_17
            android {
                def androidApiVersion = 33
                compileSdkVersion androidApiVersion
                defaultConfig {
                    targetSdkVersion androidApiVersion
                }
                compileOptions {
                    sourceCompatibility javaVersion
                    targetCompatibility javaVersion
                }
                tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).configureEach {
                    kotlinOptions {
                        jvmTarget = javaVersion.toString()
                    }
                }
                println("Setting java version to ${javaVersion.toString()} which is $javaVersion")
                println("Setting compileSdkVersion and targetSdkVersion to $androidApiVersion")
            }
        }

    }

}
subprojects {
    project.evaluationDependsOn(':app')
}

This will make sure all the sub projects have android namespace, if not it will read it from AndroidManifest.xml, and also set the compileSdkVersion to 33 (you need to change it manually to the latest in the future) and also target java version 17

CaseyHillers pushed a commit to CaseyHillers/flutter that referenced this issue May 24, 2023
Adds the `namespace` property necessary for AGP 8 compatibility to the plugin templates, with the conditional logic to ensure that it doesn't break AGP <4.2, so that new plugins will be maximally compatible.

Part of flutter#125181
@github-actions
Copy link

github-actions bot commented Jun 2, 2023

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: plugins Support for writing, building, and running plugin packages f: integration_test The flutter/packages/integration_test plugin P1 High-priority issues at the top of the work list package flutter/packages repository. See also p: labels. platform-android Android applications specifically tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
Development

No branches or pull requests

10 participants