|
| 1 | +# ViewBinding Library |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +This library helps to reduce the boilerplate code in initializing activities and fragments using ViewBinding. With this library you can initialize fragments, activities with a single line of code and can use the reference anywhere in the file. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +- Add the Jitpack.io dependency in project level gradle file |
| 10 | + |
| 11 | +```gradle |
| 12 | +allprojects { |
| 13 | + repositories { |
| 14 | + ... |
| 15 | + maven { url 'https://jitpack.io' } |
| 16 | + } |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +- Add `ViewBinding_Library` dependency in app level gradle file (Replace `Tag` with latest release) |
| 21 | + |
| 22 | +```gradle |
| 23 | +dependencies { |
| 24 | + implementation 'com.github.abhinav78910:ViewBinding_Library:Tag' |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +### Using ViewBinding_Library into your Android Project |
| 29 | + |
| 30 | +- Using ViewBinding_Library in Activity |
| 31 | + |
| 32 | +```kotlin |
| 33 | +class MainActivity : AppCompatActivity() { |
| 34 | + |
| 35 | + private val binding: ActivityMainBinding by viewBinding() |
| 36 | + |
| 37 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 38 | + super.onCreate(savedInstanceState) |
| 39 | + setContentView(binding.root) |
| 40 | + } |
| 41 | +} |
| 42 | +``` |
| 43 | +- Using ViewBinding_Library in Fragments |
| 44 | + |
| 45 | +```kotlin |
| 46 | +class TestFragment : Fragment(R.layout.fragment_test) { |
| 47 | + |
| 48 | + private val binding: FragmentTestBinding by viewBinding() |
| 49 | + |
| 50 | + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { |
| 51 | + super.onViewCreated(view, savedInstanceState) |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | +Incase of fragments Use onViewCreated instead of onCreateView. UI for the fragment is taken care by the library you need not to use onCreateView for initializing the UI for fragment. |
| 56 | + |
| 57 | +#### Quick Fixes |
| 58 | +- If you are facing this error |
| 59 | + |
| 60 | +```error |
| 61 | +Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6. Please specify proper '-jvm-target' option |
| 62 | +``` |
| 63 | +Then update your JVM target to 1.8 in the app level build.gradle file |
| 64 | + |
| 65 | +```gradle |
| 66 | +android{ |
| 67 | + ... |
| 68 | + compileOptions { |
| 69 | + sourceCompatibility JavaVersion.VERSION_1_8 |
| 70 | + targetCompatibility JavaVersion.VERSION_1_8 |
| 71 | + } |
| 72 | + kotlinOptions { |
| 73 | + jvmTarget = '1.8' |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +## License |
| 79 | + |
| 80 | + Copyright (c) 2021 Abhinav |
| 81 | + |
| 82 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 83 | + of this software and associated documentation files (the "Software"), to deal |
| 84 | + in the Software without restriction, including without limitation the rights |
| 85 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 86 | + copies of the Software, and to permit persons to whom the Software is |
| 87 | + furnished to do so, subject to the following conditions: |
| 88 | + |
| 89 | + The above copyright notice and this permission notice shall be included in all |
| 90 | + copies or substantial portions of the Software. |
| 91 | + |
| 92 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 93 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 94 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 95 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 96 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 97 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 98 | + SOFTWARE. |
0 commit comments