参考> http://www.androidchina.net/2467.html
1.使用.jar
(1)利用工具添加jar
将jar拷贝至工程的libs下(若不存在则创建)
open module setting>>Dependencies>>+>>File Dependence>> 添加libs下的.jar文件
就成功添加了。
(2)手动添加jar
打开module下的build.gradle,在dependencies里面添加依赖文件
第一种方法:添加单个文件
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile files('libs/first.jar') //compile files('jar包的路径')
}
第二种方法:添加目录下符合要求的文件
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile fileTree(include: ['*.jar'], dir: 'libs')//添加libs目录下所有的.jar包
}
该方法在需要导入多jar包的工程中使用更广
2.使用.aar
上文提到的compile fileTree不支持aar,因此需要按照以下步骤操作:
1.将aar放置于libs
2.打开module的build.gradle,添加
repositories {
flatDir {
dirs 'libs' //this way we can find the .aar file in libs folder
}
}
3.在dependencies中添加
compile(name: 'librarydemo', ext: 'aar')//说明文件名和类型
3.使用.so
添加时有个注意点就是添加的路径要设置正确,Android Studio 默认的so文件路径是app/src/main/jniLibs/armeabi
可以在main下面新建一个jniLibs目录,然后将文件全部复制进去便可以运行程序了
如果你想统一管理各种第三方文件,例如放在libs中
需要在gradle的android {}中添加以下代码来更改默认路径
sourceSets {
main {
jniLibs.srcDir "libs"
}
}
编译或运行时若提示找不到库,clean project再运行