如何制作Android自组件并从jar库中使用它?
我正在尝试创建一个 android 组件,可以将其作为 jar 库轻松添加到 android 项目中。
为此,我必须在“res/values/attr.xml”中创建新的 xml 属性,并使用以下路径将其添加到我的图形 xml 元素中:
xmlns:app="http://schemas.android.com/apk/res/com.component.mypackage"
然后,我将此项目作为 jar 库导入到另一个项目中。 要在新项目中创建图形组件,我必须更改以下路径:
xmlns:app="http://schemas.android.com/apk/res/com.mylibrary"
但路径不正确:找不到自定义属性。
我设法将 R 文件集成到库 jar 中,我可以从 xml 访问它来声明一个自定义组件,如下所示:
<PreferenceScreen
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res/com.myLibraryPackage">
<com.myLibraryPackage.mySelfComponent
android:title="Name"
android:key="name"
app:hintText="Input your name"
android:dialogTitle="Your name "
app:validator="com.myLibraryPackage.myValidatorClass" />
奇怪的是,如果我将文件 attr.xml 放入项目的资源中,它就会起作用,这意味着它找到 com.myLibraryPackage.mySelfComponent。在这种情况下,为什么它也找不到 com.myLibraryPackage ?
(我也尝试替换
xmlns:app="http://schemas.android.com/apk/res/com.myLibraryPackage"
为
xmlns:app="http://schemas.android.com/apk/res/com.myApplicationPackage"
,但它仍然不起作用)
我更喜欢使用 jar 来促进其在项目中的集成!
有谁遇到过这样的问题可以帮助我吗?
谢谢。
I'm trying to create an android component that can be easily added to android projects as a jar library.
For this, I had to create new xml attributes in "res/values/attr.xml" that I add to my graphic xml element using the path:
xmlns:app="http://schemas.android.com/apk/res/com.component.mypackage"
Then I import this project as a jar library into another project.
To create my graphic components in the new project, I must change the path below:
xmlns:app="http://schemas.android.com/apk/res/com.mylibrary"
But the path is incorrect: the custom attributes are not found.
I managed to integrate the R file in the library jar and I could access it from my xml to declare a custom component like this:
<PreferenceScreen
xmlns:android = "http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res/com.myLibraryPackage">
<com.myLibraryPackage.mySelfComponent
android:title="Name"
android:key="name"
app:hintText="Input your name"
android:dialogTitle="Your name "
app:validator="com.myLibraryPackage.myValidatorClass" />
What is strange is that if I put my file attr.xml in resources of my project, it works, which means it find com.myLibraryPackage.mySelfComponent. In that case, why it can't find also com.myLibraryPackage ?
(I also tried replacing
xmlns:app="http://schemas.android.com/apk/res/com.myLibraryPackage"
by
xmlns:app="http://schemas.android.com/apk/res/com.myApplicationPackage"
but it still doesn't work)
I would have preferred to use a jar to facilitate its integration in a project !
Has anyone encountered a problem like this who could help me?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想重用代码和资源,则无法使用 jar 文件来实现。您需要将库转换为库项目。
如果您使用的是库项目,您仍然需要引用自定义属性,就好像它包含在应用程序中一样(因为 android 会在编译应用程序时将所有资源合并在一起):
If you want reuse code and resources, it won't be possible to do it with a jar file. You'll need to convert your library to a library project.
If you're using a library project, you'd still reference the custom attribute as if it were contained in the application (since android will merge all the resources together when the application is compiled):