包含资源的库项目的构建错误

Building error on a library project including resources

我正在尝试在系统构建环境中构建一个项目。该项目包含一个包含一些资源的库项目。库项目中的代码访问这些资源。该建筑因一些错误而失败。详情如下:

项目结构为:

packages/experimental/MyApplication/
  |--AndroidManifest.xml
  |--Android.mk
  |--proguard.flags
  |--libs/
  |--res/
  |--src/
  |--mylibrary/
  |    |--AndroidManifest.xml
  |    |--Android.mk
  |    |--libs/
  |    |--res/
  |    |    |--values/strings.xml
  |    |--src/
  |    |--  |--com/example/mytest/CustomView.java

mylibrary 是库项目。其中,有一个CustomView.java访问strings.xml中定义的字符串资源。主项目的 makefile 是:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
mylib_dir := mylibrary/res
res_dir := $(mylib_dir) res

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_RESOURCE_DIR := $(addprefix $(LOCAL_PATH)/, $(res_dir))
LOCAL_AAPT_FLAGS := --auto-add-overlay
LOCAL_AAPT_FLAGS += --extra-packages com.example.popoalex.mylibrary

LOCAL_STATIC_JAVA_LIBRARIES := mylibrary okhttp

LOCAL_PACKAGE_NAME := MyApplication
#LOCAL_CERTIFICATE := platform
#LOCAL_PROGUARD_FLAG_FILES := proguard.flags

include $(BUILD_PACKAGE)
##################################################
include $(CLEAR_VARS)

LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := okhttp:libs/okhttp-1.0.0.jar

include $(BUILD_MULTI_PREBUILT)

# only include rules to build other stuff for the original package, not the derived package.
ifeq ($(strip $(LOCAL_PACKAGE_OVERRIDES)),)
# Use the folloing include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
endif

库项目的 makefile 是:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := mylibrary
LOCAL_STATIC_JAVA_LIBRARIES := gson
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
include $(BUILD_STATIC_JAVA_LIBRARY)

##################################################

include $(CLEAR_VARS)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := gson:libs/gson-2.2.4.jar
include $(BUILD_MULTI_PREBUILT)

##################################################
# Build all sub-directories

include $(call all-makefiles-under,$(LOCAL_PATH))

我使用的构建命令是:

mmm packages/experimental/MyApplication/

建筑输出为:

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.3.1
CM_VERSION=10.2-20160708-UNOFFICIAL-n8000
TARGET_PRODUCT=cm_n8000
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a-neon
TARGET_CPU_VARIANT=cortex-a9
HOST_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-3.16.0-76-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_BUILD_TYPE=release
BUILD_ID=JLS36I
OUT_DIR=/home/popoalex/Projects/cm/out
============================================
PRODUCT_COPY_FILES device/samsung/smdk4412-common/rootdir/init.trace.rc:root/init.trace.rc ignored.
PRODUCT_COPY_FILES device/samsung/smdk4412-common/configs/audio_effects.conf:system/etc/audio_effects.conf ignored.
PRODUCT_COPY_FILES device/samsung/n80xx-common/ueventd.smdk4x12.rc:root/ueventd.smdk4x12.rc ignored.
PRODUCT_COPY_FILES device/samsung/n80xx-common/ueventd.smdk4x12.rc:recovery/root/ueventd.smdk4x12.rc ignored.
PRODUCT_COPY_FILES device/samsung/n80xx-common/configs/audio_policy.conf:system/etc/audio_policy.conf ignored.
PRODUCT_COPY_FILES device/samsung/n8000/configs/gps.conf:system/etc/gps.conf ignored.
No private recovery resources for TARGET_DEVICE n8000
make: Entering directory `/home/popoalex/Projects/cm'
Proguard: /home/popoalex/Projects/cm/out/target/common/obj/APPS/MyApplication_intermediates/proguard.classes.jar
ProGuard, version 4.4
Reading program jar [/home/popoalex/Projects/cm/out/target/common/obj/APPS/MyApplication_intermediates/classes.jar]
Reading library jar [/home/popoalex/Projects/cm/out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.jar]
Reading library jar [/home/popoalex/Projects/cm/out/target/common/obj/JAVA_LIBRARIES/core-junit_intermediates/classes.jar]
Reading library jar [/home/popoalex/Projects/cm/out/target/common/obj/JAVA_LIBRARIES/ext_intermediates/classes.jar]
Reading library jar [/home/popoalex/Projects/cm/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar]
Warning: com.example.popoalex.mylibrary.CustomView: can't find referenced class com.example.popoalex.mylibrary.R$string
Warning: com.example.popoalex.mylibrary.CustomView: can't find referenced class com.example.popoalex.mylibrary.R$string
Warning: com.example.popoalex.mylibrary.CustomView: can't find referenced class com.example.popoalex.mylibrary.R
Note: com.squareup.okhttp.internal.Platform accesses a method 'setHostname(java.lang.String)' dynamically
      Maybe this is library method 'gov.nist.core.Host { void setHostname(java.lang.String); }'
      Maybe this is library method 'org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl { void setHostname(java.lang.String); }'
Note: com.squareup.okhttp.internal.Platform accesses a method 'setNpnProtocols(byte[])' dynamically
      Maybe this is library method 'org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl { void setNpnProtocols(byte[]); }'
Note: com.squareup.okhttp.internal.Platform accesses a method 'getNpnSelectedProtocol()' dynamically
      Maybe this is library method 'org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl { byte[] getNpnSelectedProtocol(); }'
Note: com.squareup.okhttp.internal.Platform accesses a method 'get(javax.net.ssl.SSLSocket)' dynamically
Note: there were 4 accesses to class members by means of introspection.
      You should consider explicitly keeping the mentioned class members
      (using '-keep' or '-keepclassmembers').
Warning: there were 3 unresolved references to classes or interfaces.
         You may need to specify additional library jars (using '-libraryjars'),
         or perhaps the '-dontskipnonpubliclibraryclasses' option.
Error: Please correct the above warnings first.
make: *** [/home/popoalex/Projects/cm/out/target/common/obj/APPS/MyApplication_intermediates/proguard.classes.jar] Error 1
make: Leaving directory `/home/popoalex/Projects/cm'

如果我把访问字符串资源的代码注释掉,就构建成功了。 proguard.flags 我只使用默认的。我运行搭建的系统是Ubuntu14.04,源码基于CM 10.2(相当于AOSP 4.3.1)

更新:

库项目构建中间文件生成成功。在目录中:out/target/common/obj/JAVA_LIBRARIES/mylibrary_intermediates/

我终于解决了这个问题。构建脚本没问题,就是上次生成的中间文件不对。我把文件夹"out/target/common/obj/APPS/MyApplication_intermediates/"和楼运行删掉后,就成功了

顺便说一句,图书馆包含并使用资源。构建的关键是 "LOCAL_AAPT_FLAGS := --extra-packages" 可以将库 jar 和资源与主项目结合起来。