简体   繁体   中英

Is it possible to use Android Support Library in a non-Android application?

There is an open source project which contains a bunch of classes that I would be interested in using. However, this project is an android application so it uses a lot of classes from the Android Platform and Android Support Library. Ideally I would like to fork this project and use it within my own project however I am not sure how to approach it. My main question is whether the Android Support Libraries can be used as dependencies in a non-Android project.

My approach has been to create a new folder in my forked repo, write a gradle build file which will copy all the necessary java files, and then attempt to compile it. That failed with a ton of import errors so I tried to do something a bit simpler like this:

build.gradle :

apply plugin: 'java'

allprojects {
    repositories {
        mavenCentral()
        google()
    }
}

dependencies {
    implementation "com.google.android:android:4.1.1.4"
    implementation "com.android.support:support-v4:28.0.0"
}

Test.java :

import android.graphics.Color;
import android.support.v4.app.Fragment;

public final class Test {
        public static void main(String[] args) {
                System.out.println("Hello");
        }
}

I was able to compile this successfully when it was just the "import android.graphics.Color". But once I added something from the support library "import android.support.v4.app.Fragment", it started giving me errors:

package android.support.v4.app does not exist

Looking for any info as to whether I am even approaching this the right way or if the Android Support Library deps will only work if I am building an android app (like using apply plugin: 'com.android.application' instead of 'java')

The support libraries depend on the Android SDK, which are included automatically by the build tools in Android Studio for an Android project.

Even if you would include those, it would not run, because the SDK is just the interface above the underlying implementation on your phone or tablet.

What you might try otherwise is using Robolectric in combination with the support libraries. Robolectric is a dummy Android SDK with the most basic implementation backed behind it, all done locally, so you don't need a device (originally it is used to run unit tests with Android code). You can check out Robolectric here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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