这个问题之前困扰了我很长的时间,刚才终于可以运行了。 Android源码中samples文件夹中提供了很多的demo。
如何编译
查看我们想要运行的demo目录内容,这里以ApiDemos为例
查看Android.mk文件的内容,它定义了如何编译和链接模块。我猜测make编译命令会用到,下面是文件内容,注意属性LOCAL_PACKAGE_NAME:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := samples tests
# Only compile source java files in this apk.
LOCAL_SRC_FILES := \
$(call all-java-files-under, src) \
src/com/example/android/apis/app/IRemoteService.aidl \
src/com/example/android/apis/app/IRemoteServiceCallback.aidl \
src/com/example/android/apis/app/ISecondary.aidl \
LOCAL_STATIC_ANDROID_LIBRARIES += \
androidx-constraintlayout_constraintlayout \
androidx.legacy_legacy-support-v4
LOCAL_USE_AAPT2 := true
LOCAL_PACKAGE_NAME := ApiDemos /////////////////////////这个ApiDemos会用到
LOCAL_LICENSE_KINDS := SPDX-license-identifier-Apache-2.0
LOCAL_LICENSE_CONDITIONS := notice
LOCAL_SDK_VERSION := current
LOCAL_DEX_PREOPT := false
LOCAL_COMPATIBILITY_SUITE := device-tests
include $(BUILD_PACKAGE)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
然后回到android目录下执行make ApiDemos编译命令(我猜make后面这个参数应该就是Android.mk中属性LOCAL_PACKAGE_NAME的值)。
正常运行后会说明编译后apk的位置和编译成功信息。
install到设备
前面看到apk是在out目录下,我们要在有out目录的目录执行adb install命令
然后可以在设备中看到我们的apidemos了