How to use Realm in a library - android

i'm trying to understand how to use Realm in a library that i'm making, i now understand RealmModules after a couple of days of research (if someone from Realm reads this, you should really improve on documentation about use in libraries). I've made a simple class that gives me the realm with library configuration:
object ChatRealm {
fun getChatRealm(): Realm{
val config = RealmConfiguration.Builder()
.name("mtchat_realmDB")
.schemaVersion(2)
.deleteRealmIfMigrationNeeded()
.modules(ChatModule())
.build()
return Realm.getInstance(config)
}
}
the module is this
#RealmModule(library = true, allClasses = true)
class ChatModule {}
and in the project Application class i setup realm like this
class App: Application() {
override fun onCreate() {
super.onCreate()
initRealm()
setupRealm()
}
private fun initRealm() {
Realm.init(this)
}
private fun setupRealm(){
Realm.setDefaultConfiguration(
RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.modules(Realm.getDefaultModule(), ChatModule())
.build()
)
}
}
Now the trouble i'm having is that the build is failing or the app is crashing for various reasons based on how i configure the gradle files.
My :app gradle is this
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
repositories {
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary= true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:design:26.0.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-vector-drawable:26.0.2'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile project(':mtchat')
}
and my library gradle is this
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
repositories {
maven { url "https://jitpack.io" }
maven { url 'https://maven.google.com' }
mavenCentral()
}
buildscript {
repositories {
jcenter()
}
}
android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile "com.android.support:appcompat-v7:$compat_version"
compile "com.android.support:support-v4:$compat_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
compile "com.android.support:cardview-v7:$compat_version"
compile "com.android.support:recyclerview-v7:$compat_version"
compile "com.android.support:design:$compat_version"
compile "de.hdodenhof:circleimageview:2.1.0"
compile 'com.github.bumptech.glide:glide:4.1.1'
compile 'com.android.volley:volley:1.0.0'
}
and this is my project gradle
buildscript {
ext.kotlin_version = '1.1.4-3'
ext.compat_version = '26.0.2'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:3.7.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
}
and based on if i add or not the apply plugin: 'realm-android' to the :app module i get either RealmObject "X" is not part of the schema for this realm or if i add the plugin to the app gradle it fails to build the project completely.
Is there anyone that has used Realm in a library and wants to explain how, clearly and in depth, maybe this could be used as future reference
EDIT: With the above configuration i get this error when building
Execution failed for task ':app:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lio/realm/ChatRealmProxyInterface;
EDIT 2:
I managed to get it working by defining modules for both app and library and avoiding naming the realm models in the app the same as the ones in the library ( is this required? Or is there a way to isolate the library models so that the user can use the same names that the library models use?) Still this took me 2 days of research and i'm still unsure if I'm doing it right. It would be REALLY great if someone with more knowledge than me made a nice tutorial or something.

you are facing Over 64K Methods Multiple dex file problem not related to realm library you need to add dependencies
Configure your app for multidex
android {
defaultConfig {
...
multiDexEnabled true
}
productFlavors {
dev {
// Enable pre-dexing to produce an APK that can be tested on
// Android 5.0+ without the time-consuming DEX build processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the production version.
minSdkVersion 14
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
If you do not override the Application class, edit your manifest file to set android:name in the tag as follows:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="android.support.multidex.MultiDexApplication" >
...
</application>
</manifest>
If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:
public class MyApplication extends SomeOtherApplication {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}

Add this to your project gradle file
dependencies {
classpath "io.realm:realm-gradle-plugin:3.5.0"
}

Related

Android Studio Build Error - Could not find method implementation() for arguments [com.google.firebase:firebase-core:16.0.0]

I am implementing FCM into my android project using Cordova
When I build my project I am getting error "Could not find method implementation() for arguments [com.google.firebase:firebase-core:16.0.0] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
"
Here is my build.gradle file code inside app directory
apply plugin: 'com.android.application'
buildscript {
repositories {
mavenCentral()
jcenter()
maven {
url "https://maven.google.com"
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.2'
implementation 'com.google.firebase:firebase-core:16.0.0' // Error generating line
}
}
You should write your repositories and dependencies in sequence manner which is suggested by Firebase.
Your build.gradle of project lavel should look like this, path of the file is just inside the project.
buildscript {
repositories {
google()
jcenter()
mavenCentral()
maven {
url 'Fabric URL'
}
}
dependencies {
classpath 'com.google.gms:google-services:3.3.1'
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "JIT PACK URL"
}
maven {
url 'MAVEN URL'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
And after that your app level build.gradle should look like this, path of the file is inside the app. C:\Users\ABC\DEF\Project Name\app\build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
flavorDimensions "versionCode"
defaultConfig {
applicationId "App Package Name as "com.companyName.appName""
minSdkVersion 19
targetSdkVersion 27
multiDexEnabled true
versionCode 15
versionName "1.0.12"
}
buildTypes {
release {
minifyEnabled false
debuggable false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// firebase push notifications
implementation 'com.google.firebase:firebase-messaging:15.0.2'
implementation 'com.google.firebase:firebase-core:15.0.2'
implementation 'com.firebase:firebase-jobdispatcher-with-gcm-dep:0.5.2'
// firebase remote config
implementation 'com.google.firebase:firebase-config:15.0.0'
}
apply plugin: 'com.google.gms.google-services'
If this not help for you you then try to change all the version of firebase and firebase remote config to latest version.
Because sometimes different version may cause problem.

With 'kotlin-kapt' plugin, Android Studio doesnt provide specific errors about Dagger 2

I use Kotlin with Dagger 2. The problem is, when I make mistake while implementing Dagger (e.g., miss #Inject for class constractor), IDE doesnt show specifically where mistake is. Insead compiler error is always the same:
Execution failed for task ':app:kaptDebugKotlin'.
Internal compiler error. See log for more details
Class with a deliberate mistake (commented #Inject):
class LoginPresenter //#Inject
constructor(private val request: LoginRequest)
Project build.gradle file:
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "com.example.kravets_a.kotlinanddagger"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
...
compile 'com.google.dagger:dagger:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
kapt "com.google.dagger:dagger-compiler:2.10"
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
}
repositories {
mavenCentral()
}
Option 1
Use apply plugin: 'kotlin-kapt'. And when error occurs, you can see it in Android Studio => on the bottom right => at Gradle Console.
Thanks to David Medenjak comment.
Option 2
Substitute apply plugin: 'kotlin-kapt' with kapt { generateStubs = true }
Compilation fails with expected result:
[com.example.kravets_a.kotlinanddagger.logic.MainActivityComponent.inject(com.example.kotlinanddagger.MainActivity)] com.example.kotlinanddagger.logic.LoginPresenter cannot be provided without an #Inject constructor or from an #Provides- or #Produces-annotated method.
Tutorials like this, this or that says that apply plugin: 'kotlin-kapt' can be used instead of kapt { generateStubs = true }. But in that case Android Studio does not provide specific compilation error.
The solution is to add this to your module's build.gradle:
kapt {
correctErrorTypes = true
}

cannot install two flavors of an app with Android Annotations

I have an app using Android Annotations and everything is working fine except one thing - you cannot install both flavors on the same device despite the fact that they have different applicationId.
Researching this I came across some problems (that other people had) with annotation processing and flavors, and as I recall this was an issue also here, but we've manage to add following snippet and everything worked.
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
resourcePackageName android.defaultConfig.applicationId
}
}
Until the other day we've didn't notice, that we couldn't install two flavors at once on one device. I've tried changing the gradle but every time I've ended up with the same problem or breaking the gradle script.
As I've mentioned, I've tried all things that I could think of and the online search didn't turn up anything useful, so if anybody have any idea I'll appreciate it.
Following there are my build.gradle scripts.
Top level:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
classpath 'com.google.gms:google-services:2.0.0'
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
App module level:
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'android-apt'
android {
signingConfigs {
config {
keyAlias 'release'
keyPassword 'keyPassword'
storeFile file('../storeFile.jks')
storePassword 'storePassword'
}
}
compileSdkVersion 23
buildToolsVersion '23.0.2'
defaultConfig {
applicationId "some.awsome.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
signingConfig signingConfigs.config
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
productFlavors {
COGNICARE_personal {
applicationId "some.awsome.app.free"
}
COGNICARE_full {
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
}
repositories {
flatDir {
dirs 'libs'
}
maven { url "https://jitpack.io" }
mavenCentral()
}
def android_annotations_version = '3.3.2'
def google_libs_version = '23.3.0'
//some lib versions omitted
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//some aar lib omitted
testCompile 'junit:junit:4.12'
compile "com.android.support:appcompat-v7:$google_libs_version"
apt "org.androidannotations:androidannotations:$android_annotations_version"
compile "org.androidannotations:androidannotations-api:$android_annotations_version"
//some libs omitted
compile 'com.android.support:multidex:1.0.0'
//some libs omitted
}
apt {
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
resourcePackageName android.defaultConfig.applicationId
}
}
Thanks in advance for any suggestion. Kudos!
OK, so thanks to #ligi pointing me to the right track.
We have a similar project with a very similar setup and the main difference are the Android Annotation used in one of them, an that has mislead me.
The bottom line is that I've had a provider in the manifest that I wasn't aware of, and the authority conflict did occur at that point.
Thanks for your time. Kudos.

Android databindingError:Cannot change dependencies of configuration ':mobileBanking:compile' after it has been resolved

I'm having a lot of trouble adding databinding to my project. There is a complicated build.gradle. Unfortunately, I cannot post too much from it.
If I add databinding {enabled = true} to my android block in my app build.gradle file I get the following error -->
Error:Cannot change dependencies of configuration ':projectName:compile' after it has been resolved.
Cannot get property 'javaCompile' on null object.
I've added the databinding library on my classpath. If I don't add the dataBinding {enabled = true} block the build succeeds with a warning that generated sources are in the wrong folder.
Any ideas?
Here is my sample build.gradle with databinding enabled. I'm using this setup in my projects and databinding is working fine. Notice that you only need to specify databinding {enabled true}. No more setup is required. You don't have to add databinding to dependencies.
build.gradle from main folder
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
}
}
subprojects {
buildscript {
repositories {
jcenter()
mavenCentral()
}
}
repositories {
jcenter()
mavenCentral()
}
}
build.gradle from app
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "foo.bar"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
//support library
compile 'com.android.support:appcompat-v7:23.1.1'
}

How to import android github example with repositories and dependencies definitions

I know there are similar questions like this but i'm new in android development and i need to learn how to import an example defining repositories and dependencies in the build.gradle project file.
After several tries i can't figure out which is the right way...
The example is this one https://github.com/neokree/MaterialNavigationDrawer
When i load the project in android studio i get an error "Configuration with name 'default' not found.", and it's ok.. i know that i have to modify the build.grandle file
repositories {
mavenCentral()
}
dependencies {
compile 'it.neokree:MaterialNavigationDrawer:1.3.3'
}
But where i have to add/replace these rows? In the buildscript area? in the allprojects? i get "Gradle DSL method not found: 'compile()'" error or fix plugin version alert if i try to change something.
original build.grandle
// 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.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
}
}
Your problem is in setting.gradle file in the MaterialNavigationDrawer project. As you can see
include ':app', ':MaterialNavigationDrawerModule'
it requires MaterialNavigationDrawerModule project to build. Go to New > Import Module and import MaterialNavigationDrawerModule.
Update build.gradle in MaterialNavigationDrawer\app to
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
applicationId "it.neokree.materialnavigationdrawer"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName '1.0'
}
buildTypes {
release {
//runProguard false
//proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile project(':MaterialNavigationDrawerModule')
}
Update build.gradle in MaterialNavigationDrawer\MaterialNavigationDrawerModule to
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName 'dev'
}
buildTypes {
release {
}
}
productFlavors {
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.balysv:material-ripple:1.0.1'
}
In MainActivity i deleted missing import
and rewrite Intent
to
Now it's working.

Categories

Resources