What is Google Application Credential? - android

I am using Google Speech API in My Android application and I am taking reference with the sample application provided by Google in docs.
In this sample, there are several lines for the authentication process in App level gradle file.
task copySecretKey(type: Copy) {
def File secretKey = file "$System.env.GOOGLE_APPLICATION_CREDENTIALS"
from secretKey.getParent()
include secretKey.getName()
into 'src/main/res/raw'
rename secretKey.getName(), "credential.json"}
preBuild.dependsOn(copySecretKey)
In this code, I am not getting what is GOOGLE_APPLICATION_CREDENTIALS and where I should paste my JSON.
Thanks in Advance.

file is the location of your json file. they are just showing you how to use the system variable assuming you have that set to the location of said fule

Related

Dynamic strings.xml file creation from API response

I would like to create my strings.xml file dynamically every time I build my project from Android Studio. Right now I'm using Loco to share strings across Android and IOS. This tool exposes an API which response is my strings.xml or .strings file (IOS). I would like to know if there is any way to call the API from gradle in order to update the strings files each time the project is build.
Maybe Fastlane can help me with that issue?
Finally, I found a solution. Basically, with a groovy script in your gradle you can call an http endpoint via URL class from java.net, get the response of the GET call as string and write the response into your string.xml file.
In my build.gradle (Project level) I defined the "syncStrings" task as below:
task syncStrings {
def strings = new URL( 'https://localise.biz/api/export/locale/it-IT.xml?format=android&key=<api_key>' ).text
File file = new File("app/src/main/res/values/strings.xml")
file.write(strings)
}
Hope it helps somebody else ;)

How to add the API key to use Google Cloud TTS?

I am trying to create a simple testing app for Cloud TTS. The official documentation does not seem to provide any specific information about Android, and the closest thing was Java.
I created a default app and then added the library like this,
implementation 'com.google.cloud:google-cloud-texttospeech:0.82.0-beta'
which caused a weird build error, so I added this after reading an existing StackOverflow question
packagingOptions {
exclude 'META-INF/proguard/androidx-annotations.pro'
exclude 'META-INF/INDEX.LIST'
}
Now it compiles, but of course it caused a runtime exception saying that GOOGLE_APPLICATION_CREDENTIALS must be defined. But how to do that with Android? The documentation page only had instructions for Windows/Linux/macOS.
In case someone sees this question in the future, I think I solved this by doing what this page does. I copied the credential.json to the raw directory, and did the following thing before calling the API.
val cred = ServiceAccountCredentials.fromStream(resources.openRawResource(R.raw.credentials));
val buil = TextToSpeechSettings.newBuilder();
buil.credentialsProvider = FixedCredentialsProvider.create(cred);
val sett = buil.build();
// Instantiates a client
TextToSpeechClient.create(sett).use { textToSpeechClient ->
Here is an example for Google Cloud TTS.
You can read the issue about Google Cloud TTS Github Issues.
Google Cloud TTS does not currently support the android.
You can use Restful API to get audio Base64 encoding and use MediaPlayer library to play audio.
Copy your credential information which contains type, private_key
Write content of Step 1 into credential.json file.
Copy file of Step 2 into /res/raw folder. (If it's not working, put file into assets folder.)
Or, you can parse environment variables and write into credential.json file.
Below code comes from GoogleCloudPlatform/android-docs-samples.
task copySecretKey(type: Copy) {
def File secretKey = file "$System.env.GOOGLE_APPLICATION_CREDENTIALS"
from secretKey.getParent()
include secretKey.getName()
into 'src/main/res/raw'
rename secretKey.getName(), "credential.json"
}

How to store app keys in a separate file locally and load from there in build.gradle?

I have some keys or app_id, for instance, fabric app_id, in my build.gradle, different for different build like -
manifestPlaceholders = [
appIcon : "#mipmap/kj_launcher",
roundappIcon: "#mipmap/kj_launcher",
fabric_id: "fabric_app_id"
]
which I indirectly use in AndroidManifest file.
So, my question is, a way to store that fabric_id inside any file which is present locally in my system and then load it over here in the above code, different for the different build?
Just like we do in .properties file and get the value from that inside build.gradle.
I don't want to expose these ids.
There's a number of ways to do this, but the way I do this is to define it the gradle.properties file.
In my local gradle.properties file:
MY_SECRET_API_KEY=Dev_Api_Key_That_I_Dont_Care_If_Devs_Have_Access_To
Then on my CI build server, I have a script that replaces Dev_Api_Key_That_I_Dont_Care_If_Devs_Have_Access_To with a value of the production API key.

How to include and read source map file in React Native on Android

I am trying to generate source maps in the Gradle build process of a React Native app and read the source map file in the application.
I used this project as a starting point: https://github.com/philipshurpik/react-native-source-maps
I didn't succeed in including it as it is, so I tried to take and modify parts of it.
In app/gradle.build I added:
project.ext.react = [
extraPackagerArgs: ["--sourcemap-output", "$buildDir/intermediates/assets/release/index.map"]
]
and indeed when I'm running the release build I see the file index.map under android/app/build/intermediates/assets/release , but when I deploy the APK to a device or emulator, I can't see this file anywhere in Android's filesystem. For that matter, I also can't see the bundle file or any files that were in android/app/src/main/assets and I do see in android/app/build/intermediates/assets/release after build.
The application code uses https://github.com/itinance/react-native-fs to read the file. This is how I first check if the file exists:
const fileExists = await RNFS.existsAssets(`${RNFS.DocumentDirectoryPath}/index.map`)
and the file doesn't exist there. RNFS.DocumentDirectoryPath returns /data/user/0/mypackagename/files , which is empty when I check (using adb shell with root access).
What am I missing? How do I get the source map file to a location which will be available to the application to read?
Turned out the gradle.build part was fine, and in order to read the source map file I needed to use just the file name, like so: RNFS.existsAssets('index.map')

Best way to store secret keys in Android applications

I have a Parse account that I'm using to store user data for my Android application. Because I'm using source control, I don't want to leave my secret key embedded in the code. Is there a way to have a "config" file that I would keep out of source control, that can then host the key? If that's not feasible, what are the best practices of handling such a situation?
Yes, you can create a folder outside of source control, put a file called app.properties in there, put your key in that properties file and add the folder to your build path. Then use a properties loader to load it from the classpath.
If you have many developers or more than one dev machine, you can set the local properties location as a variable in your build path so that each developer can configure their own.
One option is to put the key in an environment variable and then read it during your build.
in your pom.xml declare a property (secret.key)
<properties>
<secretKey>${env.SECRET_KEY}</secretKey>
<properties>
further down enable "filtering" on your resources
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
In resources maintain a "config.properties" to read with the variable ready for substitution:
secret_key=${secretKey}
Filtering in maven will replace ${secret.key} with the value from your environment variable.
If you are using gradle for your build with Android studio see section 16.6.6.2 on filtering files.
Within build.gradle add the following
import org.apache.tools.ant.filters.FixCrLfFilter
import org.apache.tools.ant.filters.ReplaceTokens
task copyProductionConfig(type: Copy) {
from 'source'
include 'config.properties'
into 'build/targetpath/config'
expand([
secretKey: System.getenv("SECRET_KEY")
])
}
In gradle you can also request input when you run gradlew using
Console console = System.console()
def password = console.readPassword("Enter Secret Key: ")
And then apply it to the appropriate config or source files.

Categories

Resources