Long story short I have decided to play around with the new Google wearable stuff, so I opened up android studio and clicked the import from sample project. (mind you it does not matter what project I chose to import and have tried many of them)
I open the project and Everything is wrong because Android studio can't find the wearable imports... BUT it CAN build it to the watch just fine. So basically if I type
mApiClient = new GoogleApiClient.Builder(this);
The GoogleApiClient is made red because it "Cannot find the object"
import com.google.android.gms.common.ConnectionResult; <-- cannot find symbol common
import com.google.android.gms.common.api.GoogleApiClient; <-- cannot find symbol common
import com.google.android.gms.common.api.ResultCallback; <-- cannot find symbol common
import com.google.android.gms.wearable.MessageApi; <-- cannot find symbol MessageApi
import com.google.android.gms.wearable.MessageEvent; <-- cannot find MessageEvent
import com.google.android.gms.wearable.Node; <-- Cannot find Node
import com.google.android.gms.wearable.NodeApi; <-- Cannot find NodeApi
import com.google.android.gms.wearable.Wearable; <-- Cannot find Wearable
Now mind you... All of this builds... It just renders the editor useless due to the fact that it gives me no code assistance and is always telling me there are 100s of errors in the project. It is worse then notepad!
Things I have tried:
Uninstalling and reinstalling Android Studio (3 times now)
Uninstalling and reinstalling ALL of the android SDK's and extra
tools
Clicking the button to Sync Project with Gradle (1000s of times)
Checking all Gradle files for the right packages (again, these are
the sample code and it does build... so it has to be getting the
right stuff...)
Threatening my PC with installing Hackintosh on it and then burning
it. (at least 6 times now)
I have tried looking around but so far have not found anything that fixed it. any help would be much appreciated.
Thanks
EDIT:
(Delayed Application wearable sample code!)
gradle files:
Module Application:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
repositories {
jcenter()
}
dependencies {
compile "com.android.support:support-v4:21.0.2"
compile "com.android.support:support-v13:21.0.2"
compile "com.android.support:cardview-v7:21.0.2"
compile 'com.google.android.gms:play-services-wearable:6.5.+'
compile 'com.android.support:support-v13:21.0.+'
wearApp project(':Wearable')
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 18
targetSdkVersion 21
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
Module: Wearable
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
dependencies {
compile 'com.google.android.gms:play-services-wearable:6.5.+'
compile 'com.android.support:support-v13:21.0.+'
compile 'com.google.android.support:wearable:1.1.+'
}
// The sample build uses multiple directories to
// keep boilerplate and common code separate from
// the main sample code.
List<String> dirs = [
'main', // main sample code; look here for the interesting stuff.
'common', // components that are reused by multiple samples
'template'] // boilerplate code that is generated by the sample template process
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt')
}
}
sourceSets {
main {
dirs.each { dir ->
java.srcDirs "src/${dir}/java"
res.srcDirs "src/${dir}/res"
}
}
androidTest.setRoot('tests')
androidTest.java.srcDirs = ['tests/src']
}
}
Hope this helps! hopefully I'm missing something stupid. On my mac I was able to import the sample code and run it (on my windows I can run it... just editing is going to be terrible.)
Sounds like it could just be a problem with your settings file somehow being corrupted. I would try reseting android studio to a default state.
See this question for steps - How to reset Android Studio
Related
This is probably a very dumb question.
I have an app written using Eclipse years ago, for Android OS 8 (2.3.x). I am trying to bring it into the modern world (still built with 8, but targetting 28), and have moved it to Android Studio. I haven't touched Android code for some years, know very little indeed about Android Studio, and less still about Gradle.
I am currently trying to deal with the different permissions environment, ie checking on start up and requesting if necessary. I have the following code in the main activity (extended from Activity) - I know this is simple and could do with other logic, I am just trying to get it going in principle at the moment:
...
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
...
onCreate(...) {
...
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(
this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
ID_ACCESS_FINE_LOCATION);
...
android-support-v4.jar is under /libs in the project.
The build cannot resolve checkSelfPermission() or requestPermissions(), despite the imports.
I have found other questions on this, and one (self-)answer was "AS imported the supportlib as a jar and this jar was from like 2014. I just replaced the jarimport with the real dependency and now it is working."
This sounds a bit like me, but unfortunately with my limited knowledge of the build environment, I don't really understand the answer, and I am worried about screwing something big time by mistake. I'd be very grateful if someone could explain in words of one syllable what I actually need to do to resolve this, or at least point me to somewhere it is explained ....
Thanks.
POSTSCRIPT
build.gradle as requested in comment (apart from my changing the target SDK, it was created automatically on import from the Eclipse tree):
apply plugin: 'com.android.application'
android {
compileSdkVersion 8
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "uk.co.nightshadearts.gpscompass"
minSdkVersion 8
targetSdkVersion 28
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile project(':googleplayservices_lib')
compile files('libs/android-support-v4.jar')
}
android-support-v4.jar is a very old and very deprecated library. You should familiarize yourself with the new Gradle-based build system. To use the latest version of ContextCompat and ActivityCompat do the following:
Remove android-support-v4.jar from libs/
Open your module's build.gradle file (the nested one, not the one in the root of your project)
Add implementation "androidx.core:core:1.1.0" (latest stable as of writing) or implementation "androidx.core:core:1.2.0-beta01" (latest release as of writing) to your dependencies block:
...
dependencies {
...
implementation "androidx.core:core:1.1.0"
}
(taken from here)
Sync project, you should get a prompt for syncing when you change your build.gradle file contents
Reimport androidx.core.content.ContextCompat and androidx.core.app.ActivityCompat
I am trying to use a custom aar in my android project. I found dozens of examples in StackOverflow and the Web. Many failed at build, none worked. The clearest was at
http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/
That came closest to working.
Here's what I did
Successfully created a very simple AAR (Ref.aar) from Ref.java
// Ref.java
package com.ramrod.Ref;
public class Ref {
// Square an integer
public static int
square(int val) {
return (val * val);
}
}
Created a test project (RefTest)
Created folder 'libs' under RefTest/app
Added Ref.aar to libs
File->New->New Module->Import .JAR/.AAR Package.
Selected Ref.jar as filename->Finish (appeared successful).
Modified build.gradle
// build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion "27.0.3"
defaultConfig {
applicationId "com.ramrod.RefTest"
minSdkVersion 11
targetSdkVersion 15
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile( name:'Ref', ext:'aar' )
}
}
Sync build.gradle (all)
Added reference to Ref.aar method (square) to onCreate in RefTest main activity.
int sq = Ref.square( 2 );
Build->Clean then Build->Rebuild.
This produced error: cannot find symbol variable Ref
I'm sure I'm doing something naive or just plain dumb, but I can't see it.
Any help appreciated.
You should:
1) create aar library and just put it in libs directory ( without "File->New->New Module->Import .JAR/.AAR Package" )
2) add to build.gradle (Module: app)
dependencies {
...
implementation fileTree(include: ['*.aar'], dir: 'libs')
...
}
After that you can use Ref.square(int);
Your apk will contain after build:
When you import an AAR from built in helper tools using Import aar/jar option,
studio creates a module with this aar.
So at this state you can see something similar to the state mentioned below.
When display panel is Android,
Change your panel mode to Project and open your testaar , you can actually see a build.gradle file for your module and the corresponding aar.
That is why your statement
compile( name:'Ref', ext:'aar' )
was not working.
To add this aar to your project(after using import aar/jar), what you can do is to first add the module to the settings.gradle (Project settings file)
include ':app', ':testaar'
then directly add to your application level build.gradle file
implementation project(':testaar')
2)Another way is to
Right-click on your Application Module ->Select Open Module Settings -> Select the Module -> Go to Dependencies tab
P.S you can also open this window from Build->Edit Libraries and Dependencies
You will come across a window as below
Click on the small + icon, then Module option and finally add the required module(testaar)
Sync your code and voila it will start working now.
Recently dove into the world of React-Native and got roadblocked immediately. Bought a course from Udemy called The Complete React Native and Redux Course and got stuck trying to compile my first application.
I've tried a bunch of different solutions and have gone through a variety of different errors so its come to the point where I have to make my first stackoverflow post. Going to post a bunch of my current settings below as well as the error im receiving.
Error
My Configuations
Java SDK/JRE's I have installed
JAVA_HOME = C:\Program Files\Java\jdk1.8.0_151
ANDROID_HOME = C:\Users\Danny\AppData\Local\Android\sdk
PATH = C:\Users\Danny\AppData\Roaming\cabal\bin;C:\Users\Danny\AppData\Roaming\npm;
React Native Properties
React-Native Version
Android Studio Properties
Project Structure
SDK/JDK Location
Installed SDK Tools
Installed SDK's
gradle-wrapper.properties
#Mon Dec 11 21:41:46 EST 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-rc-2-
all.zip
build.gradle (Module:app)
android {
compileSdkVersion 23
buildToolsVersion '25.0.1'
defaultConfig {
applicationId "com.albums"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}
build.gradle (Project: albums)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenLocal()
jcenter()
}
}
In the course im using the instructor says to use the NEXUS 5 API 23, incase that helps. Gradle builds within Android Studio no errors.
Thanks for any and all help
Solved.
Deleted my project, recreated it with react-native init ProjectName, ran AVD, compiled project through terminal. I think I altered too many configurations in my old one and needed to recreate it.
About a week ago I asked this question
Why is library module android.support.test not visible in add dependency
After much head scratching and project setup morphing I discovered that this app/build.gradle dependency configuration gets me so much further than ever before in that at least the project compiles but does not build when attempting to run:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion '22.0.1'
defaultConfig {
applicationId "com.my.package.name"
minSdkVersion 8
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets { main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } }
packagingOptions {
exclude 'LICENSE.txt'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
compile 'com.android.support.test.espresso:espresso-core:2.1'
compile 'com.android.support.test:testing-support-lib:0.1'
}
Notice compile instead of androidTestCompile on the testing support dependencies. Using androidTestCompile results in the problems noted in my previous question identified above.
Also notice the two srcDirs in the sourceSets declaration. That declaration was automatically set when I manually created the androidTest/java directory. Should this be declared differently?
Also too, getDefaultProguardFile is underlined in the ide with the tool tip "cannot resolve symbol 'getDefaultProguardFile'". In case that is crucial.
This new revelation, switching androidTestCompile to compile, leads me to believe there is something wrong with the way I set up the project structure for the unit test. Here is a picture of my project structure:
I should mention that this project/module structure was automatically created when migrating from Eclipse. By the icon decorations it looks like it at least recognizes my test class as a test class.
But now I am stuck at this error:
Error:Execution failed for task ':mymodule:dexDebug'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\myuser\AppData\Local\Android\sdk\build-tools\22.0.1\dx.bat --dex --no-optimize --output C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\dex\debug --input-list=C:\Users\myuser\AndroidstudioProjects\myproject\mymodule\build\intermediates\tmp\dex\debug\inputList.txt
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
at com.android.dx.command.dexer.Main.run(Main.java:246)
at com.android.dx.command.dexer.Main.main(Main.java:215)
at com.android.dx.command.Main.main(Main.java:106)
Not sure if this is coming from my project setup or if it is coming from the libraries due to using compile on the support.test libraries.
Any advice on why androidTestCompile doesn't work is appreciated.
Also I just 2 days ago took all the latest updates from SDK Manager to 22.0.1 to no avail.
Another thing I noticed is the only jar I seem to find containing package com.google.common.* is in the espresso library. (com.google.common.* is a dependency of the testing libraries.) But when I inspect the espresso library jar with winzip all of the com.google.common.* package folder structure is there but there are no .class files in the package folders nor subfolders. Just mentioning that in case it lends a clue. Perhaps it is somewhere else also that I had missed.
Thanks!
Edit:
my source sets now look like this
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
}
and dependencies
dependencies {
compile 'com.android.support:appcompat-v7:22.1.0'
compile 'com.android.support:support-v4:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
I still get can't resolve android.support.test in my test class (test is colored red on these lines):
import android.support.test.InstrumentationRegistry;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
shouldn't I see a testing-support, espresso-core and espresso-contrib in my External libraries?
Below is my working Espresso gradle setup based on Android Studio 1.1.0 + Espresso 2.0 + Support Library v11 + gradle environment. It looks likely that you missed androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0' library.
dependencies {
// ...
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'
}
android {
defaultConfig {
minSdkVersion 10
targetSdkVersion 21
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
packagingOptions {
//...
exclude 'META-INF/LICENSE.txt'
exclude 'LICENSE.txt'
}
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src/main/java']
res.srcDirs = ['res']
}
}
}
About the directory, yes. just maintain androidTest and main under src folder. But, your setting does not make IDE to recognize androidTest folder as a test project folder. For that, two projects with same package name causes multi dex error I think. Use androidTestCompile to espresso-related lines in gradle dependencies and clean/rebuild project.
Note that you have to set all referenced projects gradle to same testInstrumentationRunner.
I also spent times to make it work. I wrote Korean tutorial to setup and run Espresso. Sorry about the language but screenshots might be helpful or you can try Google translate to read it.
EDIT. APPENDED BELOW
I thought why your IDE did not show green color in androidTest folder (means Test Project), and found your gradle setup seems to be wrong.
The line
{ main { java.srcDirs = ['src/main/java', 'src/androidTest/java'] } } make IDE to recognize androidTest is a Project folder not Test Project folder. remove second one. androidTest will be automatically recognized as a Test Project.
I want to import a library project into my app but whenever I try to do so , Android Studio doesn't recognise it
It also gives me errors in build.gradle ..
The Library is : PagerSlidingTabStrip ....
Here are some pictures :
I have been trying to make it work for 3 days so far !!
Please Help Me :)
EDIT:
apply plugin: 'android-library'
dependencies {
compile 'com.android.support:support-v4:19.0.0'
}
android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion 8
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
EDIT2 :
FAILURE: Build failed with an exception.
* What went wrong:
A problem occurred configuring project ':Sahertoday'.
> Could not resolve all dependencies for configuration ':Sahertoday:_debugCompile'.
> Could not find com.astuetz:pagerslidingtabstrip:1.0.1.
Required by:
Saher-3:Sahertoday:unspecified
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
First of all, you can add this dependency to your project, without compiling the lib locally.
dependencies {
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
}
Otherwise if you would like to compile this lib locally, you have to define these keys in gradle.properties in the root.
ANDROID_BUILD_TARGET_SDK_VERSION=19
ANDROID_BUILD_TOOLS_VERSION=19
ANDROID_BUILD_SDK_VERSION=19
EDIT
There is also a GUI way for doing this. It is accessed by selecting the module facebook in the project tree and pressing f4. Also you can just right-click the facebook and go to Open Module Settings near the bottom. It is shown in the pictures. The numbers in the picture are top sdk version at the time of writing.
There is a simpler solution.
The constants like ANDROID_BUILD_SDK_VERSION can be replaced with normal version "numbers". So instead of
android {
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
defaultConfig {
minSdkVersion 8
targetSdkVersion Integer.parseInt(project.ANDROID_BUILD_TARGET_SDK_VERSION)
}
..file can look like this:
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
}
Go the facebook folder which you have imported in your project. Copy the gradle.properties file and paste in into your facebook module.It will remove the errors.
For those who ran into same problems while adding libraries and still can't get it work. The following local include of the .aar file worked for me:
Just download the .aar file from the maven repo manually.
In Android Studio go to File -> new Module -> import .JAR or
.AAR package and select your downloaded .aar file.
Android Studio does the rest (in the build.gradle) for you. Maybe clean and rebuild your project.
apply plugin: 'com.android.library'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
minSdkVersion 8
targetSdkVersion 4
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
}