Android custom Library - android

I have created a custom library that contains a UI that is to display data being read in from an external device. I have successfully created and applied the library to the project I need it in.
My main issue is I cant seem to use it at all. Ive tried to create an instance of the class file and this is failure. I have functions inside that class file I would like to use, but am unable since a simple declaration of a new class throws an exception for me.
Basically I dont want you to fix my problem, I want to see how this is done correctly. Ive searched the Internet for days and can not even find examples on how to do this.
I thought it would work just like creating an instance of any other class, but it does not. Thank you.

It sounds like you need to do some Eclipse project management.
The custom library including the UI and code that accesses the data source should be in an Android library project (see: "setting up a library project"). The Application which will use this functionality needs to include this library project (see: "referencing a library project").
See the Android docs about Managing Projects from Eclipse.

Related

How to create Android custom view library in Android Studio?

I've created a custom view that extends a RelativeLayout for a project I'm working on. I think it might be useful to others, so I've been trying to set it up as a library so others can find and use it.
I've haven't been able to find a clear answer on how to go about setting this up in Android Studio.
To be clear - I have already built the view and it is working fine, I just want to package it up as a library.
Do I create a new project and simply put my code there, or do I need to make a new module too? Once I do either of those things, which parts of the manifest and gradle files do I need to change?
Lastly, do I need to somehow get an AAR from the files or is it best to just upload it to GitHub and let others clone it?
In Android Studio (v 1.0.2):
File -> New module... -> Android Library
In this new module put all the code you want to share. Android Studio should update Gradle's settings accordingly.
You can publish your library as source code and/or artifacts in some Maven/Gradle repository. Publishing source code allow others to contribute to your work, fixing bugs or extending functionality. However, if you just do that, it won't be very convenient to reuse it for other developers, because working with source requires checkout and build. If you publish also artifacts (in some Maven/Gradle repository) built from that code, you will simplify the life of people using your code :)

Do the main project and the library project must share the same package?

I started creating a library that will be shared with several projects.
The problem is that the Android documentation is really short about how to handle properly the library projects. I got really stuck dealing with the conflicts between the few classes I have in common in both the main projects and the library project.
I checked the sample from Android (Tic Tac Toe) but it is really too basic to help, but I just remarked that they used the following packages:
The main project: com.example.android.tictactoe
The library project: com.example.android.tictactoe.library
Is the library supposed to be always contained in the main project package? If yes, how to do when several projects have to share the same library project since the main package name belongs to one main project only?
My biggest problem right now is that my app is calling a class from the library project instead of the main project, and I have really no idea what is the best approach to follow.
Thanks!
Update: My question is related to this one: Best practice: Extending or overriding an Android library project class
There are lots of 3rd party libraries available (such as Action Bar Sherlock) and they do not share the same package as yours.
The packages for the library project and the main app project have no requirement of being related in any way.
In some cases it does help having the library as a sub-package of your main app package: this comes from the way you split the project and if the package for your library being a sub-package would help with organising the code, by all means make it a sub-package of your main package; otherwise, don't. It's all about structuring the code in the end.
It is ok for your app to call (actually use would be the work) classes from your library project. That's what libraries are for. They help keep code contained, reusable and decoupled from other pieces of code.
The problem is the other way around: when your library is calling a class from your main project.
The most common approach for Java and Android here is to use an interface: the library provides the interface that the main project implements, then the main project passes an instance of that implemented interface for the library project to use. The best example here would be callbacks. Android provides a set of interfaces (such as View.OnClickListener) that allow you to hook into the system and perform your application's logic (from the application's point of view, the Android classes in the SDK are the libraries).
Other approaches would be providing classes to be extended (this is just like providing interfaces, but it's used when you want to either leave the library user the option of not implementing/overriding all the methods or when you want to keep some base functionality in the object you will be receiving in your library through the use of the final modifier) and reflection (stay away from it as long as possible as it is very error-prone).

Create Android project programmatically in eclipse

In short: Is there a supported way to create an Android project for eclipse (as you would get via the new Android project wizard) programmatically? Or if not supported, then is there any way at all? (this is being done within an eclipse plugin)
I've been trying to do this for a while, and it is turning out to be much more difficult than expected. I took a look at the source for both the New and the Import wizards for Android projects, and found that they both use the NewProjectCreator class. There seem to be two ways to use this class; one is via the static "create" method, and the other is to construct an instance of NewProjectCreator and call "createAndroidProjects". However, I'm having trouble getting ahold of all the necessary parameters for either of these. The static method requires both an IAndroidTarget and a ProjectPopulator, neither of which I can find implementations of (There is an anonymous implementation of ProjectPopulator in the NewProjectWizard class, but pasting this into my code is disastrous in the number of classes and fields it can't find). The instance method on the other hand requires an IWizardContainer, but I would like to be able to do this without bringing up a wizard.
At the other end of the spectrum, I've tried simply adding and Android nature to the project, but this causes all sorts of build errors, since it doesn't actually change the setup of the project to reflect whatever the Android standards are. I guess another option is to take this route and try to set up the whole project manually, but I don't know if there are any guidelines for this, or whether the project standards are likely to change.
Does anyone know how to do this? Thanks in advance!

Using custom Android widget from one project in another

I'm attempting to use a custom Android vertical scrollbar widget that seems to work fine in its own example project, but I am having trouble trying to include it into my project. I've exported a .jar from it to use in my project, but it seems to be a minefield of problems, from duplicate resources, to my XML layouts not able to get at styles inside the library, and errors "inflating class" when reading my XML layout file.
How should I go about using the widget from another project in my own?
Quite new to Java development (know the language, but not much of the overarching project/package management), so if there's some good text regarding that it would be appreciated.
Create an Android Library Project for the widget.
As far as I can understand the problem should be related to the "R" class which is created in the same package as the main activity. When you create the new project this class is located in "the wrong place".
Giving up compatibility and require the use of the same package in all projects is not an option: market will not allow you to publish more than one application per namespace. Moreover, is is not a good practice for software engineering.
A much better solution is, as already pointed out by fhucho, to create a library project with your widget to be imported in the main application project.
If you are also looking for a book on android development and already know java I usually recommend "Hello Android" by Ed Burnette, it is quick to read but gives a nice overview with examples.

Sharing Android code (Is there really no way to do this?)

I have a particular collection of code along with some XML files that I need to share with every application I will make.
At the moment I can't because as far as I am aware there is no way to do this. This seems like a massive oversight by the development team.
If the code needs changing, I have to change it in every app that I create - and will create in the future.
Are there any ways to share code in android yet?
I am using Eclipse for development.
You can create an Android library project. The TicTacToeMain sample project in the SDK shows how to reference your created library project.
You can reference a third party JAR like you would any Java project. Or are you talking about something else?
As for sharing XML files, I think you may have to just copy them to each project, but I'm not certain about that.
You can reuse particular Activity classes in applications other than the one they were installed with. Is that sufficient?
To do that, you need to set android:exported="true" in the Activity's declaration in AndroidManifest.xml. http://developer.android.com/guide/topics/manifest/activity-element.html has more details.

Categories

Resources