下面以高德地图为例,说明如何向Android Studio项目加入第三方jar包。
方法1:通过拷贝集成SDK
1、拷贝 jar 文件至 libs 文件夹下
将下载的定位 SDK jar 文件复制到工程的 libs 目录下。如图所示:
2、配置 build.gradle 文件
在 build.gradle 文件的 dependencies 中配置 compile fileTree(include: ['*.jar'], dir: 'libs')。
方法2:通过Gradle集成SDK
1、在Project的build.gradle文件中配置repositories,添加maven或jcenter仓库地址
Android Studio默认会在Project的build.gradle为所有module自动添加jcenter的仓库地址,如果已存在,则不需要重复添加。Project的build.gradle文件在Project目录中位置如图所示:
配置如下:
allprojects { repositories { jcenter() // 或者 mavenCentral() } }
2、在主工程的build.gradle文件配置dependencies
根据项目需求添加SDK依赖。引入各个SDK功能最新版本, dependencies 配置方式如下:
SDK | 引入代码 |
---|---|
3D地图 | compile 'com.amap.api:3dmap:latest.integration' |
2D地图 | compile 'com.amap.api:map2d:latest.integration' |
导航 | compile 'com.amap.api:navi-3dmap:latest.integration' |
搜索 | compile 'com.amap.api:search:latest.integration' |
定位 | compile 'com.amap.api:location:latest.integration' |
主工程的build.gradle文件在Project目录中位置:
以3D的demo工程为例添加3d地图SDK、定位SDK、搜索功能,配置如下:
android { defaultConfig { ndk { //设置支持的SO库架构(开发者可以根据需要,选择一个或多个平台的so) abiFilters "armeabi", "armeabi-v7a", "arm64-v8a", "x86", "arm64-v8a", "x86_64" } } }
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) //3D地图so及jar compile 'com.amap.api:3dmap:latest.integration' // 定位功能 compile 'com.amap.api:location:latest.integration' // 搜索功能 compile 'com.amap.api:search:latest.integration' }
以上为引入最新版本的SDK,推荐这种方式。如需引入指定版本SDK(所有SDK版本号均与官网发版一致)如下:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.amap.api:3dmap:5.0.0' compile 'com.amap.api:location:3.3.0' compile 'com.amap.api:search:5.0.0' }