72

I created a project, copied the resource files in the project, library, added it to the project structure, prescribed style Theme.AppCompat. Compiled without errors, but when you start the relegation Exception:

08-03 00:50:00.406: ERROR/AndroidRuntime(4055): FATAL EXCEPTION: main
        java.lang.NoClassDefFoundError: android.support.v7.appcompat.R$styleable
        at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:98)
        at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
        at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
        at com.example.SampleMetrRost.CentralActivity.onCreate(CentralActivity.java:12)
        at android.app.Activity.performCreate(Activity.java:4636)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1051)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1924)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
        at android.app.ActivityThread.access$600(ActivityThread.java:127)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4476)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:816)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:583)
        at dalvik.system.NativeStart.main(Native Method)

Help me, please. Spent all day today.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
Alex Malkov
  • 823
  • 1
  • 7
  • 6

9 Answers9

118

Using Gradle

If you are using Gradle, you can add it as a compile dependency.

Instructions

  1. Make sure you have the Android Support Repository SDK package installed. Android Studio automatically recognizes this repository during the build process (not sure about plain IntelliJ).

    Android Support Repository

  2. Add the dependency to {project}/build.gradle

    dependencies {
        compile 'com.android.support:appcompat-v7:+'
    }
    
  3. Click the Sync Project with Gradle Files button.

EDIT: Looks like these same instructions are on the documentation under Adding libraries with resources -> Using Android Studio.

Community
  • 1
  • 1
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
  • 3
    Thanks, but I do not use Gradle. I do not have Android Studio I program in the IntelliJ IDEA – Alex Malkov Aug 02 '13 at 21:56
  • 6
    I mostly added this in case someone else found your question and used Gradle. This should work if someone has the Android plugins for IntelliJ and uses Gradle. – Austyn Mahoney Aug 02 '13 at 22:11
  • Thanks for adding. Have you ever added the support library to a android library project and tried to run a app depending on the android library project? I seem to always get a "already added" exception while compiling. Not sure why - it is only added on the library project. – Eggman87 Sep 21 '13 at 02:08
  • For the record: it works the same way with plain IntelliJ. You'll just need to "Make project" once you finish installing the "Android Support Repository". – jpkroehling Feb 06 '14 at 11:31
  • This does not work. You still need to add it as a library in order to reference the references. I dont know why. It's been confusing me all day too. – RichieHH Mar 13 '14 at 20:48
  • What do you mean "reference the references"? Are you talking about attaching documentation to a jar or Android Library? – Austyn Mahoney Mar 13 '14 at 21:54
  • 1
    @Eggman87 I know this is late, but try `com.android.support:support-v4:+` in your library project. This will tell it to use whatever version the project using it provides. – Austyn Mahoney Mar 31 '14 at 17:00
  • @AustynMahoney thanks! The issue was fixed with a later release of AS/Gradle. – Eggman87 May 30 '14 at 04:21
  • @Eggman87 Actually as this '+' in the end instead of version worked for me now.. The error resurfaced bunch of times randomly, but now with this it works. Thanks for this, stopped wasting my time. – zed Nov 10 '16 at 14:37
85

Without Gradle (Click here for the Gradle solution)

  1. Create a support library project.

  2. Import your library project to Intellij from Eclipse project (this step only applies if you created your library in Eclipse).

  3. Right click on module and choose Open Module Settings.

  4. Setup libraries of v7 jar file Setup libraries of v7 jar file

  5. Setup library module of v7 Setup library module of v7

  6. Setup app module dependency of v7 library module Setup app module dependency of v7 library module

Community
  • 1
  • 1
Tsung Wu
  • 1,064
  • 9
  • 17
10

Using Maven

First of all you should install android libraries to your local maven repository using Maven Android SDK Deployer

Then you can add dependency to your pom like this:

    <dependency>
        <groupId>android.support</groupId>
        <artifactId>compatibility-v7-appcompat</artifactId>
        <version>${compatibility.version}</version>
        <type>apklib</type>
    </dependency>

    <dependency>
        <groupId>android.support</groupId>
        <artifactId>compatibility-v7-appcompat</artifactId>
        <version>${compatibility.version}</version>
        <type>jar</type>
    </dependency>
Bersh
  • 2,789
  • 3
  • 33
  • 47
  • The Deployer didn't work for me. Instead I followed these steps: 1) Goto ANDROID_HOME\sdk\extras\android\support\v7\appcompat 2) Create a zip file of the entire library and call it appcompat.apklib 3) Open cmd in this location and type "mvn install:install-file -Dfile=appcompat.apklib -DgroupId=com.google.android -DartifactId=support-v7-appcompat -Dversion=r7 -Dpackaging=apklib" 4) Goto /libs 5) Type "mvn install:install-file -Dfile=android-support-v7-appcompat.jar -DgroupId=com.google.android -DartifactId=support-v7-appcompat -Dversion=r7 -Dpackaging=jar" 6) Add the appropriate dependencies – bentzy Feb 13 '14 at 09:56
  • This was usefull as well: https://code.google.com/p/maven-android-plugin/wiki/ApkLib – bentzy Feb 13 '14 at 09:57
4

This is my solution:

  1. Copy&paste $ANDROID_SDK/extras/android/support/v7/appcompat to your project ROOT

  2. Open "Project Structure" on Intellij, click "Modules" on "Project Settings", then click "appcompat"->"android', make sure "Library Module" checkbox is checked.

  3. click "YOUR-PROJECT_NAME" under "appcompat", remove "android-support-v4" and "android-support-v7-compat"; ensure the checkbox before "appcompat" is checked. And, click "ok" to close "Project Structure" dialogue.

  4. back to the mainwindow, click "appcompat"->"libs" on the top-left project area. Right-click on "android-support-v4", select menuitem "Add as library", change "Add to Module" to "Your-project". Same with "android-support-v7-compat".

After doing above, intellij should be able to correctly find the android-support-XXXX modules.

Good Luck!

dialox
  • 57
  • 1
  • 2
4

Another yet simple solution is to paste these line into the build.gradle file

dependencies {

    //import of gridlayout
    compile 'com.android.support:gridlayout-v7:19.0.0'
    compile 'com.android.support:appcompat-v7:+'
}
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
  • 4
    It's not a good idea to ask for the latest version of appcompat. Instead, use this: compile 'com.android.support:appcompat-v7:{targetSdkVersion}.+' – Vikram Bodicherla Sep 06 '15 at 20:27
3

Another solution for maven (and a better solution, for me at least) is to use the maven repository included in the local android SDK. To do this, just add a new repository into your pom pointing at the local android SDK:

<repository>
    <id>android-support</id>
    <url>file://${env.ANDROID_HOME}/extras/android/m2repository</url>
</repository>

After adding this repository you can add the standard Google dependency like this:

<dependency>
    <groupId>com.android.support</groupId>
    <artifactId>support-v13</artifactId>
    <version>${support-v13.version}</version>
</dependency>

<dependency>
    <groupId>com.android.support</groupId>
    <artifactId>appcompat-v7</artifactId>
    <version>${appcompat-v7.version}</version>
    <type>aar</type>
</dependency>
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
JavierSP1209
  • 899
  • 8
  • 17
1

As a Library Project

You should add the resources in a library project as per http://developer.android.com/tools/support-library/setup.html

Section > Adding libraries with resources

You then add the android-support-v7-appcompat library in your workspace and then add it as a reference to your app project.

Defining all the resources in your app project will also work (but there are a lot of definitions to add and you have missed some of them), and it is not the recommended approach.

Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
IanB
  • 3,489
  • 1
  • 20
  • 24
  • Please also note, that if you defining all resources from appcompat project to your project, you should check that R.java generated file should be in `android.support.v7.appcompat` package. Otherwise, resources won't be visible by android-support-v7-appcompat.jar – antonv Aug 02 '13 at 21:45
  • 1
    I am using IDEA, tell me, please, how to add a library with resources. Resources he sees compile without errors, but when you start getting out the error – Alex Malkov Aug 03 '13 at 07:20
0

This is my solution, it is very similar to the previous one:

<dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v7</artifactId>
        <scope>system</scope>
        <systemPath>${android.home}/support/v7/appcompat/libs/android-support-v7-appcompat.jar</systemPath>
        <version>19.0.1</version>
</dependency>

Where {android.home} is the root directory of the Android SDK and it uses systemPath instead of repository.

db80
  • 4,157
  • 1
  • 38
  • 38
0

As an update to Austyn Mahoney's answer, configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.

It will be removed at the end of 2018. For more information see here.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135