Using R resources from an android jar file

2019-08-30 00:52发布

问题:

This has been talked about a few times, with varying states of success but most of the answers I can find are several years in the past.

Essentially, I have an application which has moved to a point where isn't being shared with customers. Customers want to extend the application but we don't want them to have access to the original code -- protecting IP on all that.

The layouts, images, etc... well, we don't care about those as much, only the java code. I could easily compile the java code into a jar and distribute it, but that's only half the answer. The java code still references a whole pile of internal resources.

The scenario I'm working with is jar file contains the following:

In Jar file - MyActivity extends Activity - onCreate() within MyActivity sets the contentView to an R-referenced layout from within the jar file.

In client application - MyClientActivity extends MyActivity

When this is run, I get a ClassNotFoundException on MyClientActivity.

Things I've noticed: - If I attach the jar code as a library, instead of a jar file, it works. - If I remove the R reference within onCreate() in MyActivity and remake my jar file, it works.

So, obviously, as pointed earlier, the R references are dead/not accessible. The thing is, this apparently can work, no? If you create an Android application and look at the auto-included APIs, the android.jar file has an R file, with internal layouts/strings/etc... which are being references from within its code.

As others have said before, this seems like a very basic and obvious usage pattern. The fact that it seemingly doesn't work is mind boggling enough to lead one to believe that it does in fact work, it's just not done in an obvious way.

Has anyone ever actually gotten it to work?

Thanks a lot,

-Cord Awtry