AOSP-Androidmk里:=与+=的区别是啥?

# AOSP-Android.mk里:=与+=的区别是啥?

:=+=这两个常见于Android.mk文件中

比如说:

LOCAL_STATIC_JAVA_LIBRARIES := android-support-v13
LOCAL_STATIC_JAVA_LIBRARIES += android-ex-camera2-portability
LOCAL_STATIC_JAVA_LIBRARIES += xmp_toolkit
LOCAL_STATIC_JAVA_LIBRARIES += glide
LOCAL_STATIC_JAVA_LIBRARIES += guava
LOCAL_STATIC_JAVA_LIBRARIES += jsr305
1
2
3
4
5
6

可以看到有:=+=

同样,我们也看到有

LOCAL_STATIC_JAVA_LIBRARIES := \
    android-support-v4 \
    android-support-v7-recyclerview \
    android-support-v7-palette \
    android-support-dynamic-animation
1
2
3
4
5

我们还看到有

LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 android-support-v7-recyclerview android-support-v7-palette
1

其实这三种写法作用是一样的。

前面我们看到

LOCAL_STATIC_JAVA_LIBRARIES := android-support-v13
LOCAL_STATIC_JAVA_LIBRARIES += android-ex-camera2-portability
LOCAL_STATIC_JAVA_LIBRARIES += xmp_toolkit
LOCAL_STATIC_JAVA_LIBRARIES += glide
LOCAL_STATIC_JAVA_LIBRARIES += guava
LOCAL_STATIC_JAVA_LIBRARIES += jsr305
1
2
3
4
5
6

其实可以写成

LOCAL_STATIC_JAVA_LIBRARIES := \
	android-support-v13	\
	android-ex-camera2-portability	\
	xmp_toolkit	\
	glide \
	guava \
	jsr305
1
2
3
4
5
6
7

以上这个是写成多行,换行连接用斜杠,这个做过linux开发的同学都知道的

也可以写成

LOCAL_STATIC_JAVA_LIBRARIES := android-support-v13	android-ex-camera2-portability	xmp_toolkit	glide guava jsr305
1

以上这个就是写成了行了,空格分开

但是,如果这样写可以吗:

LOCAL_STATIC_JAVA_LIBRARIES += android-support-v13
LOCAL_STATIC_JAVA_LIBRARIES := android-ex-camera2-portability
LOCAL_STATIC_JAVA_LIBRARIES += xmp_toolkit
LOCAL_STATIC_JAVA_LIBRARIES += glide
LOCAL_STATIC_JAVA_LIBRARIES += guava
LOCAL_STATIC_JAVA_LIBRARIES += jsr305
1
2
3
4
5
6

如果这样写,android-support-v13就没了

我们可以log输出一下给大家看。

# 实验部分

我们验证一下 :=与+=的区别

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#首次赋值
CUSTOM_PARAM := 10
#再次给值
CUSTOM_PARAM := 20
#猜猜这里输出啥?20,而不是10
#warning 输出是不是终止往下的,error会
$(warning $(CUSTOM_PARAM))
#再次设置值
CUSTOM_PARAM := 30
CUSTOM_PARAM += 40
#这里输出啥?3040
$(warning $(CUSTOM_PARAM))
CUSTOM_PARAM += 50
#这里输出啥?304050
$(warning $(CUSTOM_PARAM))
#这里是error,终止往下编译了
$(error "test...")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

图片描述

上次更新: 2022/03/28, 23:04:38