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.
Related
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.
When I make an app with package name com.example.app, src/com/example/app/MainActivity.java is created automatically. I am new to Java and I don't understand
why it uses so many folders inside folders? Why isn't it just src/MainActivity.java?
In order to avoid namespace collisions and conflicts, it's a common best practice in Java nest source code within a folder structure that is the reverse of the internet site associated with it. If everyone created jar library files in the root /src directory, eventually you'd have a collision and the code wouldn't be usable.
For instance, if I have some fancy Android library and I provided a class called Button, in a Button.java class, and you also at times wanted to use some other library that also had a Button.java in /src, your project would not compile.
Thus, in order to let everyone have their own unique Button class, the convention that was adopted was for everyone to use their reverse domain name, followed often by the project name. So the Facebook SDK, fo instance, has /src/com/facebook/android/Util.java while my own project has /src/com/myapp/misc/Util.java and I can use and reference both in my source code.
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.
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.
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.