赞
踩
前面的话
GitHub现在的项目越来越多的是用Android Studio工具写的。所以,当你从GitHub下载一个项目到本地,怎么导入Android Studio里呢。如果你是刚从eclipse转移到Android Studio没多久,在Android Studio上导入一个项目可能会碰到各种麻烦事。下面的方法会让你快速掌握Android Studio导入项目。
在这之前,你必须至少简单的了解Gradle,Gradle是比较先进的构建系统,也是一个很好的构建工具,允许通过插件自定义构建逻辑。Android Studio是基于Gradle来构建项目的,与eclipse不同,所以想要熟练掌握Android Studio的用法,最好还是先熟悉Gradle。
本文的重点是介绍Android Studio如何导入工程,关于Gradle,读者可以自行谷歌,百度,网上有很多关于Gradle的介绍。
下面介绍Android Studio如何导入project。
1、打开GitHub下载你需要的项目到本地,这里我在GitHub随便找了个开源项目作为例子。
下载后解压,工程目录结构如下
2、在你导入项目之前,最后先打开Android Studio创建一个工程。
如果将下载的项目直接导入Android Studio的话,Android Studio会去下载工程相应版本的gradle,此过程是非常慢的,而且有时还会出现各种错误。所以先用Android Studio创建一个项目,用本地创建的项目的gradle替换你下载项目中的gradle。
这里是Android Studio创建的工程目录结构,将此项目中的画圈的文件替换到从GitHub下载的工程目录里相应的文件。
其实这里要替换的内容主要是:
(1)、gradle/wrapper/gradle-wrapper.properties 文件里的最后一行https://services.gradle.org/distributions/gradle-2.14.1-all.zip 如果你不替换,那么导入的时候就会下载这个包。
(2)、build.gradle(项目根目录下,不是app目录下的)文件中的com.android.tools.build:gradle:2.2.0。gradle版本换成自己的。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

3、导入下载的工程项目到Android Studio
File–>new–>import project,选择你从GitHub下载下来的工程后点击OK即可。
4、替换Module的target SDK
工程导入后有可能还会编译报错,原因有可能是app Module中gradle配置中target SDK与你本地SDK的版本不同。
解决办法很简单,打开app/build.gradle文件。我这里是打开simple/build.gradle文件。
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.mrwang.simple"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile project(':stacklibrary')
}

因为我本地的buildToolsVersion 为24.0.1,而此下载的工程里是23.0.2,所以为编译出错。我们只要将文件里的版本改成自己已有的便可。
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "24.0.1" //这里需要改
defaultConfig {
applicationId "com.mrwang.simple"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0' //这里需要改
compile project(':stacklibrary')
}

因为simple/build.gradle依赖stackibrary,所以stackibrary/build.gradle文件也做同样的修改。然后重新编译。
5、编译成功。
你会发现simple目录的图标右下角多了个手机的图标,目录结构如下:
此时,你就可以正常运行你的项目了。
有时我们导入项目编译会发现报这样的错误
我们只需要在项目根目录的build.gradle文件里添加
classpath ‘com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1’
classpath ‘com.github.dcendents:android-maven-gradle-plugin:1.4.1’
这是因为你的build.gradle文件中可能引用了maven。
添加一下就可以了。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.1' //新添加的
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1' //新添加的
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

好了,到这里就算大功告成了。以后再导入什么项目就不怕了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。