简体   繁体   中英

How Kotlin find widget WITHOUT viewBinding.enabled = true?

Android Studio 3.6

One of the new feature in Android Studio 3.6 is

viewBinding.enabled = true

Approach#1

in build.gradle:

buildscript {
    ext.kotlin_version = '1.3.50'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0-beta01'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

in app/build.gradle:

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.31.1'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
android {
  viewBinding.enabled = true


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation('com.crashlytics.sdk.android:crashlytics:2.10.1@aar') { transitive = true; }
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.1.0-beta01'
    implementation 'org.altbeacon:android-beacon-library:2.16.3'
    implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
    implementation 'androidx.viewpager2:viewpager2:1.0.0-beta05'

    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation "androidx.core:core-ktx:+"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

My activity snippet on Kotlin ( approach#1 )

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.myproject.android.BuildConfig
import com.myproject.android.R
import com.myproject.android.adapter.CustomFragmentStateAdapter
import com.myproject.android.databinding.QrBluetoothSwipeActivityBinding
import com.myproject.android.ui.fragment.BluetoothPageFragment
import com.myproject.android.ui.fragment.QrPageFragment

class QRBluetoothSwipeActivity : AppCompatActivity() {
    private lateinit var binding: QrBluetoothSwipeActivityBinding
    private lateinit var myAdapter: CustomFragmentStateAdapter


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = QrBluetoothSwipeActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        init()
    }

    private fun init() {
        myAdapter = CustomFragmentStateAdapter(this)
        myAdapter.addFragment(QrPageFragment())
        myAdapter.addFragment(BluetoothPageFragment())
        binding.viewPager2.adapter = myAdapter
    }
}

here my qr_bluetooth_swipe_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.actviity.SplashDelayActivity">

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

And as result it's work fine. As you can see I use this:

binding.viewPager2.adapter = myAdapter

without use method findViewById .

Nice.

But I can solve this problem with another approach :

Approach#2:

Remove from app/build.gradle the block viewBinding.enabled = true

Change my activity on Kotlin ( approach#2 ) like this:

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.qr_bluetooth_swipe_activity.*

import com.myproject.android.BuildConfig
import com.myproject.android.R
import com.myproject.android.adapter.CustomFragmentStateAdapter
import com.myproject.android.ui.fragment.BluetoothPageFragment
import com.myproject.android.ui.fragment.QrPageFragment

class QRBluetoothSwipeActivity2 : AppCompatActivity() {
    private lateinit var myAdapter: CustomFragmentStateAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.qr_bluetooth_swipe_activity)
        init()
    }

    private fun init() {
        myAdapter = CustomFragmentStateAdapter(this)
        myAdapter.addFragment(QrPageFragment())
        myAdapter.addFragment(BluetoothPageFragment())
        viewPager2.adapter = myAdapter
    }
}

And as result it's work fine. As you can see I use this:

viewPager2.adapter = myAdapter

without use method findViewById .

Nice.

But the question is:

How in approach#2 the binding work WITHOUT

android {
viewBinding.enabled = true
}

?

It is working because of apply plugin: 'kotlin-android-extensions' in your app's build.gradle file (probably it is there right after apply plugin: 'kotlin-android' line as you didn't post your entire app's build.gradle file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM