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?
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 :
Or make isNetworkAvailable static :
Then you could call it simply:
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