2015-12-28 37 views
1

我使用一些gradle依赖关系创建了Android应用程序。现在我想从这个项目创建*.jar(没有资源并单独添加它们)文件或*.aar文件。 我试图创建新的库项目(与build.xml),复制我的*.java和res文件并运行ant jar,我正在修复问题一个接一个。有没有更好的解决方案来做到这一点?如何将Android Gradle应用程序打包为* .jar或* .aar文件

+0

Ant对AARs一无所知。为什么你使用Ant而不是Gradle呢? – CommonsWare

+0

为什么蚂蚁?使用gradle构建。从应用程序更改为库(aar文件),只需从'apply plugin:'com.android.application''更改为'apply plugin:'com.android.library'' –

回答

0

您需要制作两个模块。第一个模块将是jar。这应该是POJO s(普通Java对象),而不是Android。第二个模块将是aar。这可能取决于您的第一个项目,但添加了Android特定的代码/资源。

那么你的项目(MyApp)结构是这样的

MyApp/ 
MyApp/build.gradle 
MyApp/settings.gradle 
MyApp/PoJoProject/ 
MyApp/PoJoProject/build.gradle 
MyApp/AndroidProject/ 
MyApp/AndroidProject/build.gradle 

然后你settings.gradle文件看起来是这样的:

include ':PoJoProject', ':AndroidProject' 

现在在模块

MyApp/PoJoProject/build.gradle你会想要应用java插件。该模块将构建到所需的jar格式,该格式可以在正常的JVM上的任何位置运行。

plugins { 
    id 'java' 
} 

version '1.00' 
group 'com.example.multimodule.gradle' 

repositories { 
    jcenter() 
} 

dependencies { 
    compile 'com.google.code.gson:gson:2.5' 
    testCompile 'junit:junit:4.12' 
} 

compileJava { 
    sourceCompatibility = JavaVersion.VERSION_1_8 
    targetCompatibility = JavaVersion.VERSION_1_8 
} 

在你想申请的android插件MyApp/AndroidProject/build.gradle。该模块将构建为所需的aar格式,并且只能用作Android依赖项。

buildscript { 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:2.0.0-alpha3' 
    } 
} 
apply plugin: 'com.android.application' 

// version could be different from app version if needed 
// `version` & `group` could also be in the top level build.gradle 
version '1.00' 
group 'com.example.multimodule.gradle' 

repositories { 
    jcenter() 
} 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.example.multiproject.gradle.android" 
     minSdkVersion 19 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled true 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    testOptions { 
     unitTests.returnDefaultValues = true 
    } 
} 

dependencies { 
    // let the android `aar` project use code from the `jar` project 
    compile project(':PoJoProject') 
    testCompile 'junit:junit:4.12' 
} 
相关问题