How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA
Solution 1
Without Gradle (Click here for the Gradle solution)
Import your library project to Intellij from Eclipse project (this step only applies if you created your library in Eclipse).
Right click on module and choose Open Module Settings.
Setup libraries of v7 jar file
Setup library module of v7
Setup app module dependency of v7 library module
Solution 2
Using Gradle
If you are using Gradle, you can add it as a compile
dependency.
Instructions
-
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). -
Add the dependency to
{project}/build.gradle
dependencies { compile 'com.android.support:appcompat-v7:+' }
-
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
.
Solution 3
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>
Solution 4
This is my solution:
Copy&paste $ANDROID_SDK/extras/android/support/v7/appcompat to your project ROOT
Open "Project Structure" on Intellij, click "Modules" on "Project Settings", then click "appcompat"->"android', make sure "Library Module" checkbox is checked.
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.
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!
Solution 5
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:+'
}
Alex Malkov
Updated on August 22, 2020Comments
-
Alex Malkov almost 4 years
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.
-
antonv almost 11 yearsPlease 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 -
Alex Malkov almost 11 yearsThanks, but I do not use Gradle. I do not have Android Studio I program in the IntelliJ IDEA
-
Austyn Mahoney almost 11 yearsI 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.
-
Alex Malkov almost 11 yearsI 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 almost 11 yearsVery, very Thanks, very helpful.
-
Eggman87 almost 11 yearsThanks 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.
-
Maciej Swic over 10 yearsThis saved me a couple hours at least. Thanks a lot!
-
jpkrohling over 10 yearsFor 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".
-
bentzy over 10 yearsThe 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 over 10 yearsThis was usefull as well: code.google.com/p/maven-android-plugin/wiki/ApkLib
-
RichieHH over 10 yearsThis 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.
-
Austyn Mahoney over 10 yearsWhat do you mean "reference the references"? Are you talking about attaching documentation to a jar or Android Library?
-
Austyn Mahoney about 10 years@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. -
Eggman87 about 10 years@AustynMahoney thanks! The issue was fixed with a later release of AS/Gradle.
-
Andreas Wong about 10 yearsCould you elaborate a bit on how you get to step 5? I'd be very grateful... Thanks!
-
Populus almost 10 years@SiGanteng Step 5 is actually done automatically when you perform step 1 & 2
-
Vikram Bodicherla almost 9 yearsIt's not a good idea to ask for the latest version of appcompat. Instead, use this: compile 'com.android.support:appcompat-v7:{targetSdkVersion}.+'
-
zed over 7 years@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.
-
Serob_b over 7 yearssupport-v7 is not just a simple jar file