Include Shared library .so in android studio

2019-07-24 21:06发布

问题:

I extracted ffmpeg in ndk's source folder then compiled it there only for that I followed this:http://www.roman10.net/2013/08/18/how-to-build-ffmpeg-with-ndk-r9/ and successfully generated android folder with arm/lib and arm/include files.

After that I created one Android.mk file in $NDK/sources/ffmpeg/android/arm and one Android.mk in my android project(src/main/jni folder).

My src/main/jni/Android.mk is like this:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := tutorial01
LOCAL_SRC_FILES := tutorial01.c
LOCAL_LDLIBS := -llog -ljnigraphics -lz
LOCAL_SHARED_LIBRARIES := libavcodec

include $(BUILD_SHARED_LIBRARY)
$(call import-module,ffmpeg-3.0.1/android/arm)

and $NDK/sources/ffmpeg/android/arm/Android.mk is:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE:= libavcodec
LOCAL_SRC_FILES:= lib/libavcodec-57.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

I updated my build.gradle like this and successfully generated jniLibs with .so files but I am getting libavcodec/avcodec.h: No such file or directory while building project.

import org.apache.tools.ant.taskdefs.condition.Os

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

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

        defaultConfig.with {
            applicationId = "com.example.spartan.hello"
            minSdkVersion.apiLevel = 18
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-android.txt'))
        }
    }

    android.sources {
        main {
            jni {
                source {
                    srcDirs = []
                }

            }

        }

        main {
            jniLibs {
                source {
                    srcDirs = ['src/main/libs']
                }
            }
        }

    }

    android.ndk {
        moduleName = "tutorial01"
        stl = 'gnustl_shared'
    }
}


task ndkBuild(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine 'ndk-build.cmd', '-C', file('src/main').absolutePath
    } else {
        commandLine 'ndk-build', '-C', file('src/main').absolutePath
    }
}
tasks.withType(JavaCompile) {
    compileTask -> compileTask.dependsOn ndkBuild
}

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

and loading them in my activity like this:

static {
        System.loadLibrary("tutorial01");     
        System.loadLibrary("avcodec-57");   
    }

I am new to NDK so, Is it ok if we manually paste header files in jni folder?

回答1:

I took reference from third party google sample https://github.com/googlesamples/android-ndk/tree/master/hello-thirdparty which is basically for static lib(.a) and this is how I resolved my problem with gradle experimental plugin:

My build.gradle file:

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

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

        defaultConfig.with {
            applicationId = "com.example.spartan.hello"
            minSdkVersion.apiLevel = 18
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-android.txt'))
        }
    }

    repositories {
        libs(PrebuiltLibraries) {
            libavcodec {
                headers.srcDir "/home/spartan/AndroidStudioProjects/Hello/app/src/main/jni/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("/home/spartan/AndroidStudioProjects/Hello/app/src/main/jniLibs/${targetPlatform.getName()}/libavcodec-57.so")
                }
            }                

        }
    }

    android.ndk {
        moduleName = "tutorial01"

        def jniPath = "/home/spartan/AndroidStudioProjects/Hello/app/src/main/jniLibs"
        cppFlags.add("-I${file(jniPath)}".toString())
        file(jniPath).eachDirRecurse { dir ->
            cppFlags.add("-I${file(dir)}".toString())
        }
        ldLibs.addAll(["log", "android","jnigraphics"])
        stl = "gnustl_shared"
    }


    android.sources {
        main {
            jni {
                dependencies {
                    library "libavcodec" linkage "shared"
                }
            }
        }
    }

    android.productFlavors {
        create ("armv7") {
            ndk.abiFilters.add("armeabi-v7a")
        }
    }

    android.sources {
        main {
            jni {
                source {
                    srcDirs = ['src/main/none']
                }

            }

        }
    }

}

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

In my case I copied .so in jniLibs and include folder of .h files in jni folder and there is no need to add -l in android.ndk because I already added in headers.srcDir(I just added here to mention).

So here I competently replaced my Android.mk with repositories and its really simple.



回答2:

If you're using ndk-build yourself, you don't have to compile anything from gradle.

Set your jni sources to an non-existent folder or no folder:

android.sources{
   main.jni {
    source {
        srcDirs = [] // or ['src/main/none']
    }
  }
}

and remove this block:

// New code
android.ndk {
    moduleName = "tutorial01"
}