AndroidStudio rebuilds everything every time if mu

2019-08-22 06:52发布

问题:

If I build my AndroidStudio for one ABI, it works as expected.

If I support two ABIs, say for 'armeabi-v7a' and 'arm64-v8a', then Android Studio will rebuild every file, every time, even if nothing changed.

I use CMake external build to for the native part of my app.

My grade file:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.steenriver.biplane"
        minSdkVersion 19
        targetSdkVersion 19
        versionCode 115
        versionName "1.15"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        ndk {
            //abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
            abiFilters 'armeabi-v7a', 'arm64-v8a'
            //abiFilters 'arm64-v8a'
            //abiFilters 'armeabi-v7a'
        }
        externalNativeBuild {
            cmake {
                //ldFlags "-Wl,-fuse-ld=gold"
                cppFlags "-std=c++11 -DANDROID=1 -DPLAY=1 -DUSEES3=1 -DLOGTAG=biplane"
                arguments "-DCMAKE_VERBOSE_MAKEFILE=1", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_NEON=TRUE", "-DANDROID_ARM_MODE=arm"
            }
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            externalNativeBuild {
                cmake {
                    cppFlags += "-DDEBUG -DSHOWGRAPHS"
                }
            }
        }
    }
    externalNativeBuild {
        cmake {
            path '../jni/CMakeLists.txt'
        }
    }
}

And my top level CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)


# dependency: dblunt


set( dblunt_src_DIR $ENV{HOME}/src/dutch-blunt/src )
set( dblunt_build_DIR $ENV{HOME}/src/dutch-blunt/src/output )
file(MAKE_DIRECTORY ${dblunt_build_DIR})
add_subdirectory( ${dblunt_src_DIR} ${dblunt_build_DIR} )


# dependency: GBase

set( gbase_src_DIR $ENV{HOME}/apps/GBase/src )
set( gbase_build_DIR $ENV{HOME}/apps/GBase/src/output )
file(MAKE_DIRECTORY ${gbase_build_DIR})
add_subdirectory( ${gbase_src_DIR} ${gbase_build_DIR} )


# dependency: PI

set( pi_src_DIR ../../PI )
set( pi_build_DIR ../../PI/output )
file(MAKE_DIRECTORY ${pi_build_DIR})
add_subdirectory( ${pi_src_DIR} ${pi_build_DIR} )


# dependency uif
set( uif_src_DIR ../../../uif )
set( uif_build_DIR ../../../uif/output )
file(MAKE_DIRECTORY ${uif_build_DIR})
add_subdirectory( ${uif_src_DIR} ${uif_build_DIR} )


# dependency gpgoap
set( gpgoap_src_DIR $ENV{HOME}/src/GPGOAP )
set( gpgoap_build_DIR $ENV{HOME}/src/GPGOAP/output )
file(MAKE_DIRECTORY ${gpgoap_build_DIR})
add_subdirectory( ${gpgoap_src_DIR} ${gpgoap_build_DIR} )


# dependency: OpenDE
set( opende_src_DIR $ENV{HOME}/src/opende )
set( opende_build_DIR $ENV{HOME}/src/opende/output )
file(MAKE_DIRECTORY ${opende_build_DIR})
add_subdirectory( ${opende_src_DIR} ${opende_build_DIR} )


# dependency: OpenAL
set( openal_src_DIR $ENV{HOME}/src/openal-soft/jni )
set( openal_build_DIR $ENV{HOME}/src/openal-soft/jni/output )
file(MAKE_DIRECTORY ${openal_build_DIR})
add_subdirectory( ${openal_src_DIR} ${openal_build_DIR} )


# dependency: freealut
set( freealut_src_DIR $ENV{HOME}/src/freealut/src )
set( freealut_build_DIR $ENV{HOME}/src/freealut/src/output )
file(MAKE_DIRECTORY ${freealut_build_DIR})
add_subdirectory( ${freealut_src_DIR} ${freealut_build_DIR} )


# depencency: gpg (prebuilt, provided by Google)

add_library( gpg
             STATIC
             IMPORTED
)
set_target_properties( gpg
        PROPERTIES IMPORTED_LOCATION
        $ENV{HOME}/src/gpg-cpp-sdk/android/lib/gnustl/${ANDROID_ABI}/libgpg.a )


set ( CMAKE_SHARED_LINKER_FLAGS "-fuse-ld=gold ${CMAKE_SHARED_LINKER_FLAGS}" )

# now build app's shared lib
add_library(biplane SHARED
    main.cpp
    StateManager.cpp
    scorekeeper.cpp
)

target_include_directories(biplane PRIVATE
    $ENV{HOME}/src/gpg-cpp-sdk/android/include
    $ENV{HOME}/apps/GBase/src
    $ENV{HOME}/apps/GBase/src/Android
    $ENV{HOME}/src/stb
    ../../PI
    .
)

# add lib dependencies
target_link_libraries(biplane
    gpg
    pi
    uif
    gbase
    dblunt
    gpgoap
    opende
    freealut
    openal
    EGL
    GLESv3
    OpenSLES
    android
    log
    z
    m
)

How can I convince Android Studio to recompile only the files that changed?

回答1:

This was caused by the fact that the build variants all had the same output directory for the .o files.

You need to split out the build directory into release/debug and into the different ABIs.

The way to do this, is by using the CMAKE_CURRENT_BINARY_DIR macro as such:

set( gpgoap_src_DIR $ENV{HOME}/src/GPGOAP )
add_subdirectory( ${gpgoap_src_DIR} ${CMAKE_CURRENT_BINARY_DIR}/gpgoap )

If you do this, then the arm7 debug object files will end up in: app/.externalNativeBuild/cmake/debug/armeabi-v7a so they will not clash with other ABIs.

Note that you cannot re-use output directories, so you do need to postfix them for each add_subdirectory() that you do.

One final remark: the cmake documentation says that the output directory is optional for add_subdirectory() but I found this not to be the case. You need to supply it at least two arguments.