简体   繁体   中英

How to run app in Android Studio with shared library-module from other project?

I use a self-written library-module within another project like it was suggested in

https://code.google.com/p/android/issues/detail?id=105570

and

Android Studio 0.8.1 Creating Modules without copying files?

I added the lines to settings.gradle and furthermore added the included library via "Project Structure"-Dialog as a dependency to the project. I use Android Studio 1.2.2.

Everything within the IDE looks fine (imports are working, library-code can be browsed and edited, both modules can be "maked" without errors), but I can't seem to launch the project on my phone / emulator.

I get a "classNotFoundException" for the first class from the library that is used in the app.

I copy-pasted the entire project class by class from Eclipse where it would run without problems.

What do I have to do to get the compiled classes of the library into my app onto my phone?

settings.gradle

include ':app'
include ':Sudoku'
project(':Sudoku').projectDir=new File('/../Libs/sudoku')

build.gradle (app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.frozenmilkmeteoroids.sudokuapp"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    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:22.2.0'
    compile project(':Sudoku')
}

build.gradle (library)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

I also use Android Studio 1.2.2 and here is step by step of how to create the standalone library project and reference it from your main project:

  1. create the android project library as you would for a normal android project. Call it libProjectName

  2. rename the libProjectName 's app module to libModuleName

  3. in the libModuleName's build.gradle change: apply plugin: 'com.android.application' to apply plugin: 'com.android.library' and remove applicationId from defaultConfig section

  4. Go to the main project ie the project to reference libProjectName

  5. edit settings.gradle , add:

include ':libProjectName:libModuleName'

project(':libProjectName:libModuleName').projectDir=new File('relative/path/libProjectDir/libModuleDir')

  1. edit build.gradle in app Module, add:

compile project(':libProjectDir:libModuleName')


Now assuming that the module and module-directory name in Sudoku is moduleSudoku and applying the steps above, the solution is:

settings.gradle

include ':app'
include ':Sudoku:moduleSudoku'
project(':Sudoku:moduleSudoku').projectDir=new File('/../Libs/sudoku/moduleSudoku')

build.gradle (app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.frozenmilkmeteoroids.sudokuapp"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    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:22.2.0'
    compile project(':Sudoku:moduleSudoku')
}

build.gradle (library)

apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM