Migrate Native Module from Gradle Experimental Plu

2019-06-14 23:23发布

问题:

I have an Android Library project which has a part in C/C++ via Android NDK. The project started half of a year ago so we chose to use Experimental Plugin because of better NDK support. I'm using gradle-experimental:0.8.2right now. I have a com.android.model.native module and i would like to migrate it to gradle:2.2.0. The only option i see in Gradle Android Plugin DSL is:

  • AppExtension: android extension for com.android.application projects.
  • LibraryExtension: android extension for com.android.library projects.
  • TestExtension: android extension for com.android.test projects.

So the question is how to make a pure native module in gradle with stable gradle plugin?

Here is my current native module:

apply plugin: 'com.android.model.native'

apply from: "../config.gradle"
def config = ext.configuration

model {
    android {
        compileSdkVersion = config.compileSdkVersion
        buildToolsVersion = config.buildToolsVersion

        defaultConfig {
            minSdkVersion.apiLevel = config.minimumSdkVersion
            targetSdkVersion.apiLevel = config.targetSdkVersion
            versionCode = 1
            versionName = '1.0'
        }
        ndk {
            moduleName = 'apicore'
            platformVersion = config.minimumSdkVersion
            cppFlags.add("-std=c++11")
            cppFlags.add("-pthread")
            cppFlags.add("-fexceptions")
            cppFlags.add("-frtti")
            stl = "gnustl_static"
            abiFilters.addAll(config.targetPlatforms)
            ldLibs.addAll(['android', 'log'])
        }
        sources {
            main {
                jni {
                    source {
                        //include "someFile.txt"
                        // This is ignored.
                        exclude "main.cpp"
                        exclude "misc/APITest.cpp"
                        exclude "misc/APITest.h"
                    }
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }
    }
}

回答1:

You will need to create CMakeLists.txt or Android.mk to build your "libapicore.so", if you want to move to stable gradle plugin.
I think you should do next steps:

  1. For easy migration move your .h, .c, .cpp to the

    root_folder_of_project\app\src\main\cpp
  2. Also add there CMakeLists.txt. It should look like:

    cmake_minimum_required(VERSION 3.4.1)

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
    add_library(apicore SHARED #here add your cpp sources mysource1.cpp mysource2.cpp #do not include main.cpp misc/APITest.cpp misc/APITest.h )
    #include libraries needed for apicore lib target_link_libraries(apicore android log )

  3. Now rewrite your app's build.gradle and point it to CMakeLists.txt:

    apply plugin: 'com.android.application'

    android {
    compileSdkVersion = 25 buildToolsVersion = '25.0.2'
    defaultConfig { applicationId = 'com.your.app' minSdkVersion 16 targetSdkVersion 25 ndk { abifilters 'armeabi-v7a' /*,'armeabi', etc.*/ } externalNativeBuild { cmake { arguments '-DANDROID_PLATFORM=android-19', '-DANDROID_TOOLCHAIN=clang', /*or gcc*/ '-DANDROID_CPP_FEATURES=rtti', '-DANDROID_CPP_FEATURES=exceptions', '-DANDROID_STL=gnustl_static' /*CMake uses by default*/ } } }

    buildTypes {...}

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

    dependencies {...}

    With this you will build your android application with your native "libapicore.so" inside.



回答2:

I just migrated my project from Gradle Experimental Plugin to Gradle plugin for Android. The current Gradle plugin for Android still not provide something what com.android.model.native extension provided from the experimental plugin which is an ability to create a pure native module. I have to realise that i don't even need that. What i did to replace the com.android.model.native module is i made a library module where i handle the native code and building of my native libraries and i just copy the native libraries where i need them. Of course the module generate the .aar but thats not a problem i just don't use it.