2015-12-28 83 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' 
} 
相關問題