Home » Android » android – How do I prevent IntelliJ from copying resources into the project compiler output directory?

android – How do I prevent IntelliJ from copying resources into the project compiler output directory?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I have the following directory structure within IntelliJ, which contains an Android library project:

java/com/example/base/AndroidManifest.xml
java/com/example/base/assets # contains Android assets
java/com/example/base/res    # contains Android resources
java/com/example/base/Base.java
java/com/example/base/base.iml

The base.iml contains the following:

<content url="file://$MODULE_DIR$">
  <sourceFolder url="file://$MODULE_DIR$"
                isTestSource="false"
                packagePrefix="com.example.base" />
  <sourceFolder url="file://$MODULE_DIR$/gen" isTestSource="false" />
  <excludeFolder url="file://$MODULE_DIR$/res" />
</content>

When I build an Android application project that depends on my library project, I get an error:

[myapp] java.util.zip.ZipException: duplicate entry: res/drawable/image_shadow.9.png

In the Project Structure dialog, I configured Project compiler output to be an out directory under the root of my project. When I try to build my Android project, I noticed that copies of my Android resources end up under the out/production/base/com/example/base/res/ folder.

My hypothesis is that my Android resources are being treated like “normal” Java resources (the type you would load using Class.getResource()), which is why they are being copied to out/production, which in turn causes the duplicate entry exception that I am seeing.

Any idea if this is the case? If so, how can I prevent these files from being copied over? In Eclipse, the <classpathentry> element in the .classpath file has an excluding attribute that takes a glob pattern to prevent this sort of behavior.

How to&Answers:

I encountered a similar problem because the main dir was marked as “Sources”.

So in project structure, for the Android facet, under the “Structure” tab I had:


...
Resources directory: .../src/main/res
...

And under the normal “Sources” tab for the module, the main dir under src had been marked as “Sources”.

I fixed it by making the Sources tab look like:


- src (unmarked)
- main (unmarked)
- java (Sources)
- res (Resources)

So I guess the error is because the res things are being packed into the zip both as sources and resources hence the duplication.

Answer:

You should have a project structure like:

  • project/src/main/res
  • project/src/main/assets
  • project/src/main/java
  • project/src/main/AndroidManifest.xml

Anyway, for res folder you should have an entry like this:

    <sourceFolder url="file://$MODULE_DIR$/src/main/res" type="java-resource" />