Tyler Treat
Tyler Treat

Reputation: 15008

Excluding BuildConfig.class and R.class from Android library jar in Maven

I'm using the Maven Android plugin to build my Android library using the apklib packaging. This produces an apklib archive of the library, so I'm also using the Maven jar plugin to produce a jar artifact that can be used in an app.

My problem is that BuildConfig.class and R.class are being included in the jar, which I don't want (really I would like to exclude anything in gen altogether). I've been trying to create exclude rules for them but haven't had any success. Here's the jar plugin configuration I've been using in my pom:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>              
                <excludes>
                    <exclude>**/BuildConfig.java</exclude>
                    <exclude>**/R.java</exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

Any help is appreciated.

Upvotes: 1

Views: 1999

Answers (1)

Connor Brinton
Connor Brinton

Reputation: 442

The trick here is to apply your configuration to the default-jar phase of the build lifecycle and exclude .class files, rather than .java files. You do this by adding <id>default-jar</id> to your execution as follows:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <executions>
        <execution>
            <id>default-jar</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>  
                <excludes>
                    <exclude>**/BuildConfig.class</exclude>
                    <exclude>**/R.class</exclude>
                </excludes>
            </configuration>
        </execution>
    </executions>
</plugin>

You'll probably also want to exclude the classes R$attr.class, R$drawable.class, R$layout.class, and R$string.class.

Upvotes: 2

Related Questions