Использование комнаты в библиотеке Android без ссылки на комнату в приложении - PullRequest
0 голосов
/ 26 июня 2019

Кто-нибудь знает, как использовать Room в библиотеке Android без необходимости включать какие-либо ссылки на Room в приложении Android (Java)?

Вероятно, полезно отметить, что я включаю встроенный файл .aar библиотеки Android в папку libs, на которую ссылается Android App application level build.gradle. Библиотека Android и приложение Android не имеют никакой связи друг с другом, кроме как через скомпилированный файл .aar.

Вот что я в основном делаю:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MyCustomLibrary.storeSomething("The thing to store"); //method that calls Room stuff
}

Метод storeSomething работает должным образом, если я включаю Room в само приложение, и выдает следующее, если я его исключаю:

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/arch/persistence/room/RoomDatabase;

Моя цель - чтобы приложение ничего не знало о Room.

См. Ниже для всех build.gradle файлов.

Уровень проекта библиотеки Android build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Уровень плагинов для библиотеки Android build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 28


    defaultConfig {
        minSdkVersion 25
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
    buildToolsVersion = '28.0.3'

}

dependencies {
    def room_version = "1.1.1"

    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation "android.arch.persistence.room:runtime:$room_version"
    annotationProcessor "android.arch.persistence.room:compiler:$room_version"
}

Уровень проекта Android App build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        flatDir {
            dirs 'libs'
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Уровень приложения для Android build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 25
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    //def room_version = "1.1.1" (it works if I include this line!)
    implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.squareup.retrofit2:retrofit:2.6.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.6.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    //implementation "android.arch.persistence.room:runtime:$room_version" (it works if I include this line!)
    //annotationProcessor "android.arch.persistence.room:compiler:$room_version" (it works if I include this line!)
}

...