[英]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,这是如何创建独立库项目并从您的主项目引用它的分步指南:
create the android project library as you would for a normal android project. 像创建普通android项目一样创建android项目库。 Call it libProjectName 称之为libProjectName
rename the libProjectName 's app module to libModuleName 将libProjectName的应用模块重命名为libModuleName
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
Go to the main project ie the project to reference libProjectName 转到主项目,即引用libProjectName的项目
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')
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.