如何将带有 Android Room 的应用添加为使用 makefile 编译的系统应用?

Posted

技术标签:

【中文标题】如何将带有 Android Room 的应用添加为使用 makefile 编译的系统应用?【英文标题】:How to add an app with Android Room as a system app compiled with the makefile? 【发布时间】:2021-01-26 03:59:31 【问题描述】:

尝试安装一个使用 android Room 构建数据库的简单应用,作为 AOSP 中的系统应用。纠结于要在 Android.mk makefile 中包含的内容。

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := $(call all-java-files-under,src)
LOCAL_RESOURCE_DIR := /home/<username>/WORKING_DIRECTORY/packages/apps/appname/res
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_MODULE_TAGS := optional
LOCAL_USE_AAPT2 := true
LOCAL_STATIC_JAVA_LIBRARIES := \
    android-common 

LOCAL_STATIC_ANDROID_LIBRARIES := \
    androidx.annotation_annotation \
    androidx.legacy_legacy-support-core-ui \
    androidx.appcompat_appcompat \
    androidx.core_core \
    androidx.recyclerview_recyclerview \
    androidx.room_room-common \
    androidx.room_room-migration \
    androidx.room_room-runtime \
    androidx.room_room-testing \
    android-support-annotations \
    androidx.cursoradapter_cursoradapter \
    androidx.sqlite_sqlite \
    androidx.sqlite_sqlite-framework \
    androidx.lifecycle_lifecycle-common \
    androidx.lifecycle_lifecycle-common-java8 \
    androidx.lifecycle_lifecycle-runtime \
    androidx.lifecycle_lifecycle-service 

LOCAL_ANNOTATION_PROCESSOR_CLASSES := \
    androidx.room.RoomProcessor

LOCAL_PACKAGE_NAME := com.app.appname
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_CERTIFICATE := platform
LOCAL_PRIVILEGED_MODULE := true

include $(BUILD_PACKAGE)

但是,在“make -j4”之后运行模拟器时会出现错误 -

E AndroidRuntime: java.lang.RuntimeException: 找不到 com.app.appname.RoleManagerDB 的实现。 rolemanagerdb_impl不存在

E AndroidRuntime:在 com.app.appname.RoleManagerDB.getDatabase(RoleManagerDB.java:27) E AndroidRuntime: at com.app.appname.MainActivity.lambda$onCreate$0$MainActivity(MainActivity.java:28)

E AndroidRuntime: at com.app.appname.-$$Lambda$MainActivity$xxx.run(Unknown Source:4) W ActivityTaskManager: 强制完成activity com.app.appname/.MainActivity

我的应用可以在 Android Studio 上顺利编译和运行,那么我在 makefile 中缺少什么?

我的猜测是我似乎无法翻译 gradle 行:

annotationProcessor "androidx.room:room-compiler:$room_version"

到它的makefile等价物。非常感谢任何帮助!

【问题讨论】:

是的,这可能是问题所在。在编译之前,android studio 会进行注释处理,从而创建必要的文件,例如房间接口的实现。所以谷歌关于android.mk中的注释处理 @Harkal 是的,我这样做了,但是我没有遇到 Android.mk 文件的正确注释处理器语句。我试过:“androidx.room_room-compiler-nodeps”“androidx.annotation_annotation”和“android-arch-room-rxjava2”但它失败了。 【参考方案1】:

我也很难找到这方面的信息。

Android 10 蓝牙应用使用 Room,AnnotationProcessor 配置在其 makefile (https://cs.android.com/android/platform/superproject/+/android-10.0.0_r30:packages/apps/Bluetooth/Android.mk)

在我的模块中,我添加了:

include $(CLEAR_VARS)

...

LOCAL_ANNOTATION_PROCESSORS := annotation
LOCAL_ANNOTATION_PROCESSORS += room-common
LOCAL_ANNOTATION_PROCESSORS += room-compiler
LOCAL_ANNOTATION_PROCESSORS += room-migration
LOCAL_ANNOTATION_PROCESSORS += s-antlr4         # a later version of antlr4 is already on the classpath, be we want this version
LOCAL_ANNOTATION_PROCESSORS += apache-commons-codec
LOCAL_ANNOTATION_PROCESSORS += auto-common
LOCAL_ANNOTATION_PROCESSORS += s-javapoet       #the name javapoet is already defined - prefix for uniqueness
LOCAL_ANNOTATION_PROCESSORS += kotlin-metadata
LOCAL_ANNOTATION_PROCESSORS += sqlite-jdbc
LOCAL_ANNOTATION_PROCESSORS += jetbrain-annotations
LOCAL_ANNOTATION_PROCESSORS += guava-21.0
LOCAL_ANNOTATION_PROCESSORS += kotlin-stdlib

LOCAL_ANNOTATION_PROCESSOR_CLASSES := androidx.room.RoomProcessor


...

include $(BUILD_PACKAGE)

我还添加了一个新模块:

#These libraries get copied into out/host/common/obj/JAVA_LIBRARIES/
# and are required for annotation processing
include $(CLEAR_VARS)

LOCAL_LIBS_PATH := libs
COMMON_LIBS_PATH := ../../../prebuilts/tools/common/m2/repository

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := \
        annotation:$(LOCAL_LIBS_PATH)/annotation-1.0.0-beta01.jar \
        room-common:$(LOCAL_LIBS_PATH)/room-common-2.0.0-beta01.jar \
        room-compiler:$(LOCAL_LIBS_PATH)/room-compiler-2.0.0-beta01.jar \
        room-migration:$(LOCAL_LIBS_PATH)/room-migration-2.0.0-beta01.jar \
        s-antlr4:$(COMMON_LIBS_PATH)/org/antlr/antlr4/4.5.3/antlr4-4.5.3.jar \
        apache-commons-codec:$(COMMON_LIBS_PATH)/org/eclipse/tycho/tycho-bundles-external/0.18.1/eclipse/plugins/org.apache.commons.codec_1.4.0.v201209201156.jar \
        auto-common:$(COMMON_LIBS_PATH)/com/google/auto/auto-common/0.9/auto-common-0.9.jar \
        s-javapoet:$(COMMON_LIBS_PATH)/com/squareup/javapoet/1.8.0/javapoet-1.8.0.jar \
        jetbrain-annotations:$(COMMON_LIBS_PATH)/org/jetbrains/annotations/13.0/annotations-13.0.jar \
        kotlin-metadata:$(COMMON_LIBS_PATH)/me/eugeniomarletti/kotlin-metadata/1.2.1/kotlin-metadata-1.2.1.jar \
        sqlite-jdbc:$(COMMON_LIBS_PATH)/org/xerial/sqlite-jdbc/3.20.1/sqlite-jdbc-3.20.1.jar
include $(BUILD_HOST_PREBUILT)

我坚持使用设备上蓝牙使用的库版本 - 一旦开始升级库,我就遇到了依赖冲突。

【讨论】:

以上是关于如何将带有 Android Room 的应用添加为使用 makefile 编译的系统应用?的主要内容,如果未能解决你的问题,请参考以下文章

如何将项目添加到具有Android Room中父实体的外键引用的子实体?

Android Room - 带有附加字段的多对多关系

Android Room:使用 Room 插入关系实体

如何高效查询带有两个 WHERE 子句的表(Android Room)

带有更新的预填充数据库的 Android Room 迁移

Android Room LiveData观察器未更新