how to use google cloud api in java with gradle - java

enter image description here
I was following the tutorial on the Google Cloud platform, but I do not get it at all.
enter image description here
So I got a bulid file that did not exist, and I thought I could use the build / ilbs / name-jar file, but it did not.
When I ran build I saw a lot of downloads. Maybe download the api.
I created a different project and used a jar file, but I read the error "The import com.google can not be resolved" in every import com.google section
enter image description here
I am not an English citizen. This article is written in Google Translator.
Sorry for the poor English.
I could not find a good example in my language.

To have a complete set of configuration in a eclipse project you need the following configuration in the build.gradle file (see section "Installation is easy" in this link):
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.api-client:google-api-client:1.23.0'
}
You can add the Speech API dependency:
compile 'com.google.cloud:google-cloud-speech:0.53.0-alpha'
In the images you attached, there are mixing eclipse projects: ttt and fairy. In the project fairy the build.gradle doesn't seem exist and this is a cause for the unknown com.google package.
Add the build.gradle or recreate the Gradle Project, then click Refresh Gradle Project.

Related

The project uses Gradle 2.10 which is incompatible with Android Studio 2020.3

While learning android studio from udacity they asked me to download a project; I did so but while I was importing it to android studio I got an error message:
The project uses Gradle 2.10 which is incompatible with Android Studio 2020.3.
Why did I get this error?
If you did not figure this out yet, I am just starting the same project too. I changed the classpath like above. I then found a website to use the latest version of gradle and the plugin. It says "You can specify the Gradle version in either the File > Project Structure > Project menu in Android Studio, or by editing the Gradle distribution reference in the gradle/wrapper/gradle-wrapper.properties file." I did the first one.
Then, I got errors for both build.gradle files in the project and app version. I get this error: Could not find com.android.tools.build:gradle:7.0.3
If you get this error, click the link "Add google Maven repository and sync project". Then you have to click "Do refactor".
Now you come across another error in one of the build.grade files. This stackoverflow link will tell you: Gradle - Error Could not find method implementation() for arguments [com.android.support:appcompat-v7:26.0.0]
Finally, replace testCompile with testImplementation and then you should get BUILD SUCCESSFUL.
The project's Gradle version you're trying to clone is outdated. Change it based on your android studio version.
on your build.gradle(Project) change the version and sync
classpath "com.android.tools.build:gradle:4.1.2"
Got the solution :)
In gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
In build.gradle project level:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
In build.gradle module level:
Replace all "compile" with "Implementation" and do other suggested version updated and you are done here :)
Hope this works for you, if yes just press that up button :)
If not getting any solution, the best you can do is copy the "app" folder of your project(using file explorer) and paste it in a newly created project(should be of same name as of your project), in that way you'll get already synced gradle and newly created version of your app.
Because, every important thing in your project is present in its app folder and when you create a new project with same name and copy app folder from your older version of your project, you will have a newer version of your project with all same resources.
Note: You can't move through branches and other commits
I saw this issue several times with Android studio. Below trick worked me the best.
Android Studio suggests to upgrade to newer version, please upgrade it and let it sync
Mostly you would see error message again
Now, Go to File -> Invalidate Caches/Restart
It should work now
If it shows same error then I don't know what to do.
If it shows different error then repeat from step 1 to 3 for new error.
Hope, it helps some..

Cannot resolve symbol 'google' for Cloud Vision API

I'm trying to get Google Cloud Vision API working with Java in IntelliJ. I've followed all the steps at https://cloud.google.com/vision/docs/quickstart-client-libraries. I'm running the following code, just trying to import the packages I need and when I scroll over google, it is underlined in red and says 'cannot resolve symbol google'. How do I fix this?
code and error
The error shown in your screenshot usually happens when an external dependency could not be loaded.
Please make sure you accurately followed the steps in the Install the Client Library section of the guide you followed, in the Java tab of the widget.
From the screenshot I can not really tell what build settings you configured for the IntelliJ project. If you chose no build tools or the regular Ant setup, it might be easier for you to re-setup a project based on either Maven or Gradle.
With Gradle and Maven it is pretty straightforward to add the dependency to the project as described in the guide, otherwise you will have to download all the dependency libraries yourself and add them to your project folder and build environment manually as files.
If you did already use Gradle or Maven for your project, and did follow the guide but it still shows the error, then it's usually a case of IntelliJ not loading those changes yet.
In that case you can fix it by selecting your Gradle or Maven tab in Intellij, and clicking the re-import button (looks like a refresh icon from browsers) to reload the configuration, usually triggering a download of missing dependencies.
Otherwise you can also trigger a full build with running the gradle build or mvn compile targets, which also should download all configured missing dependencies.

Conflicts with google instant game and com.google.gms.google-services plugin

I'm creating an instant version of my Android game. I have followed the instructions on this example:
https://codelabs.developers.google.com/codelabs/android-instant-apps/index.html?index=..%2F..%2Findex#0
The problem is that in the feature module I need to access services implemented by the com.google.gms.google-services plugin, so I put it at the bottom of my dependencies in the gradle file for such module, but as the package name of my game is (say) com.me.mygame while the one for the feature module would be com.me.mygame.base, the aforementioned plugin fails because there's no com.me.mygame.base package defined in my app google-services.json file.
So I tried by setting the name of the feature module to com.me.mygame too and that made the trick for that and gradle would successfully build the project, but it will fail when trying to build the APKs due
com.android.dex.DexException: Multiple dex files define Lcom/me/mygame/BuildConfig
I tried to workaround this by adding this option to my gradle modules, as suggested in an answer to a related question here:
android {
dexOptions {
preDexLibraries = false
}
}
But to no avail. The problem persist.
It seems the logical thing would be that the feature module has a different package name, but as told before this will fail due the com.google.gms.google-services plugin not founding a configuration for such package.
So... I'm stuck at this and have run out of ideas, so any help will be welcomed.
Thanks a million in advance!
UPDATE: I have published a minimal example in github at the following URL:
https://github.com/fmmarzoa/IssuesWithFirebaseCrashlytics
I've took that example directly from Google Guides. The master branch is empty, those with the workarounds I've tried so far are all named 'firebase/...'. The last one is this 'firebase/updating-plugins':
https://github.com/fmmarzoa/IssuesWithFirebaseCrashlytics/tree/firebase/updating-plugins
What I have done in that one is to update several things as suggested by dchai, and it seems to do the trick for the Google Services plugin (at least it does build now) but the Firebase Crashlytics plugin which depends on the aforementioned one continues failing with the error message:
Crashlytics could not find the manifest. Not found at
/Users/fran/Desktop/Idea/android-topeka/base/build/intermediates/merged_manifests/debug/AndroidManifest.xml
Older versions of plugins don't correctly pick up applicationId for the purposes of checking against google-services.json.
If you upgrade to com.android.tools.build:gradle:3.3.0-alpha08 and com.google.gms:google-services:4.1.0, your original project structure should build fine.

How to add commons-math library in Android Studio

I am trying to import common-math library to my Android Studio project. I placed the file commons-math3-3.6.1.jar file in libs folder and in the gradle file I have this line:
compile 'commons-math3-3.6.1.jar'
But I get this error: `
Error:(32, 0) Supplied String module notation
'commons-math3-3.6.1.jar' is invalid. Example notations:
'org.gradle:gradle-core:2.2',
'org.mockito:mockito-core:1.9.5:javadoc'.
Can anyone tell me what I have to do?
Is there a reason you need to use the jar file? You can get the file directly from Maven repository using something like this:
compile 'org.apache.commons:commons-math3:3.6.1'
Available on both JCenter and MavenCentral.
in Android view open Gradle Scripts - build.gradle and add this to dependencies:
implementation 'org.apache.commons:commons-math3:3.6.1'
You can also check their websites if newer version isn't available list of latest versions

Android Library Project without sources

I've created Android Library Project which depends on three libraries : google-play-services_lib, android-support-v7-library and pull-to-refresh ( https://github.com/chrisbanes/Android-PullToRefresh/blob/master/library/src/com/handmark/pulltorefresh/library/PullToRefreshBase.java )
I marked my project as library and want to create jar which can be used as jars are used is google_play_services and android-support library. My question is what I have to do to be sure that libraries that my project depends on are not included in my jar. I want 3rd person - who will use my library - to add them with my projectes as dependencies and get it work.
Unchecked export options in java build path is enough?
I don't know exactly if you have used Gradle or not. But if it's the former, then you don't have to do nothing. If you have include your dependencies in your library project like this:
dependencies {
compile 'whatever:library'
}
Then it's ok.
Note: If your project includes resources (like layouts and more) it can not be compiled as a jar!
Note2: If you didn't use Gradle, notify me in the comment and I'll update this post explaining how to achieve that.
UPDATE 1:
First of all, let me link you to another related answer I did the last week. It gives you useful links to learn Gradle in a hurry (at least all the basics).
Second, do you use Android Studio?
UPDATE 2:
It's just a matter of taste ;)! But with Android Studio (Intellij) you don't need to configure the extra step to get Gradle working.
Well, after you have read those links I've put before, the next step is to create a library project (in Android Studio it will generate the required folders/files automatically for you).
In your lib or library folder (where you put your source code of your library) in your build.gradle put the following:
dependencies {
compile 'com.google.android.gms:play-services:4.4.52'
compile 'com.android.support:gridlayout-v7:19.1.0'
compile 'it.sephiroth.android.library.fork.actionbarpulltorefresh:actionbarpulltorefresh:0.9.92'
}
And with that you can generate your library in aar format.
NOTE: Android-PullToRefresh library is not Gradelized, so I had to search for another alternative that has been uploaded to MavenCentral (like sephiroth's one). Remember to use Gradle, please! To search already Gradelized libs!

Categories