Creating android library project with eclipse

I’ve two versions of my project. One for phones and one for tablet pc. The problem is they share a lot of functionality. The one for phones is using tabhost and the other one not using it but services, activities and permissions are the same more or less.

So I decided to sum these  common points in a library project so I get rid of duplication of work.

First thing is creating a new  android application in eclipse. Name it TestLibProject. Make it Library Project by Right Click on this project and Properties->Android then check the Is Library option. You can find this information from http://developer.android.com/guide/developing/projects/projects-eclipse.html

Anyway as the link says create another project that will be using this library project. Name it TestClientProject. Right click this project and  Properties->Android then in library panel click ‘Add’. You will see your lib project named  TestLibProject. Select and add it.

You will notice that under your   TestClientProject->Library Projects you have  testlibproject.jar exists. That jar contains the classes of your lib project.

Let’s create a dummy service that prints the time to console. It’s really dummy sorry 🙂

The Library Project( TestLibProject  ) :

DummyService.java

public class DummyService extends Service
{

 @Override
 public void onCreate()
 {
 super.onCreate();
 DummyThread dummyThread = new DummyThread();
 dummyThread.start();
 }

 @Override
 public IBinder onBind(Intent arg0)
 {
 return null;
 }

 private class DummyThread extends Thread
 {
 public DummyThread()
 {
 this.setName("DummyThread");
 }

 @Override
 public void run()
 {
 while (true)
 {
 Log.e(getName(), new Date().toLocaleString());
 try
 {
 sleep(1000);
 }
 catch (InterruptedException e)
 {
 e.printStackTrace();
 }
 }
 }
 }
}

AndroidManifest.xml

It is really interesting here. You don’t need to declare anything here. But you will need to declare your services, activities or anything you will use in your client project. But it is advised to declare these in here too.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="your.lib.package" >

    <service android:name=".DummyService" >
    </service>

</manifest>

The Client Project( TestClientProject ) :

TestClientProjectActivity.java:

BEWARE: You may need to restart eclipse to make your client project to recognize DummyService class.

public class  TestClientProjectActivity extends Activity
{
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 Intent intent = new Intent(this,  DummyService.class);
 startService(intent);
 }
}

AndroidManifest.xml:
As I said before we need to declare our service here.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="your.client.package"
 android:versionCode="1"
 android:versionName="1.0" >

 <uses-sdk android:minSdkVersion="8" />

 <application
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name" >
 <activity
 android:name=".TestClientProjectActivity"
 android:label="@string/app_name" >
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />

 <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
 </activity>
 <service android:name="your.lib.package.DummyService" >
 </service>
 </application>

</manifest>

When you run your client project you will see logs like that:

01-27 03:20:43.855 E/DummyThread ( 281): Jan 27, 2012 3:20:43 AM
01-27 03:20:44.913 E/ DummyThread ( 281): Jan 27, 2012 3:20:44 AM
01-27 03:20:45.973 E/ DummyThread ( 281): Jan 27, 2012 3:20:45 AM
01-27 03:20:46.984 E/ DummyThread ( 281): Jan 27, 2012 3:20:46 AM
01-27 03:20:47.993 E/ DummyThread ( 281): Jan 27, 2012 3:20:47 AM
01-27 03:20:49.013 E/ DummyThread ( 281): Jan 27, 2012 3:20:49 AM
01-27 03:20:50.072 E/ DummyThread ( 281): Jan 27, 2012 3:20:50 AM
01-27 03:20:51.093 E/ DummyThread ( 281): Jan 27, 2012 3:20:51 AM
01-27 03:20:52.103 E/ DummyThread ( 281): Jan 27, 2012 3:20:52 AM
01-27 03:20:53.108 E/ DummyThread ( 281): Jan 27, 2012 3:20:53 AM

Our library project works!

Happy coding…

Advertisements

Running scripts on startup/shutdown @Ubuntu

Hi,

As I’m using RxTx libraries for serial communication and I couldn’t handle the locks and permissions on the ports when Tomcat closes( please comment if you know:)), I need to remove locks and give appropriate permissions to ports on startup. Today I saw a post @  http://embraceubuntu.com/2005/09/07/adding-a-startup-script-to-be-run-at-bootup/ and I tried it on my pc. I’m using Ubuntu 11.10.

First write your script and make it executable and move it to /etc/init.d/.

$ chmod +x /home/gokceng/Desktop/myScript.sh

$ sudo cp /home/gokceng/Desktop/myScript.sh /etc/init.d/

$ sudo update-rc.d  myScript.sh start 99 2 3 4 5 .

The parameters on the last line:

myScript.sh : your script to run

start : When your script run, it can be  stop

99 : On start in this scenario, it makes your script to run after everything. It could be 1 for being first or anything between 1 and 99.

2 3 4 5 : run levels of your  script

. : Don’t forget to put this 🙂

My script is like that, it is useless but it is only for explanation:

### BEGIN INIT INFO
# Provides: myScript
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start daemon at boot time
# Description: Enable service provided by daemon.
### END INIT INFO
#!/bin/bash
sudo chmod 666 /home/gokceng/Desktop/myTest

The lines between BEGIN INIT INFO and END INIT INFO is for getting “missing LSB information” error. You can get more information about these lines from http://wiki.debian.org/LSBInitScripts

For more compact way of running your script both on startup and shutdown you can also use this:

update-rc.d yourScript start 20 2 3 4 5 . stop 20 0 1 6 .

You can see other usages check this link: http://translated.by/you/man-update-rc-d-8-install-and-remove-system-v-style-init-script-links/original/

I hope it helps you.

No generator named “X” is defined in the persistence unit – Eclipse JPA

Hi,

If you are working with a JPA project using Eclipse sometimes you need generators. But Eclipse JPA plug-in (Dali) complains about custom generators:

 

@GeneratedValue(generator = “foreign“)
@GenericGenerator(name = “foreign“, strategy = “foreign”, parameters = { @Parameter(name = “property”, value = “yourEntity”) })

 No generator named “foreign” is defined in the persistence unit

 

The solution, I don’t like it though, is  Window->Preferences->Java Persistence->JPA->Errors/Warnings->Queries and Generators and set level of “Generator is not defined in the persistence unit” to warning. You are ok now.