向android studio中导入带有jni的eclipse项目

sound 9年前

来自: http://blog.csdn.net/pwiling/article/details/50601155


引入

之前一直是用eclipse做android开发,相对于Google力推的android studio来说,eclipse不仅是个吃内存大户,而且android studio的界面更人性化,除此之外,eclipse对于高分屏并不支持,在笔者的Macbook pro上显得很是粗糙。所以决定把之前在正在eclipse上开发的一个使用NDK开发的android项目转移到android studio上。整个过程很是痛苦,参考了多个博客,终于完成了。记下来,希望能给你有所帮助。

步骤

关于向android studio中导入一般的android项目,请点击这篇博客,这篇博客已经说得很好了,我想要说的是如何处理项目中的jni部分。

按照上面的博客导入,如果是一般的android工程,你会发现可以运行成功,但是如果是用NDK开发的native工程,你会发现native c++部分并没有编译,你可以查看libs/armabi/目录下的.so库,你会发现文件的修改日期仍然是你在eclipse上最后编译的日期。

而且android studio不像eclipse,即使ndk环境有误,只要libs/armabi/目录下有编译成功的.so库,eclipse会调用该so库,笔者之前就是抱着这种想法,由于关于jni部分的代码,我并不会再修改,所以就没有配置ndk环境,但是后来发现,如果虽然能编译成功,但是jni部分就和没运行一样,传进去的任何值,返回都是零。

即使配置好了ndk环境,由于你是导入的并非原生在android studio上开发的ndk项目,所以要在根目录下的build.gradle文件中修改些内容。
首先要

sourceSets {          main {              manifest.srcFile 'AndroidManifest.xml'              java.srcDirs = ['src']              resources.srcDirs = ['src']              aidl.srcDirs = ['src']              renderscript.srcDirs = ['src']              res.srcDirs = ['res']              assets.srcDirs = ['assets']              jniLibs.srcDirs = ['libs']          }

添加jniLibs.srcDirs = [‘libs’]这时由于在android studio中
然后

  task buildNative(type: Exec, description: 'Compile JNI source via NDK') {          def ndkDir = android.ndkDirectory          commandLine "$ndkDir/ndk-build",                  '-C', file('/src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source                  '-j', Runtime.runtime.availableProcessors(),                  'all',                  'NDK_DEBUG=1'      }        task cleanNative(type: Exec, description: 'Clean JNI object files') {          def ndkDir = android.ndkDirectory          commandLine "$ndkDir/ndk-build",                  '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source                  'clean'      }        clean.dependsOn 'cleanNative'        tasks.withType(JavaCompile) {          compileTask -> compileTask.dependsOn buildNative      }

上述的src/main/jni是你的jni源码路径。
整个build.gradle应如下面:

buildscript {      repositories {          jcenter()      }      dependencies {          classpath 'com.android.tools.build:gradle:1.5.0'      }  }  apply plugin: 'com.android.application'    dependencies {      compile fileTree(include: '*.jar', dir: 'libs')  }      android {      compileSdkVersion 21      buildToolsVersion '23.0.2'        sourceSets {          main {              manifest.srcFile 'AndroidManifest.xml'              java.srcDirs = ['src']              resources.srcDirs = ['src']              aidl.srcDirs = ['src']              renderscript.srcDirs = ['src']              res.srcDirs = ['res']              assets.srcDirs = ['assets']              jniLibs.srcDirs = ['libs']          }            // 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 buildNative(type: Exec, description: 'Compile JNI source via NDK') {          def ndkDir = android.ndkDirectory          commandLine "$ndkDir/ndk-build",                  '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source                  '-j', Runtime.runtime.availableProcessors(),                  'all',                  'NDK_DEBUG=1'      }        task cleanNative(type: Exec, description: 'Clean JNI object files') {          def ndkDir = android.ndkDirectory          commandLine "$ndkDir/ndk-build",                  '-C', file('src/main/jni').absolutePath, // Change src/main/jni the relative path to your jni source                  'clean'      }        clean.dependsOn 'cleanNative'        tasks.withType(JavaCompile) {          compileTask -> compileTask.dependsOn buildNative      }  }