向android studio中匯入帶有jni的eclipse項目
引入
之前一直是用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/ // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... // This moves them out of them default location under src//... 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 }}