I'm trying to integrate Firebase on Unity3d in order to receive push notifications.
I get the following error when the app launching:
I/dalvikvm( 7881): Could not find method android.content.Context.isDeviceProtectedStorage, referenced from method com.google.firebase.FirebaseApp.zza
I/FirebaseInitProvider( 7881): FirebaseApp initialization unsuccessful
The firebase-messaging sample work fine with my google-services.json. So I suppose that the console configuration is correct.
1. I created a library project on Android Studio.
I followed a web site to build a .jar from Android Studio and the official documentation to add firebase.
http://www.thegamecontriver.com/2015/04/android-plugin-unity-android-studio.html
project gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.1'
classpath 'com.google.gms:google-services:3.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app gradle file:
apply plugin: 'com.android.library'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
sourceSets {
main {
java {
srcDir 'src/main/java'
}
}
}
defaultConfig {
minSdkVersion 15
targetSdkVersion 24
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile files('libs/classes.jar')
}
task deleteOldJar(type: Delete) {
delete 'release/NotificationPlugin.jar'
}
task exportJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
rename('classes.jar', 'NotificationPlugin.jar')
}
exportJar.dependsOn(deleteOldJar, build)
apply plugin: 'com.google.gms.google-services'
At this point a .jar is successfully generated.
2. I created a JarResolver dependencies in Unity.
I use the PlayServicesResolver v1.1 found into other google unity package (Google analytics for exemple, other plugins upgrade it)
https://github.com/googleanalytics/google-analytics-plugin-for-unity
FirebaseDependencies.cs
using Google.JarResolver;
using UnityEditor;
[InitializeOnLoad]
public static class FirebaseDependencies
{
private static readonly string PluginName = "Firebase";
static FirebaseDependencies()
{
PlayServicesSupport svcSupport = PlayServicesSupport.CreateInstance(
PluginName,
EditorPrefs.GetString("AndroidSdkRoot"),
"ProjectSettings");
svcSupport.DependOn("com.google.firebase",
"firebase-core",
PluginVersion.PlayServicesVersionConstraint);
svcSupport.DependOn("com.google.firebase",
"firebase-messaging",
PluginVersion.PlayServicesVersionConstraint);
}
}
PluginVersion.cs
public class PluginVersion
{
public const string PlayServicesVersionConstraint = "9.6+";
}
After the ResolveClientJars command, all dependencies are copy into the Plugins/Android folder.
AndroidManifest.xml
firebase-analytics-9.6.1
firebase-analytics-impl-9.6.1
firebase-common-9.6.1
firebase-core-9.6.1
firebase-iid-9.6.1
firebase-messaging-9.6.1
google-services.json
play-services-basement-9.6.1
play-services-tasks-9.6.1
support-annotations-24.0.0.jar
support-v4-24.0.0
Everything seems to work perfectly at this point and there are no error during the build process.
What I'm doing wrong?
You could use Firebase Push Notifications without creation additional plugin. But the flow is a little bit tricky. Here are quick steps:
In the Unity Editor. In the Build Settings select Google Android
Project flag and Export your project. Make sure that your bundle id
correspond to the bundle id at the Firebase Console.
In the Android Studio. Import your Unity project. IMPORTANT: Don't
Open, Import!
Setup .gradle scripts and add google-services.json file as described here.
Build project from Android Studio.
Related
I've create a flutter plugin with:
flutter create --template plugin flutter_plugin
I've put my aar file inside flutter_plugin/android/src/main/libs folder
I've modified flutter_plugin/android/build.gradle
and changed rootProject.allprojects section to
rootProject.allprojects {
repositories {
google()
jcenter()
flatDir {
dirs "src/main/libs"
}
}
}
And added dependencies section, after android {}:
dependencies {
implementation (name:"mylib",ext:"aar")
}
but when i try running with: flutter run
I get gradle exception, apparently it tried to look for my mylib.aar inside example directory: example/src/main/libs/mylib.aar and failed.
I can put my lib inside example dir, but i don't think it's the right way,
as i want my aar to be part of the plugin.
My solution to add local .aar depencies to a flutter plugin is as follows (based on the instructions here How to add .aar dependency in library module?):
create a libs folder in your android plugin source code (<plugin_name>/android/libs) (on top level of your plugin, not in the example project)
add your .aar files in this folder (e.g. myFirstDependency.aar)
add your .aar dependencies (without the .aar extension) to your plugins build.gradle file (<plugin_name>/android/build.gradle) e.g.
dependencies {
...
implementation (name: 'myFirstDependency', ext: 'aar')
implementation (name: 'myOtherDependency', ext: 'aar')
}
in order for gradle to be able to find these dependencies you need to instruct gradle to search in the libs folder inside of the plugin. Therefore add the following to your plugins build.gradle (<plugin_name>/android/build.gradle, same as for your dependencies) under rootProject.allprojects repositories section.
(NOTE: there is also a buildscripts a repositories section which you should not change for this).
rootProject.allprojects {
repositories {
google()
jcenter()
//Add this below <plugin_name> is whatever your plugin is called eg. url_launcher
flatDir {
dirs project(':<plugin_name>').file('libs')
// e.g. dirs project(':url_launcher').file('libs') - don't miss the ':'
}
}
}
With that, you don't have the .aar library dependencies directly in your plugin.
I created a libs directory in the android directory of my plugin. I placed the aar I generated in the libs directory:
I updated the build.gradle file for the android code to include the following:
rootProject.allprojects {
repositories {
google()
jcenter()
flatDir {
dirs 'libs'
}
}
}
And in the dependencies section:
implementation fileTree(dir: 'libs', include: ['*.aar'])
In my experience, adding direct plugin aar in another plugin project (flutter plugin's android project) is either very difficult or not allowed, so in that case, you have two options,
If your aar dependancy is available on gradle, use gradle that in your build file. You can search for it here
Or if you are building aar yourself from another project, then you can publish aar to your local maven and use that dependency in your flutter plugin by adding "mavenLocal()" to your plugins build file.
For example here is what I did to fix same issue:
My dependancy's build.gradle
group 'com.companyname.artifactname'
version '1.0-SNAPSHOT'
buildscript {
repositories {
mavenLocal()
google()
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
}
}
allprojects {
repositories {
mavenLocal()
google()
jcenter()
maven {
url "https://maven.google.com" // Google's Maven repository
}
}
}
apply plugin: 'com.android.library'
uploadArchives {
repositories {
mavenLocal()
}
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 2
versionName "2.0.1"
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:support-v4:23.2.1'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
apply plugin: 'maven-publish'
publishing {
publications {
library(MavenPublication) {
version "1.1"
artifact(bundleRelease)
}
}
}
Then just run following command to publish it to local maven repository
gradlew publishToMavenLocal
And then just add mavenLocal() in your flutter plugin's repositories section and add dependancy as
compile 'com.companyname.artifactname:libraryname:1.1#aar'
With the flatDir solution you will get a warning when you try to sync your project with the Gradle files:
using flatDir should be avoided because it doesn't support any meta-data formats.
You can put this in your build.gradle instead to get rid of the warning:
android {
sourceSets {
main.jniLibs.srcDirs += project(':<plugin_name>').file('libs')
}
}
dependencies {
implementation files('libs/<dependency_name>.aar')
}
Unfortunately, with both solutions you'll get this error when you try to build the project:
Direct local .aar file dependencies are not supported when building an AAR.
The resulting AAR would be broken because the classes and Android resources from any local .aar
file dependencies would not be packaged in the resulting AAR. Previous versions of the Android
Gradle Plugin produce broken AARs in this case too (despite not throwing this error).
The accepted answer seems to be correct. I put the library inside the app's project instead because both options of the accepted answer were not possible in my case.
For this I had to create a new directory in the android directory of my app (i.e. example/android/<dependency_name>) and put my .aar file and a new build.gradle in that directory.
android/build.gradle:
dependencies {
implementation project(':<dependency_name>')
}
example/android/<dependency_name>/build.gradle:
configurations.maybeCreate("default")
artifacts.add("default", file('<dependency_name>.aar'))
example/android/settings.gradle:
include ':<dependency_name>'
i managed to make it work by adding
maven { url("${project(':test_flutter_plugin').file('libs').path}/my_lib") }
into rootProject.allprojects
of plugin/android/build.gradle
I just downloaded Android Studio and created a new project and I'm getting gradle build errors:
Failed to resolve: com.android.support.test.espresso-core:2.2.2
and
Failed to resolve: com.android.support.appcompat-v7:25.3.1
This error was resolved reinstalling the SDK Tools + Repository + API when launching android studio as admin.
I've installed API Level 25 which what I want to build on and have downloaded the SDK Build-Tools. I have also already download the support repository
Here's my app file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion '25.0.3'
defaultConfig {
applicationId "com.jtsalas.mirrorcontrol"
minSdkVersion 25
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_7
}
productFlavors {
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
}
build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
From the SDK manager, make sure you have both the Android Support Repository and Google Repository installed and up to date. You should then be able to find the relevant artifacts in sub folders of your /extras/android/m2repository directory
From your error it seems that you are not including espresso libraries. The solution to this is adding espresso core library which is part Android Testing support library which is hosted in the google's Maven repository think this as kind of git repository but for dependencies.
So we tell the gradle build system to look in the Maven repository for dependencies by specifying its URL.
This is done by adding Maven url in the application level build.gradle file under repositories block
repositories {
jcenter()
maven{
url "https://maven.google.com"
}
}
and in the module level build.gradle file mention the dependencies that you want from the maven repository by mention their name as follows:
dependencies{
//other dependencies go here
//testing dependencies
androidTestCompile 'com.android.support.test.espresso:espresso-core:3.0.1'
}
That is the reason for including Maven repository url in the app level build.gradle file, hope this helps.
In project.gradle file, the allprojects root align this way:
allprojects {
repositories {
google()
jcenter()
maven { url "https://jitpack.io" }
}
}
jitpack is used as the dependency for multiple libraries, if you're not using any sort of library that don't requires it then not include maven line.
Well, I don't know the perfect answer but..... how about comparing with my SDK Tools?
I solved it by uninstalling Android Studios and deleting old versions of Android Studios in my C:\Users[Username] and reinstalled Android Studio as administrator.
It seems like you updated android studio and opening previous project in it.The simplest way is create new project and copy
1. compileSdkVersion 26
2. buildToolsVersion "26.0.1"
3. targetSdkVersion 26
4. compile 'com.android.support:appcompat-v7:26.+'
and paste them in appropriate places in app level build gradle.
it will ask to update to take advantages .. allow it to update.
best luck ... It worked for me.
if your project is Flutter,
clean project [by 'flutter clean' command]
In project.gradle file add [ google() ]
in Android studio: File Menu -> Invalidate...
project.gradle:
allprojects {
repositories {
google()
jcenter()
}
}
First of all, I know there are a lot of subjects about this problems, but I still can't fix it.
I'm just trying to customize aviary sdk by following the guide here : https://developers.aviary.com/docs/android/setup-guide# (section 4.2, not 4.1)
here are my root gradle files :
build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
settings.gradle :
include ':app', ':Aviary-Android-Sample-App'
here is my app build.gradle file :
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.agaze.myapplication"
minSdkVersion 10
targetSdkVersion 20
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:20.0.0'
compile project(':Aviary-Android-Sample-App')
}
Here my files tree:
Thanks in advance for your tips.
Antonin
Check your submodules and make sure each submodule in your project has its own build.gradle file.
Check this guide for more info
tools.android.com/tech-docs/new-build-system/user-guide
Each library you add to your project should have it's own build.gradle file. If it does not have you can get this error.
For a solution you can add your sdk as a module with Android Studio. To do this you can follow the path in Android Studio:
File > New > Import Module and select directory of your sdk and add to your project.
Select Gradle System and let Android Studio create gradle files for your sdk.
You can check the link below for more information and how to add a module to your project.
http://www.truiton.com/2015/02/android-studio-add-library-project/
I hope this'll help you.
From your screen shot.It's obvious that the Aviary-Android-Sample-App is an eclipse project(no gradle file there) but not a valid gradle module.
If you want to import it into android studio, in android studio, click File -> new module -import eclipse adt project and go on under the instruction.
And you are good to go.
Edit:
Also, it seems that you want to modified the Aviary sdk, but the module you want to import is a sample app. Maybe you have downloaded the wrong file. The correct link looks like this one https://creativesdk.adobe.com/downloads.html
I had a problem to run android-studios build gradle, so I tested many solution but unfortunately I couldn't solve my problem:
Now i uninstall my old Android studio and Android-sdk , then I download android studio 1.5.1 that contains default SDK .
I install them and I create an empty project , But it take 30 minutes to build project, I will add dialog window that shown in this period of time .
Finally project created with this error :
Error:(23, 17) Failed to resolve: junit:junit:4.12
Could any one help me ,please ?
build.gradle file content:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.armangar.app.fortest"
minSdkVersion 23
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
}
top build.gradle file content:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
testCompile is the configuration for unit tests (located in src/test) and androidTestCompile is used for the test api (located in src/androidTest).
The main distinction between the two is that test runs in a regular Java JVM, and androidTest tests run on an Android device (or an emulator).
So, use
testCompile 'junit:junit:4.12'
instead of
androidTestCompile 'junit:junit:4.12'
source: http://tools.android.com/tech-docs/unit-testing-support
I think you have SSL problem.
Try by replacing
jcenter { url 'http://jcenter.bintray.com' }
with
jcenter()
in both repository blocks in build.gradle file and try to build gradle again.
I change jcenter() to mavenCentral() in build.gradle .
So my problem solved
EDIT:
This issue was related to my proxy . So I could not download from jcenter().
If you have a problem same this one, please check your Internet proxy .
So I have created an Android library and successfully compiled it into a .aar file. I called this aar file: "projectx-sdk-1.0.0.aar". Now I want my new project to depend on this aar so what I have done is follow this post.
But the post confuses me since I do not get the desired result:
The package name of the aar is : com.projectx.photosdk and the module inside is called sdk
Here is my current project structure:
|-SuperAwesomeApp
|--.idea
|--gradle
|--App
|---aars
|----projectx-sdk-1.0.0.aar
|---build
|---jars
|---src
|---build.gradle
And here is my Gradle build file:
apply plugin: 'android'
buildscript {
repositories {
mavenCentral()
flatDir {
dirs 'aars'
}
}
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:gridlayout-v7:19.0.1'
compile 'com.android.support:support-v4:19.0.1'
compile 'com.android.support:appcompat-v7:19.0.1'
compile 'com.projectx.photosdk:sdk:1.0.0#aar'
// compile files( 'aars/sdk-1.0.0.aar' ) // Does not work either
}
EDIT
The errors I am getting:
Failed to refresh Gradle project 'SuperAwesomeApp'
Could not find com.projectx.photosdk:sdk:1.0.0.
Required by:
SuperAwesomeApp:App:unspecified
You put your flatDir block in the wrong repostories block. The repositories block inside buildscript tells Gradle where to find the Android-Gradle plugin, but not the rest of the dependencies. You need to have another top-level repositories block like this:
repositories {
mavenCentral()
flatDir {
dirs 'aars'
}
}
I tested this and it works okay on my setup.
With recent versions of Android Studio, tested with 1.3, to use local .AAR file and not one fetched from maven/jcenter repository, just go to File > New > New module and choose Import .JAR/.AAR Package.
What you will end up with is a new module in your project that contains very simple build.gradle file that looks more or less like this:
configurations.create("default")
artifacts.add("default", file('this-is-yours-package-in-aar-format.aar'))
Of course, other projects have to reference this new module with regular compile project directive. So in a project that uses this new module which is simple a local .aar file has this in it's build.gradle
[...]
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
[...]
compile project(':name-of-module-created-via-new-module-option-described-above')
}
[...]
In Android Studio 3.1.3 with gradle 3.0.1.
Simply adding implementation fileTree(dir: 'libs', include: ['*.aar']) or implementation files('libs/app-release.aar') without any other flatdir works.
These days (over 1 year after this question) with Android Studio >1.0, local dependency does work properly:
The android sdk looks for dependencies in a default local repo of: $ANDROID_HOME/extras/android/m2repository/
In a local library project you can publish the aar to this directory. Here's a snippet that can be added to your module's build.gradle file (ex: sdk/build.gradle)
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
+ "/extras/android/m2repository/")
pom.version = '1.0-SNAPSHOT'
pom.groupId = 'your.package'
pom.artifactId = 'sdk-name'
}
}
}
some reference gradle docs http://gradle.org/docs/current/userguide/artifact_management.html
In your library project, run ./gradlew uploadArchives to publish the aar to that directory
In the application project you want to use the library in, add the dependency to your project/app/build.gradle. compile 'your.package:sdk-name:1.0-SNAPSHOT'
For local dependency, the next gradle build should find the previously deployed archive and that's it!
In my case, I use the above for local dev, but also have a Bamboo continuous integration server for the Library that publishes each build to a shared Nexus artifact repository. The full library code to deploy the artifact then becomes:
uploadArchives {
repositories {
mavenDeployer {
if (System.getenv("BAMBOO_BUILDNUMBER") != null) {
// Deploy to shared repository
repository(url: "http://internal-nexus.url/path/") {
authentication(userName: "user", password: "****")
}
pom.version = System.getenv("BAMBOO_BUILDNUMBER")
} else {
// Deploy to local Android sdk m2repository
repository(url: "file://localhost" + System.getenv("ANDROID_HOME")
+ "/extras/android/m2repository/")
pom.version = '1.0-SNAPSHOT'
}
pom.groupId = 'your.package'
pom.artifactId = 'sdk-name'
}
}
}
In order to tell applications to download from my internal Nexus repository, I added the internal Nexus maven repository just above jcenter() in both "repositories" blocks in the project/build.gradle
repositories {
maven {
url "http://internal-nexus.url/path/"
}
jcenter()
}
And application dependency then looks like compile 'your.package:sdk-name:45' When I update the 45 version to 46 is when my project will grab the new artifact from the Nexus server.
With the newest Gradle version there is now a slightly updated way of doing what Stan suggested (see maving publishing)
apply plugin: 'maven-publish'
publishing {
publications {
aar(MavenPublication) {
groupId 'org.your-group-id'
artifactId 'your-artifact-id'
version 'x.x.x'
// Tell maven to prepare the generated "*.aar" file for publishing
artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
}
}
repositories {
maven {
url("file:" + System.getenv("HOME") + "/.m2/repository")
}
}
}
It seems adding .aar files as local dependency is not yet supported(Planned to be supported in 0.5.0 Beta)
https://code.google.com/p/android/issues/detail?id=55863
But the way you are using your library in dependency will only work if your library is on central maven repository or in the local maven repository.
Refer this for How to use local maven repository to use .aar in module dependencies.
http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds
This is for Kotlin DSL (build.gradle.kts) assuming you put the files in my-libs subdirectory relative to where the build file is located:
dependencies {
implementation(
fileTree("my-libs/") {
// You can add as many include or exclude calls as you want
include("my-first-library.aar")
include("another-library.aar")
// You can also include all files by using a pattern wildcard
include("*.jar")
exclude("the-bad-library.jar")
}
)
// Other dependencies...
}
For more ways to do this, see Gradle documentations and this post and this post.