ponnamkarthik / FlutterToast

Toast Plugin for Flutter
MIT License
1.44k stars 354 forks source link

Compilation Error with FlutterToast #465

Open calcitem opened 11 months ago

calcitem commented 11 months ago

I am experiencing a compilation error with the Github Action, the error message is as follows:

/home/runner/.gradle/.tmp/gradle_download9382158347681222790bin
[+9398 ms] Compiling build file '/opt/hostedtoolcache/flutter/stable-3.13.6-x64/.pub-cache/hosted/pub.dev/fluttertoast-8.2.2/android/build.gradle' using BuildScriptTransformer.
[  +99 ms] Using default execution profile
[        ] Using Kotlin Gradle Plugin gradle76 variant
[   +1 ms] FAILURE: Build failed with an exception.
[ +101 ms] Could not execute [class org.jetbrains.kotlin.gradle.plugin.statistics.KotlinBuildStatHandler.buildFinished]
[   +1 ms] * What went wrong:
[        ] A problem occurred configuring project ':fluttertoast'.
[        ] > Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
[        ]    > Namespace not specified. Specify a namespace in the module's build file. See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.
[        ]      If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

The complete log of the compilation can be found at: https://github.com/calcitem/Sanmill/actions/runs/6397029084/job/17364177927

Interestingly, the pull request here https://github.com/ponnamkarthik/FlutterToast/pull/443/commits/c7e27bbf1d131f43dc3095ce42160cd05397c515 was already merged.

These three lines were added:

if (project.android.hasProperty('namespace')) {
    namespace 'io.github.ponnamkarthik.toast.fluttertoast'
}

However, it appears that your namespace block needs to be moved inside the defaultConfig block. Please refer to the code structure below:

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
    compileSdkVersion 31

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }
    defaultConfig {
        minSdkVersion 16
        if (project.android.hasProperty('namespace')) {
            namespace 'io.github.ponnamkarthik.toast.fluttertoast'
        }
    }
    lintOptions {
        disable 'InvalidPackage'
    }
}

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

In the above code, the namespace block has been moved inside the defaultConfig block. This should correctly set the namespace for your project.

The aforementioned solution is conjectural and remains unverified. The provisional approach I'm presently utilizing is detailed here:

https://github.com/calcitem/FlutterToast/commit/5a1d491871a79eebf057c7bae410af9620a0b9ed

Though this alternative allows for successful compilation, it might not align with your initial objective of maintaining compatibility with older versions of the Android Gradle Plugin (AGP).

ginnyhuang commented 5 months ago

Hi, I face the same issue with compileSdkVersion 34 and gradle version 8.1.1, and it let my build failed. It is neccessary to use SDK 34, and it's neccessary with gradle 8.0 above. With gradle 8.0 above, the namespace in modules that app use is necessary. So I have add below code in android/build.gradle:

subprojects{
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            project.android {
                if (namespace == null) {
                    namespace project.group
                }
            }
        }
    }
}

And it solve in other library, expect for fluttertoast.

The error:

Incorrect package="io.github.ponnamkarthik.toast.fluttertoast" found in source AndroidManifest.xml: C:\Users\WishMobile\AppData\Local\Pub\Cache\hosted\pub.dev\fluttertoast-8.2.2\android\src\main\AndroidManifest.xml.
Setting the namespace via the package attribute in the source AndroidManifest.xml is no longer supported.
Recommendation: remove package="io.github.ponnamkarthik.toast.fluttertoast" from the source AndroidManifest.xml: C:\Users\WishMobile\AppData\Local\Pub\Cache\hosted\pub.dev\fluttertoast-8.2.2\android\src\main\AndroidManifest.xml.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':fluttertoast:processDebugManifest'.
> A failure occurred while executing com.android.build.gradle.tasks.ProcessLibraryManifest$ProcessLibWorkAction
   > Incorrect package="io.github.ponnamkarthik.toast.fluttertoast" found in source AndroidManifest.xml: C:\Users\WishMobile\AppData\Local\Pub\Cache\hosted\pub.dev\fluttertoast-8.2.2\android\src\main\AndroidManifest.xml.
     Setting the namespace via the package attribute in the source AndroidManifest.xml is no longer supported.
     Recommendation: remove package="io.github.ponnamkarthik.toast.fluttertoast" from the source AndroidManifest.xml: C:\Users\WishMobile\AppData\Local\Pub\Cache\hosted\pub.dev\fluttertoast-8.2.2\android\src\main\AndroidManifest.xml.

I've try the below code, but it still shows the same error message.

                if (namespace == null) {
                    if (project.group == "io.github.ponnamkarthik.toast.fluttertoast") {
                        namespace "kotlin.io.github.ponnamkarthik.toast.fluttertoast"
                    } else {
                        namespace project.group
                    }
                }

How can I fix it? Many thanks ! :)

ginnyhuang commented 5 months ago

OK, I found a temporary solution to pass the issue. I print the project.group and find it shows "com.example.FlutterToast". So I just change the code as below and it works.

if (namespace == null) {
    if (project.group == "com.example.FlutterToast") {
        namespace "io.github.ponnamkarthik.toast.fluttertoast"
    } else {
        namespace project.group
    }
}