向 Android Studio 项目添加支持库

我刚刚安装了新的 Android Studio,正在寻找导入 Android 支持库的方法。

还有什么选择呢?在 Eclipse 中只需两次单击。我谷歌了一下,什么都没找到。肯定是太新了。

112571 次浏览

= = = = = = = = = = = = = = = = UPDATE = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

因为 Android Studio 引入了一个新的构建系统: 格拉德尔。Android 开发人员现在可以使用一个简单的、声明性的 DSL 访问一个单独的、权威的构建,这个构建可以为 Android Studio IDE 和命令行构建提供动力。

像这样编辑你的 build.gradle:

apply plugin: 'android'


android {
compileSdkVersion 19
buildToolsVersion "19.0.3"


defaultConfig {
minSdkVersion 18
targetSdkVersion 19
versionCode 1
versionName "1.0"
}


buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}


dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.+'
}

注意: 在 compile 'com.android.support:support-v4:21.+'中使用 +,以便评级总是可以使用最新的版本。

= = = = = = = = = = = = DEPRECATED = = = = = = = = = = = = = = =

因为 Android Studio 是基于 IntelliJ IDEA 的,所以这个过程和 IntelliJ IDEA 12 CE 的过程是一样的

1.Open 工程项目结构 (Press F4 on PC and Command+; on MAC) on your project).

2.Select Modules on the left pane.

3. 选择你的项目,你会在第三栏上方看到 依赖性标签。

4.点击底部的加号。然后将弹出一个基于树的目录选择对话框,导航到包含 android-support-v4.jar 的文件夹,按 好的

5. 按 好的

我有一段时间不再从事 Android 项目了。 虽然下面提供了一些关于如何配置 android 工作室项目的线索,但是我不能保证它能完美地工作。

原则上,IntelliJ 尊重构建文件,并将尝试使用它来配置 IDE 项目。反之亦然,IDE 的更改通常不会影响构建文件。

由于大多数 Android 项目都是由 Gradle 开发的, 了解这个工具总是一个好主意。

I'd suggest referring to @skyfishjy's answer, as it seems to be more updated than this one.


下面的内容没有更新

虽然 android 工作室是基于 IntelliJ IDEA,但同时它也依赖 gradle 来构建你的 apk。从0.2.3开始,这两种方法在 GUI 配置方面不能很好地发挥作用。 As a result, in addition to use the GUI to setup dependencies, it will also require you to edit the build.gradle file manually.

假设您有一个测试项目 > 测试结构。 要查找的 build.gradle 文件位于 TestProject/Test/build.gradle

Look for the dependencies section, and make sure you have

编译‘ com.android.support: support-v4:13.0. +’

下面是一个例子。

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'


repositories {
mavenCentral()
}


dependencies {
compile 'com.android.support:support-v4:13.0.+'
}


android {
compileSdkVersion 18
buildToolsVersion "18.0.1"


defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}

您还可以从 maven 存储库添加第三方库

编译组: ‘ com.google.code.gson’,名称: ‘ gson’,版本: ‘2.2.4’

The above snippet will add gson 2.2.4 for you.

在我的实验中,似乎添加分级也会为您设置正确的 IntelliJ 依赖关系。

You can simply download the library which you want to include and copy it to libs folder of your project. Then select that file (in my case it was android-support-v4 library) right click on it and select "Add as Library"

Maven 的依赖特性更加简单:

  1. 打开 档案-> 项目结构..。菜单。
  2. Select 模组 in the left pane, choose your project's main module in the middle pane and open 依赖性 tab in the right pane.
  3. 单击右侧面板中的 加号并从列表中选择 “ Maven 依赖”
  4. Enter "support-v4" into the search field and click the icon with magnifying glass.
  5. 从下拉列表中选择“ com.google.android: support-v4: r7@jar”。
  6. 点击“确定”。
  7. 清理并重建您的项目。

希望这个能帮上忙!

In Android Studio 1.0, this worked for me :-
打开 build.gradle (Module : app)文件并粘贴以下内容(最后) :-

dependencies {
compile "com.android.support:appcompat-v7:21.0.+"
}

Note that this dependencies is different from the dependencies inside buildscript in build.gradle (Project)
当你编辑这个渐变文件时,一条信息显示你必须同步这个文件。点击“立即同步”

资料来源: https://developer.android.com/tools/support-library/setup.html#add-library

Android 不再从 SDK 管理器下载这些库,必须通过 Google 的 Maven 库来访问。

您必须在 build.gradle 文件中执行类似的操作:

allprojects {
repositories {
jcenter()
maven {
url "https://maven.google.com"
}
}
}






dependencies {
...
compile "com.android.support:support-core-utils:27.0.2"
}

查找有关设置过程 给你和不同支持库修订版 给你的更多细节。

安卓 X< sup > [ About ]

implementation 'androidx.appcompat:appcompat:1.0.2'