向android studio中匯入帶有jni的eclipse項目

來源:互聯網
上載者:User

向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    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.