问题描述
我想通过从另一个 Android 应用程序启动 Activity 来向浏览器应用程序添加一些功能.当我制作主项目时,它给了我 package does not exist
.请注意,我看到 AndroidLib 已成功构建到 out/target/product/generic/data/app/AndroidLib.apk
I want to add some features into Browser app by start an activity from another android application. It gives me package does not exist
while I make the Main Project. Notice that I see the AndroidLib is built successfully into an out/target/product/generic/data/app/AndroidLib.apk
这里有两个 android.mk 文件:AndroidLib(一个普通的安卓应用)
Here are two android.mk files:AndroidLib(a normal Android App)
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_STATIC_JAVA_LIBRARIES :=
google-ps
android-support-v4
LOCAL_SRC_FILES :=
$(call all-java-files-under, src)
LOCAL_MODULE := AndroidLib
LOCAL_PACKAGE_NAME := AndroidLib
LOCAL_MODULE_TAGS := tests
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := google-ps:libs/google-play-services.jar
include $(BUILD_PACKAGE)
# additionally, build tests in sub-folders in a separate .apk
include $(call all-makefiles-under,$(LOCAL_PATH))
浏览器应用
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_STATIC_JAVA_LIBRARIES :=
android-common
guava
android-support-v13
android-support-v4
LOCAL_SRC_FILES :=
$(call all-java-files-under, src)
src/com/android/browser/EventLogTags.logtags
LOCAL_PACKAGE_NAME := Browser
LOCAL_PROGUARD_FLAG_FILES := proguard.flags
LOCAL_EMMA_COVERAGE_FILTER := *,-com.android.common.*
# We need the sound recorder for the Media Capture API.
LOCAL_REQUIRED_MODULES := SoundRecorder, AndroidLib
include $(BUILD_PACKAGE)
# additionally, build tests in sub-folders in a separate .apk
include $(call all-makefiles-under,$(LOCAL_PATH))
然后我做:
make -j Browser
它给了我:
packages/apps/Browser/src/com/android/browser/BrowserActivity.java:36: package com.om.AndroidLib does not exist
import com.om.AndroidLib.MainActivity;
推荐答案
更新
本质上你的问题是这条线:
Essentially your problem is that the line:
LOCAL_REQUIRED_MODULES := SoundRecorder, AndroidLib
告诉 Android 构建您的浏览器依赖于首先构建的 AndroidLib
,但是它不会以任何方式链接到 AndroidLib
.为此,您需要:
Tells the Android build that your Browser depends on AndroidLib
being built first, however it will not be linked to AndroidLib
in any way. To do that you need:
LOCAL_JAVA_LIBRARIES := AndroidLib
除了要求 AndroidLib
是一个 java 库项目,使用构建类型:
Except that requires the AndroidLib
to be a java library project, using the build type:
include $(BUILD_JAVA_LIBRARY)
不幸的是,Java 库不能包含资源.并且无法将一个应用链接到 Android 构建系统中的另一个应用.
And java libraries cannot include resources, unfortunately. And there is no way to link one app to another app in the Android build system.
我们经常解决这个问题,坦率地说,这样做有点混乱.但我们找到的唯一解决方法是将所有资源直接包含在使用这些资源的应用程序中.对不起.
We are working around that issue frequently, and it is frankly a bit of a mess to do so. But the only workaround we have found is to include all the resources directly in the apps that use those resources. Sorry.
如何使您的 AndroidLib
成为一个 Java 库并链接到它
How to make your AndroidLib
a java library and link to it
您的 android lib 可能应该构建为 Java 库,而不是应用程序包.所以,而不是:
Your android lib should probably be built as a java library, and not as a app package.So instead of:
include $(BUILD_PACKAGE)
你可能应该这样做:
include $(BUILD_JAVA_LIBRARY)
然后您的库将与其他系统库一起放置在 out/target/product/generic/system/framework/AndroidLib.jar
中.
Then you lib will be placed in out/target/product/generic/system/framework/AndroidLib.jar
along with the other system libs.
此外,您不需要 LOCAL_MODULE_NAME
用于 lib,它仅适用于应用程序.所以剥掉那个.
Also you do not need a LOCAL_MODULE_NAME
for a lib, it is only applicable for an app. So strip that.
并且您可能应该考虑将您的 LOCAL_MODULE_TAGS
更改为:
And you should probably consider changing your LOCAL_MODULE_TAGS
to:
LOCAL_MODULE_TAGS := user
tests
表示你的库应该只为测试而构建,user
说为所有用户构建构建你的库(并且隐含地也为工程构建)
tests
indicates that you lib should only be built for testing, user
says to build you lib for all user builds (and implicitly also for engineering builds)
在您的应用程序 Android.mk
中,您应该添加一行:
In you app Android.mk
you should then add a line:
LOCAL_JAVA_LIBRARIES := AndroidLib
这样您的应用程序将在类路径中使用 AndroidLib
构建.
Such that your app will be built with AndroidLib
in the classpath.
在您的应用程序 AndroidManifest.xml
中,您还需要在 application
标签中放置一个使用库:
In you apps AndroidManifest.xml
you also need to put a uses library in the application
tag:
<uses-library android:name="AndroidLib" />
最后你需要创建一个权限xml文件,内容如下:
And last you need to create a permissions xml file, with the following content:
<?xml version="1.0" encoding="utf-8"?>
<permissions>
<library name="AndroidLib"
file="/system/framework/AndroidLib.jar"/>
</permissions>
并更新您的产品配置文件,以便将 xml 文件部署到目标:
And update your product configuration file such that the xml file will be deployed to target:
PRODUCT_COPY_FILES += device/XXX/libs/AndroidLib.xml:system/etc/permissions/AndroidLib.xml
用权限xml文件的路径替换device/XXX/libs/
.
Replace device/XXX/libs/
with the path to the permissions xml file.
这就是我们在 Android 版本中添加和使用 Android 库的方式.我希望它有帮助.:)
This is how we add and use an Android library in our Android builds. I hope it helps. :)
这篇关于在 AOSP App 中添加一个 android 项目作为库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!