Mikhail Shikin
Mikhail Shikin

Reputation: 51

Gradle get current build type

all!

I'm trying to get Gradle generate different files (Android.mk and Application.mk) for the release and debug builds. Default way, gradle android plugin doing so doesn't suit me, because it doesn't allow to modify Application.mk, I want to. The main problem is that I can not identify current build type. I tried the following:

android {
    ...
    defaultConfig {
        project['CONFIGURATION_FLAGS'] = ''

        project['APP_ABI'] = ''
        project['APP_PLATFORM'] = 'android-9'
        project['APP_STL'] = 'gnustl_static'
        project['NDK_TOOLCHAIN_VERSION'] = 'clang'
    }
    buildTypes {
        release {
            project['CONFIGURATION_FLAGS'] = '-fvisibility=hidden'
            project['APP_ABI'] = 'armeabi x86'
        }
        debug {
            project['CONFIGURATION_FLAGS'] = '-g -DDEBUG -DENABLE_LOG'
            project['APP_ABI'] = 'armeabi'
        }
    }
}
task processTemplates {
    def templatesDir = System.getProperty('user.dir') + '/app/templates'
    def jniDir = System.getProperty('user.dir') + '/app/src/main/jni'

    // Android.mk
    def configFlags = project['CONFIGURATION_FLAGS']

    def androidMk = file(templatesDir + '/Android.mk').text
    androidMk = androidMk.replaceAll '<CONFIGURATION_FLAGS>', configFlags
    def newAndroidMk = new File(jniDir + '/Android.mk')
    newAndroidMk.text = androidMk

    // Application.mk
    def appAbi = project['APP_ABI']
    def appPlatform = project['APP_PLATFORM']
    def appStl = project['APP_STL']
    def toolchain = project['NDK_TOOLCHAIN_VERSION']

    def applicationMk = file(templatesDir + '/Application.mk').text
    applicationMk = applicationMk.replaceAll '<APP_ABI>', appAbi
    applicationMk = applicationMk.replaceAll '<APP_PLATFORM>', appPlatform
    applicationMk = applicationMk.replaceAll '<APP_STL>', appStl
    applicationMk = applicationMk.replaceAll '<NDK_TOOLCHAIN_VERSION>', toolchain
    def newApplicationMk = new File(jniDir + '/Application.mk')
    newApplicationMk.text = applicationMk
}

But found that the setting of parameters is performed 2 times, that is, for each type of build, regardless of the current build type. Which leads to the fact that for any type of build it set debug options. Can anyone advise me how to solve this problem?

Upvotes: 5

Views: 5980

Answers (1)

Alexander Blinov
Alexander Blinov

Reputation: 393

Add a task which depends on each assembleXxx task and property setting up after it invoked

see my answer for similar problem.

Upvotes: 2

Related Questions