How to consume reusable gui element/widget with re

2019-01-20 18:37发布

问题:

I try to use the dateslider in my android project to have a combined date-time picker im my gui.

The authors of the widget suggest to include the java- and resource-sources directly into my app.

1st try copy java and resources into main app:

Result: The widget java classes cannot compile because the resourceids R.xxxx cannot be resolved.

Reason: The widget classes are implemented in package "com.googlecode.android.widgets.DateSlider" and my app has a different namespace "my.namespace.myApp" so that the resourceids come from my.namespace.myApp.R.xxx.

To fix this i would have to touch every widget java source to import my.namespace.myApp.

Is there a way to have 2 resource-sets with different namespaces so that there are my.namespace.myApp.R.xxx and com.googlecode.android.widgets.DateSlider.R in the main app?

2nd try put widget java+resources into seperate jar/library:

result: every thing compiled. but after appstart i get a runtimererror: the runtime cannot resolve the widget resource IDs from the jar/lib.

Note: i can call methods from the jar as long as these do not need resources.

So my question: what is the best way to consume a reusable gui element with resources in android?

I am using android 2.2.

Note: Android: How do I create reusable components? does not help because it tells you how to create library-projects.

update 16.3.2012

Since the current version 16 /17-pre of of the eclipse adt-tools do not support resources in jars (as of try 2) what is the best/easiest way to consume them until there is support for this?

update 4.4.2012

with the new R17-tools i succeeded to consume libraryproject-with-resources that creates the jar. Android-Lint helped me to find out what to change in the lib to make it usable.

  • eclipse-workspace

    • DateSliberLib
      • src
      • res
      • ...
    • MyAppUsingLib
      • src
      • res
      • ...

    with this layout MyAppUsingLib is running fine


However I am still not able to use the DateSliberlib.jar alone

  • eclipse-workspace
    • MyAppUsingLib
      • src
      • res
      • lib
        • DateSliberLib.jar
      • ...

This setting can be comiled but the app crashes because it cannot find the resources of the lib.

[update 2014-11-17]

6 months ago i switched from eclipse/ant-build to android-studio/gradle build which introduced *.aar files that are jar-files with android resources.

android-studio/gradle build can cope with resources in libs.

回答1:

Is there a way to have 2 resource-sets with different namespaces so that there are my.namespace.myApp.R.xxx and com.googlecode.android.widgets.DateSlider.R in the main app?

No, sorry.

Note: Android: How do I create reusable components? does not help because it tells you how to create library-projects.

However, that is the right answer. Download the full project from their repo, import it into Eclipse, and mark it as a library project (Properties > Android). Then, add it as a library project to your app's project.

Eventually (which now appears like it will be the R18 version of tools or later), the tools should support packaging reusable components in a JAR, with resources, in such a manner that you can add them to a host project and the resources will be blended in automatically. Right now, that's not an option.