Skip to content

Commit 482d6ae

Browse files
committed
update notes
1 parent 9f14343 commit 482d6ae

File tree

5 files changed

+310
-68
lines changed

5 files changed

+310
-68
lines changed

Gradle&Maven/Composing builds简介.md

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -245,30 +245,29 @@ Composing builds:A composite build is simply a build that includes other build
245245
![Image](https://raw.githubusercontent.com/CharonChui/Pictures/master/buildsrc_composingbuild.png?raw=true)
246246

247247
**使用方式**
248-
1.新建module,名为versionPlugin(自起)
249-
2.在该module下的build.gradle文件中,添加如下代码:
248+
249+
1. 新建module,名为versionPlugin(自起)
250+
2. 在该module下的build.gradle文件中,添加如下代码:
250251

251252
```groovy
252253
buildscript {
254+
ext.kotlin_version = "1.5.10"
253255
repositories {
254-
jcenter()
256+
mavenCentral()
255257
}
256258
dependencies {
257-
// 因为使用的 Kotlin 需要需要添加 Kotlin 插件
258-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
259+
// 因为使用的Kotlin需要需要添加Kotlin插件
260+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
259261
}
260262
}
261263
262264
apply plugin: 'kotlin'
263265
apply plugin: 'java-gradle-plugin'
264266
265267
repositories {
266-
// 需要添加 jcenter 否则会提示找不到 gradlePlugin
267-
jcenter()
268-
google()
268+
mavenCentral()
269269
}
270270
271-
272271
compileKotlin {
273272
kotlinOptions {
274273
jvmTarget = "1.8"
@@ -284,18 +283,18 @@ gradlePlugin {
284283
plugins {
285284
version {
286285
// 在app模块需要通过id引用这个插件
287-
id = 'com.hi.dhl.plugin'
286+
id = 'com.xx.xx.plugin'
288287
// 实现这个插件的类的路径
289-
implementationClass = 'com.hi.dhl.plugin.Deps'
288+
implementationClass = 'com.xx.xx.versionplugin.Deps'
290289
}
291290
}
292291
}
293292
```
294293

295-
3.在versionPlugin/src/main/java/包名/目录下新建Deps.kt文件,添加你的依赖配置,如:
294+
3. 在versionPlugin/src/main/java/包名/目录下新建Deps.kt文件,添加你的依赖配置,如:
296295

297296
```groovy
298-
package com.hi.dhl.plugin
297+
package com.xx.xx.versionplugin
299298
300299
class Deps : Plugin<Project> {
301300
override fun apply(project: Project) {
@@ -308,24 +307,48 @@ class Deps : Plugin<Project> {
308307
}
309308
```
310309

311-
5.在settings.gradle文件内添加includeBuild(“versionPlugin”),注意是includeBuild哦~,Rebuild项目
312-
6.后面就可以在需要使用的gradle文件中使用了,如app下的build.gradle,在首行添加以下内容:
310+
或者也可以按依赖类型用不同的类配置,例如
311+
312+
```kotlin
313+
object CustomLibs {
314+
...
315+
object Glide {
316+
private const val glideVersion = "4.11.0"
317+
const val glide = "com.github.bumptech.glide:glide:$glideVersion"
318+
const val glideCompiler = "com.github.bumptech.glide:compiler:$glideVersion"
319+
}
320+
321+
object Retrofit {
322+
private const val retrofitVersion = "2.9.0"
323+
const val retrofit = "com.squareup.retrofit2:retrofit:$retrofitVersion"
324+
const val converter_gson = "com.squareup.retrofit2:converter-gson:$retrofitVersion"
325+
}
326+
}
327+
```
328+
329+
330+
331+
4. 在settings.gradle文件内添加`includeBuild 'versionPlugin'`,注意是includeBuild哦~,Rebuild项目
332+
333+
5. 后面就可以在需要使用的gradle文件中使用了,在app或其他module下的build.gradle,在首行添加以下内容:
313334

314335
```groovy
315336
plugins {
316-
// 这个id就是在versionPlugin文件夹下build.gradle文件内定义的id
317-
id "com.hi.dhl.plugin"
337+
id 'com.android.application'
338+
id 'kotlin-android'
339+
id 'kotlin-kapt'
340+
// 通过id来使用该plugin,这个id就是在versionPlugin文件夹下build.gradle文件内定义的id
341+
id 'com.xx.xx.plugin'
318342
}
319343
```
320344

321-
注意: plugins{}需要放在app模块build.gradle文件内的首行位置
322-
323345
使用如下:
324346

325347
```groovy
326-
android {
327-
implementation Deps.appcompat
328-
}
348+
dependencies {
349+
implementation CustomLibs.Glide.glide
350+
kapt CustomLibs.Glide.glideCompiler
351+
}
329352
```
330353

331354

JavaKnowledge/Java内存模型.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,25 @@ Class Reordering {
217217
在Java内存模型中,描述了在多线程代码中,哪些行为是正确的、合法的,以及多线程之间如何进行通信,代码中变量的读写行为如何反应到内存、CPU缓存的底层细节。
218218

219219

220+
## 问题: 匿名内部类访问局部变量时,为什么这个局部变量必须用final修饰?
221+
这个问题并不是很严谨,严格来说应该是Java 1.8之前,匿名内部类访问局部变量时,才需要用final修饰。
222+
我们平时经常会用匿名内部类访问局部变量的情况,编译器都会提示我们要对这个局部变量加final修饰,但是我们却并没有去仔细考虑过这是为什么?
223+
224+
上面说到类和成员变量保存到堆内存中。而局部变量则保存在栈内存中。
225+
假设在main()方法中有一个局部变量a,然后main()方法里面又去创建了一个匿名内部类使用该局部变量a。
226+
a是在栈内存中的,当main()方法执行结束,a就被清理了。但是你创建的内部类中的方法却可能在main()方法执行完成后再去执行,但是这时候局部变量已经不存在了,那怎么解决这个问题呢?
227+
因此实际上是在访问它的副本,而不是访问原始的局部变量。
228+
229+
在Java的参数传递中,当基本类型作为参数传递时,传递的是值的拷贝,无论你怎么改变这个拷贝,原值是不会改变的;当对象作为参数传递时,传递的是对象的引用的拷贝,无论你怎么改变这个新的引用的指向,原来的引用是不会改变的(当然如果你通过这个引用改变了对象的内容,那么改变实实在在发生了)。知识点三,当final修饰基本类型变量时,不可更改其值,当final修饰引用变量时,不可更改其指向,只能更改其对象的内容。
230+
231+
在Java中内部类会持有外部类的引用和方法中参数的引用,当反编译class文件后,内部类的class文件的构造函数参数中会传入外部类的对象以及方法内局部变量,不管是基本数据类型还是引用变量,如果重新赋值了,会导致内外指向的对象不一致,所以java就暴力的规定使用final,不能重新赋值。
232+
所以用final修饰实际上就是为了变量值(数据)的一致性。 这里所说的数据一致性,对引用变量来说是引用地址的一致性,对基本类型来说就是值的一致性。
233+
234+
235+
当然在JDK 1.8及以后,看起来似乎编译器取消了这种限制,没有被声明为final的变量或参数也可以在匿名内部类内部被访问了。但实际上是因为Java 8引入了effectively final的概念(A variable or parameter whose value is never changed after it is initialized is effectively final)。对于effectively final(事实上的final),可以省略final关键字,本质不变,所以,实际上是诸如effectively final的变量或参数被Java默认为final类型,所以才不会报错,而上述的根本原因没有任何变化。
236+
237+
238+
It's about the scope of variables , Because anonymous inner classes appear inside a method , If it wants to access the parameters of the method or the variables defined in the method , Then these parameters and variables must be modified to final. Because although anonymous inner classes are inside methods , But when it's actually compiled , Inner classes are compiled into Outer.Inner, This means that the inner class is at the same level as the method in the outer class , A variable or parameter in a method in an external class is just a local variable of the method , The scope of these variables or parameters is only valid inside this method . Because internal classes and methods are at the same level when compiling , So the variables or parameters in the method are only final, Internal classes can be referenced .
220239

221240
---
222241

Jetpack/architecture/2.ViewBinding简介.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,15 @@ class MainActivity : AppCompatActivity() {
231231

232232
#### ActivityViewBinding
233233

234+
```kotlin
235+
inline fun <T : ViewBinding> AppCompatActivity.viewBinding(crossinline bindingInflater: (LayoutInflater) -> T) =
236+
lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
237+
val invoke = bindingInflater.invoke(layoutInflater)
238+
setContentView(invoke.root)
239+
invoke
240+
}
241+
```
242+
234243
```kotlin
235244
import android.os.Looper
236245
import android.view.LayoutInflater
@@ -293,8 +302,18 @@ binding.mtv.text = "Hello World"
293302
不幸的是,该属性委托仅对Activity有效,而对Fragment无效。
294303

295304
#### FragmentViewBinding
296-
305+
需要在gradle中增加`implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"`的依赖:
297306
```kotlin
307+
import android.view.View
308+
import androidx.fragment.app.Fragment
309+
import androidx.lifecycle.DefaultLifecycleObserver
310+
import androidx.lifecycle.Lifecycle
311+
import androidx.lifecycle.LifecycleOwner
312+
import androidx.lifecycle.Observer
313+
import androidx.viewbinding.ViewBinding
314+
import kotlin.properties.ReadOnlyProperty
315+
import kotlin.reflect.KProperty
316+
298317
class FragmentViewBindingDelegate<T : ViewBinding>(
299318
val fragment: Fragment,
300319
val viewBindingFactory: (View) -> T

0 commit comments

Comments
 (0)