in titanium, is it possible to access the 3rd-party-jar-bundled resource files( xml ) from an android module?
Asked Answered
A

2

19

I have been working on how to access the 3rd-party-jar-bundled resource files ( xml files, images , referred here ) from an android module. Is it possible to do this?

Because these resource files are called by the compiled-3rd-party jar files, which means, I am not able to use TiRHelper.getApplicationResource method in the native java code unless I:

  1. de-compile the jar files to java code.
  2. replace all the native resource reference to TiRHelper.getApplicationResource
  3. re-compile the 3rd-party java source code to jar file.

(This third party project is https://github.com/ShareSDKPlatform/ShareSDK-for-Android/tree/master/Sample, I can easily compile & run it. You can see there are resource files in /res folder, and one of the jar files used these resource files. )

I searched a whole day but found no answer.

I got some clues from the official document , tried but not success. the error show that the resource is not accessbile by the java code from titanium module.

also, this same question( http://developer.appcelerator.com/question/178857/unable-to-load-resources-from-3rd-party-librarys-resourced-in-android-module ) is not answered.

this answer( http://developer.appcelerator.com/question/49671/android-r-references-in-appcelerator-module#answer-266726 ) gives me a clue that the methods:

getResource() gives an Error
getAndroidResource() gives an Error
but getApplicationResource() DOES work

and there is a clue that the 'apklib' may be useful, but after read some posts, I found it has been deleted from maven official website. and now Google recommends use 'aar' file instead. But according to this Titanium official issue (https://jira.appcelerator.org/browse/TIMOB-18565 ), it is not supported by titanium yet.

so, I want to know, is it possible to let the java code ( e.g. an activity) access its 3rd-party-jar-bundled resource files ( value/string.xml or so ) from Titanium module? if so, is there any sample code that I can refer to?

thank a lot!

Aside answered 14/4, 2015 at 12:21 Comment(2)
A JAR file does NOT contain the resources from the res/ folder. An AAR file (or apklib in the old days) contains the resources too as part of the bundle. When you "re-compile the 3rd-party java source code to jar file" you are probably losing the resources.Flyover
@androidLearner yes, jar doesn't contain resource files, and the apklib only works in old Maven version, and aar file isn't supported by Titanium yet. However, the resources files I am mentioning is the files under android/res folder of a native android project. I have found the answer. anyway, thanks for your attention!Aside
A
9

(fast & short answer: put the resources files as part of your "Titanium" project, but not put them into the "module". If you insist putting them into the "module", read on.)

Finally, I got the answer.

The official document is correct, just put the 3rd-party-jar's resource files( res folder of a native android project) under titanium module's android/platform/android folder. that's it!

e.g. my module project structure is shown as below:

# name: test_ti_module_qq_login:
android
  \--assets
  \--build
  \--dist
  \--lib
  \--libs
  \--src
  \--platform
     \--android
        \--res    # just put the resources here in your module! 
           \--drawable
           \--drawable-hdpi
           \--drawable-xhdpi
           \--layout
           \--values
assets
documentation
example
LICENSE
... 

and FYI, the native android resource folder looks like:

# A native Android project structure   
▸ assets/          
▸ bin/             
▸ gen/             
▸ libs/           
▾ res/     # these are the bundled resource files
  ▸ drawable-hdpi/     
  ▸ drawable-xhdpi/    
  ▸ layout/     
  ▸ values/    
  ▸ src/       

The root cause that why Titanium kept showing the resource files missing is that I missed some of the resource files. (but it's a bit straight that the native code demo could be run without errors, whatever, finally I got the solution )

addition:

It seems that many people met the same problem? I will open source my project in these days.

Aside answered 23/4, 2015 at 0:4 Comment(2)
Hi, do you have an idea on how to work with the Android Manifest.xml file that came inside the AAR file?Seam
merge that file into the titanium's tiapp.xml. read more about its official document.Aside
K
3

Updating with based on the new information. To get that to work you would just need to merge the res folder from github into your platforms/android/res folder. Then when you compile your module as normal.

===========================

If you're accessing from within the module in java, try using TiRHelper.getApplicationResource for things you're providing.

So for example if you've added:

platforms/android/values/strings.xml hello!

The call would be

TiRHelper.getApplicationResource("string.helloworld");

Class Srouce

Javadoc

If you've found this helpful, please up vote. If not let me know so I can adjust.

Klein answered 15/4, 2015 at 1:14 Comment(8)
thanks Brad, the resource is called by the 3rd-party-jar files, but not the Ti generated java files. It works just fine in its Demo (native android project). but when I migrate it to my ti project, it will complain no resource found.Aside
the resources are bundled with the 3rd-party-jar files.Aside
Do you have access to the source of the 3rd party module? If so you would want to compile it as a library and when everything is built it should be parsed out. If not you may need to add in the strings into your resource files.Klein
I don't have the "source code" of the 3rd party module, only got the "jar" files and the bundled resource files. what do you mean the 'add in the strings into your resource files' ?Aside
By that I mean go into your strings.xml file and add in the missing values.Klein
The problem is : I am not able to make the 3rd-party-jar class access the strings.xml .Aside
One other idea, you could take the code from source modify private static String clsPrefixAndroid = "android.R$"; to point to the package of the application.R$ and see if that works.Klein
yes, thanks, this idea will be my "last chance" if there's no answer found. :)Aside

© 2022 - 2024 — McMap. All rights reserved.