Accessing strings.xml values in android library project - android

I have MyAppProject as android application project and MyLibProject as android library project.
I have strings.xml(2 strings) and one xyz.java class in MyLibProject.
xyz.java class has 2 get methods to access the strings.xml values.
I have added reference library project "MyLibProject" in "MyAppProject" application project.
For some usecase, I will check whether Library project xyz.java class is in classpath then get the
values from xyz.class.
How to access the strings.xml texts in xyz.java class get method?

As far as I'm aware, you should be able to access any references via a fully qualified name for the R file you're attempting to access, via the Context. Something like (untested):
Context.getString(com.YOUR.LIB.R.string.YOURSTRING));
Failing that, you could possibly import the com.YOUR.LIB.R file directly into your activity.
Hope this helps! And I hope that I understand what you're trying to achieve. If not, please provide more information and possibly some code you've already tried, may help explain your problem :)

This question has been very old, but creating Android libraries will never get old. I have been working on creating Android libraries for a few days now, and myself encountered this issue of not been able to use res directory resources in library's java files due to some reason. After 2 days of searching, I found that I was missing package declaration in my library's manifest file.
So, to make it working, check if you have declared package in manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourpackage">
</manifest>
It worked for me after this change, now I can use resources in library's java files. I hope it works for you too.

Related

How to Use Ant Script replace strings, switch resources folders, auto generate R.java and compile final Apk?

We hope to build multiple applications based on the same source code. The base source code will be stored in SVN so at daily development we just need to modify one codebase.
While for different customers, we hope to given some some level of branding. Mostly changing images and titles, these can be done by just change the resources.
After some research, We find that it is really touch to do these multiple version applications. The main problem is that R.java is generated based on the package name. And package name identify the application. In the source code there are many files import this package.name.R, which means for different application, there will be different package name, and different R's reference need to be change in most source code file.
We find some articles on web about using Ant to do this task, but none of them are really specified. We hope someone could help!
Generally the workflow will be like this:
Changing the package name in AndroidManifest.xml
Go over all source code file, find and replace anything referenced to R to the new package name.
Switch the res folder to the new client's res folder (We will have this folder ready)
Auto generate R.java file.
Start normal compile and build process.
Get Apk.
We hope someone could point out how can we achieve these task by using Ant, or any other better solution.
Thank you!
I think you should create an Android Library Project (http://developer.android.com/tools/projects/index.html#LibraryProjects) with the common code, check it into VCS, and then create separate projects for every customer which you should also check into VCS to keep track of them.
You will be able to generate different customized versions from same source code.

Android Multiple APK from same code base

My goal is to create multiple android APKs. All that is different is the package name and I manually override some things in the res folder.
I understand you can do a library project. The problem with that is we have to manage multiple manifest files and the version with that. That is not what I want to do.
I was looking into aapt to create the new package name and shared res folder. The problem is I do not know where to start. I see lots of examples like:
Custom Android build.xml for rename manifest package
also below is exactly what I want to do:
http://blog.uncommons.org/2010/07/19/building-two-versions-of-the-same-android-app/
Can anyone provide direction on where to start?
The best way to do this is to create a library project, and than create separate projects for each of the APKs that you want to have. Each APK project can than have its own Manifest and reference the Library Project. Believe me when I tell you that this is much easier than trying to rename packages during build/compile time. You can easily call into activities that are shared in the library package just as long as you use the fully qualified name in the individual APK's manifest file.
http://developer.android.com/tools/projects/projects-eclipse.html
I had the same problem before and made a batch file to copy files like images, mp3 files, etc. to the workspace folder where I have my application folder in it. You have to make everything dynamic if you don't want to use the library project. I hope this helps, else you can ask me anything you want.

can't get R.anim in my project

I started some android programming and I am trying to do some animation in my activity. but all references are using interpolators and files from res/anim folder which I can't find in my project. Also, when I am using R.anim I am getting an error that it doesn't exist.
Is there any API to download?
Thank you
You need to import the R file. You should see something like below in the import list of your class file.
import <your.app.package>.R
I had the same problem, and none of the solutions above worked.
In the end, I got it to work by referencing it as android.R.anim as explained here.
Had similiar issue, but could access all other R.* locations but not anim.
Problem was with an anim xml file missing:
xmlns:android="http://schemas.android.com/apk/res/android"
Nothing in IDE detected any error. Hope this helps someone else.
No, R file is not some API. It is a file generated by the Android build system from existing resources. If you can't find some particular references to resources, this means these resources don't exist. You should put them where they belong. Read more about providing resources for your applications.
No api. Your config is wrong in some way. Attach some code, possibly a screenshot of your project browser so we can see the folder/file structure.
In my case there was yet another reason why my R.anim.shake hadn't been recognized. It was because my project is divided into 2 modules (app and library). I placed the animation file (shake.xml) in the app module while I was trying to reference it from the library module. Moving the shake.xml file to library/src/main/res/anim solved the problem.

How to reference R.java of other apps in Android, like com.android.music?

I am trying to develop an app to supplement the built-in music player. I've used git to download the com.android.music package and looked around at its code. I can launch the music player by copying some of its code and launching activities with intents.
Now what I need to do is get a handle to its current view. In the MusicUtils.java file, I see a line that says
View nowPlayingView = a.findViewById(R.id.nowplaying);
I'd like to do the same thing. Only I don't have access to the R.java file, so I can't write e.g. R.id.nowplaying. How do I do this? How do I reference the music players R.java? I do know the R.java stuff is declared public so that shouldn't be a problem. Right?
Is it even possible? I saw this related question and am now wondering: How to load com.android.music code into Eclipse and compile?
Btw, I'm working against the Android 2.2 SDK, but it'd be helpful to know if the answer is different for older versions such as 1.5. Thanks.
You cannot get a handle on a View that is not in your process, even if you have access to the R class.
The R class is code generated by the Android plateform from the XML's resources files found in the /res directory of the project. It means you want to use the git repo where you got the MusicUtils.java and go to the root dir of the related project to look out in /res/layout/. You then have to find an XML layout file declaring the View having the id "nowplaying".
Copy this file into your own project 'res/layout' directory, and Android will compile a R class for you to access the R.id.nowplaying you miss so much.
You have the source code, so take the resources and build your own R file. Also, you can access android system resources by android.R.<type>.<name> as well.

How to distribute the android reusable code in a package?

I have developed some reusable android component which is basically a class . This class has some resource dependencies e.g. some png drawables, some xml layouts etc. So this class referenced the auto-generated R file.I would like to distribute this code in a single package like jar file to other developers for use in their applications.
I have read that the only possible solution is to distribute code together with all my resources, which others have to copy to their "res" folder (source).
So I created a jar file having the class file (say MyClass which is in the package com.xyz.android.app) and resources and tried to use this in my new application.
So I added the jar file to my new applications build path using add external jars option in eclipse and copied all the resources to my new application's res folder. (The activity class say MainActivity of my new application is in com.abc.myapplication package, just for the case if it may helpful)
But when I run this new application there is java.lang.ClassCastException in the MyClass class. I tried to debug the application and then I found that in the MyClass class, there is "R cannot be resolved" problem.
Then I changed MainActivity's package to com.xyz.android.app (which is not the way, other developers will be happy to do), But again the same problem.
But When I just copy the source java file such that both MainActivity.java and MyClass.java are in com.xyz.android.app package then application runs fine.
So if I need to distribute such that other users need not to bother these package naming things,
how can I accomplish this? Please help !!
Edit
In android.jar, there are also some resources. How are they referenced in a project? How are they used in android classes? There is also android.R file?
Is it not possible to do the same thing i.e. to make jar file like android.jar for my reusable code?
As Nic Strong noted, there is nothing much built into the project to manage this, and current indications are that the upcoming changes to the SDK tools may only help a bit.
I am organizing some other tools to help deal with this problem. I hope to have developer documentation published in a few days.
This is not so easy to do at the moment. The problem is the project using your jar does not know to look in there for drawables etc.
The good news is it should soon be possible (hopefully with SDK 2.2 which is rumoured to be released at IO next week). See this blog post http://mobilebytes.wordpress.com/2010/04/27/android-tools-version6-coming/
I've been playing with Mark Murphy's ParcelHelper class at http://github.com/commonsguy/cwac-parcel. In a nutshell, it's a collection of methods that let you access the components of 'R' by name without needing to import 'R' into your code.
If I understand your question right, this is exactly what you want. You may still need to copy some resources, such as styleables, into your project.

Categories

Resources