Keywords: Android Studio | Gradle | Android Library Building | Project Migration | Modular Development
Abstract: This article provides a comprehensive guide on migrating multi-project Android applications from Eclipse to Android Studio and the Gradle build system. By analyzing common error scenarios such as missing package attributes in AndroidManifest.xml, non-existent R resource packages, and Gradle dependency configuration issues, it offers complete solutions from project restructuring to Gradle configuration. The focus is on using settings.gradle for multi-module project management, correct application of the android-library plugin, and best practices in dependency declaration, helping developers avoid common pitfalls during migration and achieve efficient project building and maintenance.
Project Structure Migration and Gradle Basic Configuration
When migrating from Eclipse to Android Studio and the Gradle build system, understanding Gradle's standard project structure is crucial. Gradle follows a directory layout similar to Maven, which is particularly important for managing multi-module projects. The basic structure includes: in the project root directory, src/main/java stores Java source code, src/main/res stores resource files, and src/main/AndroidManifest.xml defines application configuration. For library modules, the android-library plugin must be used instead of the standard android plugin.
During migration, common errors include missing package attributes in AndroidManifest.xml. For example, the error <manifest> does not have package attribute encountered in the original issue requires adding a package="com.example.mylib" attribute. Another typical issue is the non-existence of the R resource package, often due to incorrect dependency configuration or build order problems.
Multi-Module Project Configuration and Dependency Management
For projects containing multiple application modules and library modules, the module structure must be defined via the settings.gradle file. Assuming a project includes a main application module MyApp and a library module MyLib, the directory structure can be organized as:
Project Root
+-- MyApp
| +-- src
| +-- build.gradle
+-- MyLib
| +-- src
| +-- build.gradle
+-- build.gradle
+-- settings.gradleIn settings.gradle, use the include directive to declare modules:
include ':MyApp', ':MyLib'The library module's build.gradle must apply the android-library plugin:
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
}The main application module's build.gradle needs to declare a dependency on the library module:
dependencies {
compile project(':MyLib')
}Avoid incorrectly adding compile dependencies in the root project's build.gradle, as this causes the Could not find method compile() error. The correct approach is to declare dependencies in the application module's build.gradle.
Build Script Optimization and Common Issue Resolution
Gradle build script configuration must adhere to the requirements of the Android plugin. In the root project's build.gradle, define the build script repository and dependencies:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}For external dependencies, such as the Android Support Library, it is recommended to use Maven coordinates instead of local JAR files. For example, replace compile files('libs/android-support-v4.jar') with:
dependencies {
compile "com.android.support:support-v4:13.0.0"
compile project(':MyLib')
}This avoids conflicts when multiple modules reference the same JAR file. Additionally, utilize Android Studio's "Sync Project" feature to quickly synchronize the project after modifying Gradle files, without the need for re-importing.
Advanced Configuration and Continuous Integration Considerations
For complex projects, build configuration can be further optimized. For example, define version information via the defaultConfig block:
android {
defaultConfig {
versionCode 1
versionName "1.0"
}
}Add a wrapper task to ensure build environment consistency:
task wrapper(type: Wrapper) {
gradleVersion = '1.4'
}For projects using the NDK, additional configuration for native library file handling is required. For instance, copy .so files via a custom task:
task copyNativeLibs(type: Copy) {
from fileTree(dir: 'libs', include: '**/*.so')
into 'build/native-libs'
}
tasks.withType(Compile) { compileTask -> compileTask.dependsOn copyNativeLibs }These configurations ensure project buildability on continuous integration servers, provided the Android SDK is installed.
Summary and Best Practices
The key to successfully migrating Eclipse projects to Android Studio and Gradle lies in: first, restructuring the project to conform to Gradle standards; second, correctly configuring settings.gradle and inter-module dependencies; and finally, optimizing build scripts to leverage Gradle's dependency management capabilities. Avoiding common errors, such as improper AndroidManifest.xml configuration or incorrect dependency declaration locations, can significantly improve development efficiency. By adopting Maven repository dependencies and modular design, projects become easier to maintain and extend.