Borrowing code from another android application - android

So I'm trying to make myself a little alarm clock app to learn android. Just to do some things that I've always wanted in an alarm clock, and have it be my own. Motivations aside, after coding the interface and a bunch of other functions, I've decided to borrow a bunch of the code, if not most, of the android alarm clock source.
I've already brought in all the res stuff, and I've included all the src files under their original com.android.alarmclock namespace so that they can still reference each other properly. However it refuses to generate the R file for the new files.
When I clean and rebuild and all that, it still generates a build file and an R file for the stuff in my package (com.nathantempelman.alarmapp) but I still get an error on all the android AlarmClock files.
Should I be changing the AlarmClock files so that they reference the R from my package somehow? Or did I miss something when I updated the AndroidManifest file that is stopping another R from building or something?
Or is it something else entirely? Is it a stupid idea to try to drag another application's source into mine? Should I open it up in an external project and try to reference it somehow?
I'd love to hear some ideas, if anyone has had a similar situation.
Thanks for reading.

That is not how it's done. You want your Alarmapp to extend ALarmClock or extend the ALarmCLock classes or interfacces you want or need.
You can't drag source code in like that without re-factoring and tieing up all the loose ends, hence alarmApp extends AlarmClock.
Take a look here Hope this helps

I tried the library method, but that didn't work. In the end, I ended up just dropping the smaller project into the bigger project, combining the strings xml and android manifest, changing the intent classes of the activities in the android manifest, and dragging all the relevant pictures in. Quite a bit of work in the end, but it can be done.
Quick checklist:
Splice android manifest files
- if the package names are different, make sure the package name at the top of your manifest only extends as far as both packages are the same
Make sure all the resources make it over
Splice the strings.xml, styles.xml, and anything else that is common
between the two projects
Thought I'd post this in case someone else tries to do the same thing eventually. Best of luck

Related

Convert an Android project in a library to replicate the App with few changes

I'm having a lot of doubts and after two days of searching I still haven't found a good answer for my problem.
I have an app's project that is already in the store and this project have lots of Activities, Fragments, Services, IntentServices, Layouts, and resources (images, strings, integers, colors, styles, raws, drawables, etc...).
Now, I need to implement a new app that is almost a copy of the first one where I need only to change the background, some resources (but almost 99% stay the same), and maybe in one or two fragments I'll need to change some code (implement different logic statements).
I decided and I think it's natural to convert the all project in an library project.
I did:
1º Create a new project called framework and configured as library. Copied all code and resources (with the exception of assets folder) from the original project to here.
2º In the original project I leave only the manifest, proguard file. I change the manifest and proguard file to reference the classes of the new library.
I tested the app to see if was still working and it was Ok. Until now I did only the separation.
Now, came the problem. I need to create a solution reusable,and scalar for the library that allows me to use it in different apps.
In my original project I created a subclass of Application class that I use for global variables.
Without any others ideas, I decided the following:
1º Convert my MyApplication class in an abstract class. Also, I create an interface ApplicationInterface with some methods like isBackgroundOverlayed() or getBackgroundResource() that I use in the fragments to change the backgrounds or any other logic from the originally defined.
2º In the App's project, I created a new Application class that extends the abstract class MyApplication and implements the interface ApplicationInterface and which is declared in the Application tag in the manifest file.
Conclusion: This all works and I was able to have two app's projects with different background and other images like logos and even change some code logic. An example is in the original App when the user press one certain button, the App shows a dialog choice. In the new app when the user press the button it execute one fragment (without choice).
My concerns: I don't feel this is the best solution for the problem or best practice or good "pattern".
Do you have any ideas?
Thank you in advance!
ps: Sorry if I haven't use this forum properly. I search for an answer but I didn't found any.
I think you have achieved what you wanted, but that was a lengthy and time consuming process. One more thing I would like to mention is you should not change library projects for different applications
As said on Android developers guide :
Library projects contain shareable Android source code and resources that you can reference in Android projects. This is useful when you have common code that you want to reuse.
you can read more from here
Also you have to figure out,which lib is doing which work in case you forget what changes you have made
I would suggest you to keep a separate copy of original project and then import it into new work space in Eclipse (by checking copy project into current work space so that you have a new copy of your project) and consider changing its package name to get a new project
you can refer ans on this link to see how to change package name`

Using R resources from an android jar file

This has been talked about a few times, with varying states of success but most of the answers I can find are several years in the past.
Essentially, I have an application which has moved to a point where isn't being shared with customers. Customers want to extend the application but we don't want them to have access to the original code -- protecting IP on all that.
The layouts, images, etc... well, we don't care about those as much, only the java code. I could easily compile the java code into a jar and distribute it, but that's only half the answer. The java code still references a whole pile of internal resources.
The scenario I'm working with is jar file contains the following:
In Jar file
- MyActivity extends Activity
- onCreate() within MyActivity sets the contentView to an R-referenced layout from within the jar file.
In client application
- MyClientActivity extends MyActivity
When this is run, I get a ClassNotFoundException on MyClientActivity.
Things I've noticed:
- If I attach the jar code as a library, instead of a jar file, it works.
- If I remove the R reference within onCreate() in MyActivity and remake my jar file, it works.
So, obviously, as pointed earlier, the R references are dead/not accessible. The thing is, this apparently can work, no? If you create an Android application and look at the auto-included APIs, the android.jar file has an R file, with internal layouts/strings/etc... which are being references from within its code.
As others have said before, this seems like a very basic and obvious usage pattern. The fact that it seemingly doesn't work is mind boggling enough to lead one to believe that it does in fact work, it's just not done in an obvious way.
Has anyone ever actually gotten it to work?
Thanks a lot,
-Cord Awtry

R cannot be resolved for inflating raw queries

I know this is a pretty common question and I looked around the web and this forum for an answer but none of them seem to be working for me. I did the typical stuff like deleted my R.java and cleaned my project, made sure my class did not have a import for the R.java class. I tried rebuilding my project etc.
So here is what I have going on. I am trying to inflate a database from some raw SQL statements. I am using the book The Busy Coders Guide to Advanced Android Development book as a guide to do this.
it gives the following line of code:
InputStream stream=ctxt.getResources().openRawResource(R.raw.packaged_db);
and says the file is located within the res/raw directory like so "res/raw/packaged_db.txt"
I have placed my sql dump file: res/raw/raw_game_data.sql
and here is my line of code that is throwing the error:
InputStream inputStream = context.getResources().openRawResource(R.raw.raw_game_data);
any ideas or suggestions on what I am missing?
Thanks,
Generally, only one R.java is built for your entire project, and it is built in only one place, with one package - the package of your app, declared in your manifest. If that package is com.example.trek, and you refer to R in a class in com.example.trek.tribbles - or any other package - you will need to explicitly import com.example.trek.R
You list off some corrective measures without indicating that you understood the actual problem they were intended to correct, or if you had that problem. I'm not unsympathetic to this attitude in the context of Eclipse, but you can waste a lot of time this way. If you understand Java, then you only need to learn aapt to be able to drop down to the command line and attempt the process yourself.
That said, developers.android.com seem to have removed the documentation for aapt. ...well, don't worry, I'm sure you'll never have problems with Eclipse!

NetBeans and Android image

New to Android development and have decided to use NetBeans 6.9.1 as my IDE. So far the process has been somewhat painful, but I'm getting things rolling. However, I am creating an ImageView subclass for my first custom View and I can't figure out how to add my Box.png file to the project. Drag and Drop doesn't work, there are no right-click options to add a file to the Resources folder, no dropdown menus to add images, no way to add the image to a package. Could use some insight, thanks!
Just go to the project folder and copy the images you want into the res/drawable folder. The IDE helps you a lot with code completion, error checking, etc... but that simple task can be done by hand.
Then, you can reference your resources by using something like: R.drawable.image Notice that I'm not using the image extension. If you wonder what R is, let me give you a brief explanation:
Each resource that is saved in the resources directory is referenced in the R class. That's a file that is autogenerated by Android and it's used to reference those resources from your code. In this case, it will be in R.drawable.* since it's a drawable resource. There are other kind of resources, like layouts: R.layout.something or strings R.string.whatever. That's essential for the android development, so you better read some tutorials (or buy books) in order for you to get started.
So, in your case will be something like setImageDrawable(R.layout.wood); However, I highly recommend to read first a couple of tutorials. Google about it, you will find tons of them.

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.

Categories

Resources