I already went through the process of creating an application from the command line. This time I am going to show how to create a library project and integrate it into your app.

To create your library project create a folder and execute this command from inside that folder:

1
2
3
4
android create lib-project \
--name <project_name> \
--target 1 --path . \
--package com.example.whatever

Then you can you ahead and write some code. I created a view that will print a hello message (src/com/ncona/hello/Hello.java):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.ncona.hello;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Hello extends View
{
    /**
     * Initialize the view
     */
    public Hello(final Context context) {
      super(context);
    }

    /**
     * Draws everything
     * @param canvas
     */
    @Override
    protected void onDraw(final Canvas canvas) {
        drawHello(canvas);
    }

    /**
     * Draws a hello message
     * @param canvas
     */
    public void drawHello(final Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setTextSize(20);
        canvas.drawText("Hello World", (float)10, (float)10, paint);
    }
}

then you can create a jar using ant:

1
ant debug

You will find a file named classes.jar in your bin folder. To use this library you just need to copy this jar into your project’s libs folder and the library’s classes will be available for you to use.

It becomes a little more complicated when your project has resource files because resources have to be compiled into your application and made available via the R variable. Lets say we wanted to move the “Hello World” string to a resource file instead of having it in the code. We would have a file similar to this (res/values/strings.xml):

1
2
3
4
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello_world">Hello World</string>
</resources>

And we would make some changes to our code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.ncona.hello;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Hello extends View
{
    /**
     * Context from which this view is being used
     */
    private Context context;

    /**
     * Initialize the view
     */
    public Hello(final Context ctx) {
      super(ctx);
      context = ctx;
    }

    /**
     * Draws everything
     * @param canvas
     */
    @Override
    protected void onDraw(final Canvas canvas) {
        drawHello(canvas);
    }

    /**
     * Draws a hello message
     * @param canvas
     */
    public void drawHello(final Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.WHITE);
        paint.setTextSize(20);
        canvas.drawText(
            context.getString(R.string.hello_world),
            (float)10,
            (float)10,
            paint
        );
    }
}

If you tried to grab the generated jar and add it to your project you would get a run time exception when calling drawHello because R.string.hello_world is not found. For this reason and so all resources from all libraries can be available from within your app we need to do a little more work.

First we need to create a folder where we will add all the libraries we want to compile within our app and copy our library code in there:

1
2
3
4
5
6
7
8
9
10
cd yourApplicationProjectFolder
mkdir linkedlibs
cd linkedlibs
cp -r /path/to/libProjectFolder .
cd libProjectFolder
android update project --path . --target 1
cd ../..
android update project --target 1 --path . \
--library ./linkedlibs/libProjectFolder/ \
--subprojects

Build your project:

1
2
ant clean
ant debug

Note: If get a message complaining about some identifier not working, look at the target version on your project.properties file and make sure it corresponds to your target version. It seems like one of the steps above modifies this number.

Note: Make sure that you remove the jar from your libs folder or the build will fail.

[ android  java  mobile  productivity  ]
Introduction to Bazel
Introduction to Apache Ant
Android development with Docker
Android development with Docker
Adding a drawer to your Android app