flutter_inapp_purchase icon indicating copy to clipboard operation
flutter_inapp_purchase copied to clipboard

cannot build in Flutter 3.29.0. error "FlutterInappPurchasePlugin.kt:10:48 Unresolved reference: Registrar"

Open hatano0x06 opened this issue 1 year ago • 2 comments

Version of flutter_inapp_purchase flutter_inapp_purchase: ^5.6.1

Platforms you faced the error (IOS or Android or both?) Android

Actual behavior

e: file:///C:/Users/81708/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_inapp_purchase-5.6.1/android/src/main/kotlin/com/dooboolab/flutterinapppurchase/FlutterInappPurchasePlugin.kt:10:48 Unresolved reference: Registrar
e: file:///C:/Users/81708/AppData/Local/Pub/Cache/hosted/pub.dev/flutter_inapp_purchase-5.6.1/android/src/main/kotlin/com/dooboolab/flutterinapppurchase/FlutterInappPurchasePlugin.kt:100:37 Unresolved reference: Registrar

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':flutter_inapp_purchase:compileReleaseKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* 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.

flutter doctor -v

[√] Flutter (Channel stable, 3.29.0, on Microsoft Windows [Version 10.0.26100.3194], locale ja-JP) [653ms]
    • Flutter version 3.29.0 on channel stable at C:\Users\81708\Desktop\develop\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 35c388afb5 (8 days ago), 2025-02-10 12:48:41 -0800
    • Engine revision f73bfc4522
    • Dart version 3.7.0
    • DevTools version 2.42.2

[√] Windows Version (11 Pro 64-bit, 24H2, 2009) [3.1s]

[√] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [7.9s]
    • Android SDK at C:\Users\81708\AppData\Local\Android\sdk
    • Platform android-35, build-tools 34.0.0
    • Java binary at: C:\Program Files\Android\Android Studio3\jbr\bin\java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.3+-12282718-b509.11)
    • All Android licenses accepted.

[√] Chrome - develop for the web [329ms]
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop Windows apps (Visual Studio Build Tools 2022 17.13.0) [328ms]
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools
    • Visual Studio Build Tools 2022 version 17.13.35806.99
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop
      development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK

[!] Android Studio (version 2021.3) [24ms]
    • Android Studio at C:\Program Files\Android\Android Studio1
    • 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
    X Unable to determine bundled Java version.
    • Try updating or re-installing Android Studio.

[!] Android Studio (version 2024.1) [22ms]
    • Android Studio at C:\Program Files\Android\Android Studio2
    • 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
    X Unable to determine bundled Java version.
    • Try updating or re-installing Android Studio.

[√] Android Studio (version 2024.2) [21ms]
    • Android Studio at C:\Program Files\Android\Android Studio3
    • 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 21.0.3+-12282718-b509.11)

[√] VS Code (version 1.97.2) [21ms]
    • VS Code at C:\Users\81708\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.104.0

[√] Connected device (4 available) [399ms]
    • Android SDK built for x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Windows (desktop)                  • windows       • windows-x64    • Microsoft Windows [Version 10.0.26100.3194]
    • Chrome (web)                       • chrome        • web-javascript • Google Chrome 132.0.6834.197
    • Edge (web)                         • edge          • web-javascript • Microsoft Edge 133.0.3065.69

[√] Network resources [443ms]
    • All expected network resources are available.

! Doctor found issues in 3 categories.

hatano0x06 avatar Feb 18 '25 09:02 hatano0x06

This plugin doesn´t work any more with the latest version of flutter. I rewrite the code, becouse migrate to other plugins will be more complicated. Change this, and the issue will finish:

Replace the content of FlutterInappPurchasePlugin with this:

package com.dooboolab.flutterinapppurchase

import io.flutter.embedding.engine.plugins.FlutterPlugin import io.flutter.embedding.engine.plugins.activity.ActivityAware import android.content.Context import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel.MethodCallHandler import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding import io.flutter.plugin.common.BinaryMessenger import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding //import io.flutter.plugin.common.PluginRegistry.Registrar import android.content.pm.PackageManager.NameNotFoundException import io.flutter.plugin.common.MethodCall

/** FlutterInappPurchasePlugin */ class FlutterInappPurchasePlugin : FlutterPlugin, ActivityAware, MethodCallHandler { private var androidInappPurchasePlugin: AndroidInappPurchasePlugin? = null private var amazonInappPurchasePlugin: AmazonInappPurchasePlugin? = null private var channel: MethodChannel? = null private lateinit var messenger: BinaryMessenger //var context: Context? = null private lateinit var context: Context

override fun onAttachedToEngine(flutterPluginBinding: FlutterPluginBinding) {
    context     = flutterPluginBinding.applicationContext
    messenger   = flutterPluginBinding.binaryMessenger //ADD FOR LISTENER
    isAndroid   = isPackageInstalled(context, "com.android.vending")
    isAmazon    = isPackageInstalled(context, "com.amazon.venezia")

    // In the case of an amazon device which has been side loaded with the Google Play store,
    // we should use the store the app was installed from.
    if (isAmazon && isAndroid) {
        if (isAppInstalledFrom(context, "amazon")) {
            isAndroid = false
        } else {
            isAmazon = false
        }
    }
    channel = MethodChannel(messenger!!, "flutter_inapp")
    if (isAndroid) {
        androidInappPurchasePlugin = AndroidInappPurchasePlugin()
        androidInappPurchasePlugin!!.setContext(context)
        androidInappPurchasePlugin!!.setChannel(channel)
        channel!!.setMethodCallHandler(androidInappPurchasePlugin)
    } else if (isAmazon) {
        amazonInappPurchasePlugin = AmazonInappPurchasePlugin()
        amazonInappPurchasePlugin!!.setContext(context)
        amazonInappPurchasePlugin!!.setChannel(channel)
        channel!!.setMethodCallHandler(amazonInappPurchasePlugin)
    }
}

override fun onDetachedFromEngine(binding: FlutterPluginBinding) {
    channel!!.setMethodCallHandler(null)
    channel = null
    if (isAndroid) {
        androidInappPurchasePlugin!!.setChannel(null)
    } else if (isAmazon) {
        amazonInappPurchasePlugin!!.setChannel(null)
    }
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
    if (isAndroid) {
        androidInappPurchasePlugin!!.setActivity(binding.activity)
    } else if (isAmazon) {
        amazonInappPurchasePlugin!!.setActivity(binding.activity)
    }
}

override fun onDetachedFromActivity() {
    if (isAndroid) {
        androidInappPurchasePlugin!!.setActivity(null)
        androidInappPurchasePlugin!!.onDetachedFromActivity()
    } else if (isAmazon) {
        amazonInappPurchasePlugin!!.setActivity(null)
    }
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
    onAttachedToActivity(binding)
}

override fun onDetachedFromActivityForConfigChanges() {
    onDetachedFromActivity()
}

private fun setAndroidInappPurchasePlugin(androidInappPurchasePlugin: AndroidInappPurchasePlugin) {
    this.androidInappPurchasePlugin = androidInappPurchasePlugin
}

private fun setAmazonInappPurchasePlugin(amazonInappPurchasePlugin: AmazonInappPurchasePlugin) {
    this.amazonInappPurchasePlugin = amazonInappPurchasePlugin
}

companion object {
    private var isAndroid = false
    private var isAmazon = false

    fun getStore(): String {
       return if (!isAndroid && !isAmazon) "none" else if (isAndroid) "play_store" else "amazon"
    }

    /*fun registerWith(registrar: Registrar) {
        val instance = FlutterInappPurchasePlugin()
        instance.onAttached(registrar.context(), registrar.messenger())
    }*/

    private fun isPackageInstalled(ctx: Context, packageName: String): Boolean {
        return try {
            ctx.packageManager.getPackageInfo(packageName, 0)
            true
        } catch (e: NameNotFoundException) {
            false
        }
    }

    fun isAppInstalledFrom(ctx: Context, installer: String?): Boolean {
        val installerPackageName = ctx.packageManager.getInstallerPackageName(ctx.packageName)
        return installer != null && installerPackageName != null && installerPackageName.contains(
            installer
        )
    }
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
    // NO NEED TO BE IMPLEMENTED...
    TODO("Not yet implemented")
}

}

willicom2000 avatar Feb 19 '25 17:02 willicom2000

This is the fix: https://github.com/hyochan/flutter_inapp_purchase/pull/497

moodstubos avatar Feb 19 '25 18:02 moodstubos

Closes since 6.0.0.rc-x has been released!

hyochan avatar Aug 04 '25 03:08 hyochan