jul
jul

Reputation: 37464

How to declare a non-activity class in the manifest file of an Android library?

in order to refactor my code, I'm moving some methods I'm using in every project to an Android library.

Eg. I've created a simple library project with the following class:

public class HttpUtils {

    public boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) 
          context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    } 
}

and the following (default) manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxx.utils"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    </application>

</manifest>

I can import it in another Android project using

import com.xxx.utils.http.HttpUtils;

but if I try using HttpUtils.isNetworkAvailable, I get HttpUtils.isNetworkAvailable cannot be resolved to a type.

I read that, for a library activity, it should be added to the manifest file, but how can I add a simple class, like my HttpUtils, in the library manifest to be able to access its method from another project?

Upvotes: 0

Views: 1275

Answers (2)

gezdy
gezdy

Reputation: 3322

Why would you like to add this class like a library. Add it just in your package, then you can call it from any Activity like that :

boolean isNetworkAvailable = new HttpUtils().isNetworkAvailable(this);

Or make isNetworkAvailable static :

public static boolean isNetworkAvailable(Context context) {....}

Then you could call it simply:

boolean isNetworkAvailable = HttpUtils.isNetworkAvailable(this);

In this way, there is nothing to declare in the manifest file.

EDIT : ok, I understand. You have several projects. So for this, you have 2 choices :

1)you can create from eclipse new project and in the second window select "Mark this project as a library". Then in all your project add this library project in the build path

2)create a jar file

Upvotes: 3

CommonsWare
CommonsWare

Reputation: 1006554

how can I add a simple class in the library manifest

You don't.

You have some other problem, such as:

  • not having the library project correctly attached to the host project
  • having the wrong import (e.g., to some other class named HttpUtils)

It would help if you had the correct error message. AFAIK, it is not possible for "cannot be resolved to a type" to be put on the name of a method.

Upvotes: 1

Related Questions