简体   繁体   English

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

[英]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 https://code.google.com/p/android/issues/detail?id=105570

and

Android Studio 0.8.1 Creating Modules without copying files? Android Studio 0.8.1创建模块而不复制文件?

I added the lines to settings.gradle and furthermore added the included library via "Project Structure"-Dialog as a dependency to the project. 我将这些行添加到settings.gradle,并通过“项目结构”-对话框添加了所包含的库,作为对项目的依赖关系。 I use Android Studio 1.2.2. 我使用的是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. IDE中的所有内容看起来都不错(导入正常,可以浏览和编辑库代码,可以“制作”两个模块而没有错误),但是我似乎无法在手机/仿真器上启动该项目。

I get a "classNotFoundException" for the first class from the library that is used in the app. 我从应用程序中使用的库中获得了第一个类的“ classNotFoundException”。

I copy-pasted the entire project class by class from Eclipse where it would run without problems. 我从Eclipse逐类复制粘贴了整个项目类,该类将在运行时不会出现问题。

What do I have to do to get the compiled classes of the library into my app onto my phone? 我要怎么做才能将库的已编译类放入应用程序到手机上?

settings.gradle settings.gradle

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

build.gradle (app) 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 (library) 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'
}

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: 我也使用Android Studio 1.2.2,这是如何创建独立库项目并从您的主项目引用它的分步指南:

  1. create the android project library as you would for a normal android project. 像创建普通android项目一样创建android项目库。 Call it libProjectName 称之为libProjectName

  2. rename the libProjectName 's app module to libModuleName libProjectName应用模块重命名为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 libModuleName的build.gradle中进行更改: apply plugin: 'com.android.application'apply plugin: 'com.android.library'并从defaultConfig部分删除applicationId

  4. Go to the main project ie the project to reference libProjectName 转到主项目,即引用libProjectName的项目

  5. edit settings.gradle , add: 编辑settings.gradle ,添加:

include ':libProjectName:libModuleName' 包括':libProjectName:libModuleName'

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

  1. edit build.gradle in app Module, add: 应用模块中编辑build.gradle ,添加:

compile project(':libProjectDir:libModuleName') 编译项目(':libProjectDir:libModuleName')


Now assuming that the module and module-directory name in Sudoku is moduleSudoku and applying the steps above, the solution is: 现在假设Sudoku中的模块模块目录名称为moduleSudoku并应用上述步骤,解决方案是:

settings.gradle settings.gradle

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

build.gradle (app) 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 (library) 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'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 其他项目中的Android Studio重用模块库 - Android Studio reuse module library at other project 如何从其他项目导入android studio模块? - How to Import android studio module from other project? Android Studio将不会运行最近从github克隆的项目,未找到app.iml模块 - Android Studio will not run a project recently cloned from github, app.iml module not found Android Studio:如何添加和使用项目作为库而不是模块 - Android Studio: how to add and use project as library, not as module 如何将Android Studio中的模块(而非项目)链接到Firebase? 与将应用程序链接到Firebase有什么不同? - How do I link a module (not project) in android studio to firebase? how is it different from linking app to firebase? 在Android Studio上将模块或项目导入为库 - Import module or project as Library on Android Studio 无法将项目应用程序从 Android Studio 运行到我的外部设备 - Unable to run project app from Android Studio to my external device 在 Android Studio 中,如何将应用项目更改为库? - in Android Studio, how can I change an app project to a library? 如何运行一个android库项目? - How to run an android library project? 如何从android studio中的库模块调用活动 - How to call activity from a library module in android studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM