Re: [j2objc-discuss] ResourceBundle

77 views
Skip to first unread message

Tom Ball

unread,
Jan 10, 2020, 11:16:38 AM1/10/20
to j2objc-discuss
Resources need to copied into the main bundle's resources section with the relative path (see Java Resources doc). With Xcode, that means that resources with different paths need separate Copy Files build phases in the build target, where the Subpath is the relative path of the resource (the same path it has in the jar file).

An alternative to creating iOS resources is to use the https://github.com/google/j2objc/blob/master/scripts/gen_resource_source.py script. This script converts resource file contents into byte array constants that are linked with an associated class, which are loaded the same way as iOS resources using Class.getResource() and getResourceAsStream(). This avoids Xcode build steps for resources, and takes the same app space as the iOS resources did.

On Thu, Jan 9, 2020 at 11:54 PM <harri....@gmail.com> wrote:
I'm puzzled how to get properties files read with-in a translated third-party library especially that the properties file is in a path. My guess is that I would put it in the main bundle of app but it doesn't maintain the directory structure.

Any help is appreciated.

--
You received this message because you are subscribed to the Google Groups "j2objc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to j2objc-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/j2objc-discuss/e932c5a4-0dcd-4ca5-a4ba-4f94a8e5603e%40googlegroups.com.

Tom Ball

unread,
Jan 10, 2020, 11:18:53 AM1/10/20
to j2objc-discuss
BTW, one minor issue with using gen_resource_source.py is that from the iOS perspective these are not resource entries in the app, so only Java classes can reference them as resources. So only use it for resources associated with transpiled Java libraries.

Nikolay Spassov

unread,
May 12, 2023, 10:40:25 AM5/12/23
to j2objc-discuss
Hi Tom,
Thank you for this explanation. Me an my colleagues are having difficulties figuring out the correct path for the properties files when added inside a dynamic framework.

Is there a way to print the path created by getBundle at which the lookup for properties files will happen?

Tom Ball

unread,
May 12, 2023, 12:37:49 PM5/12/23
to j2objc-...@googlegroups.com
I think the problem is that we're using [NSBundle mainBundle], which (if I remember correctly) NSBundle didn't have a bundleForClass: method back then. So if you build j2objc (which we recommend), try changing that line to "NSBundle *bundle = [NSBundle bundleForClass:[IOSClass class]];". Any JRE core class can be used in place of IOSClass, but it is guaranteed to be linked into the app.

If that doesn't work, or you don't build j2objc, then consider copying Java_java_lang_SystemClassLoader_getResourceAsStream() into your project and invoke it natively.

Please let us know if either of these work for you, as we don't work with dynamic frameworks. 

Tom Ball

unread,
May 12, 2023, 1:42:25 PM5/12/23
to j2objc-...@googlegroups.com
That line change won't work, as IOSClass will always be in the main bundle. I'll work up a proposed change and send it out later today.

Tom Ball

unread,
May 12, 2023, 3:34:28 PM5/12/23
to j2objc-...@googlegroups.com
Framework bundles are now searched when locating a resource using getResource() methods in Class and ClassLoader.

Reply all
Reply to author
Forward
0 new messages