Android在Android Studio(AS)中导入第三方库,以SlidingMenu为例

第一种方式

第一步,下载SlidingMenu


第二步,在你的项目中,把SlidingMenu-master中的library文件夹(我把这个文件夹重命名为slidingmenu)import Module进项目中,与文件夹中与app文件夹同级。


第三步,在你的项目根目录中找到settings.gradle


在settings.gradle文件中添加以下代码:

include ':app', ':slidingmenu'

第四步,在build.gradle(这里是在Module:app【在你想要的地方进行添加依赖】)中添加依赖 implementation project(':slidingmenu')

第五步,修改完第4步后【Sync Now】AS就提示有错误了。进行解决。

根据错误提示,修改配置。

Configuration 'compile' is obsolete and has been replaced with 'implementation' and 'api'.

dependencies {
//    compile 'com.android.support:support-v4:13.0.0'
    implementation 'com.android.support:support-v4:13.0.0'
}
//apply plugin: 'android-library'
apply plugin: 'com.android.library'

点击Update Build Tools version and sync project

【Sync Now】


点击Update Build Tools version and sync project

点击AS工具类Build/Make Project

在你的项目中的build.gradle中将以下代码进行拷贝到你的第三方库中的build.gradle中。对它进行修改。


//项目中的
dependencies {
        classpath 'com.android.tools.build:gradle:3.1.2'
        

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
//第三方库中
dependencies {
//        classpath 'com.android.tools.build:gradle:0.4.+'
        classpath 'com.android.tools.build:gradle:3.1.2'
    }

点击Add Google Maven repository and sync project

点击AS工具类Build/Make Project

第六步,修改slidingmenu的build.gradle中的一些参数,如compileSdkVerdion、buildToolsVersion与你AS相匹配(可参照app的build.gradle中的设置)。

//android {
//    compileSdkVersion 17
//    buildToolsVersion '27.0.3'
//
//    defaultConfig {
//        minSdkVersion 7
//        targetSdkVersion 16
//    }
android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 27
    }

点击AS工具类Build/Make Project

出现如下错误:


将其修改为:

	float distanceInfluenceForSnapDuration(float f) {
		f -= 0.5f; // center the values about 0.
		f *= 0.3f * Math.PI / 2.0f;
//		return (float) FloatMath.sin(f);
		return (float) Math.sin(f);
	}

点击AS工具类Build/Make Project

出现问题:



将你的第三方库中的build.gradle文件代码进行修改,版本跟着你想要导入的Module版本一样【这和app(Module)的一样】

dependencies {
    implementation 'com.android.support:support-v4:13.0.0'
//    implementation 'com.android.support:appcompat-v7:27.1.1'
}

【Sync Now】

点击AS工具类Build/Make Project

出现错误:


查看错误信息:



解决方案:

现在的KeyEventCompat类中的hasNoModifiers方法已经被KeyEvent实现了。所以修改为以下代码:

//import android.support.v4.view.KeyEventCompat;

case KeyEvent.KEYCODE_TAB:
				if (Build.VERSION.SDK_INT >= 11) {
					// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
					// before Android 3.0. Ignore the tab key on those devices.
//					if (KeyEventCompat.hasNoModifiers(event)) {
//						handled = arrowScroll(FOCUS_FORWARD);
//					} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
//						handled = arrowScroll(FOCUS_BACKWARD);
//					}
					if (event.hasNoModifiers()) {
						handled = arrowScroll(FOCUS_FORWARD);
					} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
						handled = arrowScroll(FOCUS_BACKWARD);
					}
				}
				break;

完工,点击AS工具类Build/Make Project,没有报错。到这里我们就把slidingmenu作为第三方库加入到app【Module】中了


第二种方式

第一步,在new Module的页面


选中Android Library,Next

 

Application/Library name——项目名称

Module name——Module名称

Finish


展开:


将第三方库中,res和src(文件夹名改成为java)包和AndroidManifest.xml文件替换刚才新建的Module里面的java和res包以及AndroidManifest.xml文件。


然后点击Open Module Settings(倒数第三个)。


点击后,看到如下页面


点击左上角绿色加号


点击后,出现如下界面:


选中想要导入到的Module app。选中OK。出现如下界面。


选中OK。

点击AS工具类Build/Make Project

出现如下错误:


修改代码(原因已经在方式一中进行讲解了):

//import android.support.v4.view.KeyEventCompat;

case KeyEvent.KEYCODE_TAB:
				if (Build.VERSION.SDK_INT >= 11) {
					// The focus finder had a bug handling FOCUS_FORWARD and FOCUS_BACKWARD
					// before Android 3.0. Ignore the tab key on those devices.
//					if (KeyEventCompat.hasNoModifiers(event)) {
//						handled = arrowScroll(FOCUS_FORWARD);
//					} else if (KeyEventCompat.hasModifiers(event, KeyEvent.META_SHIFT_ON)) {
//						handled = arrowScroll(FOCUS_BACKWARD);
//					}
					if (event.hasNoModifiers()) {
						handled = arrowScroll(FOCUS_FORWARD);
					} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {
						handled = arrowScroll(FOCUS_BACKWARD);
					}
				}
				break;




// We want the duration of the page snap animation to be influenced by the distance that
	// the screen has to travel, however, we don't want this duration to be effected in a
	// purely linear fashion. Instead, we use this method to moderate the effect that the distance
	// of travel has on the overall snap duration.
	float distanceInfluenceForSnapDuration(float f) {
		f -= 0.5f; // center the values about 0.
		f *= 0.3f * Math.PI / 2.0f;
//		return (float) FloatMath.sin(f);
		return (float) Math.sin(f);
		
	}

修改完成后,点击AS工具类Build/Make Project

没有报错。

赶紧写几行代码,测试一下。【关于SlidingMenu 使用,可以参考鸿神的博客Android SlidingMenu 使用详解

left_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="神奇奇迹"
        android:textColor="@color/colorAccent"
        android:textSize="50sp"
        android:gravity="center"/>

</LinearLayout>

MainActivity.java

package com.xiaobaiyang.toleadslidingmenu;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        SlidingMenu menu = new SlidingMenu(this);
        menu.setMode(SlidingMenu.LEFT);
        menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
        menu.setBehindWidth(300);
        menu.setFadeDegree(0.35f);
        menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
        menu.setMenu(R.layout.left_menu);

    }

}

运行结果:




导入第三方库,第二种方式相比于第一种方式简单,第一种方式就是修改的配置比较多一些。当时配置文件熟悉后。根据自己喜好两种方式任选其一便好。


猜你喜欢

转载自blog.csdn.net/shenqixiayang/article/details/80197060