AS运用NDK编译及调用

环境

android studio 2.0

安装

File>Other Settings>Default Project Structure…

注意,这里有可能你会发现你的路经不能点击,那是因为被墙了,破墙下载

配置

  • build.gradle
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.4.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}
  • app/build.gradle

注意自己写时,要添加=.apiLevel,写法不一样

apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"

        defaultConfig.with {
            applicationId = "com.xuie.jnidemo"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }

    compileOptions.with {
        sourceCompatibility=JavaVersion.VERSION_1_7
        targetCompatibility=JavaVersion.VERSION_1_7
    }

    /*
     * native build settings
     */
    android.ndk {
        moduleName = "hello-jni"
        /*
         * Other ndk flags configurable here are
         * cppFlags.add("-fno-rtti")
         * cppFlags.add("-fno-exceptions")
         * ldLibs.addAll(["android", "log"])
         * stl       = "system"
         */
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
    }
    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }
        create("arm7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
        create("arm8") {
            ndk.abiFilters.add("arm64-v8a")
        }
        create("x86") {
            ndk.abiFilters.add("x86")
        }
        create("x86-64") {
            ndk.abiFilters.add("x86_64")
        }
        create("mips") {
            ndk.abiFilters.add("mips")
        }
        create("mips-64") {
            ndk.abiFilters.add("mips64")
        }
        // To include all cpu architectures, leaves abiFilters empty
        create("all")
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
}

编译

  • 新建JniInterface.java
    这里直接写出函数添加了native关键字,用补全,就会在hello-jni.c里自动生成函数名称,比以前用javah有质有飞跃。。。
public class JniInterface {

    public native String  stringFromJNI();

    static {
        System.loadLibrary("hello-jni");
    }
}
  • 自身调用
JniInterface jniInterface = new JniInterface();
((TextView)findViewById(R.id.tv)).setText(jniInterface.stringFromJNI());
  • 他人调用
    我们本身NDK调用,是不希望自己的工程里出现C/C++,那就直接可以调用.so,我以armeabi-v7a架构为例进行调用(因为我手机是这个架构),拷贝目录build/intermediates/binaries/debug/all/lib/armeabi-v7a/下的libhello-jni.so到所用工程main/jniLibs/armeabi-v7a下,调用时java目录下同样需要添加JniInterface.java注意包名要根原来一模一样,然后再调用,实际项目中要将所有架构下的.so都拷贝进去
JniInterface jniInterface = new JniInterface();    
((TextView)findViewById(R.id.tv)).setText("[Use]" + jniInterface.stringFromJNI());

参考

https://github.com/googlesamples/android-ndk

源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值