Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do We Point androidTest to a Peer Eclipse-Style Project?

I am helping out the Guardian Project on NetCipher. For legacy reasons, they want to keep their existing project structure, which was based on Eclipse. However, rather than having the tests in a tests/ subdirectory of the library, they went with a peer project model. So, off of the repo root, libnetcipher/ is the library and netciphertest/ are the instrumentation tests.

The instrumentation tests were never set up for Gradle builds, unlike libnetcipher/ itself. So, I am adding stuff to the libnetcipher/build.gradle file to have it point its androidTest sourceset to the netciphertest/ directory, rather than its normal location.

This works:

    androidTest {
        manifest.srcFile '../netciphertest/AndroidManifest.xml'
        java.srcDirs = ['../netciphertest/src']
        resources.srcDirs = ['../netciphertest/src']
        aidl.srcDirs = ['../netciphertest/src']
        renderscript.srcDirs = ['../netciphertest/src']
        res.srcDirs = ['../netciphertest/res']
        assets.srcDirs = ['../netciphertest/assets']
    }

However, the duplicate ../netciphertest bits are icky. I could define that as a constant to minimize the duplication, of course. I'm trying to figure out if there's a better approach to the whole problem.

For example, this doesn't work:

    androidTest.setRoot('../netciphertest')

    androidTest {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
    }

I thought that perhaps the srcDirs and srcFile stuff might be interpreted relative to the setRoot() value, but they seem to be interpreted relative to the project's own root.

Is there a more elegant solution than what I have working?

like image 342
CommonsWare Avatar asked Mar 29 '16 20:03

CommonsWare


2 Answers

Reading over the Gradle documentation, I couldn't find anything about relative vs absolute paths for the srcDirs, but it seems that if you just give src that it will assume ./src regardless of the root that was set. Maybe this could be a Gradle feature request or might work as expected in later versions of Gradle? Not sure, didn't look into it.

That being said, assuming you don't need aidl, renderscripts, or resources (since they don't appear used in the project), then is this less "icky"?

androidTest {
    setRoot '../netciphertest'
    java.srcDirs = ['../netciphertest/src']
}

All the others will be automatically be setup like this if you don't override them and just set the root.

androidTest.manifest.srcFile = /NetCipher/netciphertest/AndroidManifest.xml
androidTest.res.srcDirs = [/NetCipher/netciphertest/res]
androidTest.assets.srcDirs = [/NetCipher/netciphertest/assets]

// Override these, if you wish
androidTest.resources.srcDirs = [/NetCipher/netciphertest/resources]
androidTest.aidl.srcDirs = [/NetCipher/netciphertest/aidl]
androidTest.renderscript.srcDirs = [/NetCipher/netciphertest/rs]

You can print out the paths within Gradle and play around with it more if you like by including this section within the sourceSets block.

println "androidTest.manifest.srcFile = ${androidTest.manifest.srcFile}"
println "androidTest.java.srcDirs = ${androidTest.java.srcDirs}"
println "androidTest.resources.srcDirs = ${androidTest.resources.srcDirs}"
println "androidTest.aidl.srcDirs = ${androidTest.aidl.srcDirs}"
println "androidTest.renderscript.srcDirs = ${androidTest.renderscript.srcDirs}"
println "androidTest.res.srcDirs = ${androidTest.res.srcDirs}"
println "androidTest.assets.srcDirs = ${androidTest.assets.srcDirs}"
like image 114
OneCricketeer Avatar answered Oct 18 '22 02:10

OneCricketeer


If anyone is still confused by setRoot method, just calling it at the end of your sourceSet block.

This is how this method implements.

override fun setRoot(path: String): AndroidSourceSet {
    return initRoot(path)
}

private fun initRoot(path: String): AndroidSourceSet {
    javaSource.setSrcDirs(listOf("$path/java"))
    javaResources.setSrcDirs(listOf("$path/resources"))
    res.setSrcDirs(listOf("$path/${SdkConstants.FD_RES}"))
    assets.setSrcDirs(listOf("$path/${SdkConstants.FD_ASSETS}"))
    manifest.srcFile("$path/${SdkConstants.FN_ANDROID_MANIFEST_XML}")
    aidl.setSrcDirs(listOf("$path/aidl"))
    renderscript.setSrcDirs(listOf("$path/rs"))
    jni.setSrcDirs(listOf("$path/jni"))
    jniLibs.setSrcDirs(listOf("$path/jniLibs"))
    shaders.setSrcDirs(listOf("$path/shaders"))
    return this
}

The document is too bad.

like image 1
shingo Avatar answered Oct 18 '22 01:10

shingo