user3813658
user3813658

Reputation: 11

Trouble adding PDFBox to an Android application with Gradle?

I'm working on a project that assembles and builds with Gradle (though I've been using eclipse to write the code), and have been having trouble using Apache's PDFBox. I have set the classpath to the PDFBox .jar, typing echo %CLASSPATH% in cmd returns:

C:\Users\MY_NAME\.m2\repository\org\apache\pdfbox\pdfbox\1.8.6\pdfbox-1.8.6.jar

I was doing research on this earlier and someone said that you need to start the classpath with .;, which had no effect on any of my attempts to fix my problems. Anyways, when I try to compile the program with the gradlew.bat wrapper, I get these errors:

error: package org.apache.pdfbox.pdmodel does not exist
error: package org.apache.pdfbox.util does not exist

the lines these errors reference are at the very top of my .java file, simply:

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.util.*;

I tried multiple different ways of editing the build.gradle file based on both the gradle documentation and other examples of build.gradle files I have seen online. The base build.gradle file is as follows:

allprojects {
    repositories {
        mavenCentral()
        maven {
            url "https://nexus.spritzinc.com/content/repositories/PublicReleases"
        }
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

I have tried adding a dependencies{} section in multiple places. The build.gradle file looking like this:

allprojects {
    repositories {
        mavenCentral()
        maven {
            url "https://nexus.spritzinc.com/content/repositories/PublicReleases"
        }
    }
}
dependencies {
    compile 'org.apache.pdfbox:pdfbox:1.8.6'
}
task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

compiled just fine, but gave me the same include errors described above. Putting the contents of dependencies{} right after repositories{} in allprojects{} gave me the following error:

Could not find method compile() for arguments [org.apache.pdfbox:pdfbox:1.8.6] on org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler_Decorated@7051777c.

I have also tried adding apply plugin: 'java' and apply plugin: 'eclipse' at the top of the build.gradle file, but no combination of any of these fixes work. I have copy/pasted the pdfbox-1.8.6.jar file into the project's libs folder, and that doesn't seem to help it either. Is there anyone out there who can help me include the pdfbox-1.8.6.jar file in my gradle build?

Upvotes: 1

Views: 4069

Answers (1)

Tilman Hausherr
Tilman Hausherr

Reputation: 18861

The only time I got the error you got was when I removed the "apply plugin" line. Anyway, here's my gradle script that worked:

apply plugin: 'java'

sourceCompatibility = '1.7'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'org.tilman.HelloWorld'
}

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.10'

    compile 'org.apache.pdfbox:pdfbox:1.8.6'
}

and here's my java program:

package org.tilman;

import org.apache.pdfbox.pdmodel.PDDocument;

public class HelloWorld
{
    public static void main(String[] args)
    {
        PDDocument doc = new PDDocument();
        System.out.println("Hello world: " + doc);
    }
}

Upvotes: 2

Related Questions