Gradle cannot find symbol class in Android Studio project - android-studio

Gradle has trouble resolving a dependency in an Android Studio project. I have a library that has a dependency to a java module called "common". I have the dependency speficied in the buld.gradle (see below), however, I still get "cannot find symbol MyClass" when building. How can I fix this?
The line in error is the import that references the package in "common".
Building works fine with maven, but not with gradle.
This is the build.gradle of my lib.
apply plugin: 'com.android.library'
android {
compileSdkVersion 17
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 17
targetSdkVersion 17
}
buildTypes {
release {
minifyEnabled false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':common')
}

Related

Class Not found when try to acces the class in the library of a android module from another Android module

I have a Android library module (Common), which has a library dependency (com.android.support:multidex:1.0.1). Alongside "Common" module I have an Android module (Module1)ich requires "MultiDex" (located as dependency within the "Common" Library Module), but I get an error: android.support.multidex.MultiDex does not exist.
Common Library build.gradle:
apply plugin: 'com.android.library'
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
Module1 build.gradle:
dependencies {
compile project(':Common');
}
But in Module1 i am unable to use MultiDex
In your build.gradle you need to enable multidex as:
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
Hope this helps..

failed to build tool version android

I tried to open an old project of mine in a freshly installed android studio (today), It gave me this error :
failed to find build tools revision 23.0.0 rc2
install build tools 23.0.0 rc2 and sync project
I looked for it in the internet and I tried to add this in the gradle file :
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
}
And it gave me another error:
gradle DSL method not found: 'android()'
possible causes:
the project may be using a version of gradle that does not contain the method.
gradle settings.
the build file may be missing a gradle plugin.
apply gradle plugin.
this is my gradle file :
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
}
Right click on your project > Open Module Settings > Properties Tab, then change :
Compile Sdk Verion to API 23: Android 6.0
Build Tools Version 23.0.0
I checked the build tools version installed in my Android Studio and I have Android SDK Build-Tools rc3, Not rc2.
I changed it in build.gradle (Module App) to
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc3"}
After that it works.
Check if you have the same issue.
In your top level build.gradle you have to remove the android block
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
}
Just use:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
}
}
allprojects {
repositories {
jcenter()
}
}
You have to use the android block, in the app/build.gradle where you should define something like:
apply plugin: 'com.android.application'
android {
compileSdkVersion XX
buildToolsVersion "XX.X.X"
defaultConfig {
minSdkVersion XX
targetSdkVersion XX
versionCode 1
versionName "1.0"
}
buildTypes {
release {
//......
}
}
}
dependencies {
//......
}
theres 2 gradle files
the one you have to modify is the one under this directory
APPLICATION NAME / APP / BUILD.GRADLE
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "PUT THE CODE YOU WANT HERE EX "23.0.0 r3""
defaultConfig {
applicationId "org.app.appname"
minSdkVersion 10
targetSdkVersion 22
versionCode 9
versionName "1.10.3"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.google.android.gms:play-services:7.5.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile files('libs/volley.jar')
}
not the one which looks like these
// Top-level build file where you can add configuration options common to all sub-projects/modules.
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()
}
}
In my case I tried everything and finally I had to do a simple change from this
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"
}
to this
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0" //DELETE rc2 AND SYNC PROJECT
}
It depends on which version of Android SDK Build-tools you have installed, you could check it via the SDK Manager, in my case I haven't the 23.0.0 rc2 installed but the 23.0.1, so you need to edit your app/build.gradle
for me it was then:
android {
compileSdkVersion 22
buildToolsVersion "23.0.1"
}

How to EXCLUDE an external library jar from Proguard in Android Studio?

The build.gradle of one of the modules in my project is pretty simple:
apply plugin: 'com.android.library'
android {
compileSdkVersion 8
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 8
targetSdkVersion 8
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard.cfg'
}
}
}
dependencies {
compile project(':timsvc')
compile files('libs/jsoup-1.8.2.jar')
}
However, when I build it, it fails to find symbols in the external library jsoup-1.8.2.jar.
Since timsvc is a module of mine, having its own build.gradle and proguard.cfg, I can control its minify level, and so I don't have any problems with it.
But I cannot do the same for jsoup-1.8.2.jar because it is an externally provided jar.
Is there a way to exclude it from Proguard in Android Studio?
If so, how do I accomplish that?
The best is to see the libraries documentations and readme page (like Github).
but in genneral you can add -keep options to your proguard configuration file for the classes in those external jars. For instance:
-keep class example.** { *; }

gradle error "Execution failed for task ':app:compileDebugJava' " while build groovy in android studio

I got a error while building groovy code in android studio.
the error msg is
C:\Users\Administrator\Desktop\lytest110\app\src\main\java\gzzhe\com\lytest110\MainActivity.java:41: error: can't find symbol
Log.d("lytest110", GroovyTest.getMsg());
^
symbol: parameter GroovyTest
possition: class MainActivity
1 error
FAILED
:app:compileDebugGroovy
FAILURE: Build failed with an exception.
my build.gradle is:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.13.2'
classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
the build.gradle in app directory:
apply plugin: 'com.android.application'
apply plugin: 'me.champeau.gradle.groovy-android'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "gzzhe.com.lytest110"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile files('libs/groovy-2.4.0-beta-3-grooid.jar')
compile files('libs/android-support-v4.jar')
compile fileTree(include: ['*.jar'], dir: 'libs')
// compile 'com.android.support:support-v4:19.+'
}
I have put the groovy file into main/groovy/package/name directory.
the groovy is annotated with #CompileStatic.
This problem have puzzled me for several days.
From your error it seems like you are calling Groovy class with Java. According to the Android Groovy plugin readme. it says
Groovy sources must be found inside src/main/groovy. Note that the plugin follows the behavior of the Groovy plugin in Gradle, which means that if you want to be able to have Groovy classes referencing Java classes that themselves reference Groovy classes, then those Java classes must be found inside src/main/groovy too (this is called joint compilation).
So you just put your MainActivity.java to your ../main/groovy/package/name directory

Unable to resolve Robotium Dependency Android Studio

I am trying to use Robotium for writing test cases in Android Studio and I am having a hard time configuring things.
Following is my build.gradle inside the app.
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName '1.0'
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
lintOptions {
abortOnError false
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.1'
androidTestCompile 'junit:junit:4.11'
}
When I clean the project, I get the following error:
Error:A problem occurred configuring project ':NativeSDK'.
Could not resolve all dependencies for configuration ':NativeSDK:_debugTestCompile'.
Could not find com.jayway.android.robotium:robotium-solo:5.1.
Required by:
InAppSDK:NativeSDK:unspecified
Could not find junit:junit:4.11.
Required by:
InAppSDK:NativeSDK:unspecified
Any help on how to resolve this?
I hope that in your top level build.gradle you have the repositories{mavenCentral()} or repositories{jcenter()} in your buildscript{}
When you provide the reference to an artifact in mavenCentral() or jCenter() you need to sync the project after disabling the offline work at least once under studio>preference>gradle>offline work.
Having this will solve the issue. Its just the local cache of the studio has not been updated as per my understanding. You can check the available library version in mavenCentral() or jcenter() in your case its 5.1 for robotium. (better to use 5.2.1 which is the newest as of today)
make sure that you don't have the robotium jar in your libs folder as you are compiling libs file tree. If you have the jar file when compiling it will throw a UNEXPECTED_TOP_LEVEL exception.

Resources