Thomas Kaliakos
Thomas Kaliakos

Reputation: 3304

Is it possible to create jar that includes android functions?

As the topic indicates I would like to create a jar library that uses some android functions (no layouts) and that will be included in an Android project. Is that possible and how? From the research I've made I managed to include a simple jar file that uses pure Java (JAVA SE 1.6), but when I tried creating a jar file I encountered the following exception when I tried to run the Andoid app:
FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: mylib.pleasework.amen

I tried including android.jar in my library and removing the java library, so that the jar file is build against android sdk, but it didn't work.
I tried including the jar file under a /libs folder as it is said to be the correct way to import jars in android projects from ADT v17 and after, but that didn't work either.
The jar I want to create will not use any resources (xml layouts, strings.xml) just Log.d and WifiManager.
I am aware of Android Library Project but my library source is sensitive and I am afraid that it won't be safe if exposed in a Android library project. I was thinking of creating a jar and using ProGuard ( http://developer.android.com/tools/help/proguard.html ) obfuscate it. I think I mentioned everything. Any help will be appreciated.

Thanks,
Thomas

Upvotes: 0

Views: 229

Answers (2)

Thomas Kaliakos
Thomas Kaliakos

Reputation: 3304

The way I did it in the end was: to create an Android Library project (check isLibrary checkbox in project properties) export it through Eclipse (right click on the project->export->jar file, careful to deselect all resources - res folder, androidmanifest.xml, *.png etc) and put it in the project you want by importing it under /libs folder.
I don't know if this is the best solution but it worked for me.
Used ADT r20, Eclipse 3.7.1, Android api level 7

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007379

As the topic indicates I would like to create a jar library that uses some android functions (no layouts) and that will be included in an Android project. Is that possible and how?

Use the jar command, or the <jar> Ant task. I am sure that there are ways to export a JAR from Eclipse, but I personally have never used them.

For example, in this GitHub repo I have a reusable component and a sub-project that is a sample app. My build.xml for the repo contains the following custom task:

<target name="jar" depends="debug">
    <jar
        destfile="bin/CWAC-WakefulIntentService.jar"
        basedir="bin/classes"
    />
</target> 

This generates a JAR file, that other Android applications can use by adding to their libs/ directories.

I am aware of Android Library Project but my library source is sensitive and I am afraid that it won't be safe if exposed in a Android library project.

It won't be safe exposed as a JAR, then, either. You can create an Android library project for public consumption that replaces the src/ tree's contents with a compiled JAR in libs/ in the library.

Upvotes: 1

Related Questions