向android studio中導入帶有jni的eclipse項目
來自: 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 }</pre> <p>上述的src/main/jni是你的jni源碼路徑。 <br />
整個build.gradle應如下面:</p>
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 }
}</pre></div>