简体   繁体   English

如何将Android Gradle应用打包到* .jar或* .aar文件

[英]How to pack Android Gradle app to *.jar or *.aar file

I created Android App with some gradle dependencies. 我创建了具有gradle依赖关系的Android App。 Now I want to create from this project *.jar (without resources and add them separately) file or *.aar file. 现在,我想从该项目中创建*.jar (没有资源并单独添加)或*.aar文件。 I tried to create new library project (with build.xml), copied my *.java and res files and run ant jar , I'm fixing problem one by one. 我试图创建一个新的库项目(使用build.xml),复制了我的*.java和res文件并运行ant jar ,我正在一步一步地解决问题。 Is there any better solution to do this? 有更好的解决方案吗?

You need to make two modules. 您需要制作两个模块。 The first module would be the jar . 第一个模块是jar This should be POJO s (Plain Old Java Object) and nothing Android. 这应该是POJO (普通的Java旧对象),而不是Android。 The second module would be the aar . 第二个模块是aar This could depend on your first project but add on the Android specific code / resources. 这可能取决于您的第一个项目,但要添加Android特定的代码/资源。

Then your project ( MyApp ) structure would look like this 然后您的项目( MyApp )结构将如下所示

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

Then your settings.gradle file would look something like this: 然后,您的settings.gradle文件将如下所示:

include ':PoJoProject', ':AndroidProject'

Now in the modules 现在在模块中

In MyApp/PoJoProject/build.gradle you will want to apply the java plugin. MyApp/PoJoProject/build.gradle您将要应用java插件。 This module will build to the desired jar format that can be ran anywhere on the normal JVM . 该模块将构建为所需的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
}

In the MyApp/AndroidProject/build.gradle you want to apply the android plugin. 您要在MyApp/AndroidProject/build.gradle中应用android插件。 This module will build to the desired aar format and can only be used as an Android dependency. 该模块将构建为所需的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'
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM