Android系统应用开发之Android studio导入eclipse项目(gradle)

使用脚本

放到源码根目录下
执行,记得源码还需要拷贝gradlew和gradlew.bat到源码根目录下… .这两个文件的生成时随便新增一个Android studio工程就可以获取到的

. ./auto_build_gradle.sh 项目路径
. ./auto_build_gradle.sh packages/apps/Settings/
高版本构建失败建议删除  manifest.srcFile "AndroidManifest.xml"
#!/bin/bash
PACKAGE_PATH=./$1
GRADLE_VERSION="3.6.1"
BUILD_GRADLERPATH=./$1/build.gradle
BUILD_GRADLE_PROPERTIES=./$1/gradle.properties
BUILD_GRADLEW=./$1/gradlew
BUILD_GRADLEW_BAT=./$1/gradlew.bat
BUILD_LOCAL_PROPERTIES=./$1/local.properties

source build/envsetup.sh > /dev/null
DEFAULT_RES_DIR="res"
DEFAULT_RES_DIR1="res-ist"
#DEFAULT_RES_DIR3="res"
DEFAULT_SRC_DIR1="src_override"
DEFAULT_SRC_DIR2="src"
DEFAULT_SRC_DIR="java"
DEFAULT_ASSERT_DIR="assets"
DEFAULT_AIDL_DIR="aidl"

SDK_VERISON="28"
READ_TARGET_VERSION="`get_build_var PLATFORM_SDK_VERSION`"
touch ${BUILD_GRADLERPATH}
touch ${BUILD_GRADLE_PROPERTIES}
touch ${BUILD_LOCAL_PROPERTIES}

if [ ! -d "${PACKAGE_PATH}" ] ;then
      echo -e "\033[31m 找不到该路径!\033[0m"
      return;
fi

if [ "${READ_TARGET_VERSION}" != "" ] ;then
	echo "Read platform sdk versionCode is ${READ_TARGET_VERSION} "
	SDK_VERISON=${READ_TARGET_VERSION}
fi
if [ ! -f "${PACKAGE_PATH}/AndroidManifest.xml" ] ;then
	      echo -e "\033[31m 未在该路径下配置AndroidManifest.xml文件!请确认是否为eclipse项目 \033[0m"
      return;
fi


packageName=`grep -r "package=" ${PACKAGE_PATH}/AndroidManifest.xml | sed 's#[[:space:]]##g' | sed  's/package="//g' | sed 's/"//g'`

echo -e "\033[34m ${PACKAGE_PATH}/AndroidManifest.xml 解析到的包名为... $packageName \033[0m"

echo_buildgradle(){
echo "
buildscript {
    repositories {
        jcenter()
        mavenCentral()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:$GRADLE_VERSION'
    }
}
apply plugin: 'com.android.application'

android {
    compileSdkVersion ${SDK_VERISON}
    buildToolsVersion "\"${SDK_VERISON}.0.0\""
    defaultConfig {
        applicationId "\"${packageName}\""
        minSdkVersion 26
        targetSdkVersion ${SDK_VERISON}
        versionCode 1
        versionName "\"1.0\""
		multiDexEnabled true
        testInstrumentationRunner "\"android.support.test.runner.AndroidJUnitRunner\""
    }
    buildTypes {
        debug {
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }


    sourceSets {
        main {
            java.srcDirs = ["\"${DEFAULT_SRC_DIR}\"", "\"${DEFAULT_SRC_DIR1}\"" , "\"${DEFAULT_SRC_DIR2}\"" ]
            res.srcDirs = ["\"${DEFAULT_RES_DIR}\"","\"${DEFAULT_RES_DIR1}\""]
            manifest.srcFile "\"AndroidManifest.xml\""
			assets.srcDirs = ["\"${DEFAULT_ASSERT_DIR}\""]
			aidl.srcDirs = ["\"${DEFAULT_AIDL_DIR}\""]
        }

        androidTest {
            res.srcDirs = ["\"tests/res\""]
            java.srcDirs = ["\"tests/src\""]
            manifest.srcFile "\"tests/AndroidManifest.xml\""
        }
    }

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

final String ANDROID_X_VERSION = "\"1.0.0\""

repositories {
    mavenCentral()
    google()
    jcenter()
    mavenCentral()
}

dependencies {
	implementation "\"androidx.recyclerview:recyclerview:\${ANDROID_X_VERSION}\""
    implementation "\"androidx.cardview:cardview:\${ANDROID_X_VERSION}\""
    implementation "\"androidx.appcompat:appcompat:\${ANDROID_X_VERSION}\""
    implementation "\"androidx.preference:preference:\${ANDROID_X_VERSION}\""
    implementation "\"androidx.exifinterface:exifinterface:\${ANDROID_X_VERSION}\""
    implementation "\"com.google.android.material:material:\${ANDROID_X_VERSION}\""
	implementation "\"com.android.support:leanback-v17:${SDK_VERISON}.0.0\""
    androidTestImplementation('junit:junit:4.12')
    androidTestImplementation('com.android.support.test:runner:1.0.1')
    androidTestImplementation('org.robolectric:robolectric:3.4.2') {
        exclude group: 'xmlpull'
    }
    androidTestImplementation('com.thoughtworks.xstream:xstream:1.4.8');
    androidTestImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.1'
}

" > ${BUILD_GRADLERPATH}
}

echo_gradle_properties(){
	echo "# Until all the dependencies move to android X
android.useAndroidX=true
android.enableJetifier=true" > ${BUILD_GRADLE_PROPERTIES}
}

echo_local_properties(){
	echo "## This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
#
# Location of the SDK. This is only used by Gradle.
# For customization when using a Version Control System, please read the
# header note.
#Sat Mar 12 15:26:59 CST 2022
sdk.dir=C\:\\\\Users\\\\97515\\\\AndroidSDK
" > ${BUILD_LOCAL_PROPERTIES}
}


if [ -f "${BUILD_GRADLERPATH}" ] ;then
	rm -rf ${BUILD_GRADLERPATH}
fi
echo_buildgradle
echo -e "\033[34m ${BUILD_GRADLERPATH}创建完成....\033[0m"

if [ -f "${BUILD_GRADLE_PROPERTIES}" ] ;then
	  rm -rf ${BUILD_GRADLE_PROPERTIES}
fi
echo_gradle_properties
echo -e "\033[34m ${BUILD_GRADLE_PROPERTIES}创建完成....\033[0m"


if [ -f "${BUILD_LOCAL_PROPERTIES}" ] ;then
	  rm -rf ${BUILD_LOCAL_PROPERTIES}
fi
echo_local_properties
echo -e "\033[34m ${BUILD_LOCAL_PROPERTIES}创建完成....\033[0m"

if [ ! -f "./gradlew.bat" ] || [ ! -f "./gradlew" ] ;then
	echo -e "\033[31m 请先从Android studio项目中拷贝gradlew和gradlew.bat到此目录 --> `pwd`!\033[0m"
	return
fi

cp -r gradlew $BUILD_GRADLEW
cp -r gradlew.bat $BUILD_GRADLEW_BAT




你可能感兴趣的:(Android系统开发,Android,脚本,android)