4

I'm finally migrating my eclipse android projects to Android Studio. I've created a Project which contains two modules. One module is an library project. The other is plain Activity Project which includes the library module.

Project/
   app/     (Activity Project)
   br_lib/  (Library Project)

In the apps 'module settings' i added an dependency to the lib module. The lib modules project builds normally, but my app modules build fails everytime i make a reference to one of the lib modules Classes:

import org.apache.http.client.HttpClient;
import org.cddevlib.breathe.setup.Utils <--  // lib refrence;


public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.convertName(""); // <--- lib module static class;
    }
}

Errors:

Error:(9, 34) error: cannot find symbol class Utils
Error:(25, 9) error: cannot find symbol variable Utils

What is wrong? What am i missing? Thank you

This is the app modules gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"

defaultConfig {
    applicationId "pro.breathe.cddev.org.br_gold"
    minSdkVersion 22
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'),     'proguard-rules.pro'
        }
    }
}

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

This is the lib modules gradle:

    apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0"

defaultConfig {
    minSdkVersion 9
    targetSdkVersion 9
}

dependencies {
    compile 'com.android.support:support-v4:22.2.1'
    compile 'com.android.support:design:22.2.1'
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.android.support:cardview-v7:22.2.1'
    compile 'com.android.support:gridlayout-v7:22.2.1'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile 'com.google.android.gms:play-services:7.8.0'
}

buildTypes {
    release {
        minifyEnabled true
        proguardFiles 'proguard.cfg'
      }
    }
}

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

This is the projects gradle:

buildscript {
    repositories {
    jcenter()
    }
dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
}

allprojects {
repositories {
    jcenter()
}
}

settings gradle is:

include ':app', ':br_lib'
Christian Dud
  • 71
  • 1
  • 8

1 Answers1

2

Ran into the same issue today in a project with 5 modules (2 of which are com.android.library modules). After lots of trial and error changes to my build.gradle file, the solution ended up relating to including the publishNonDefault true attribute in the library module's gradle file.

I found this post to be very helpful: StackOverflow - Product Flavors and Library Publication

My Application gradle file ended up looking like (code intentionally omitted):

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'NOTICE'
        exclude 'META-INF/LICENSE'
    }

    defaultConfig {
    }

    productFlavors {
        funapp {
            applicationId "com.funapp.package"
        }
        funappbeta {
            applicationId "com.funapp.beta"
        }
    }

    signingConfigs {
        googleplay {
        }
    }

    buildTypes {
        debug {
            minifyEnabled false
            debuggable true
            signingConfig signingConfigs.debug
        }
        release {
            debuggable false
            signingConfig signingConfigs.googleplay
        }
    }
}

configurations {
    funappDebugCompile
    funappReleaseCompile
    funappbetaDebugCompile
    funappbetaReleaseCompile

    // Establish debug compile dependencies for build variants
    [funappDebugCompile, funappbetaDebugCompile].each {
        it.extendsFrom commonFunSdkDebugCompile
    }
    // Establish release compile dependencies for build variants
    [funappReleaseCompile, funappbetaReleaseCompile].each {
        it.extendsFrom commonFunSdkReleaseCompile
    }
    // Common Android and JUnit testing dependencies (JUnit tests can be run
    // using only a JVM; Android tests require a device or emulator)
    [androidTestCompile, testCompile].each {
        it.extendsFrom commonTestCompile
    }
}

dependencies {
    commonFunSdkDebugCompile project   (path: ':fun-sdk', configuration: 'debug')
    commonFunSdkReleaseCompile project (path: ':fun-sdk', configuration: 'release')

And my Library gradle file looks like:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion '25.0.2'

    publishNonDefault true // <-- This happened to be the key component
    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }

    defaultConfig {
    }

    buildTypes {
        debug {
        }
        release {
        }
    }
}

configurations {
    debug
    release
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
HelloImKevo
  • 547
  • 6
  • 11