Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 向android studio中導入帶有jni的eclipse項目

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

編輯:關於Android編程

引入

之前一直是用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
    }
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved