react-native-community / upgrade-support

A central community-backed place to request and give help when upgrading your app.
MIT License
251 stars 2 forks source link

MainApplication.kt: Unresolved reference: BuildConfig #263

Closed JeremyBradshaw7 closed 4 months ago

JeremyBradshaw7 commented 4 months ago

Environment

info Fetching system and libraries information...
System:
  OS: macOS 14.3
  CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
  Memory: 111.25 MB / 16.00 GB
  Shell:
    version: 3.2.57
    path: /bin/bash
Binaries:
  Node:
    version: 18.0.0
    path: /usr/local/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.yarn/bin/yarn
  npm:
    version: 8.6.0
    path: /usr/local/bin/npm
  Watchman:
    version: 2022.03.21.00
    path: /usr/local/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK:
    API Levels:
      - "23"
      - "28"
      - "29"
      - "30"
      - "31"
      - "32"
      - "33"
      - "34"
    Build Tools:
      - 23.0.1
      - 23.0.2
      - 25.0.0
      - 25.0.1
      - 25.0.2
      - 26.0.2
      - 26.0.3
      - 27.0.0
      - 27.0.3
      - 28.0.2
      - 28.0.3
      - 29.0.2
      - 29.0.3
      - 30.0.0
      - 30.0.1
      - 30.0.2
      - 30.0.3
      - 31.0.0
      - 32.0.0
      - 33.0.0
      - 33.0.1
      - 34.0.0
    System Images:
      - android-16 | Google APIs Intel x86 Atom
      - android-22 | Google APIs Intel x86 Atom
      - android-28 | Google APIs Intel x86 Atom
      - android-29 | Google APIs Intel x86 Atom
      - android-29 | Google APIs Intel x86 Atom_64
      - android-30 | Android TV Intel x86 Atom
      - android-33 | Intel x86_64 Atom
      - android-34 | Intel x86_64 Atom
    Android NDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11330709
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /Library/Java/JavaVirtualMachines/zulu-17.jdk/Contents/Home/bin/javac
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.73.5
    wanted: 0.73.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Things I’ve done to figure out my issue

Upgrading version

0.73.5

Description

Android build has these errors in ./gradlew build command:

> Task :app:compileDebugKotlin FAILED
e: file:///Users/jeremybradshaw/ccf/android/app/src/main/java/com/ccf/MainApplication.kt:41:58 Unresolved reference: BuildConfig
e: file:///Users/jeremybradshaw/ccf/android/app/src/main/java/com/ccf/MainApplication.kt:43:50 Unresolved reference: BuildConfig
e: file:///Users/jeremybradshaw/ccf/android/app/src/main/java/com/ccf/MainApplication.kt:44:49 Unresolved reference: BuildConfig
e: file:///Users/jeremybradshaw/ccf/android/app/src/main/java/com/ccf/MainApplication.kt:52:9 Unresolved reference: BuildConfig
e: file:///Users/jeremybradshaw/ccf/android/app/src/main/java/com/ccf/MainApplication.kt:57:41 Unresolved reference: BuildConfig

MainApplication.kt has the same references to BuildConfig as a brand new react native typescript project, which doesn't break in the same way, so this is very odd.

After some googling of the error I've tried adding this to my build.gradle, but it hasn't helped:

    buildFeatures {
      buildConfig = true
    }

My MainApplication.kt file:

package com.ccf;

import android.app.Application
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactHost
import com.facebook.react.ReactPackage
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
import com.facebook.react.defaults.DefaultReactNativeHost
// import com.facebook.react.flipper.ReactNativeFlipper
import com.facebook.soloader.SoLoader

import com.reactnativeultimateconfig.UltimateConfigModule  // react-native-ultimate-config
import org.devio.rn.splashscreen.SplashScreenReactPackage  // react-native-splash-screen

class MainApplication : Application(), ReactApplication {
  override val reactNativeHost: ReactNativeHost =
      object : DefaultReactNativeHost(this) {
        override fun getPackages(): List<ReactPackage> =
            PackageList(this).packages.apply {
              // Packages that cannot be autolinked yet can be added manually here, for example:
              // add(MyReactNativePackage())
            }

        override fun getJSMainModuleName(): String = "index"

        override fun getUseDeveloperSupport(): Boolean = BuildConfig.DEBUG

        override val isNewArchEnabled: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
        override val isHermesEnabled: Boolean = BuildConfig.IS_HERMES_ENABLED
      }
  override val reactHost: ReactHost
    get() = getDefaultReactHost(this.applicationContext, reactNativeHost)

  override fun onCreate() {
    super.onCreate()
    SoLoader.init(this, false)
    if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
      // If you opted-in for the New Architecture, we load the native entry point for this app.
      load()
    }
    // ReactNativeFlipper.initializeFlipper(this, reactNativeHost.reactInstanceManager)
    UltimateConfigModule.setBuildConfig(BuildConfig::class.java) // react-native-ultimate-config
  }
}

Been stuck on this a day - please help!

alejandrodotor8 commented 4 months ago

Same, and also => Unresolved reference: PackageList

JeremyBradshaw7 commented 4 months ago

For me it was a missing import in the MainApplication.kt file:

import com.ccf.BuildConfig

though that's not in the default project so doesn't show up on upgrade-helper. It's something to do with Android Gradle Plugin (AGP) 8 I think.

EDIT: See my later comment

iBotPeaches commented 4 months ago

I stumbled upon this with same issue. I thought it was odd I didn't have that BuildConfig import, but my machine and others worked fine with debug & release builds. It was only our CI pipeline having an issue.

I haven't yet added the BuildConfig import myself, but something isn't adding up here.

JeremyBradshaw7 commented 4 months ago

I revisited this and it turns out that the import in MainApplication.kt was a sticking plaster fix. The actual issue, as I found out later after hitting an application crash, was that I'd added the incorrect namespace string to my android/app/build.gradle file as part of the 0.73 upgrade (I used the application id rather than the package id, normally the same for most projects but not for ours). Correct this and the import in MainApplication.kt is no longer needed.

iBotPeaches commented 4 months ago

Thanks - we figured out our root issue as well. During the jump from 72 to 73 we changed to Kotlin .kt files. This meant our fastlane pipeline with https://github.com/joshdholtz/fastlane-plugin-rename_android_package, was no longer properly renaming packages.

puneetkansal04 commented 3 months ago

@thymikee @JeremyBradshaw7 @iBotPeaches not resolved for me i am also facing same issue

iBotPeaches commented 3 months ago

not resolved for me i am also facing same issue

There's a solid amount of context and details in this thread. Paired with your message of pinging 3 people with absolutely zero context I don't see how you expect this to go.

Marcos-Vinicius9 commented 3 months ago

Same problem. Nothing solved it for me

EDIT:

using android studio and following the following steps just resolved this issue for me.

Android Studio -> Build -> Clean Project -> rebuild project

doing this generated a buildConfig file in my com.myapp.com.BuildConfig and I just imported it.

belkhoujaons commented 2 months ago

I had the same problem and I fixed it when i found that the first line in MainActivity.kt is importing the wrong appname it was package com.myapp while it needs to be package com.{you_actual_app_name}