Mateusz Kowalczyk
Mateusz Kowalczyk

Reputation: 2066

Using jar libraries without Eclipse

I have created my project using android create project. I have my own code in package com.notreal under src/notreal/. According to just about everywhere, all I have to do is to put the library into libs/

The library I'm trying to use is Google's Gson library. I have downloaded a .zip which expanded to a directory with couple of .jar files. I moved the whole directory into libs/ and am including com.google.json in my source.

The issue is that it can't see the library. I get error: package com.google.json does not exist. I have tried everything I could find, from using ant -lib lib debug, and -lib libs, doing the both with all the .jar files straight in libs/.

Quite literally every single answer out there assumes that one is using Eclipse. I am not using Eclipse so please don't post how to do it with Eclipse.

Lib here - http://code.google.com/p/google-gson/downloads/detail?name=google-gson-2.2.2-release.zip

ls libs/ - gson-2.2.2.jar gson-2.2.2-javadoc.jar gson-2.2.2-sources.jar

Upvotes: 0

Views: 188

Answers (2)

StarPinkER
StarPinkER

Reputation: 14271

You can specify the classpath for javac in ant. Then the compiler can see the library.

<javac ...>
  <classpath>
    <pathelement location="lib/"/>
    <pathelement path=".../"/>
  </classpath>
</javac>

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007276

Remove the gson-2.2.2-javadoc.jar and gson-2.2.2-sources.jar files, as they are not JAR files containing compiled Java code. From there, you should be OK:

package com.example.asdfasd;

import android.app.Activity;
import android.os.Bundle;
import com.google.gson.Gson;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Gson g=new Gson();
  }
}

With just gson-2.2.2.jar in libs/, this compiles from the command line and Eclipse without issue.

Upvotes: 1

Related Questions