0

In basic Java, if I write code to solve a common problem and want to use it in other projects, I would package it as a jar file and simply add it to those other projects.

But in Android--when that code may use drawables, strings and other resources--what can I do?

It looks like this question was asked here How to distribute the android reusable code in a package?, a year and a half ago, with the determination that hopefully this would be possible in the near future. I'm hoping someone has an update or a better answer.

The only option I know at the moment is to provide the full source and leave it to the using developer to move all classes into their project, add strings to their existing strings file, add their drawables to the project's other drawables, etc. That is a nuisance and error-prone. Is there an easy way to add third-party code that contains these resources?

Community
  • 1
  • 1
Chad Schultz
  • 7,770
  • 6
  • 57
  • 96

2 Answers2

1

Apparently the only solution is to distribute the other code as an Android project, then to reference that other project in Eclipse. Of course, that makes versioning and collaboration more difficult--my guess is that a team would need to establish a naming convention for these third-party projects they use in their workspaces. Perhaps add a version number to the project name, so that one app can use a more recent version of the library without breaking apps using an older version?

Chad Schultz
  • 7,770
  • 6
  • 57
  • 96
0

I wish there was a copy & paste kind of way. But the best I do is anything reusable is like com.company.widget.widgetName or if its an entire activity com.company.activities.activityname. Then 3 steps follow for:

  1. Copy and paste the code into src/
  2. Copy and paste resources in res/ & appropriate folders
  3. Modify the code to import the correct "R" class.

This is the best I have found, in-terms of disadvantages/advantages. If for each resource you write your own custom "depends file". You can make a script to make copy & pasting easier. I put dependencies of the resources in the comments at the top of the activity class.

The benefit of having a reusable resource code in one place and all project reference it is, a bug fix, helps all projects. The dis-benefit an upgrade & new features, breaks most projects.

over_optimistic
  • 1,399
  • 2
  • 18
  • 27