Using a hybrid Android Library Module in an Android Application Module does not work with Gradle-experimental:0.3.0-alpha7

394 views
Skip to first unread message

Maurice Wingbermühle

unread,
Nov 6, 2015, 10:53:57 AM11/6/15
to adt-dev
Hi Guys,

I'm working on converting a existing Eclipse project to Android Studio, to be able to debug my C++ SDK more easily.
We already have it compiling with the Android NDK for well over a year using a set of Android.mk files, but for this case, we want to be able to embed the SDK in an Android app to test and debug.

I'm using Android Studio 1.5 beta and currently Gradle 2.6 and gradle-experimental:0.3.0-alpha7 wrapper.

I also have a project in Android Studio already, in which I jammed all sources together in 1 module, but the project is packed with symlinks and is not really portable, and hard to setup for other developers.
So I decided to start over and build the project configuration from ground up. I'm using a Android Library Module for the SDK, which is used by the Android Application module (test app).

The SDK module consists of a C++ core, with a JNI and Java wrapper around it. 
The module compiles and works fine - all output is nicely generated into an AAR file in build/outputs/aar/

My issue is that when I try to compile the app, it fails, saying that my SDK packages do not exist.

My app gradle files looks like this:

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

model {
    android
{
        compileSdkVersion
= 19
        buildToolsVersion = "23.0.1"

        defaultConfig.with {
            applicationId
= "com.mycompany.myapp"
            minSdkVersion.apiLevel = 15
            targetSdkVersion.apiLevel = 19
            versionCode = 1
            versionName = "1.0"
        }

   
}

    android
.buildTypes {
        release
{
            minifyEnabled
= false
            proguardFiles += file('proguard-rules.pro')
       
}
        debug
{
            debuggable
= true
            ndk.with {
                debuggable
= true
            }
       
}
   
}

    android
.sources {
       
main {
            java
{
                source
{
                    srcDirs
+= 'src'
                }
           
}
       
}
   
}
}

dependencies
{
    compile project
(':sdk')
    compile project
(':sdk-testing')
    compile fileTree
(dir: 'libs', include: ['*.jar'])
    compile
'com.android.support:support-v4:19.1.0'
    compile 'com.android.support:appcompat-v7:19.1.0'
}

tasks
.withType(JavaCompile) {
    sourceCompatibility
= JavaVersion.VERSION_1_7
    targetCompatibility
= JavaVersion.VERSION_1_7
}


My SDK gradle files looks like this:

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

model {
    android
{
        compileSdkVersion
= 19
        buildToolsVersion = "23.0.1"

        defaultConfig.with {
            versionName
= '1.0'
        }
   
}

    android
.ndk {
        moduleName
= "sdk"
        toolchain = "gcc"
        cppFlags += ["-D ANDROID_LOGCAT=1",
                     
"-DGRADLE",
                     
"-std=c++11",
                     
"-frtti",
                     
"-fexceptions",
                     
"-I${projectDir}/../../../".toString(),
                     
"-I${projectDir}/../../../src".toString(),
        ldFlags += ["-Lsrc/main/jniLibs/armeabi"]
        ldLibs
+= ["log", "atomic"]
        stl
= "c++_shared"
    }

    android
.sources {
       
main {
           
// Java sources
            java.source.srcDirs += '../../../external/src'

            // local folder with symlinks, created by gradle task :sdk:createLinks
            jni.source.srcDirs += 'src/cpp'
        }
   
}

    android
.productFlavors {
        create
("all") {
        }
   
}

    android
.buildTypes {
        release
{
            minifyEnabled
= false
        }
        debug
{
            debuggable
= true
            ndk.with {
                debuggable
= true
            }
       
}
   
}
}

dependencies
{
    compile fileTree
(dir: 'libs', include: ['*.jar'])
}

tasks
.withType(JavaCompile) {
    sourceCompatibility
= JavaVersion.VERSION_1_7
    targetCompatibility
= JavaVersion.VERSION_1_7
}

task createLinks
(type:Exec) {
    workingDir
'scripts'
    commandLine './createLinks.sh'

    standardOutput = new ByteArrayOutputStream()
    ext
.output = {
       
return standardOutput.toString()
   
}
}

tasks
.whenTaskAdded { task ->
   
if (task.name == 'preBuild') {
        task
.dependsOn createLinks
   
}
}

I also have another pure Java module in my project, called sdk-testing, which works fine when I add the dependency "compile project('sdk-testing')"

There is a way how I can get it to work:
I first let gradle build the SDK, and then manually copy the AAR file to 'app/libs', and add the following to the app build.gradle file:
repositories {
    flatDir
{
        dirs
'libs'
    }
}

And add the dependency:
compile(name:'sdk-all-debug', ext:'aar')

This is of course very inconvenient, because every time I change the SDK, I need to recompile the SDK, copy it, and then copile the app, instead of just using the 'Run App' or 'Debug App' buttons. 
Is there anyone who has experienced this before? Am I forgetting something, or using a wrong command?

Any help is appreciated :)

Kind Regards,

Maurice








Raymond Chiu

unread,
Nov 9, 2015, 3:23:19 PM11/9/15
to adt-dev
Hi Maurice,

Can you clarify what do you mean when you say "My issue is that when I try to compile the app, it fails, saying that my SDK packages do not exist."?  Does your app contains C++ code that depends on the SDK?  Or is it pure Java and uses the Java wrapper provided by the SDK?

It should work if it's the latter.  Please let me know the error message you see.  Thanks.

Raymond
  
...
Message has been deleted

Eric Hall

unread,
Nov 10, 2015, 12:29:31 PM11/10/15
to adt...@googlegroups.com
Hi Maurice,
We've actually got a pretty similar setup for our SDK/APP, though while our SDK is at 3.0-alpha7m the app is still sitting back at Gradle 1.3 which makes things fun.

One thing I didn't see in your posted gradle files was a settings.gradle file for the project which declares the sdk project and includes it in your app project.

something like the following
include ':app', ':sdk', ':sdk-testing'
project(':sdk').projectDir = new File(settingsDir, '<path to sdk project>')
project(':sdk-testing').projectDir = new File(settingsDir, '<path to sdk-testing project>')
Back when we had all our projects sitting at 1.3 this allowed us to rebuild the sdk automatically when building the app.

Hope this helps,
Eric


On Mon, Nov 9, 2015 at 11:27 PM, Maurice Wingbermühle <wate...@gmail.com> wrote:
Hi Raymond,

I'll try to clarify:

My SDK module contains Java and C++, and will be compiled to a Android library (AAR) file. This is working fine, output is as expected (I unzip the compiled AAR, and I can find the Java classes and .so files that I need).
The App modules is pure Java, no C++, and will be compiled to an APK.
When I try to use this SDK module in my App Module, the App module does not compile.
The error that I get is:

error: package com.company.sdk.package does not exist

... and that for every usage of a SDK class or package.
When I inspect my project on disk, I cannot see the SDK files in the 'exploded-aar' directory in the app module's build/intermediates directory.

I do have a work-around:
Only when I manually copy the AAR file to my (self-created) libs directory, and add the extra line to the dependency section of the App build.gradle, it does work, but this is highly inconvenient.

I hope that I have clarified the issue a bit.
Kind regards,

Maurice


Op maandag 9 november 2015 21:23:19 UTC+1 schreef Raymond Chiu:

--
You received this message because you are subscribed to the Google Groups "adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to adt-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Maurice Wingbermühle

unread,
Nov 11, 2015, 2:19:54 AM11/11/15
to adt-dev
Hi Eric, 

My current settings.gradle file just contains the first line of the content you suggested.
Although I like the idea of this, it does not really match my goal on how to use Android Studio.
Because we allready have a complete buildchain ready using pure NDK and scripting, we want to use AS to fill the gap that we currently have: debugging. 
If I set-up the SDK as a seperate Gradle project, I do not get to profit from the capabilities to debug the C++ part of the SDK, which is exactly the point that I'm trying to achieve.

In my opinion, it should be possible to define the SDK as a Module, which is then used by the App Module.
It nearly works, it just does not copy the AAR file from the SDK module to the app, and extracts it to the exploded-aar directory.

Also, Android Studio does not seem to recognize the C++ part in the SDK module, and does not add the 'app-native' Launch configuration on its own.

Anyway, thanks for thinking along, and pointing me to a feature which I didn't know it existed. It might be useful one day.
Kind regards,

Maurice

Op dinsdag 10 november 2015 18:29:31 UTC+1 schreef Eric Hall:
Reply all
Reply to author
Forward
0 new messages