Unresolved reference for button id in MainActivity.kt - android-studio

when I try to directly type the button id in main activity of kotlin , it does not pick it up automatically, instead it says unresolved reference, what is wrong with Android Studio??

In my case I had the same problem and have solved it this way with Android Studio 4.x:
Put this in your apps build.gradle or build.gradle.kts. The latter in my case because my project is made with Kotlin KMM.
buildFeatures {
viewBinding = true
}
Here is my complete build.gradle.kts so you can see where it belongs to:
plugins {
id("com.android.application")
kotlin("android")
id("androidx.navigation.safeargs.kotlin")
}
dependencies {
implementation(project(":shared"))
implementation("com.google.android.material:material:1.3.0")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.constraintlayout:constraintlayout:2.0.4")
implementation("androidx.navigation:navigation-fragment-ktx:2.3.4")
implementation("androidx.navigation:navigation-ui-ktx:2.3.4")
}
android {
compileSdkVersion(30)
defaultConfig {
applicationId = "************************"
minSdkVersion(24)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
buildFeatures {
viewBinding = true
}
}
Here you can read all about it. Also if you are using Android Studio lower than 4.x how it has to be done.
https://developer.android.com/topic/libraries/view-binding
Don't forget to sync your project. It will only appear after that step. You can check it by having a look at dataBinding folder which you will find at that path.
build > generated > data_binding_base_class_source_out > debug > out > rest is app specific
and the last folder will be "dataBinding" folder.
Have fun!

It's hard to advise anything without being given any code.
I'm guessing you're using Kotlin synthetic import? Then you would also need to import something like this import kotlinx.android.synthetic.main.activity_main.* to be able to use view elements without findById. Also, make su you have integrated Kotlin extensions plugin.
Also, I can't rule out this possibility, since I can't see any of your code, but maybe check if the view element you're trying to access in MainActivity is in it's layout?

Related

Android Studio MainActivity.kt can't reference ids in main_activity.xml

I am new to Android Studio. I wrote a simple code in the main_activity.xml file, with ids. But when I try to reference the ids from my MainActivity.kt file. It shows an error Unresolved reference: btnDatePicker i.e an unresolved error.
I don't know what wrong. Here's a screenshot of my MainActivity.kt file. As you can see, when I try to call the id btnDatePicker, it returns an error.
And Here's a screenshot of my activity_main.xml, as you'll see I have circled the particular id I'm trying to reference.
Id cannot be referenced directly
Under normal conditions, you can bind the control using the following code:
val button = findViewById<Button>(R.id.btnDatePicker)
and if you want to use id directly, i think this article can help you
View Binding
Just try to rebuild project,Build -> Rebuild project
We have 2 variant and I recommend it. I recommend you use View Binding. Because Koltin synthetic(just writing id element) is deprecated.
To enable view binding in a module, set the viewBinding build option to true in the module-level build.gradle file, as shown in the following example:
android {
...
buildFeatures {
viewBinding = true
}
}
Usage
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding(layoutInflater)
val view = binding.root
setContentView(view)
}
then use your xml elements
binding.btnDataPicker.setOnClickListener {
...
}
Just add this in your gradle file in plugins id 'kotlin-android-extensions'
plugins {
id 'kotlin-android-extensions'
}
Just add this in your gradle file in plugins id 'kotlin-android-extensions'

Why does Android Studio not run my generateConfig but command line does

I'm trying to generate identical assets for each of the subprojects in a project. Since exactly the same thing has to happen for each subproject, I'm trying to avoid having to add the generation code to each subproject.
What I've got so far is:
// in my outer build.gradle
allprojects { proj ->
gradle.projectsEvaluated {
def configTemplate = project.file('src/main/templates/configuration.json')
def android = proj.getProperties()['android']
if (configTemplate.exists() && android != null) {
task generateConfigFiles(type: ConfigureTask) {
template configTemplate
environments proj.rootProject.file('scripts/environments.json')
environmentName host
destination new File(project.buildDir, 'templates/configuration.json')
}
android.applicationVariants.all { variant ->
variant.mergeAssets.dependsOn generateConfigFiles
}
}
}
}
// each application build.gradle
android {
sourceSets {
main {
assets.srcDir 'build/templates'
}
}
}
This seems to work exactly as expected when I run gradle from the command line ./gradlew mergeQaDebugAssets runs the generateConfigFiles task before running mergeQaDebugAssets
If I attempt to run this from Android Studio using the gradle window, it never runs generateConfigFiles
I've also gotten it to work by just including the projectsEvaluated step in each of the project build.gradle files, but, as noted, I'm trying to avoid the code duplication and maintenance headaches that brings.

Please explain the Android build.gradle groovy syntax

What does the following groovy syntax really mean?
The Gradle docs tout how the build.gradle is just groovy. The Android team has simplified the default build.gradle to the point that it doesn't look like code (to me at least). Please explain what this is doing in terms of groovy syntax. For example, are these global variable declarations that the Android plugin uses?
Bonus points if you include references to http://groovy-lang.org/syntax.html as part of your explanation.
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.crittercism"
minSdkVersion 15
targetSdkVersion 21
versionCode 5
versionName "5.0"
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
You can think of a gradle build script as some code which is delegated to an object which can respond to method calls written in it.
The script uses a lot of Groovy syntactic sugar, so removing them, it should look like this:
apply( [plugin: 'com.android.application'] );
android({
compileSdkVersion( 21 );
buildToolsVersion( "21.1.2" );
defaultConfig({
applicationId( "com.crittercism" );
minSdkVersion( 15 );
targetSdkVersion( 21 );
versionCode( 5 );
versionName( "5.0" );
});
});
dependencies({
compile( fileTree([dir: 'libs', include: ['*.jar']]) );
});
So the script is really a bunch of method calls:
def apply(Map)
def android(Closure)
def dependencies(Closure)
This android(Closure) will receive a closure and will delegate the methods called in it to an object which can respond to these methods:
def compileSdkVersion(Integer)
def buildToolsVersion(String)
...
Given that, we can parse the script, delegate it to some object and then execute it.
Delegating using DelegatingBaseScript is one way to do it (not sure if Gradle does it this way). Here is a dumbed down working version:
import org.codehaus.groovy.control.CompilerConfiguration
gradleScript = '''
apply plugin: 'com.android.application'
android({
compileSdkVersion( 21 )
buildToolsVersion( "21.1.2" )
})'''
class PocketGradle {
def config = [apply:[]].withDefault { [:] }
def apply(map) {
config.apply << map.plugin
}
def android(Closure closure) {
closure.delegate = new Expando(
compileSdkVersion: { Integer version ->
config.android.compileSdkVersion = version
},
buildToolsVersion : { String version ->
config.android.buildToolsVersion = version
},
)
closure()
}
}
def compiler = new CompilerConfiguration(scriptBaseClass: DelegatingScript.class.name)
shell = new GroovyShell(this.class.classLoader, new Binding(), compiler)
script = shell.parse gradleScript
script.setDelegate( gradle = new PocketGradle() )
script.run()
assert gradle.config == [
apply: ['com.android.application'],
android: [
compileSdkVersion: 21,
buildToolsVersion: '21.1.2'
]
]
You can execute the script in Groovy Web Console (click "Edit in console" and then "Execute script").
Most of the syntax explanation are in the DSL section:
Command chains
Groovy lets you omit parentheses around the arguments of a method call for top-level statements. "command chain" feature extends this by allowing us to chain such parentheses-free method calls, requiring neither parentheses around arguments, nor dots between the chained calls.
There is also Groovy ConfigSlurper, but i'm not sure if it can go as far as Gradle wants to.
Thanks to AndroidGuy for supplying the excellent video that informed me of the information below. The video is 35 minutes long, so here's the TL;DR.
Most of this syntax is a mixture of method calls and closures. The closures are represented by curly braces. Also note that method calls do not require parenthesis.
apply plugin: 'com.android.application'
This is calling the apply method on the project object with a single named parameter "plugin". The project object is the top level object supplied by Gradle.
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
This is setting the dependencies property of the project object. Groovy properties are basically shorthand for getters and setters. The dependencies property is a Closure object that delegates to DependencyHandler. Groovy delegation is essentially a way to augment the scope resolution of a closure. The dependencies closure contains a single method call to compile, which takes a FileTree positional parameter. The FileTree is generated by the fileTree method which is defined in the project object. The compile method is still a bit nebulous to me. It appears to come from the Java plugin, but it isn't explicitly documented there. The 'compile' part is still a bit magical to me.
android {
...
}
I'll leave the 'android' section as an exercise to the reader. The Android Gradle Domain Specific Language (DSL) is not available on the web. You have to download it.

Why do I get a "connot be applied to 'Closure'" Warning in my gradle script?

I have a hard time figuring out, why IntelliJ warns me about this part in my build.gradle file:
apply plugin: 'distribution'
(...)
distributions {
main {
baseName = 'someName'
contents {
from { 'src/readme' }
}
}
}
shot:
I took it straight from the gradle user guide and the build seems to work ok. So, is this a false positive or should I take this serious? If so, what's the problem here and how would one check the API / code to find the expected types and so on?
it is a false positive, but if you want to make it disappear use the following
apply plugin: 'distribution'
distributions.create('someNameButMain', {
baseName = 'someName'
contents {
from { 'src/readme' }
}
})

Apply configuration to specific projects in gradle build script

I have a gradle project that has java applications as well as android applications.
root/
build.gradle
settings.gradle
java1/
java2/
android1/
android2/
java3/
etc.
What is the best practice for structuring my build script? I am a total gradle novice and am migrating the project from maven to gradle.
I wanted to do something instead of
configure(subprojects) {}
to apply plugins and other specific things.
such as
configure(java1, java2, java3) { // java specifics }
configure(android1, android2) { // android specifics }
I am probably approaching this from the wrong way.
More explicitly I need to apply the plugin java only for the java projects and the android plugin for the android projects.
configure(subprojects.findAll {it.name == "java1" || it.name == "java2"}) {
Under the filtering section in the guide
Hope this helps someone else out.
There are multiple ways, depending on what you want... Some examples:
// Configures just project java1
project(":java1") { ... }
// Configures projects java1 and java2
["java1","java2"].each { name ->
project(":$name") { ... }
}
You can use normal groovy to find/iterate over all the projects.
Another option:
configure([ project(':sub1'), project(':sub2'), ... ]) {
...
}
The shortest and easiest option:
configure(subprojects.findAll()) {
if (it.name.equals('MY_PROJECT')) {
...
} else {
...
}
}
Another approach...
In the settings.gradle you do define your projects like this:
gradle.ext.javaProjects=[]
gradle.ext.androidProjects=[]
javaProject('java1')
javaProject('java2')
javaProject('java3')
androidProject('android1')
androidProject('android2')
def javaProject(String name) {
gradle.ext.javaProjects.add(name)
include ":$name"
}
def androidProject(String name) {
gradle.ext.androidProjects.add(name)
include ":$name"
}
Now you can reference those in your root build.gradle:
def javaProjects = subprojects.findAll {gradle.ext.javaProjects.contains(it.name)};
def androidProjects = subprojects.findAll {gradle.ext.javaProjects.contains(it.name)};
configure(javaProjects) {
...
}
configure(androidProjects) {
...
}
Maybe thats overkill... but i usually have the project definition method in my settings.gradle anyway. E.g. if you want to put all java projects under a java folder you could define things like this:
def javaProject(String name) {
gradle.ext.javaProjects.add(name)
include ":$name"
project(":$name").projectDir = file('java/'+ name)
}

Resources