Android NDK toolchain on Windows for JNI

I’ve found a few posts and resources suggesting how to set a custom GCC toolchain for the gradle ‘cpp’ plugin with the goal of cross-compiling a JNI project for both Windows x86_64 and Android ARM. But when I specify the non-standard name of the compiler it is not found with this message showing up from “gradlew components”:

Binaries
    Shared library 'myJni:android:debug:sharedLibrary' (not buildable)
        build using task: :myJniAndroidDebugSharedLibrary
        build type: build type 'debug'
        flavor: flavor 'default'
        target platform: platform 'android'
        tool chain: unavailable
        shared library file: build\libs\myJni\shared\android\debug\libmyJni.so
        No tool chain is available to build for platform 'android':
          - Tool chain 'visualCpp' (Visual Studio): Don't know how to build for platform 'android'.
          - Tool chain 'gcc' (GNU GCC):
              - Could not find C compiler 'arm-linux-androideabi-gcc.cmd'. Searched in: ...\sdk\ndk-bundle\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin

I can list the compiler .cmd file when I concatenate it and the search dir inside “sdk\ndk-bundle”, but Gradle still cannot find it. Removing the trailing “.cmd” makes no difference. What might I be doing wrong, or is there a workaround?

So far I have this in my “build.gradle” file:

model {
    repositories {
        libs(PrebuiltLibraries) {
            mylib {
                headers.srcDir new File(myIncludeDir.toString())
                binaries.withType(StaticLibraryBinary) {
                    staticLibraryFile = new File(myBinPath.toString())
                }
            }
            jni {
                headers.srcDirs = [
                        new File(Paths.get(javaHome, "include").toString()),
                        new File(Paths.get(javaHome, "include", "win32").toString())
                ]
            }
        }
    }
    platforms {
        android {
            architecture 'arm'
            operatingSystem 'android'
        }
        win64 {
            architecture 'x86_64'
            operatingSystem 'windows'
        }
    }
    components {
        myJni(NativeLibrarySpec) {
            sources {
                cpp.lib library: 'mylib', linkage: 'api'
                cpp.lib library: 'jni', linkage: 'api'
            }
            targetPlatform 'android'
            targetPlatform 'win64'
        }
    }
    buildTypes {
        debug
        release
    }
    toolChains {
        visualCpp(VisualCpp) {
        }
        gcc(Gcc) {
            target('android') {
                path Paths.get(System.getenv("ANDROID_HOME"), "ndk-bundle", "toolchains",
                        "arm-linux-androideabi-4.9", "prebuilt" , "windows-x86_64" , "bin").toString()
                cCompiler.executable = "arm-linux-androideabi-gcc.cmd"
            }
        }
    } 
}

It looks like I posted too soon. The issue appears to be that:

  • gradle won’t execute .cmd files
  • the cmd file actually points to a Clang build that uses the gcc tools

But Windows builds still fail due to passing an unsupported (on Windows) command line flag: ‘-fPIC’: