Unable to build GStreamer tutorials using Android Studio
Asked Answered
M

1

5

I am trying to build the tutorials that are bundled with gstreamer-sdk-android-arm-debug-2013.6. The Android.mk file in the src/jni directory (tutorial 1 project) references environment variables such as GSTREAMER_SDK_ROOT. From what I have read, Android Studio does not use/pass environment variables to the build scripts. Is there a best practice for modifying makefiles and for defining/retrieving the key/value pairs required by the build scripts?

Mullane answered 17/12, 2014 at 16:29 Comment(3)
Android Studio can't work with Makefiles, so you'll have to set up Gradle build files if you want to use it as anything more than a text editor.Salamis
I am using makefiles with Android Studio for the ndk component in other projects, such as mupdf and it works fine. The only issue I see is with these environment variables used by the Android.mk for the GStreamer tutorials. I could hard code the values in the Android.mk files and I'm pretty sure it would build - but I'm guessing there is a better way.Mullane
That said, if there is a way to convert these tutorials to use only Gradle build files, I'd be all for it - but don't know where to start.Mullane
M
4

Ok, I have a working solution. You CAN pass environment variables to ndk-build (or any other process spawned by gradle Exec). In my case, I wanted to set these for both the clean and build tasks. This is is done using tasks.withType(Exec). The environment parameter is set here for all Exec tasks.

For GSTREAMER_SDK_ROOT, I added an entry to local.properties:

gst.dir=/Users/svenyonson/sdk/gstreamer-sdk-android-arm-debug-2013.6

For PATH, I used the default for the spawned process and added in what I needed.

Here is a working version of build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.gst_sdk_tutorials.tutorial_1"
        minSdkVersion 19
        targetSdkVersion 19
    }

    sourceSets.main {
        jni.srcDirs = []
        jniLibs.srcDir 'src/main/libs'
        java.srcDirs += 'src/main/jni/src'
    }

    tasks.withType(Exec) {

        def localProperties = new Properties()
        localProperties.load(project.rootProject.file('local.properties').newDataInputStream())
        def gstDir = localProperties.getProperty('gst.dir')

        environment = [:]
        environment['PATH'] = System.getenv("PATH")+ ":/usr/local/bin"
        environment['GSTREAMER_SDK_ROOT'] = gstDir
    }


    task buildNative(type: Exec, description: 'Compile JNI source via NDK') {

        def ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
        commandLine "$ndkDir/ndk-build",
            '-C', file('src/main/jni').absolutePath,
            '-j', Runtime.runtime.availableProcessors(),
            'all',
            'NDK_DEBUG=1',
            'V=1',
            'APP_PLATFORM=android-19'

    }

    task cleanNative(type: Exec, description: 'Clean JNI object files') {
        def ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
        commandLine "$ndkDir/ndk-build",
            '-C', file('src/main/jni').absolutePath,
            'clean'
    }

    clean.dependsOn 'cleanNative'

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

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

The project now builds and runs. The only other things you will need to do is add ndk.dir to local.properties:

sdk.dir=/Users/svenyonson/sdk/android-sdk
ndk.dir=/Users/svenyonson/sdk/android-ndk-r9d
gst.dir=/Users/svenyonson/sdk/gstreamer-sdk-android-arm-debug-2013.6

One more thing: These examples will not build using android-ndk-r10d. Be sure to use android-ndk-r9d.

Mullane answered 17/12, 2014 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.