How to define macros for native code inside an assembly type in gradle?

I created a native code library in Android Studio. It has some time-dependent parts (in C) that need to be under certain levels of latency, and I need to measure them. Therefore, I performed some measurement procedures. However, I do not want the measurement routines to work in release compilation.

So, I have defined macros that will allow me to compile or not generate code based on the type of assembly:

In my native code:

#ifdef MEASURE
measureRoutine1();
if (MEASURE == 1) {
    measureRoutine2();
}
#endif

In my gradle file:

buildTypes {
        release {
            signingConfig signingConfigs.storeSignature
            debuggable false
            jniDebuggable false
            versionNameSuffix "2.3"
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            jniDebuggable true
            minifyEnabled false
            versionNameSuffix '2.3.1'
        }
        measure1 {
            initWith debug
            externalNativeBuild {
                cmake {
                    cppFlags += "-DMEASURE=0"
                    cFlags += "-DMEASURE=0"
                }
            }
        }
        measure2 {
            initWith debug
            externalNativeBuild {
                cmake {
                    cppFlags += "-DMEASURE=1"
                    cFlags += "-DMEASURE=1"
                }
            }
        }
    }

Now my problem is that no matter which buildType compiler is compiled, the MEASURE flag is not defined in my own code, so the measurement code never compiles.

What am I doing wrong? How can I achieve what I need?

PS: , MEASURE, native, - cFlags, buildTypes Config. , MEASURE , .

defaultConfig {
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 6
        versionName "2.3"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -fexceptions -O2 -ffast-math -DMEASURE=0"
                cFlags "-O2 -ffast-math -DMEASURE=0"
            }
        }
    }

1:

MEASURE buildType ( release release debug), , . , . - , ?

Android Studio 2.3.3 . , , , , .

2:

, , , Android Studio buildType. , boolean build java debug buildType, BuildConfig.java , true, , ( buildType). , , , .

gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.0"

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            versionNameSuffix '2.3.1'
            externalNativeBuild {
                cmake {
                    cppFlags += "-DMEASURE=0"
                    cFlags += "-DMEASURE=0"
                }
            }
            buildConfigField "Boolean", "DEBUG_MODE", "Boolean.parseBoolean(\"true\")"
        }    
    }

    externalNativeBuild {
        cmake {
            path 'src/main/cpp/CMakeLists.txt'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
}

BuildConfig.java:

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.company.TestApp";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.02.3.1";
  // Fields from build type: debug
  public static final Boolean DEBUG_MODE = Boolean.parseBoolean("true");
}

, , BuildConfig.DEBUG_MODE :

Error:(14, 27) error: cannot find symbol variable DEBUG_MODE

?

+4
1

:

, , , gradle.

: , , make-, , "app", , .

, , , debug "nativelibrary", + , "app".

:

'nativelibrary' gradle:

defaultConfig {
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"  

        //DEFINE THE DEFAULT PUBLISH CONFIG WHICH IS RELEASE
        defaultPublishConfig 'release'

        //ALLOW IT TO PUBLISH A NON DEFAULT WHICH WILL BE THE DEBUG OR
        //A CUSTOM BUILD TYPE
        publishNonDefault true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

(': nativelib') gradle:

dependencies {

    //ERASE THE FOLLOWING LINE
    //compile project(':nativelib')

    //ADD BUILD TYPE SPECIFIC COMPILE TYPES AND REQUEST A SPECIFIC 
    //CONFIGURATION FOR THE 'nativelibrary' DEPENDING ON THE 
    //REQUESTED BUILD TYPE.
    releaseCompile project(path: ':nativelib', configuration: "release")
    debugCompile project(path: ':nativelib', configuration: "debug")


    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

, .

0

Source: https://habr.com/ru/post/1679284/


All Articles