Android Studio编译ndk的.so库 JNI

发表于

谭东编写。

我们用Android Studio的ndk来编写hello-jni的so库,所有架构的。(谷歌官方的教程)

1、Android Studio下载NDK和配置NDK。
20160323124405

2、Android Studio新建Project:hello-jni,然后app上右键新建jni目录。结构如第3点的图片。

3、配置ndk编译参数和gradle。主要是以下3个文件配置修改。

20160323124650

4、我们先看Project根目录的build.gradle,也就是最外层的全局build.gradle和gradle->wrapper下的gradle-wrapper.properties文件配置。

build.gradle:


// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
       jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle-experimental:0.4.0'
//0.4.0要匹配gradle-wrapper.properties里的distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip
//而0.2.0则一般要distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip
  }
}

allprojects {
  repositories {
  jcenter()
  }
}

20160323125441

gradle-wrapper.properties:


distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.8-all.zip

20160323125630

 

5、接下来配置app下的build.gradle。
第一行的apply plugin: ‘com.android.application’要变为apply plugin: ‘com.android.model.application’。也就是多加了一个model。
后面的配置也需要在外层加入model{},内部的值的设置也要改成xx=yy的等号形式。
defaultConfig{}要改成defaultConfig.with{}形式。buildTypes{}要写成android.buildTypes{}形式。里面的
proguardFiles也要改成proguardFiles.add(file(‘proguard-rules.txt’))或者proguardFiles += file(‘proguard-rules.pro’)这种形式。


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

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

        defaultConfig.with {
            applicationId = "com.example.hellojni"
            minSdkVersion.apiLevel = 4
            targetSdkVersion.apiLevel = 23
        }
    }

    /*
     * 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")
    }
}

6、配置好后,我们在jni目录里新建c/c++的源文件。hello-jni.c。

hello-jin.c:


/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/hellojni/HelloJni.java
 */
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
#if defined(__arm__)
  #if defined(__ARM_ARCH_7A__)
    #if defined(__ARM_NEON__)
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a/NEON (hard-float)"
      #else
        #define ABI "armeabi-v7a/NEON"
      #endif
    #else
      #if defined(__ARM_PCS_VFP)
        #define ABI "armeabi-v7a (hard-float)"
      #else
        #define ABI "armeabi-v7a"
      #endif
    #endif
  #else
   #define ABI "armeabi"
  #endif
#elif defined(__i386__)
   #define ABI "x86"
#elif defined(__x86_64__)
   #define ABI "x86_64"
#elif defined(__mips64)  /* mips64el-* toolchain defines __mips__ too */
   #define ABI "mips64"
#elif defined(__mips__)
   #define ABI "mips"
#elif defined(__aarch64__)
   #define ABI "arm64-v8a"
#else
   #define ABI "unknown"
#endif

    return (*env)->NewStringUTF(env, "Hello from JNI !  Compiled with ABI " ABI ".");
}

7、之后我们再编写MainActivity里的调用代码。

载入so库:
static {
System.loadLibrary(“hello-jni”);
}
native方法:
public native String unimplementedStringFromJNI();
public native String stringFromJNI();
onCreate里调用方法:
stringFromJNI()


/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.hellojni;

import android.app.Activity;
import android.widget.TextView;
import android.os.Bundle;

public class HelloJni extends Activity
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        /* Create a TextView and set its content.
         * the text is retrieved by calling a native
         * function.
         */
        TextView  tv = new TextView(this);
        tv.setText( stringFromJNI() );
        setContentView(tv);
    }

    /* A native method that is implemented by the
     * 'hello-jni' native library, which is packaged
     * with this application.
     */
    public native String  stringFromJNI();

    /* This is another native method declaration that is *not*
     * implemented by 'hello-jni'. This is simply to show that
     * you can declare as many native methods in your Java code
     * as you want, their implementation is searched in the
     * currently loaded native libraries only the first time
     * you call them.
     *
     * Trying to call this function will result in a
     * java.lang.UnsatisfiedLinkError exception !
     */
    public native String  unimplementedStringFromJNI();

    /* this is used to load the 'hello-jni' library on application
     * startup. The library has already been unpacked into
     * /data/data/com.example.hellojni/lib/libhello-jni.so at
     * installation time by the package manager.
     */
    static {
        System.loadLibrary("hello-jni");
    }
}

8、运行编译即可:

20160323131142

9、打开build目录->intermediates->binaries->debug->all->obj,即可找到我们的不同架构编译的so库了。

20160323131307

10、关于Android Studio中so库的调用使用。

调用so库的项目文件的包名和类名一定要和编译so库时一致,才能符合ndk命名规范,成功调用。

Android Studio加入so库到libs目录后需要配置下。

在app目录下的build.gradle下要加入配置文件:


 task nativeLibsToJar(type: Zip, description: "create a jar archive of the native libs") {
        destinationDir file("$projectDir/libs")
        baseName "Native_Libs2"
        extension "jar"
        from fileTree(dir: "libs", include: "**/*.so")
        into "lib"
    }

    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn(nativeLibsToJar)
    }

20160323143023

然后点击Build里的Make Project编译即可加入so库。将so库变为jar包形式。

20160323143945

20160323143046

然后同包名和类名里调用即可。

20160323143126

20160323145041

另一种方式引入so库,更加的简洁快速。

在app->src->main下新建jniLibs目录。将不同cpu架构的so库拖进去。点击Make Project就可以了。然后将项目结构由Project切换到Android查看就可以看到jniLibs引入了。项目运行就可以正常调用了。

20160323153430

20160323153507
谭东编写。

 


文章评论
共收到 0 条评论