我目前正在使用 Kotlin 开发 React Native 项目,在 Android Studio 的构建过程中遇到了一些问题。具体来说,我在 Kotlin 文件中遇到了未解决的参考:R 和未解决的参考:BuildConfig 错误。
React Native 版本:0.74.3 Kotlin 版本:1.9.23 摇篮版本:8.6 Android Studio 版本:最新
我的MainActivity.kt
package com.thediamlaundry.diamapp
import android.os.Build
import android.os.Bundle
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import expo.modules.ReactActivityDelegateWrapper
class MainActivity : ReactActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
// Set the theme to AppTheme BEFORE onCreate to support
// coloring the background, status bar, and navigation bar.
// This is required for expo-splash-screen.
setTheme(R.style.AppTheme);
super.onCreate(null)
}
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "main"
/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate {
return ReactActivityDelegateWrapper(
this,
BuildConfig.IS_NEW_ARCHITECTURE_ENABLED,
object : DefaultReactActivityDelegate(
this,
mainComponentName,
fabricEnabled
){})
}
/**
* Align the back button behavior with Android S
* where moving root activities to background instead of finishing activities.
* @see <a href="https://developer.android.com/reference/android/app/Activity#onBackPressed()">onBackPressed</a>
*/
override fun invokeDefaultOnBackPressed() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) {
if (!moveTaskToBack(false)) {
// For non-root activities, use the default implementation to finish them.
super.invokeDefaultOnBackPressed()
}
return
}
// Use the default back button implementation on Android S
// because it's doing more than [Activity.moveTaskToBack] in fact.
super.invokeDefaultOnBackPressed()
}
}
我的MainApplication.kt
package com.thediamlaundry.diamapp
import android.app.Application
import android.content.res.Configuration
import com.facebook.react.PackageList
import com.facebook.react.ReactApplication
import com.facebook.react.ReactNativeHost
import com.facebook.react.ReactPackage
import com.facebook.react.ReactHost
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
import com.facebook.react.defaults.DefaultReactNativeHost
import com.facebook.soloader.SoLoader
import expo.modules.ApplicationLifecycleDispatcher
import expo.modules.ReactNativeHostWrapper
class MainApplication : Application(), ReactApplication {
override val reactNativeHost: ReactNativeHost = ReactNativeHostWrapper(
this,
object : DefaultReactNativeHost(this) {
override fun getPackages(): List<ReactPackage> {
// Packages that cannot be autolinked yet can be added manually here, for example:
// packages.add(new MyReactNativePackage());
return PackageList(this).packages
}
override fun getJSMainModuleName(): String = ".expo/.virtual-metro-entry"
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() = ReactNativeHostWrapper.createReactHost(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()
}
ApplicationLifecycleDispatcher.onApplicationCreate(this)
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
ApplicationLifecycleDispatcher.onConfigurationChanged(this, newConfig)
}
}
在从 android studio 构建 AAB 时,因为我已经使用了博览会的免费套餐。
I got this error > Task :app:compileReleaseKotlin FAILED
e: file:///Users/pratikbhattarai/Documents/GitHub/diamFrontend/diamApp/android/app/src/main/java/com/thediamlaundry/diamapp/MainActivity.kt:18:14 Unresolved reference: R
e: file:///Users/pratikbhattarai/Documents/GitHub/diamFrontend/diamApp/android/app/src/main/java/com/thediamlaundry/diamapp/MainActivity.kt:35:11 Unresolved reference: BuildConfig
e: file:///Users/pratikbhattarai/Documents/GitHub/diamFrontend/diamApp/android/app/src/main/java/com/thediamlaundry/diamapp/MainApplication.kt:31:60 Unresolved reference: BuildConfig
e: file:///Users/pratikbhattarai/Documents/GitHub/diamFrontend/diamApp/android/app/src/main/java/com/thediamlaundry/diamapp/MainApplication.kt:33:52 Unresolved reference: BuildConfig
e: file:///Users/pratikbhattarai/Documents/GitHub/diamFrontend/diamApp/android/app/src/main/java/com/thediamlaundry/diamapp/MainApplication.kt:34:51 Unresolved reference: BuildConfig
e: file:///Users/pratikbhattarai/Documents/GitHub/diamFrontend/diamApp/android/app/src/main/java/com/thediamlaundry/diamapp/MainApplication.kt:44:9 Unresolved reference: BuildConfig
我已经尝试过 gradle 同步。已使用 ./gradlew clean 命令。它让我构建成功,但是当我尝试构建项目时,它给了我这个错误。我已经检查了所有依赖项,并认为一切都是正确且兼容的。但在构建时我收到此错误
我只是通过添加我的命名空间=“com.thediamlaundry.diamapp”来解决我的问题,它只是命名空间=“com.diamapp”。在应用程序级别 build.gradle 中添加此内容后,我的问题得到了解决