转战Androidstudio之引入jar和so

新迁移的项目之前一直使用百度api来实现一个定位功能,但是迁移到as后发现报错找不到库文件

Couldn’t load BaiduMapSDK_v3_2_0_15 from loader

但是明明是存在这个包的啊。。郁闷

1、找了一堆的资料先贴一个正常as工程状态下引入so文件的方法:

在“src/main”目录中新建名为“jniLibs”的目录; 2.将so文件复制、粘贴到“jniLibs”目录内。 注:如果没有引用so文件,可能会在程序执行的时候加载类库失败,有类似如下的DEBUG提示: java.lang.UnsatisfiedLinkError: Couldn’t load library xxxx from loader dalvik.system.PathClassLoader

好了,我说过了这是正常的as工程引入so文件的方法,可是对于我迁移过来的eclipse 项目根本不存在所谓的main java包该怎么办呢??

就是这个样子

好在找到了方法2

在build.gradle中加入

自定义一个任务,在其中指定项目所依赖的so文件的目录,这里用了**/*.so来写,为了省事,,指定需要拷入的目录 into "lib",那么动态运行库就被拷入到lib目录中

task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {destinationDir file("$projectDir/libs")baseName "Native_Libs2"extension "jar"from fileTree(dir: "libs", include: "**/*.so")into "lib"}tasks.withType(JavaCompile) {compileTask -> compileTask.dependsOn(nativeLibsToJar)}完整的gradle配置文件如下()

buildscript {repositories {mavenCentral()}dependencies {classpath 'com.android.tools.build:gradle:1.2.3'}}apply plugin: 'android'dependencies {compile fileTree(dir: 'libs', include: '*.jar')}android {compileSdkVersion 19buildToolsVersion "22.0.1"sourceSets {main {manifest.srcFile 'AndroidManifest.xml'java.srcDirs = ['src']resources.srcDirs = ['src']aidl.srcDirs = ['src']renderscript.srcDirs = ['src']res.srcDirs = ['res']assets.srcDirs = ['assets']}// Move the tests to tests/java, tests/res, etc…instrumentTest.setRoot('tests')// Move the build types to build-types/<type>// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, …// This moves them out of them default location under src/<type>/… which would// conflict with src/ being used by the main source set.// Adding new build types or product flavors should be accompanied// by a similar customization.debug.setRoot('build-types/debug')release.setRoot('build-types/release')task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {destinationDir file("$projectDir/libs")baseName "Native_Libs2"extension "jar"from fileTree(dir: "libs", include: "**/*.so")into "lib"}tasks.withType(JavaCompile) {compileTask -> compileTask.dependsOn(nativeLibsToJar)}}}运行、、、、、、、、bingo!!

3、整理jar包引入

既然都引入so文件了,当然也要学习下如何引入jar包了

1.将jar文件复制、粘贴到app的libs目录中; 2.右键点击jar文件,并点击弹出菜单中的“Add As Library”,将jar文件作为类库添加到项目中; 3.选择指定的类库。 注:如果不执行2、3步,jar文件将不起作用,并且不能使用import语句引用。

好的!结束就整理到这里了。。。。。。

版权声明:本文为博主原创文章,未经博主允许不得转载。

去陌生的街角,去做一切我们曾经或现在也很想做的事情,

转战Androidstudio之引入jar和so

相关文章:

你感兴趣的文章:

标签云: