-2

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'))
            }
        }
    }
}
emKaroly
  • 756
  • 1
  • 10
  • 22
  • 1
    I don't understand what you really want. Put in your `build.gradle` of application `classpath 'com.android.tools.build:gradle:2.2.2'` and inform us what is happening when your app finish the build of the project. – Rodrigo Paixão Dec 01 '16 at 11:25
  • Stable gradle doesn't have `com.android.model.native` plugin – emKaroly Dec 01 '16 at 11:33
  • This is your `build.gradle` for the app, there is another `build.gradle` to the project. It is there where you find the version of your gradle. – Rodrigo Paixão Dec 01 '16 at 11:46
  • @RodrigoPaixão I can change my gradle version thats not the problem. The stable gradle doesnt have native module. You really don't understand what i want. i am using this: http://tools.android.com/tech-docs/new-build-system/gradle-experimental – emKaroly Dec 01 '16 at 11:52
  • I have the same issue, sadly the migration guide does not mention anything. In the googlesamples for gradle 2.2 and ndk they avoid having native library modules and instead just have a script which builds all of the libraries and it seems like it is the only option currently. For reference gradle-experimental sample with com.android.model.native https://github.com/googlesamples/android-ndk/tree/gradle-experimental/hello-libs, ndk-build sample without https://github.com/googlesamples/android-ndk/tree/master-ndkbuild/hello-libs – Someone Dec 16 '16 at 15:18
  • I really don't know what should i think about this. I have a feeling that I overlooked something. They put the native module to experimental plugin for reason. I have separate module for JNI and pure C++ stuff i think it makes sense. – emKaroly Dec 16 '16 at 18:56

2 Answers2

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.

Sheikh
  • 1,116
  • 6
  • 15
  • Thank you for answer, you wrapped it up nicely. The problem is, this way i am basically merging my app module which is pure java and android stuff with pure c/c++ stuff. From my point of view the app should be divided at least into 3 parts: 1. Main Android App module 2. JNI/NDK stuff(wrapper for c/c++) 3. C/C++ stuff – emKaroly Dec 19 '16 at 07:46
  • Okay, understood. It is possible. Did you see the **hello-libs** sample from [google github](https://github.com/googlesamples/android-ndk/tree/master/hello-libs) ? This can answer your questions. But It is built into 2 parts: 1) Main Android App module; 2) JNI/NDK stuff + C/C++ stuff – Sheikh Dec 19 '16 at 11:20
1

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.

emKaroly
  • 756
  • 1
  • 10
  • 22