Packaging Android resource files within a distributable Jar file

后端 未结 9 2256
自闭症患者
自闭症患者 2020-11-28 18:26

I am working on some reusable Android code that I would like to distribute to other developers for use within their own applications. The code has some resource dependencies

相关标签:
9条回答
  • 2020-11-28 19:08

    I don't have any optimistic news for you. In general you cannot distribute your own package as easily as you would expect. The main problem is the autogenerated R file. You probably make some references to it - you have your layouts and drawables. User of your library will have his own R file - containing ids for his resources, not for the ones your library provides.

    In case of drawable resources you could read them in classic way, without R file. But as far as I know you are not able to inflate layouts from external xml files.

    So the ugly hack here would be to distribute your library together with all your resources, which user would have to copy to his "res" folder. To be honest, there is no good solution to your problem... sorry. Regards!

    0 讨论(0)
  • 2020-11-28 19:10

    I just found that the aapt tool in the Android SDK allows the following option:

       --non-constant-id
           Make the resources ID non constant. This is required to make an R java class
           that does not contain the final value but is used to make reusable compiled
           libraries that need to access resources.
    

    This sounds promising, though I have not tried this so I haven't a clue if it works. I suspect this is to get around the problem addressed by @J.J.Kim's post above.

    0 讨论(0)
  • 2020-11-28 19:11

    I was using user558185 solution but here is a more simple way. If you resources is in a library project (for a Cordova Plugin for example), you can get resource with context.getResources().getIdentifier(name, className, context.getPackageName());

    This happen when the library project resources R file is not generated properly and the R packageName did not match the required packageName by the library project.

    The usage is the same as user558185 answer, but it use a native Android method.

    0 讨论(0)
提交回复
热议问题