【发布时间】:2019-02-15 16:46:05
【问题描述】:
【问题讨论】:
-
您是否将项目转换为 androidx?对我来说,问题是在那之后发生的。
标签: android android-studio kotlin import android-gradle-plugin
【问题讨论】:
标签: android android-studio kotlin import android-gradle-plugin
检查“build.gradle(:app)”文件,
plugins {
id 'com.android.application'
id 'kotlin-android'
}
如果缺少kotlin扩展,添加kotlin-android-extensions如下图,点击“立即同步”
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
【讨论】:
你可以试试
或只需删除 apply plugin: 'kotlin-android-extensions' ,同步 gradle 插件,然后我再次添加它。
【讨论】:
这是一步一步的答案:
Gradle
Open Gradle Config
plugins 部分打开新源,然后添加:id 'kotlin-android-extensions'
sync
结果:现在可以导入kotlinx.android.synthetic.main.activity_main.*
【讨论】:
module gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
project gradle
buildscript{
ext.kotlin_version = '1.3.11'
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
【讨论】:
Google 现已弃用 Synthetics。尽量避免使用它们,因为它可能会导致您的应用出现空指针异常和意外行为。
阅读更多:
Migrate from Kotlin synthetics to Jetpack view binding 来自官方开发者网站。
【讨论】:
只需在顶部插件部分的 build.gradle(Module:YourProjectName.app) 中添加以下行:
plugins{
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
}
大部分前两行已经存在,只需要添加第三行并同步项目
【讨论】:
在build.gradle (:app),添加:
buildFeatures {
viewBinding true
}
在MainActivity:
private lateinit var binding: ActivityMainBinding
修改onCreate:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setListeners()
}
设置监听器:
/**
* Attaches listeners to all the views.
*/
private fun setListeners() {
val clickableViews: List<View> =
listOf(
binding.view1,
binding.view2,
// ...
)
for (item in clickableViews) {
item.setOnClickListener { ... }
}
}
【讨论】:
对我来说,它只是将apply plugin: 'kotlin-android-extensions' 添加到应用程序的 build.gradle,按同步 gradle 文件,我就能够获得 synthetics
【讨论】:
Kotlin Android 扩展已弃用。迁移到 Jetpack 视图绑定。见下文: https://developer.android.com/topic/libraries/view-binding/migration
【讨论】:
简单的朋友,你忘了星号。
import kotlinx.android.synthetic.main.activity_main.*
这只是发生在我身上。
【讨论】:
id 'kotlin-android-extensions'
id 'kotlin-android'
删除插件并添加其中两个。 id 'kotlin-android-extensions' id 'kotlin-android'
应该添加。重新启动项目。
所以我发现问题出在 gradle 插件中,那么您需要重新启动,重建您的项目。
【讨论】:
希望这个帮助...可能与从布局中获取视图的新方法有关
科特林:
打开 gradle app.module 并在里面添加这一行
安卓{
android{viewBinding.enabled = true
...
}
(然后同步)
覆盖 fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater) // 2.1
setContentView(binding.root) // 2.2 而不是 (R.layout.activity_main)
现在视图以这种方式调用
binding.btn1.setOnClickListener{...}
或
binding.txtviewTitle.text = "Welcome to the jungle" // or any R.string
注意:将 gradle module.app 与该行同步后,您会发现任何具有相同名称的活动+绑定
看2.1参考
【讨论】:
buildscript {
ext.kotlin_version = '1.3.72'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
【讨论】:
我以这种方式解决了我的问题: 在 build.gradle 和插件中添加 id 'kotlin-android-extensions' 几秒钟后,当我写下按钮的名称时,自动 导入 kotlinx.android.synthetic.main.activity_main.* 导入代码
【讨论】:
这为我修好了:
<facet type="kotlin-language" name="Kotlin"> <configuration version="3" platform="JVM 1.8" allPlatforms="JVM [1.8]" useProjectSettings="false"> <compilerSettings /> <compilerArguments> <option name="jvmTarget" value="1.8" /> <option name="pluginOptions"> <array> <option value="plugin:org.jetbrains.kotlin.android:enabled=true" /> <option value="plugin:org.jetbrains.kotlin.android:defaultCacheImplementation=hashMap" /> </array> </option> </compilerArguments> </configuration> </facet>
【讨论】: