Android Studio "cannot resolve symbol String" - android

I have a small project that was started in Eclipse. I then exported it to a gradle file, and imported it to AS (0.5.7).
At first, not much seemed to work as it should, but after a "build => make project", I didn't seem to get any highlighted errors or so.
So I tried to run the app to an emulated device. Well, the device never launched, and now I get red squiggly lines under mentions of "String", "ArrayList" etc, saying it "cannot resolve the symbol".
What the f?
I have tried cleaning and rebuilding, as well as "sync project with gradle files".
Where do I go from here? I want to get going with developing in AS so bad!
edit: Screenshot of project setup: http://i.imgur.com/ycNyPaT.png
Contents of build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}

There is simpler and I think more correct way:
Just select menu item 'File/Invalidate Caches/Restart...'
For me this successfully resolved the issue (was caused by surprising power off of PC)

So project arrangement should be as follows:
create app folder within your project.
within app folder make following folders: libs and src
inside src create main folder
inside main create java and assets
move contents of old src to java
move contents of old libs to libs
move res folder to src
move AndroidManifest.xml to src
move assets folder into src
create build.gradle inside app folder with following content:
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
}
create settings.gradle in project root with following content:
include 'app'
build.gradle in root should have following structure:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}

Android Studio 4.2.1 with Gradle 7.0.2.
Just had this problem with a module in my project. Basically many fundamental symbols like String, #Override there are not resolved. Non of the syncing or deleting cache etc helped me. The project builds without problem though, and after a successful build, the problem remained.
It turns out the issue relates to the line in the build.gradle file of the module:
apply plugin: 'java-library'
I had it there for ages and I haven't opened the module in a long time, so probably since some newer version of Gradle it's not accepted.
So the solution for me is to change the line to:
apply plugin: 'com.android.library'

Just writing this for people like me, who also landed here through googling this error.
What actually solved it for current Android Studio 3.1.+ was this

You just need to sync the project as you have opened an external project from a zip file in the android studio.
After you sync the project, you will see folders like Java,res folder,etc, instead of these not useful folders. And also, all the errors will be gone too.
When you open the external project, it will show like this.
https://photos.app.goo.gl/BFa113X9DWH8eNDt7
Then you click on the install, it will install the required components required for the project and then it will sync the project. After syncing, the Gradle will be build and your project is ready to run.

Remove the following line from the file that reported with the error if there is.
import static com.google.android.gms.internal.a.R;

try deleting [project]/[module]/build folder.
and then rebuild the project from menu->build->rebuild project
thats what worked for me.

Related

Converting an existing project from Eclipse to Gradle in Android Studio

I found an open source project I wanted to work on but I'm having trouble setting up the initial configuration. The project seems to have been written in Eclipse and I'm trying to get it to work with Android Studio. After being through a number of errors, I'm finally stuck on the following error seen in the configuration menu before I run.
AndroidManifest.XML doesn't exist or has the incorrect root tag
I found a number of answers that suggest like this that suggest I use the sync project with Gradle command but my project wasn't setup with Gradle because I'm building on someone else's project. This is my first time using Android Studio so my following attempt to fix this might not be great. I decided to try to make the project a Gradle project by adding my own build.gradle and settings.gradle files. My layout looks like this:
top level:
inside java folder:
I tried to copy working example of the build and settings Gradle files. My settings.gradle contains the following:
include ':'
My top level build.gradle contains:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.11.+'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
//compile project(":")
}
}
allprojects {
repositories {
mavenCentral()
}
}
My java level build.gradle contains:
apply plugin: 'android'
android {
compileSdkVersion 20
buildToolsVersion "17.0.0"
defaultConfig {
applicationId "org.pocketworkstation.pckeyboard"
minSdkVersion 8
targetSdkVersion 19
versionCode 1037
versionName "v1.37"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
jni.srcDirs = ['jni']
}
androidTest.setRoot('tests')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
}
And because I think it might be important, my project structure modules:
Trying to synchronize at the moment doesn't generate any word so I assume it's okay but that's a big assumption. Any ideas what I have to change?
Android Studio doesn't consider your project to be a Gradle-based project; you can tell this because a number of the entries you're seeing in the Project Structure dialog don't show up for Gradle-based projects (i.e. the Project, Libraries, Facets, and Artifacts) entries. Close your project and re-import it as a Gradle-based project and you should be okay.

Instrumentation test not creating with Gradle

I just recently converted my project to Gradle and it works fine. I'm trying to add some tests to it now. I followed the Android Gradle user guide for setting up my build.gradle with tests. Here is my build.gradle:
buildscript {
repositories {
maven {
url 'http://repo1.maven.org/maven2'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
testPackageName "com.instrumentTest.java"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('src/instrumentTest')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
I created a package under src/ called com.instrumentTest.java that I put a sample ServiceTest.java class in to test my Service. I run the test using gradle aDT and get this:
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':<MyProject>:compileDebugAidl'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:69)
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(PostExecutionAnalysisTaskExecuter.java:35)
Caused by: com.android.ide.common.internal.LoggedErrorException: Failed to run command:
<some big command>
Error Code:
1
Output:
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "public"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "public"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "class"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "MyServiceTest"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "extends"
\src\com\instrumentTest\java\MyServiceTest.java:10: syntax error don't know what to do with "ServiceTestCase<MyService>"
It outputs the syntax error don't know what to do with for each line of code in my sample test class. Not really sure what's going on. The documentation for setting all of this up is pretty fuzzy.
I'm also not really sure the difference between assembleDebugTest, connectedInstrumentTest, and installDebugTest based on their descriptions from running gradle tasks. Is there something I have to do to my manifest in order to have the tests visible to Gradle? Am I supposed to create my tests in an entirely different project? I read that previously people would create separate test projects, but since I set the root for instrumentTest in the build.gradle file I assume we can do it all from one project now?
In build.gradle, you declared your instrumentTest folder to be in src/instrumentTest, which means if you have a test inside that's in the com.instrumentTest package, it should be located at src/instrumentTest/com/instrumentTest/MyServiceTest. Instead you've put the file at src/com/instrumentTest/java/MyServiceTest.
For reasons I don't quite understand it's trying to compile it with the AIDL compiler, and your java file isn't valid AIDL. If you move it to the directory it's expecting for instrumentTest, it should compile okay.
There's an explanation of the Gradle tasks at http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Android-tasks.
assembleDebugTest - The task to assemble the output(s) of the project
connectedInstrumentTest - Runs checks that requires a connected device or emulator. they will run on all connected devices in parallel.
installDebugTest - installs the test APK on the connected device or emulator
You can try this instead if you don't want to mess around with the gradle file:
http://rexstjohn.com/unit-testing-with-android-studio/
Basically, you set up and run an Android Test configuration which can run all tests in the module, in a specific package, class or method. So you can set up your test classes wherever you want. It will pick up the test classes without having to modify the build.gradle file and running commands. That also answers your last question, it can be all from one project.
I just started using Android Studio and Gradle and couldn't get the tests to run properly at first until I tried the above. I found it odd having to modify the build.gradle file just to set up a simple test.

Migrating from Eclipse to Android-Studio (gradle)

I'm trying to migrate my applications to use gradle, but I'm facing some problems including library projects.
My project tree is this:
My projects root
- MyLib1
-- res
-- src
-- libs
- MyLib2
-- res
-- src
-- libs
- MyLib3
-- res
-- src
-- libs
- MyAppBase
-- res
-- src
-- libs
- MyApp - full version
-- res
-- src
-- libs
- MyAppFree - free version
-- res
-- src
-- libs
With Eclipse I had the following dependencies
MyAppBase depends on:
-MyLib1
-MyLib2
-MyLib3
MyApp depends on:
-MyAppBase
-MyLib1
-MyLib2
-MyLib3
MyAppFree depends on:
-MyAppBase
-MyLib1
-MyLib2
-MyLib3
This organization worked well within Eclipse, but now with Android-Studio and gradle I'm having problems.
I've got the following build.gradle file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
apply plugin: 'android'
dependencies {
//compile project('../MyLib1') <- error
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 18
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 4
targetSdkVersion 14
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
How can I include the projects MyLib1, MyLib2, MyLib3 as a dependency so It will be compiled along with my project???
At present, all dependencies need to live under the project's root directory, so you'd need to set up your project to be rooted at the directory above MyLib1, MyLib2, MyApp, etc. This limitation will be lifted in the future; you can track its progress at https://code.google.com/p/android/issues/detail?id=56367. Your libraries would be library modules under that root, and your app(s) would be Android application modules. Each module has its own separate build.gradle file and can compile to a JAR (plain Java library), AAR (Android library, which includes code + resources), or APK (Android app).
I'm not sure if MyAppFree and MyApp are separate Eclipse projects; if they are, under Android Studio and Gradle I'd encourage you to combine them into one module that has free and paid flavors. Build flavors are designed explicitly to aid this sort of use case. See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Product-flavors for more info.
UPDATE
In the comments below it looks like you have a very large number of libraries. In that you probably don't want to build them all from source all the time, or manage a project that has dozens of modules. In that scenario, keeping modules that don't change very often as separate projects makes more sense. Those projects can compile to JAR or AAR, which brings us back to your original question of how to make those work in Android Studio.
You could copy JAR files into a libs directory under your project root and link them in. I believe there are problems trying to do the same with AAR libraries; see https://code.google.com/p/android/issues/detail?id=63908 to track the progress of that. If you don't want to maintain multiple copies of the libraries, you could either try symlinking the directories (I think that will work), or you could set up a local Maven repository and have the side projects publish their artifacts to that. I don't have links with detailed instructions on that at my fingertips; you could start with http://www.gradle.org/docs/current/userguide/publishing_maven.html.
There will be a fair learning curve to getting a local Maven repo set up, but once it's done, you'll probably find that it solves your problem pretty cleanly, and if you're in a shop with multiple developers and would like an organization-wide Maven repo, perhaps with artifacts that are published to it from a build server, you can set that up.
Maybe go to the menu "Project Structure" (Ctrl+Shift+Alt+S) and add the desired dependencies to the correct modules.
In Android Studio Your Project Structure should be looking like this
YourProjectName
-libraries
--myLib1
--myLib2
--myLib3
-MyAppBase
--build.gradle( It will have dependency of all three libraries like mentioned in last)
--src
--res
-MyApp
--src
---main
----java
----res
----Manifest
---full
----java
----res
----Manifest
---free
----java
----res
----Manifest
--build.gradle ( It will have dependency of only MyAppBase because your MyAppBase is already having dependency of all of your three libraries so no need to include them again)
Now you can have your build flavors in this last mentioned build.gradle file like
buildTypes {
debug {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
release{
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
signingConfig signingConfigs.prodSigning // write your signingConfig
}
}
productFlavors{
full {
//required configuration for full version
}
free {
//required configuration for full version
}
}
Note your directory name and product falvours name in build.gradle file must be same so while compilation (from Built Variant tab available in Left panel or command prompt) it will automatically take the code/res/manifest from respective folder.
Above configuration will give four type of following build variants
debugFull,debugFree,releaseFull,releaseFree
You Can add dependency in build.gradle of MyAppBase like
dependencies {
compile project(':libraries:myLib1')
compile project(':libraries:myLib2')
compile project(':libraries:myLib3')
}
And in the MyApp module's build.gradle file like this
dependencies {
compile project(':MyAppBase')
}

Gradle build fails looking for Google Play Services in Android Studio

I'm trying to run my project in Android Studio 0.3.6, but I always get this error :
Gradle: A problem occurred configuring root project 'myapp_android'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':_DebugCompile'.
> Could not resolve com.google.android.gms:play-services:3.1.36.
Required by:
:myapp_android:unspecified
> Could not GET 'http://maven.hq.couchbase.com/nexus/content/repositories/releases/com/google/android/gms/play-services/3.1.36/play-services-3.1.36.pom'. Received status code 503 from server: Service Temporarily Unavailable
Here is my complete build.gradle file :
home = System.getenv("ANDROID_HOME")
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
maven {
url "http://maven.hq.couchbase.com/nexus/content/repositories/releases/"
}
maven {
url "http://files.couchbase.com/maven2/"
}
}
dependencies {
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:18.0.+'
compile 'com.google.android.gms:play-services:3.1.36'
compile 'com.couchbase.cblite:CBLite:1.0.0-beta'
compile 'com.couchbase.cblite:CBLiteEktorp:1.0.0-beta'
compile 'com.couchbase.cblite:CBLiteJavascript:1.0.0-beta'
compile fileTree(dir: 'libs', include: '*.jar')
instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:4.3'
}
android {
compileSdkVersion 18
buildToolsVersion "18.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
tasks.withType(Compile) {
options.encoding = 'UTF-8'
}
And finally, my SDK Manager :
I tried with many versions of Android Studio (0.2.8, 0.2.9, 0.2.13, 0.3.2, 0.3.6) and it's always the same. I tried changing the Play Services version, I tried changing the order of the dependencies, nothing did the trick.
Is there something not correct with my setup? I looked for answers and pretty much everything I found (build.gradle structure, SDK missing items) was already fine...
I finally found what was the problem. #gnuf pointed me in the right direction. But the problem was that Android Studio, in Project Structure | Platform Settings | Android SDK, showed the wrong SDK. It showed the SDK it used was C:/Program Files (x86)/Android/android-studio/sdk, but in fact, I discovered it tried to build with another SDK located in C:\Users\my_username\AppData\Local\Android\android-studio\sdk.
Needless to say, it didn't work. There was nothing installed in this SDK. So I copied the contents of C:/Program Files (x86)/Android/android-studio/sdk to C:\Users\my_username\AppData\Local\Android\android-studio\sdk and it worked! My first build in three weeks.
So I think it's a bug of Android Studio. If anyone runs into this problem, make sure you only have one SDK on your computer. If you have more than one, make sure they all have the required dependencies. If you can, just copy the content of the most up to date to the others.
It may be the case that the location where you've installed the SDK components is different than the one that Android Studio is using.
Can you verify that the local Maven repositories (installed by the Google Repository and Android Support Repository) are under the Android SDK that you're using in your Android Studio project?
In Android Studio, go to Project Structure | Platform Settings | Android SDK. (On my machine, the path for Android SDK Location is /opt/boxen/homebrew/Cellar/android-sdk/22.2.1 ) In that directory, ensure that the subdirectory extras/google/m2repository/com/google/android/gms/play-services/ exists.
It looks for Google dependencies like Google Play Services in your SDK extras, not in Maven repositories over the network. Install the "Google Repository" in your SDK manager and you should be good to go.

Can't build project, problems with gradle?

I'm trying to switch from eclipse to android studio for my android development.
However, I still haven't found the right way to import my existing project.
I don't know if it is important: but I'm using a mac
I did the export step in eclipse, imported this gradle build in Android Studio, but when I try to build my project, it gives me this error:
Gradle:
FAILURE: Could not determine which tasks to execute.
* What went wrong:
Task 'assembleDebug' not found in project ':ProjectName'.
* Try:
Run gradle tasks to get a list of available tasks.
Could not execute build using Gradle installation '/Users/<username>/Development/Build/gradle-1.6'.
This is the build.gradle file that eclipse gave me:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.+'
}
}
apply plugin: 'android'
dependencies {
compile project(':ProjectName:library:ActionBarSherlock')
compile project(':ProjectName:library:facebook')
compile files('../../../../../../../ProjectName/libs/gcm.jar')
compile files('../../../../../../../ProjectName/libs/libGoogleAnalyticsV2.jar')
compile files('../../../../../../../ProjectName/libs/commons-lang3-3.1.jar')
compile files('../../../../../../../ProjectName/libs/actionbarsherlock-plugin-maps-4.2.0.jar')
compile files('../../../../../../../NiteOwl/libs/volley.jar')
compile project(':ProjectName:library:PullToRefresh')
compile project(':ProjectName:library:google-play-services_lib')
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 8
targetSdkVersion 17
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
instrumentTest.setRoot('tests')
}
}
I've seen a lot of possible solutions, but none of them worked for me, any idea what I'm doing wrong?
The path to the project has no spaces in it
jar libraries => ProjectName/libs
android libraries => ProjectName/library
all of this was working in eclipse
We recently moved our project to gradle as well. We ran into issues due to the library projects.
To solve it we added a settings.gradle file in the root of your project with
include ':libs:actionbarsherlock'
include ':yourprojectname'
Add all your library projects from eclipse into the settings.gradle
We also made a build.gradle file for each of the library Projects.
AFAIK the export from eclipse doesn't deal well with library projects.
In the build.gradle at the root of your project (the one that's probably mostly empty), add the following line:
task assemble{}
Found at https://code.google.com/p/android/issues/detail?id=57531
I've had this error appear when I had a stray empty flavors stanza in my build.gradle:
productFlavors {
}

Categories

Resources