简体   繁体   English

如何在IntelliJ IDEA中添加库(android-support-v7-appcompat)

[英]How do I add a library (android-support-v7-appcompat) in IntelliJ IDEA

I created a project, copied the resource files in the project, library, added it to the project structure, prescribed style Theme.AppCompat . 我创建了一个项目,复制了项目中的资源文件,库,将它添加到项目结构中,规定了样式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. 今天花了一整天。

Using Gradle 使用Gradle

If you are using Gradle, you can add it as a compile dependency. 如果您使用的是Gradle,则可以将其添加为compile依赖项。

Instructions 说明

  1. Make sure you have the Android Support Repository SDK package installed. 确保安装了Android Support Repository SDK软件包。 Android Studio automatically recognizes this repository during the build process (not sure about plain IntelliJ). Android Studio在构建过程中自动识别此存储库(不确定简单的IntelliJ)。

    Android支持存储库

  2. Add the dependency to {project}/build.gradle 将依赖项添加到{project}/build.gradle

     dependencies { compile 'com.android.support:appcompat-v7:+' } 
  3. Click the Sync Project with Gradle Files button. 单击“ Sync Project with Gradle Files按钮。

EDIT : Looks like these same instructions are on the documentation under Adding libraries with resources -> Using Android Studio . 编辑 :看起来这些相同的说明在Adding libraries with resources -> Using Android Studio下的文档中

Without Gradle ( Click here for the Gradle solution ) 没有Gradle( 点击这里查看Gradle解决方案

  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). 从Eclipse项目将您的库项目导入Intellij(此步骤仅适用于您在Eclipse中创建库的情况)。

  3. Right click on module and choose Open Module Settings. 右键单击模块,然后选择“打开模块设置”。

  4. Setup libraries of v7 jar file 设置v7 jar文件的库 设置v7 jar文件的库

  5. Setup library module of v7 设置v7的库模块 设置v7的库模块

  6. Setup app module dependency of v7 library module 设置v7库模块的app模块依赖关系 设置v7库模块的app模块依赖关系

Using Maven 使用Maven

First of all you should install android libraries to your local maven repository using Maven Android SDK Deployer 首先,您应该使用Maven Android SDK Deployer将android库安装到本地maven存储库

Then you can add dependency to your pom like this: 然后你可以像这样添加你的pom依赖:

    <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>

This is my solution: 这是我的解决方案:

  1. Copy&paste $ANDROID_SDK/extras/android/support/v7/appcompat to your project ROOT 将$ ANDROID_SDK / extras / android / support / v7 / appcompat复制并粘贴到您的项目ROOT

  2. Open "Project Structure" on Intellij, click "Modules" on "Project Settings", then click "appcompat"->"android', make sure "Library Module" checkbox is checked. 在Intellij上打开“项目结构”,单击“项目设置”上的“模块”,然后单击“appcompat” - >“android”,确保选中“库模块”复选框。

  3. click "YOUR-PROJECT_NAME" under "appcompat", remove "android-support-v4" and "android-support-v7-compat"; 点击“appcompat”下的“YOUR-PROJECT_NAME”,删除“android-support-v4”和“android-support-v7-compat”; ensure the checkbox before "appcompat" is checked. 确保选中“appcompat”之前的复选框。 And, click "ok" to close "Project Structure" dialogue. 然后,单击“确定”关闭“项目结构”对话框。

  4. back to the mainwindow, click "appcompat"->"libs" on the top-left project area. 回到主窗口,单击左上方项目区域中的“appcompat” - >“libs”。 Right-click on "android-support-v4", select menuitem "Add as library", change "Add to Module" to "Your-project". 右键单击“android-support-v4”,选择menuitem“Add as library”,将“Add to Module”更改为“Your-project”。 Same with "android-support-v7-compat". 与“android-support-v7-compat”相同。

After doing above, intellij should be able to correctly find the android-support-XXXX modules. 完成上述操作后,intellij应该能够正确找到android-support-XXXX模块。

Good Luck! 祝好运!

Another solution for maven (and a better solution, for me at least) is to use the maven repository included in the local android SDK. maven的另一个解决方案(至少对我来说是一个更好的解决方案)是使用本地android SDK中包含的maven存储库。 To do this, just add a new repository into your pom pointing at the local android SDK: 为此,只需将新存储库添加到指向本地android SDK的pom中:

<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: 添加此存储库后,您可以添加标准的Google依赖项,如下所示:

<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>

Another yet simple solution is to paste these line into the build.gradle file 另一个简单的解决方案是将这些行粘贴到build.gradle文件中

dependencies {

    //import of gridlayout
    compile 'com.android.support:gridlayout-v7:19.0.0'
    compile 'com.android.support:appcompat-v7:+'
}

As a Library Project 作为图书馆计划

You should add the resources in a library project as per http://developer.android.com/tools/support-library/setup.html 您应该根据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. 然后,在工作区中添加android-support-v7-appcompat库,然后将其添加为应用程序项目的参考。

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. 定义应用程序项目中的所有资源也会起作用(但是要添加很多定义并且您错过了其中一些定义),并且这不是推荐的方法。

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 . 其中{android.home}是Android SDK的根目录,它使用systemPath而不是repository

As an update to Austyn Mahoney's answer , configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'. 作为Austyn Mahoney回答的更新,配置'compile'已经过时,已被'implementation'和'api'取代。

It will be removed at the end of 2018. For more information see here . 它将在2018年底删除。有关详细信息,请参阅此处

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何建立android-support-v7-appcompat? - How do I get android-support-v7-appcompat to build? android-support-v7-appcompat库错误 - android-support-v7-appcompat library error android-support-v7-appcompat支持库不与android 2.2一起使用 - android-support-v7-appcompat support library not woking with android 2.2 找不到库参考../android-support-v7-appcompat - Library reference ../android-support-v7-appcompat could not be found android-support-v7-appcompat库项目不起作用 - android-support-v7-appcompat library project won't work android-support-v7-appcompat中没有样式的父项 - No parent in style in android-support-v7-appcompat 您如何确保将支持库项目(例如android-support-v7-appcompat)的资源(例如样式)添加到您的项目中? - How do you ensure resources (e.g. styles) of a support library project (e.g. android-support-v7-appcompat), are added to your project? appcompat_v7和android-support-v7-appcompat之间的区别? - Difference between appcompat_v7 and android-support-v7-appcompat? 类型容器&#39;Android Dependencies&#39;引用非现有库android-support-v7-appcompat / bin / android-support-v7-appcompat.jar - Type The container 'Android Dependencies' references non existing library android-support-v7-appcompat/bin/android-support-v7-appcompat.jar 尽管使用了android-support-v7-appcompat库,但Android 2.2的操作栏仍无法正常工作 - Despite using android-support-v7-appcompat library, Action bar is working wrong with android 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM