How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA
Asked Answered
S

9

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.

Schlieren answered 2/8, 2013 at 20:53 Comment(2)
Ran into the same problem, check https://mcmap.net/q/88805/-how-to-add-android-support-v7-widget-gridlayout-into-intellijidea for a simple and clean way to solve it.Hepzi
youtube.com/watch?v=MIzgaMiyKoMAccurate
B
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

Beestings answered 8/8, 2013 at 12:23 Comment(2)
Could you elaborate a bit on how you get to step 5? I'd be very grateful... Thanks!Hilariahilario
@SiGanteng Step 5 is actually done automatically when you perform step 1 & 2Triiodomethane
U
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.

Uboat answered 2/8, 2013 at 21:47 Comment(9)
Thanks, but I do not use Gradle. I do not have Android Studio I program in the IntelliJ IDEASchlieren
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.Uboat
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.Donny
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".Mandal
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.Proctology
What do you mean "reference the references"? Are you talking about attaching documentation to a jar or Android Library?Uboat
@Donny 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.Uboat
@AustynMahoney thanks! The issue was fixed with a later release of AS/Gradle.Donny
@Donny 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.Somnifacient
B
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

Beestings answered 8/8, 2013 at 12:23 Comment(2)
Could you elaborate a bit on how you get to step 5? I'd be very grateful... Thanks!Hilariahilario
@SiGanteng Step 5 is actually done automatically when you perform step 1 & 2Triiodomethane
X
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>
Xerarch answered 3/9, 2013 at 9:9 Comment(2)
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 dependenciesRushy
This was usefull as well: code.google.com/p/maven-android-plugin/wiki/ApkLibRushy
C
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!

Castrato answered 1/7, 2014 at 7:29 Comment(0)
T
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:+'
}
Tenrec answered 18/8, 2015 at 14:28 Comment(1)
It's not a good idea to ask for the latest version of appcompat. Instead, use this: compile 'com.android.support:appcompat-v7:{targetSdkVersion}.+'Kilauea
B
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>
Barricade answered 19/3, 2015 at 19:46 Comment(0)
G
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.

Gingerly answered 2/8, 2013 at 21:15 Comment(2)
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.jarTireless
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 errorSchlieren
V
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.

Vedette answered 6/5, 2015 at 15:51 Comment(1)
support-v7 is not just a simple jar fileGalina
L
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.

Lac answered 22/8, 2018 at 0:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.