如何使用来自其他项目的共享库模块在 Android Studio 中 运行 应用程序?

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

我在另一个项目中使用了自己编写的库模块,就像

中建议的那样

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

Android Studio 0.8.1 Creating Modules without copying files?

我将这些行添加到 settings.gradle 并进一步通过 "Project Structure"-Dialog 添加了包含的库作为项目的依赖项。我使用 Android Studio 1.2.2.

IDE 中的一切看起来都很好(导入工作正常,可以浏览和编辑库代码,两个模块都可以 "maked" 没有错误),但我似乎无法启动我的 phone / 模拟器上的项目。

我从应用程序中使用的库中得到第一个 class 的 "classNotFoundException"。

我从 Eclipse 中 class 复制粘贴了整个项目 class,运行 没有问题。

我需要做什么才能将已编译的 classes 库导入到我的 phone 应用程序中?

settings.gradle

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

build.gradle(应用程序)

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(图书馆)

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'
}

我也使用 Android Studio 1.2.2,这里是如何创建独立库项目并从主项目中引用它的分步说明:

  1. 像创建普通 android 项目一样创建 android 项目库。将其命名为 libProjectName

  2. libProjectNameapp 模块重命名为 libModuleName

  3. libModuleName 的 build.gradle 更改: apply plugin: 'com.android.application'apply plugin: 'com.android.library' 并从 defaultConfig 部分

  4. 中删除 applicationId
  5. 转到主项目,即要引用的项目 libProjectName

  6. 编辑 settings.gradle,添加:

include ':libProjectName:libModuleName'

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

  1. app模块中编辑build.gradle,添加:

compile project(':libProjectDir:libModuleName')


现在假设数独中的 modulemodule-directory 名称是 moduleSudoku 和应用上述步骤,解决方案是:

settings.gradle

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

build.gradle(应用程序)

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(图书馆)

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'
}