it is clear to me how to get a debug key for use with Google Maps v2 library, and also how to get a release key. Currently the relevant section of my manifest file looks like this:
<!-- Debug -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="[my debug key here]"/>
<!-- Release
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="[my release key here]"/>
-->
The relevant key is uncommented, the other one is commented.
Can anyone indicate a comfortable way to avoid this annoyance of commenting/uncommenting these pieces of manifest file everytime a debug rather than release version is needed?
With version 2 API's you can use the same key for release and debug. In your google api's console edit your allowed android apps and on each line put your debug/release key, and then your app name. You can use multiple lines, then it will work with both keys.
Different Google Map API keys for debug build and release build can be defined in build.gradle:
...
android {
...
buildTypes {
debug {
resValue "string", "google_maps_api_key", "<debug_key>"
...
}
release {
resValue "string", "google_maps_api_key", "<release_key>"
...
}
}
}
Just replace <debug_key> and <release_key> with your actual keys.
And refer to this resource value in AndroidManifest.xml:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_api_key"/>
This solution is also described in the following Stack Overflow question:
Manage Google Maps API Key with Gradle in Android Studio
Alternatively, you can place your debug key in app/src/debug/res/values/google_maps_api.xml with a content similar to this:
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">AIzaXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx</string>
In the same way, place the release key in app/src/release/res/values/google_maps_api.xml.
In this manner you have both keys and same source code. This is very convenient for open source projects where you want to publish your source code but not your API keys. You just need to ignore / not upload the google_maps_api.xml file and you're good to go.
Related
While decompiling my android app APK file, I have found the fabric ApiKey key in manifest file. How to secure Fabric API Key from APK decompilation?
I have done following code changes for hiding Fabric API key from manifest file. But still it is visible after APK decompilation.
I added my fabric API key
FabricAPIKey=0123456789ABCDEF012345123456789ABCDEF012
in gradle.properties.
In build.gradle(Module)
...........
def FABRIC_API_ID = FabricAPIKey
.....
buildTypes {
debug {
..........
manifestPlaceholders = [//this is used for defining the variable for manifest file
FABRIC_API_KEY:FABRIC_API_ID
]
}
release{ ..........
manifestPlaceholders = [//this is used for defining the variable for manifest file
FABRIC_API_KEY:FABRIC_API_ID
]
}
And in AndroidManifest.xml
<meta-data
android:name="io.fabric.ApiKey"
android:value="${FABRIC_API_KEY}" />
Mike from Fabric here. Seva's point should be well noticed - "a sufficiently motivated hacker can eventually get to it, given a debugger and enough time".
If you want, you can place the API key and Build secret in a fabric.properties file. Copy your api key out of your android manifest, and delete the line that reads: <meta-data android:name="com.crashlytics.ApiKey" android:value="YOUR_API_KEY_HERE"/>
Then make a file called fabric.properties and place this folder in the root of the module that applies crashlytics in its' build.gradle In the fabric.properies file, add:apiKey=YOUR_API_KEY_HERE
Once that's complete, refresh your dependencies to pull in the change: ./gradlew clean --refresh-dependencies
Try to save that in strings.xml and refer that here.
Then manifest will only show resource id in int format.
But if you open the strings.xml file it will be retrieved.
i am developing an android application using android studio 2.3. I added google map activity to my project using built in add activity dialog.
When i check the google map api key it is saved in a separate xml file inside values folder but under debug varient. I need to add a new api key that is going to use only for relase mode (builds). How do i add such a separate api key xml file for release mode as well?
Thanks in advance!!
AndroidManifest.xml:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_api_key"/>
app/build.gradle:
android {
...
buildTypes {
debug {
resValue "string", "google_maps_api_key", "[YOUR DEBUG KEY]"
}
release {
resValue "string", "google_maps_api_key", "[YOUR RELEASE KEY]"
}
}
}
Just create same xml file in release folder.
You're default debug key is probably in file project_location/src/debug/res/values/some_file_name.xml.
You need to create same file in project_location/src/release/res/values/some_file_name.xml And put you'r release key there.
Recreate the debug and\or release folder structure in the same folder as your "main" one. Any files placed here will replace the ones in main depending on the build type.
I have printed my SHA1 key:
keytool -list -v -keystore keystore.jks
generated an API key in Google Console,
updated the manifest file as follows:
<meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyC1YOsomestringsarehiddedLu5_TnCxlyuRM"/>
Then I created a simple Activity as in Google Samples:
public class MapActivity extends FragmentActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_sample);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
}
And of course I added the activity inside Manifest file. I always get the same error.
Ensure that the following Android Key exists:
API Key: AIzaSyC1YOn9myapikeystringsxxu5_TnCxlyuRM
Android Application (<cert_fingerprint>;<package_name>):
B2:E8:75:4F:01:DD:xx:xx:xx:xx:xx:xx:74:A5:85:2C:A4:38:48;md.mycompany.catalog
The strange part is that the SHA1 key which I pasted in Google Console is not the same as the one above. I tried again, and it still shows another SHA1 key. I even tried this SHA1 and still it does not work.
p.s. I have Youtube API integrated in same app, and everything works perfectly, with same KEY.
From the comments you stated that you built the key from the Release version of the app, but the error message suggests that you were testing locally which would also suggest that you're testing with the debug version of your APK.
Since the SHA1 key differs from release to debug, you can keep both keys in your manifest but comment out the release version key while testing locally. Like so:
Edit (April 28 2016) - With Android Studio and build flavors, this technique is unnecessary considering you can point to different strings given a buildType (debug or release) and flavor. I would now recommend andorid:value="#string/maps_v2_api_key" instead of hard coding as such.
buildTypes {
release {
resValue 'string', 'maps_v2_api_key', '"123...xyz"'
}
debug {
resValue 'string', 'maps_v2_api_key', '"345...vut"'
}
}
i have same problem i spent two weeks to get a solution at last i got it.
just remove key from res/values/google_maps_api.c and repalace it by new key
follow below steps you will get a new key.
1> Find SHA1 by Using that command in command prompt:
keytool -list -v -keystore "%USERPROFILE%\.android\debug.keystore" -alias androiddebugkey -storepass android -keypass android
2> Get Package name from android manifest and use finger print of SHA1 for create an new api key
3> copy created key and past it in res/values/google_maps_api.xml and also past in android manifest
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">Paste Here New Api</string>
</resources>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="Paste Key Here" />
The keys for release and debug versions would be different. Also you could have as many flavours as you can. Check this at left menu(corner) of Android Studio, called BuildVariants
I had a similar problem, for me the fix was to just add google play services in the android sdk manager and then regenerate the api key.
Go to :
https://accounts.google.com/ServiceLogin?service=cloudconsole<mpl=api&osid=1&passive=true&continue=https://console.developers.google.com/#identifier
And generate the key from credentials tab.
Add the package name(application id) and SHA-1 for your project and you are done.
I'm using Google Maps v2 API in my project. In Google Maps v2 the debug/release API key is defined in AndroidManifest.xml. I have seen the link but in that map key is defined in a xml layout file not in AndroidManifest.xml. So can I define both debug and release keys for my project in AndroidManifest.xml?
I want something like this in AndroidManifest.xml:
If debug mode:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/debug_map_api_key"/>
If release mode:
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/release_map_api_key"/>
Using build.gradle
buildTypes {
debug {
buildConfigField("String", "map_api_key", "\"your debug map api key here\"")
}
release {
buildConfigField("String", "map_api_key", "\"your release map api key here\"")
}
}
I solved this issue using this steps:
In Google Developer API Console
Click on Create New Android key...
In cmd.exe/Terminal: keytool -list -v -keystore mystore.keystore
Password: android
Now enter SHA1 key;package name for debug and press enter
Enter SHA1 key;package name for release
Click on Create
Now use this API key your project
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/common_map_api_key"/>
One of the Best way to use build.gradle file in latest Android 5.0 and Android 6.0, Android 9+ (API 20, 21,22,23, 24, 25,26, 27 28, 29)
Well you can use them simply without creating product flavors in gradle. This is another example we can achieve via Gradle. You can achieve it with two simple steps.
Add custom value to manifestplaceholders build.gradle file.
See below
buildTypes {
debug {
manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"]
}
release {
manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"]
}
}
Edit manifest file like below.
part of my manifest file
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="${mapApiKeyValue}" />
This solution works for the latest Android 5.0 and Android 6.0 (API 20, 21,22,23)
Updated on 5/3/2018 for Xamarin Form and Xamarin Native Apps
Open AssemblyInfo.cs in Android Project and addd the following code
#if DEBUG
[assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "DebugKey123123123")]
#else
[assembly: MetaData("com.google.android.maps.v2.API_KEY", Value = "ReleaseKey123123123")]
#endif
To Check the AndroidManifest file, goto obj/Debug/android folder and open the manifest file and check the meta info,
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="DebugKey123123123" />
For organizations that need to maintain separate keys, you can place them in separate directories in Android Studio. Make sure the subdirectory of src you use matches a flavor or buildType
From Building Your Project with Gradle:
To build each version of your app, the build system combines source code and resources from:
src/main/ - the main source directory (common to all variants)
src/<buildType>/ - the build type source directory
src/<flavorName>/ - the flavor source directory
In projectroot/yourapp/build.gradle:
buildTypes {
debug {
runProguard false
debuggable true
}
release {
runProguard true
debuggable false
...
}
In projectroot/yourapp/src/main/AndroidManifest.xml:
...
<application
android:name=".MyApplication"
android:theme="#style/Theme">
<!-- Don't put your key here -->
...
In projectroot/yourapp/src/debug/AndroidManifest.xml, fully qualify the name of the app.
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:name="com.hipmunk.android.HipmunkApplication">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="yourdebugkey" />
</application>
</manifest>
In projectroot/yourapp/src/release/AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:name="com.hipmunk.android.HipmunkApplication">
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="release key" />
</application>
</manifest>
Since you are using gradle you can do the following:
build.gradle
android {
.. .. ...
buildTypes {
debug {
resValue "string", "google_maps_api_key", "[YOUR DEV KEY]"
}
release {
resValue "string", "google_maps_api_key", "[YOUR PROD KEY]"
}
}
}
And in your AndroidManifest.xml
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_api_key"/>
This way you only have one AndroidManifest.xml and you set value based on your build type. Hope this helps.
I want to automatically set different android map api V2 keys for development and production.
Log in to Google APIs Console
Under "Simple API Access" click "Edit Allowed Android apps..." on the right side
Enter one SHA-1 fingerprint per line like the instructions say:
"One SHA1 certificate fingerprint and package name (separated by a semicolon) per line. Example:
45:B5:E4:6F:36:AD:0A:98:94:B4:02:66:2B:12:17:F2:56:26:A0:E0;com.example
45:B6:E4:6F:36:AD:1A:98:94:B4:02:66:2B:12:17:F1:56:26:A0:E0;com.example"
Now, just use the same "Simple API key" and it'll work for your debug and publish certificate without having to change anything.
I may be wrong, but I think you can use the same V2 API key for both development and production builds. In your Google APIs Console, after generating a simple Android key, you just need to enter the SHA-1 fingerprints of your production signing key, and all the development Android debug signing keys you may have. Then in your manifest, just use that simple Android key and the app should work for both debug and production builds.
One of the easiest solution.You can achieve it with two simple steps.
Add custom value to manifestplaceholders build.gradle file.
See below
buildTypes {
debug {
manifestPlaceholders = [ mapApiKeyValue:"GHjaSyAjlyp3O831lgaonHMXsd-_DpQ3002x3S4"]
}
release {
manifestPlaceholders = [ mapApiKeyValue:"AIzaSyAuMGDLr2HeuRed4JA0CrdYYdZRjeC3EA"]
}
}
Edit manifest file like below.
part of my manifest file
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="${mapApiKeyValue}" />
This solution works for the latest Android 5.0 and Android 6.0 (API 20, 21,22,23)