如何引入第三方jar包(以google play service为例)

来源:互联网 发布:python搭建网站源码 编辑:程序博客网 时间:2024/05/22 12:31

在Android Studio(AS)的开发中,遇到引入外部类库或第三方jar包的情况,确实是件很恼人的事情。因为AS是基于Gradle构建,所以和传统的导入方式有些不一样。

笔者也曾为此头疼过好一阵,现在终于有了解决办法,拿出来和大家分享一下。

这里以导入Google Services library为例,具体操作步骤如下。

1. 打开你的buid.gradle文件。应该用哪一个呢?Project中的,还是Module中的?(如图所示,Module中的)

2 . 将google-play-services.jar这个jar包拷到指定的位置。原来可能是在这个目录下:$ANDROID-SDK/extras/google/google_play_services/libproject/google-play-services_lib/libs ,比如现在我把它放到了$GOOGLE_PLAY_LIB目录下。

 

3 . 如果你已经有了别的dependencies(如Android Studio Library),我们把它加到list当中去,也就是将你需要编译的部分,从dependencies中移到list里。

例如先前长这样:

List myDependencies = ["com.android.support:appcompat-v7:18.0.+"]dependencies {        //moved into List above, so there'll be nothing here.

 

4.  加入Google Play Library之后:

List myDependencies = ["com.android.support:appcompat-v7:18.0.+", fileTree (dir: '$GOOGLE_PLAY_LIB', includes: ['*.jar'])]

 

PS:不要忘了把$GOOGLE_PLAY_LIB 改成你本地的目录,如 E://Android/Development/Lib 

 

5.  修改dependencies里面的内容:

List myDependencies = ["com.android.support:appcompat-v7:18.0.+", fileTree (dir: 'E://Android/Development/Lib', includes: ['*.jar'])]dependencies {    compile myDependencies} 

 

6.  然后就是耐心等待项目compile和make了。^_^

笔者成功的测试过Google Android Map v2,如果你有遇到问题,欢迎留言。 

 

=======以下是另一个网友留言,有更简便一些,不过得注意路径的问题========

只需要从SDK中下载 Google Repository & Google Play Services,然后在build.gradle文件中申明,如下所示:

dependencies {compile('com.android.support:appcompat-v7:18.0.+')compile('com.android.support:support-v4:13.0.0')compile ('com.google.android.gms:play-services:3.2.25')}

 

再或者,直接:compile files('libs/google-play-services.jar')

0 0
原创粉丝点击